]> git.tdb.fi Git - libs/core.git/blobdiff - source/utils.cpp
Add missing includes
[libs/core.git] / source / utils.cpp
index 9898a07dca5fe52d8511ad71da78c4f5e8b864cb..d67cc7e8bcd3947f62947be762af28aa60bf3f30 100644 (file)
@@ -5,8 +5,9 @@ Copyright © 2006-2007 Mikko Rasa
 Distributed under the LGPL
 */
 
+#include <algorithm>
 #include <list>
-#include <msp/core/error.h>
+#include <msp/core/except.h>
 #include "utils.h"
 
 using namespace std;
@@ -162,7 +163,7 @@ string c_unescape(const std::string &str)
        string result;
        for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
        {
-               if(numeric_type==1)
+               if(numeric_type==16)
                {
                        unsigned digit=0;
                        if(*i>='0' && *i<='9')
@@ -182,7 +183,7 @@ string c_unescape(const std::string &str)
                                numeric_type=0;
                        }
                }
-               else if(numeric_type==2)
+               else if(numeric_type==8)
                {
                        unsigned digit=0;
                        if(*i>='0' && *i<='7')
@@ -190,12 +191,9 @@ string c_unescape(const std::string &str)
                        else
                                throw InvalidParameterValue("Invalid octal digit");
 
-                       if(numeric_pos==0 && digit>3)
-                               throw InvalidParameterValue("Octal escape sequence must start with [0-3]");
-
                        numeric_value=(numeric_value<<3 | digit);
                        ++numeric_pos;
-                       if(numeric_pos==2)
+                       if(numeric_pos==3)
                        {
                                result+=numeric_value;
                                numeric_type=0;
@@ -203,12 +201,18 @@ string c_unescape(const std::string &str)
                }
                else if(escape)
                {
-                       if(*i=='x' || (*i>='0' && *i<='7'))
+                       if(*i=='x')
                        {
-                               numeric_type=(*i=='x' ? 1 : 2);
+                               numeric_type=16;
                                numeric_pos=0;
                                numeric_value=0;
                        }
+                       else if(*i>='0' && *i<='3')
+                       {
+                               numeric_type=8;
+                               numeric_pos=1;
+                               numeric_value=*i-'0';
+                       }
                        else if(*i=='n')
                                result+='\n';
                        else if(*i=='t')
@@ -272,9 +276,9 @@ string c_escape(const string &str, bool escape_8bit)
                        result+="\\\'";
                else if(*i=='\\')
                        result+="\\\\";
-               else if(*i<' ' || (escape_8bit && (*i&0x80)))
+               else if(static_cast<unsigned char>(*i)<' ' || (escape_8bit && (*i&0x80)))
                {
-                       char buf[4]={'\\', (*i>>6)&7, (*i>>3)&7, *i&7};
+                       char buf[4]={'\\', '0'+((*i>>6)&7), '0'+((*i>>3)&7), '0'+(*i&7)};
                        result.append(buf, 4);
                }
                else