2021-01-02 04:24:29 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-07-28 02:16:03 +00:00
|
|
|
#include <algorithm>
|
2021-05-30 15:09:59 +00:00
|
|
|
#include <cstring>
|
2021-01-09 00:38:28 +00:00
|
|
|
#include <numeric>
|
2021-01-02 04:24:29 +00:00
|
|
|
#include <stdarg.h>
|
2021-03-20 16:02:12 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2021-07-28 02:16:03 +00:00
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define __PRETTY_FUNCTION__ __FUNCSIG__
|
2021-10-17 11:32:09 +00:00
|
|
|
#elif not defined(__GNUC__)
|
2021-07-28 02:16:03 +00:00
|
|
|
#define __PRETTY_FUNCTION__ __func__
|
|
|
|
#endif
|
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-01-09 00:38:28 +00:00
|
|
|
};
|