2021-01-02 04:24:29 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-07-28 02:16:03 +00:00
|
|
|
#include <algorithm>
|
2021-12-03 20:57:05 +00:00
|
|
|
#include <cstdarg>
|
2021-05-30 15:09:59 +00:00
|
|
|
#include <cstring>
|
2021-01-09 00:38:28 +00:00
|
|
|
#include <numeric>
|
2021-03-20 16:02:12 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2021-07-28 02:16:03 +00:00
|
|
|
|
2021-01-02 04:24:29 +00:00
|
|
|
class StringHelper
|
|
|
|
{
|
|
|
|
public:
|
2021-01-09 00:38:28 +00:00
|
|
|
static std::vector<std::string> Split(std::string s, const std::string& delimiter)
|
2021-01-02 04:24:29 +00:00
|
|
|
{
|
|
|
|
std::vector<std::string> result;
|
|
|
|
|
|
|
|
size_t pos = 0;
|
|
|
|
std::string token;
|
|
|
|
|
|
|
|
while ((pos = s.find(delimiter)) != std::string::npos)
|
|
|
|
{
|
|
|
|
token = s.substr(0, pos);
|
|
|
|
result.push_back(token);
|
|
|
|
s.erase(0, pos + delimiter.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (s.length() != 0)
|
|
|
|
result.push_back(s);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-01-09 00:38:28 +00:00
|
|
|
static std::string Strip(std::string s, const std::string& delimiter)
|
2021-01-02 04:24:29 +00:00
|
|
|
{
|
|
|
|
size_t pos = 0;
|
|
|
|
std::string token;
|
|
|
|
|
|
|
|
while ((pos = s.find(delimiter)) != std::string::npos)
|
|
|
|
{
|
|
|
|
token = s.substr(0, pos);
|
|
|
|
s.erase(pos, pos + delimiter.length());
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2021-04-30 21:23:22 +00:00
|
|
|
static std::string Replace(std::string str, const std::string& from, const std::string& to)
|
|
|
|
{
|
|
|
|
size_t start_pos = str.find(from);
|
|
|
|
|
|
|
|
if (start_pos == std::string::npos)
|
|
|
|
return str;
|
|
|
|
|
|
|
|
str.replace(start_pos, from.length(), to);
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2021-01-09 00:38:28 +00:00
|
|
|
static bool StartsWith(const std::string& s, const std::string& input)
|
2021-01-02 04:24:29 +00:00
|
|
|
{
|
|
|
|
return s.rfind(input, 0) == 0;
|
|
|
|
}
|
|
|
|
|
2021-01-09 00:38:28 +00:00
|
|
|
static bool Contains(const std::string& s, const std::string& input)
|
2021-01-02 04:24:29 +00:00
|
|
|
{
|
|
|
|
return s.find(input) != std::string::npos;
|
|
|
|
}
|
|
|
|
|
2021-01-09 00:38:28 +00:00
|
|
|
static bool EndsWith(const std::string& s, const std::string& input)
|
2021-01-02 04:24:29 +00:00
|
|
|
{
|
2021-05-30 15:09:59 +00:00
|
|
|
size_t inputLen = strlen(input.c_str());
|
2021-01-02 04:24:29 +00:00
|
|
|
return s.rfind(input) == (s.size() - inputLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string Sprintf(const char* format, ...)
|
|
|
|
{
|
|
|
|
char buffer[32768];
|
2021-03-20 16:02:12 +00:00
|
|
|
// char buffer[2048];
|
2021-10-17 11:32:09 +00:00
|
|
|
std::string output;
|
2021-01-02 04:24:29 +00:00
|
|
|
va_list va;
|
|
|
|
|
|
|
|
va_start(va, format);
|
|
|
|
vsprintf(buffer, format, va);
|
|
|
|
va_end(va);
|
|
|
|
|
|
|
|
output = buffer;
|
|
|
|
return output;
|
|
|
|
}
|
2021-01-09 00:38:28 +00:00
|
|
|
|
|
|
|
static std::string Implode(std::vector<std::string>& elements, const char* const separator)
|
|
|
|
{
|
|
|
|
return std::accumulate(std::begin(elements), std::end(elements), std::string(),
|
2021-03-20 16:02:12 +00:00
|
|
|
[separator](std::string& ss, std::string& s) {
|
|
|
|
return ss.empty() ? s : ss + separator + s;
|
|
|
|
});
|
2021-01-09 00:38:28 +00:00
|
|
|
}
|
2021-05-30 15:09:59 +00:00
|
|
|
|
|
|
|
static int64_t StrToL(const std::string& str, int32_t base = 10)
|
|
|
|
{
|
|
|
|
return std::strtoull(str.c_str(), nullptr, base);
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string BoolStr(bool b) { return b ? "true" : "false"; }
|
|
|
|
|
2021-07-28 02:16:03 +00:00
|
|
|
static bool HasOnlyDigits(const std::string& str)
|
2021-05-30 15:09:59 +00:00
|
|
|
{
|
|
|
|
return std::all_of(str.begin(), str.end(), ::isdigit);
|
|
|
|
}
|
2021-12-03 20:57:05 +00:00
|
|
|
|
2022-12-08 18:15:18 +00:00
|
|
|
static bool HasOnlyHexDigits(const std::string& str)
|
|
|
|
{
|
|
|
|
return std::all_of(str.begin(), str.end(), ::isxdigit);
|
|
|
|
}
|
|
|
|
|
2022-06-02 22:48:52 +00:00
|
|
|
static std::string ToUpper(const std::string& str)
|
|
|
|
{
|
|
|
|
std::string buff = str;
|
|
|
|
std::transform(buff.begin(), buff.end(), buff.begin(), ::toupper);
|
|
|
|
return buff;
|
|
|
|
}
|
|
|
|
|
2021-12-03 20:57:05 +00:00
|
|
|
static bool IEquals(const std::string& a, const std::string& b)
|
|
|
|
{
|
|
|
|
return std::equal(a.begin(), a.end(), b.begin(), b.end(),
|
2022-01-17 00:43:07 +00:00
|
|
|
[](char a, char b) { return tolower(a) == tolower(b); });
|
2021-12-03 20:57:05 +00:00
|
|
|
}
|
2021-01-09 00:38:28 +00:00
|
|
|
};
|