X-Git-Url: http://git.tdb.fi/?p=libs%2Fcore.git;a=blobdiff_plain;f=source%2Fstrings%2Futils.cpp;h=748d717464690851585e22f9ccdc45da5885eabc;hp=80e59f07a6cb5802eb741aa870a0498e2308fea0;hb=HEAD;hpb=9955efca718d8be72b63c7c2182ca59e7b9d0935 diff --git a/source/strings/utils.cpp b/source/strings/utils.cpp index 80e59f0..748d717 100644 --- a/source/strings/utils.cpp +++ b/source/strings/utils.cpp @@ -1,7 +1,7 @@ -#include #include #include #include +#include #include "utils.h" using namespace std; @@ -40,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; -} - } @@ -55,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); @@ -71,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) @@ -144,24 +136,24 @@ string join(const string &str1, const string &sep, const string &str2) return append(result, sep, str2); } -string c_unescape(const std::string &str) +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 invalid_argument("c_unescape"); @@ -176,8 +168,8 @@ 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 invalid_argument("c_unescape"); @@ -191,47 +183,47 @@ 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 invalid_argument("c_unescape"); escape = false; } - else if(*i=='\\') + else if(c=='\\') escape = true; else - result += *i; + result += c; } if(escape) @@ -244,37 +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 }; for(unsigned j=0; j<3; ++j) - buf[1+j] = '0'+((static_cast(*i)>>(6-j*3))&7); + buf[1+j] = '0'+((static_cast(c)>>(6-j*3))&7); result.append(buf, 4); } else - result += *i; + result += c; } return result;