-#include <algorithm>
#include <list>
#include <stdexcept>
+#include <cctype>
+#include <msp/core/algorithm.h>
#include "utils.h"
using namespace std;
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;
-}
-
}
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);
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<string> split(const string &str, const string &sep, int max_split)
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 invalid_argument("c_unescape");
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");
}
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)
{
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<unsigned char>(*i)<' ' || (escape_8bit && (*i&0x80)))
+ else if(static_cast<unsigned char>(c)<' ' || c==0x7F || (escape_8bit && (c&0x80)))
{
char buf[4] = { '\\', 0 };
for(unsigned j=0; j<3; ++j)
- buf[1+j] = '0'+((static_cast<unsigned char>(*i)>>(6-j*3))&7);
+ buf[1+j] = '0'+((static_cast<unsigned char>(c)>>(6-j*3))&7);
result.append(buf, 4);
}
else
- result += *i;
+ result += c;
}
return result;