106 lines
3.0 KiB
C++
106 lines
3.0 KiB
C++
#include <VUtils/StringUtils.h>
|
|
|
|
#include <iomanip>
|
|
#include <sstream>
|
|
|
|
namespace VUtils {
|
|
void StringUtils::leftTrim(std::string &s) {
|
|
s.erase(s.begin(), std::find_if(s.begin(), s.end(),
|
|
std::not1(std::ptr_fun<int, int>(std::isspace))));
|
|
}
|
|
|
|
void StringUtils::rightTrim(std::string &s) {
|
|
s.erase(std::find_if(s.rbegin(), s.rend(),
|
|
std::not1(std::ptr_fun<int, int>(std::isspace)))
|
|
.base(),
|
|
s.end());
|
|
}
|
|
|
|
void StringUtils::trim(std::string &s) {
|
|
leftTrim(s);
|
|
rightTrim(s);
|
|
}
|
|
|
|
std::string StringUtils::leftTrimCopy(std::string s) {
|
|
leftTrim(s);
|
|
return s;
|
|
}
|
|
|
|
std::string StringUtils::rightTrimCopy(std::string s) {
|
|
rightTrim(s);
|
|
return s;
|
|
}
|
|
|
|
std::string StringUtils::trimCopy(std::string s) {
|
|
trim(s);
|
|
return s;
|
|
}
|
|
|
|
std::vector<std::string> StringUtils::split(const std::string &s, const std::string &delimiter) {
|
|
size_t pos_start = 0, pos_end, delim_len = delimiter.length();
|
|
std::string token;
|
|
std::vector<std::string> res;
|
|
|
|
while ((pos_end = s.find(delimiter, pos_start)) != std::string::npos) {
|
|
token = s.substr(pos_start, pos_end - pos_start);
|
|
pos_start = pos_end + delim_len;
|
|
res.push_back(token);
|
|
}
|
|
|
|
res.push_back(s.substr(pos_start));
|
|
return res;
|
|
}
|
|
|
|
std::string StringUtils::urlDecode(const std::string &val) {
|
|
std::string ret;
|
|
char ch;
|
|
int i, ii;
|
|
for (i = 0; i < val.length(); i++) {
|
|
if (int(val[ i ]) == 37) {
|
|
sscanf(val.substr(i + 1, 2).c_str(), "%x", &ii);
|
|
ch = static_cast<char>(ii);
|
|
ret += ch;
|
|
i = i + 2;
|
|
} else {
|
|
ret += val[ i ];
|
|
}
|
|
}
|
|
return (ret);
|
|
}
|
|
|
|
std::string StringUtils::urlEncode(const std::string &value) {
|
|
std::ostringstream escaped;
|
|
escaped.fill('0');
|
|
escaped << std::hex;
|
|
|
|
for (char c : value) {
|
|
if (isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
|
|
escaped << c;
|
|
continue;
|
|
}
|
|
escaped << std::uppercase;
|
|
escaped << '%' << std::setw(2) << int((unsigned char) c);
|
|
escaped << std::nouppercase;
|
|
}
|
|
|
|
return escaped.str();
|
|
}
|
|
std::string StringUtils::join(const std::vector<std::string> &vector, const std::string &delimiter) {
|
|
std::stringstream string;
|
|
for (int i = 0; i < vector.size(); ++i) {
|
|
if (i != 0)
|
|
string << delimiter;
|
|
string << vector[ i ];
|
|
|
|
}
|
|
return string.str();
|
|
}
|
|
bool StringUtils::hasNullByte(int size, const char *string) {
|
|
for (int i = 0; i < size; ++i) {
|
|
if (string[ i ] == '\0') {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
}// namespace VUtils
|