X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Futils.cpp;h=c2bafb9b85527c9b570dea735588397900f4f6ca;hp=0318e994e22a6f66eea7a2f03e53660435445b37;hb=cc69674bf670178a8076f2ce9740c2e60c4644c9;hpb=b254c706617223da1dd1b9543a74715e42a8a5b0 diff --git a/source/utils.cpp b/source/utils.cpp index 0318e99..c2bafb9 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -1,10 +1,11 @@ /* $Id$ This file is part of libmspstrings -Copyright © 2006-2007 Mikko Rasa +Copyright © 2006-2008 Mikko Rasa Distributed under the LGPL */ +#include #include #include #include "utils.h" @@ -45,6 +46,14 @@ vector do_split(const string &str, const string &sep, int max_split) 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 { @@ -78,6 +87,21 @@ string toupper(const string &str) 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 split(const string &str, const string &sep, int max_split) { return do_split(str, sep, max_split); @@ -162,7 +186,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 +206,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 +214,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 +224,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') @@ -274,7 +301,7 @@ string c_escape(const string &str, bool escape_8bit) result+="\\\\"; else if(static_cast(*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