X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fstrings%2Futils.cpp;h=748d717464690851585e22f9ccdc45da5885eabc;hp=93bbbbf2ae81d3c073b2abe3320029108ccd45ba;hb=HEAD;hpb=967785734be5c3fc6f75da122c2d93ebbb338271 diff --git a/source/strings/utils.cpp b/source/strings/utils.cpp index 93bbbbf..748d717 100644 --- a/source/strings/utils.cpp +++ b/source/strings/utils.cpp @@ -1,6 +1,7 @@ -#include #include -#include +#include +#include +#include #include "utils.h" using namespace std; @@ -39,14 +40,6 @@ 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; -} - } @@ -54,8 +47,8 @@ namespace Msp { int strcasecmp(const string &s1, const string &s2) { - string::const_iterator i1 = s1.begin(); - string::const_iterator i2 = s2.begin(); + auto i1 = s1.begin(); + auto i2 = s2.begin(); for(; (i1!=s1.end() && i2!=s2.end()); ++i1, ++i2) { const char c1 = ::tolower(*i1); @@ -70,30 +63,30 @@ int strcasecmp(const string &s1, const string &s2) string tolower(const string &str) { string result(str); - transform(result.begin(), result.end(), result.begin(), ::tolower); + transform(result, [](char c){ return std::tolower(c); }); return result; } string toupper(const string &str) { string result(str); - transform(result.begin(), result.end(), result.begin(), ::toupper); + transform(result, [](char c){ return std::toupper(c); }); return result; } bool isnumrc(const string &str) { - return check_str(str, isdigit); + return all_of(str.begin(), str.end(), [](char c){ return std::isdigit(c); }); } bool isalpha(const string &str) { - return check_str(str, isalpha); + return all_of(str.begin(), str.end(), [](char c){ return std::isalpha(c); }); } bool isalnum(const string &str) { - return check_str(str, isalnum); + return all_of(str.begin(), str.end(), [](char c){ return std::isalnum(c); }); } vector split(const string &str, const string &sep, int max_split) @@ -129,26 +122,40 @@ string strip(const string &s) return result; } -string c_unescape(const std::string &str) +string &append(string &str, const string &sep, const string &other) +{ + if(!str.empty() && !other.empty()) + str += sep; + str += other; + return str; +} + +string join(const string &str1, const string &sep, const string &str2) +{ + string result = str1; + return append(result, sep, str2); +} + +string c_unescape(const string &str) { bool escape = false; unsigned numeric_type = 0; unsigned numeric_pos = 0; unsigned numeric_value = 0; string result; - for(string::const_iterator i=str.begin(); i!=str.end(); ++i) + for(auto c: str) { 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; + if(c>='0' && c<='9') + digit = c-'0'; + else if(c>='a' && c<='f') + digit = c-'a'+10; + else if(c>='A' && c<='F') + digit = c-'A'+10; else - throw InvalidParameterValue("Invalid hexadecimal digit"); + throw invalid_argument("c_unescape"); numeric_value = (numeric_value<<4 | digit); ++numeric_pos; @@ -161,10 +168,10 @@ string c_unescape(const std::string &str) else if(numeric_type==8) { unsigned digit = 0; - if(*i>='0' && *i<='7') - digit = *i-'0'; + if(c>='0' && c<='7') + digit = c-'0'; else - throw InvalidParameterValue("Invalid octal digit"); + throw invalid_argument("c_unescape"); numeric_value = (numeric_value<<3 | digit); ++numeric_pos; @@ -176,51 +183,51 @@ string c_unescape(const std::string &str) } else if(escape) { - if(*i=='x') + if(c=='x') { numeric_type = 16; numeric_pos = 0; numeric_value = 0; } - else if(*i>='0' && *i<='3') + else if(c>='0' && c<='3') { numeric_type = 8; numeric_pos = 1; - numeric_value = *i-'0'; + numeric_value = c-'0'; } - else if(*i=='n') + else if(c=='n') result += '\n'; - else if(*i=='t') + else if(c=='t') result += '\t'; - else if(*i=='r') + else if(c=='r') result += '\r'; - else if(*i=='b') + else if(c=='b') result += '\b'; - else if(*i=='v') + else if(c=='v') result += '\v'; - else if(*i=='a') + else if(c=='a') result += '\a'; - else if(*i=='f') + else if(c=='f') result += '\f'; - else if(*i=='\"') + else if(c=='\"') result += '\"'; - else if(*i=='\'') + else if(c=='\'') result += '\''; - else if(*i=='\\') + else if(c=='\\') result += '\\'; else - throw InvalidParameterValue("Invalid escape sequence"); + throw invalid_argument("c_unescape"); escape = false; } - else if(*i=='\\') + else if(c=='\\') escape = true; else - result += *i; + result += c; } if(escape) - throw InvalidParameterValue("Stray backslash at end of string"); + throw invalid_argument("c_unescape"); return result; } @@ -229,35 +236,37 @@ string c_escape(const string &str, bool escape_8bit) { string result; - for(string::const_iterator i=str.begin(); i!=str.end(); ++i) + for(char c: str) { - if(*i=='\n') + if(c=='\n') result += "\\n"; - else if(*i=='\t') + else if(c=='\t') result += "\\t"; - else if(*i=='\r') + else if(c=='\r') result += "\\r"; - else if(*i=='\b') + else if(c=='\b') result += "\\b"; - else if(*i=='\v') + else if(c=='\v') result += "\\v"; - else if(*i=='\a') + else if(c=='\a') result += "\\a"; - else if(*i=='\f') + else if(c=='\f') result += "\\f"; - else if(*i=='\"') + else if(c=='\"') result += "\\\""; - else if(*i=='\'') + else if(c=='\'') result += "\\\'"; - else if(*i=='\\') + else if(c=='\\') result += "\\\\"; - else if(static_cast(*i)<' ' || (escape_8bit && (*i&0x80))) + else if(static_cast(c)<' ' || c==0x7F || (escape_8bit && (c&0x80))) { - char buf[4] = {'\\', '0'+((*i>>6)&3), '0'+((*i>>3)&7), '0'+(*i&7)}; + char buf[4] = { '\\', 0 }; + for(unsigned j=0; j<3; ++j) + buf[1+j] = '0'+((static_cast(c)>>(6-j*3))&7); result.append(buf, 4); } else - result += *i; + result += c; } return result;