52 lines
1.4 KiB
C++
52 lines
1.4 KiB
C++
#pragma once
|
|
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
class StringHelper {
|
|
public:
|
|
static std::vector<std::string> split(const std::string &input, char delimiter) {
|
|
std::vector<std::string> tokens;
|
|
std::string token;
|
|
for (char ch : input) {
|
|
if (ch == delimiter) {
|
|
if (!token.empty()) {
|
|
tokens.push_back(token);
|
|
token.clear();
|
|
}
|
|
} else {
|
|
token += ch;
|
|
}
|
|
}
|
|
if (!token.empty()) {
|
|
tokens.push_back(token);
|
|
}
|
|
return tokens;
|
|
}
|
|
|
|
static std::vector<std::string> split(const std::string &input, std::string delimiter) {
|
|
std::vector<std::string> tokens;
|
|
size_t start = 0;
|
|
size_t end = input.find(delimiter);
|
|
while (end != std::string::npos) {
|
|
tokens.push_back(input.substr(start, end - start));
|
|
start = end + delimiter.length();
|
|
end = input.find(delimiter, start);
|
|
}
|
|
tokens.push_back(input.substr(start));
|
|
return tokens;
|
|
}
|
|
|
|
static std::string trimToSize(const std::string &input, size_t maxSize) {
|
|
if (input.length() <= maxSize) {
|
|
return input;
|
|
}
|
|
|
|
size_t len = maxSize;
|
|
while (len > 0 && (static_cast<unsigned char>(input[len]) & 0xC0) == 0x80) {
|
|
len--;
|
|
}
|
|
|
|
return input.substr(0, len) + "...";
|
|
}
|
|
}; |