]> git.tdb.fi Git - libs/core.git/blobdiff - source/utils.cpp
Fix octal escape generation in c_escape
[libs/core.git] / source / utils.cpp
index d67cc7e8bcd3947f62947be762af28aa60bf3f30..c2bafb9b85527c9b570dea735588397900f4f6ca 100644 (file)
@@ -1,7 +1,7 @@
 /* $Id$
 
 This file is part of libmspstrings
 /* $Id$
 
 This file is part of libmspstrings
-Copyright © 2006-2007 Mikko Rasa
+Copyright © 2006-2008 Mikko Rasa
 Distributed under the LGPL
 */
 
 Distributed under the LGPL
 */
 
@@ -46,6 +46,14 @@ vector<string> do_split(const string &str, const string &sep, int max_split)
        return result;
 }
 
        return result;
 }
 
+bool check_str(const std::string &str, int (*pred)(int))
+{
+       for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
+               if(!pred(*i))
+                       return false;
+       return true;
+}
+
 }
 
 namespace Msp {
 }
 
 namespace Msp {
@@ -79,6 +87,21 @@ string toupper(const string &str)
        return result;
 }
 
        return result;
 }
 
+bool isnumrc(const string &str)
+{
+       return check_str(str, isdigit);
+}
+
+bool isalpha(const string &str)
+{
+       return check_str(str, isalpha);
+}
+
+bool isalnum(const string &str)
+{
+       return check_str(str, isalnum);
+}
+
 vector<string> split(const string &str, const string &sep, int max_split)
 {
        return do_split<false, false>(str, sep, max_split);
 vector<string> split(const string &str, const string &sep, int max_split)
 {
        return do_split<false, false>(str, sep, max_split);
@@ -278,7 +301,7 @@ string c_escape(const string &str, bool escape_8bit)
                        result+="\\\\";
                else if(static_cast<unsigned char>(*i)<' ' || (escape_8bit && (*i&0x80)))
                {
                        result+="\\\\";
                else if(static_cast<unsigned char>(*i)<' ' || (escape_8bit && (*i&0x80)))
                {
-                       char buf[4]={'\\', '0'+((*i>>6)&7), '0'+((*i>>3)&7), '0'+(*i&7)};
+                       char buf[4]={'\\', '0'+((*i>>6)&3), '0'+((*i>>3)&7), '0'+(*i&7)};
                        result.append(buf, 4);
                }
                else
                        result.append(buf, 4);
                }
                else