X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Futils.cpp;h=5728332d595c1bba62d126f9ddbee776c6164004;hp=244055b232cb81df6347f9200d315683d372aff3;hb=5b1368cb791cab043f0435628cacbaff36e39b7b;hpb=43a991406be78b2905ef1f13f4e7589f6f6c1ba3 diff --git a/source/utils.cpp b/source/utils.cpp index 244055b..5728332 100644 --- a/source/utils.cpp +++ b/source/utils.cpp @@ -1,11 +1,13 @@ /* $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" using namespace std; @@ -17,10 +19,10 @@ vector do_split(const string &str, const string &sep, int max_split) { vector result; - unsigned start=0; + unsigned start = 0; while(start=0 && result.size()==static_cast(max_split)) @@ -35,7 +37,7 @@ vector do_split(const string &str, const string &sep, int max_split) if(end>str.size()) break; - start=end+(long_sep ? sep.size() : 1); + start = end+(long_sep ? sep.size() : 1); if(allow_empty && start==str.size()) result.push_back(string()); @@ -44,26 +46,27 @@ 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 { +} -/** -Compares two strings, ignoring case. -@param s1 First string -@param s2 Second string +namespace Msp { -@return -1 if s1s2 -*/ int strcasecmp(const string &s1, const string &s2) { - string::const_iterator i1=s1.begin(); - string::const_iterator i2=s2.begin(); + string::const_iterator i1 = s1.begin(); + string::const_iterator i2 = s2.begin(); for(; (i1!=s1.end() && i2!=s2.end()); ++i1, ++i2) { - const char c1=::tolower(*i1); - const char c2=::tolower(*i2); + const char c1 = ::tolower(*i1); + const char c2 = ::tolower(*i2); if(c1!=c2) return c1-c2; } if(i1!=s1.end()) return *i1; @@ -71,9 +74,6 @@ int strcasecmp(const string &s1, const string &s2) return 0; } -/** -Returns a lowercase copy of the given string. -*/ string tolower(const string &str) { string result(str); @@ -81,9 +81,6 @@ string tolower(const string &str) return result; } -/** -Returns an uppercase copy of the given string. -*/ string toupper(const string &str) { string result(str); @@ -91,19 +88,19 @@ string toupper(const string &str) return result; } -vector split_fields(const string &str, const string &sep, int max_split) +bool isnumrc(const string &str) { - return do_split(str, sep, max_split); + return check_str(str, isdigit); } -vector split_fields(const string &str, char sep, int max_split) +bool isalpha(const string &str) { - return split_fields(str, string(1, sep), max_split); + return check_str(str, isalpha); } -vector split_long(const string &str, const string &sep, int max_split) +bool isalnum(const string &str) { - return do_split(str, sep, max_split); + return check_str(str, isalnum); } vector split(const string &str, const string &sep, int max_split) @@ -116,80 +113,160 @@ vector split(const string &str, char sep, int max_split) return split(str, string(1, sep), max_split); } -/** -Splits a string to parts. - -@param str String to be split -@param sep A set of separator characters -@param allow_empty Whether or not to produce empty parts for sequences of - more than one separator character -*/ -vector split(const string &str, const string &sep, bool allow_empty) +vector split_long(const string &str, const string &sep, int max_split) { - vector result; - - unsigned start=0; - if(!allow_empty) - start=str.find_first_not_of(sep); - - while(start(str, sep, max_split); +} - return result; +vector split_fields(const string &str, const string &sep, int max_split) +{ + return do_split(str, sep, max_split); } -vector split(const string &str, char sep, bool allow_empty) +vector split_fields(const string &str, char sep, int max_split) { - return split(str, string(1, sep), allow_empty); + return split_fields(str, string(1, sep), max_split); } -/** -Builds a single string from the strings in the given sequence by concatenating -them. +string strip(const string &s) +{ + string result = s; + if(!result.erase(0, result.find_first_not_of(" \t\r\n")).empty()) + result.erase(result.find_last_not_of(" \t\r\n")+1); + return result; +} -@param seq A sequence of strings -@param sep Separator to be inserted between strings -*/ -template -string join(const T &seq, const string &sep) +string c_unescape(const std::string &str) { + bool escape = false; + unsigned numeric_type = 0; + unsigned numeric_pos = 0; + unsigned numeric_value = 0; string result; - for(typename T::const_iterator i=seq.begin(); i!=seq.end(); ++i) + for(string::const_iterator i=str.begin(); i!=str.end(); ++i) { - if(i!=seq.begin()) - result+=sep; - result+=*i; + if(numeric_type==16) + { + unsigned digit = 0; + if(*i>='0' && *i<='9') + digit = *i-'0'; + else if(*i>='a' && *i<='f') + digit = *i-'a'+10; + else if(*i>='A' && *i<='F') + digit = *i-'A'+10; + else + throw InvalidParameterValue("Invalid hexadecimal digit"); + + numeric_value = (numeric_value<<4 | digit); + ++numeric_pos; + if(numeric_pos==2) + { + result += numeric_value; + numeric_type = 0; + } + } + else if(numeric_type==8) + { + unsigned digit = 0; + if(*i>='0' && *i<='7') + digit = *i-'0'; + else + throw InvalidParameterValue("Invalid octal digit"); + + numeric_value = (numeric_value<<3 | digit); + ++numeric_pos; + if(numeric_pos==3) + { + result += numeric_value; + numeric_type = 0; + } + } + else if(escape) + { + if(*i=='x') + { + 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') + result += '\t'; + else if(*i=='r') + result += '\r'; + else if(*i=='b') + result += '\b'; + else if(*i=='v') + result += '\v'; + else if(*i=='a') + result += '\a'; + else if(*i=='f') + result += '\f'; + else if(*i=='\"') + result += '\"'; + else if(*i=='\'') + result += '\''; + else if(*i=='\\') + result += '\\'; + else + throw InvalidParameterValue("Invalid escape sequence"); + + escape = false; + } + else if(*i=='\\') + escape = true; + else + result += *i; } + if(escape) + throw InvalidParameterValue("Stray backslash at end of string"); + return result; } -template string join >(const list &, const string &); -template string join >(const vector &, const string &); -/** -Returns a copy of the given string with leading and trailing whitespace -removed. -*/ -string strip(const string &s) +string c_escape(const string &str, bool escape_8bit) { - string result=s; - if(!result.erase(0, result.find_first_not_of(" \t\r\n")).empty()) - result.erase(result.find_last_not_of(" \t\r\n")+1); + string result; + + for(string::const_iterator i=str.begin(); i!=str.end(); ++i) + { + if(*i=='\n') + result += "\\n"; + else if(*i=='\t') + result += "\\t"; + else if(*i=='\r') + result += "\\r"; + else if(*i=='\b') + result += "\\b"; + else if(*i=='\v') + result += "\\v"; + else if(*i=='\a') + result += "\\a"; + else if(*i=='\f') + result += "\\f"; + else if(*i=='\"') + result += "\\\""; + else if(*i=='\'') + result += "\\\'"; + else if(*i=='\\') + result += "\\\\"; + else if(static_cast(*i)<' ' || (escape_8bit && (*i&0x80))) + { + char buf[4] = {'\\', '0'+((*i>>6)&3), '0'+((*i>>3)&7), '0'+(*i&7)}; + result.append(buf, 4); + } + else + result += *i; + } + return result; }