301 lines
7.2 KiB
C
301 lines
7.2 KiB
C
|
#pragma once
|
|||
|
#include <string>
|
|||
|
#include <sstream>
|
|||
|
#include <vector>
|
|||
|
#include <algorithm>
|
|||
|
#include <iomanip>
|
|||
|
#include <QString>
|
|||
|
|
|||
|
|
|||
|
class StringHelper
|
|||
|
{
|
|||
|
public:
|
|||
|
static bool Contains(std::string strSource ,char str)
|
|||
|
{
|
|||
|
if (strSource.find(str) != std::string::npos)
|
|||
|
{
|
|||
|
return true;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
static bool Contains(std::string strSource ,std::string str)
|
|||
|
{
|
|||
|
if (strSource.find(str) != std::string::npos)
|
|||
|
{
|
|||
|
return true;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
static bool Contains(std::vector<std::string>& strSource, std::string str)
|
|||
|
{
|
|||
|
if (std::find(strSource.begin(), strSource.end(), str) == strSource.end())
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
return true;
|
|||
|
}
|
|||
|
|
|||
|
static bool Contains(std::vector<QString>& strSource, QString& str)
|
|||
|
{
|
|||
|
return std::find(strSource.begin(), strSource.end(), str) != strSource.end();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
static std::string toLower(std::string source)
|
|||
|
{
|
|||
|
std::transform(source.begin(), source.end(), source.begin(), [](unsigned char c){ return std::tolower(c); });
|
|||
|
return source;
|
|||
|
}
|
|||
|
|
|||
|
static std::string toUpper(std::string source)
|
|||
|
{
|
|||
|
std::transform(source.begin(), source.end(), source.begin(), [](unsigned char c){ return std::toupper(c); });
|
|||
|
return source;
|
|||
|
}
|
|||
|
|
|||
|
static std::string trimStart(std::string source, const std::string &trimChars = " \t\n\r\v\f")
|
|||
|
{
|
|||
|
return source.erase(0, source.find_first_not_of(trimChars));
|
|||
|
}
|
|||
|
|
|||
|
static std::string trimEnd(std::string source, const std::string &trimChars = " \t\n\r\v\f")
|
|||
|
{
|
|||
|
return source.erase(source.find_last_not_of(trimChars) + 1);
|
|||
|
}
|
|||
|
|
|||
|
static std::string trim(std::string source, const std::string &trimChars = " \t\n\r\v\f")
|
|||
|
{
|
|||
|
return trimStart(trimEnd(source, trimChars), trimChars);
|
|||
|
}
|
|||
|
|
|||
|
static std::string replace(std::string source, const std::string &find, const std::string &replace)
|
|||
|
{
|
|||
|
std::size_t pos = 0;
|
|||
|
while ((pos = source.find(find, pos)) != std::string::npos)
|
|||
|
{
|
|||
|
source.replace(pos, find.length(), replace);
|
|||
|
pos += replace.length();
|
|||
|
}
|
|||
|
return source;
|
|||
|
}
|
|||
|
|
|||
|
static bool startsWith(const std::string &source, const std::string &value)
|
|||
|
{
|
|||
|
if (source.length() < value.length())
|
|||
|
return false;
|
|||
|
else
|
|||
|
return source.compare(0, value.length(), value) == 0;
|
|||
|
}
|
|||
|
|
|||
|
static bool endsWith(const std::string &source, const std::string &value)
|
|||
|
{
|
|||
|
if (source.length() < value.length())
|
|||
|
return false;
|
|||
|
else
|
|||
|
return source.compare(source.length() - value.length(), value.length(), value) == 0;
|
|||
|
}
|
|||
|
|
|||
|
static std::vector<std::string> split(const std::string& str, char delimiter)
|
|||
|
{
|
|||
|
std::vector<std::string> tokens;
|
|||
|
std::string token;
|
|||
|
std::stringstream ss(str);
|
|||
|
|
|||
|
while (std::getline(ss, token, delimiter)) {
|
|||
|
tokens.push_back(token);
|
|||
|
}
|
|||
|
|
|||
|
return tokens;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
static std::vector<std::string> split(const std::string &source, const std::vector<std::string> &delimiters)
|
|||
|
{
|
|||
|
std::vector<std::string> output;
|
|||
|
std::string temp = source;
|
|||
|
|
|||
|
while (!temp.empty())
|
|||
|
{
|
|||
|
size_t minPos = std::string::npos;
|
|||
|
std::string foundDelim;
|
|||
|
|
|||
|
// 找到最先出现的分隔符
|
|||
|
for (const auto &delim : delimiters)
|
|||
|
{
|
|||
|
size_t pos = temp.find(delim);
|
|||
|
if (pos != std::string::npos && pos < minPos)
|
|||
|
{
|
|||
|
minPos = pos;
|
|||
|
foundDelim = delim;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
// 如果找到分隔符
|
|||
|
if (minPos != std::string::npos)
|
|||
|
{
|
|||
|
if (minPos > 0) // 确保不会加入空字符串
|
|||
|
{
|
|||
|
output.push_back(temp.substr(0, minPos));
|
|||
|
}
|
|||
|
temp = temp.substr(minPos + foundDelim.length()); // 跳过分隔符
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
output.push_back(temp);
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return output;
|
|||
|
}
|
|||
|
|
|||
|
template<typename T>
|
|||
|
static std::string toString(const T &subject)
|
|||
|
{
|
|||
|
std::ostringstream ss;
|
|||
|
ss << subject;
|
|||
|
return ss.str();
|
|||
|
}
|
|||
|
|
|||
|
template<typename T>
|
|||
|
static std::string tostring_fix(const T &subject,int setpre)
|
|||
|
{
|
|||
|
std::ostringstream oss;
|
|||
|
oss << std::fixed << std::setprecision(setpre) << subject;
|
|||
|
// 获取格式化后的字符串
|
|||
|
std::string formattedString = oss.str();
|
|||
|
return formattedString;
|
|||
|
}
|
|||
|
|
|||
|
template<typename T>
|
|||
|
static T fromString(const std::string &subject)
|
|||
|
{
|
|||
|
std::wistringstream ss(subject);
|
|||
|
T target;
|
|||
|
ss >> target;
|
|||
|
return target;
|
|||
|
}
|
|||
|
|
|||
|
static bool isEmptyOrWhiteSpace(const std::string &source)
|
|||
|
{
|
|||
|
if (source.length() == 0)
|
|||
|
return true;
|
|||
|
else
|
|||
|
{
|
|||
|
for (std::size_t index = 0; index < source.length(); index++)
|
|||
|
{
|
|||
|
if (!std::isspace(source[index]))
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
return true;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
static std::vector<std::string> GetWords(const std::string& str, const std::vector<std::string>& splitter = {"(", ")"}) {
|
|||
|
std::vector<std::string> list;
|
|||
|
std::vector<std::string> words;
|
|||
|
std::istringstream stream(str);
|
|||
|
std::string word;
|
|||
|
|
|||
|
// 按空格和逗号拆分字符串
|
|||
|
while (stream >> word) {
|
|||
|
words.push_back(word);
|
|||
|
}
|
|||
|
|
|||
|
bool bracketOn = false;
|
|||
|
std::string bracket;
|
|||
|
|
|||
|
for (const auto& word : words) {
|
|||
|
if (word.find(splitter[0]) != std::string::npos && word.find(splitter[1]) != std::string::npos && !bracketOn) {
|
|||
|
list.push_back(word); // 括号都在同一个单词里,直接加入
|
|||
|
}
|
|||
|
else if (word.find(splitter[0]) != std::string::npos && !bracketOn) {
|
|||
|
bracket = word + " ";
|
|||
|
bracketOn = true;
|
|||
|
}
|
|||
|
else if (word.find(splitter[1]) != std::string::npos && bracketOn) {
|
|||
|
bracket += word;
|
|||
|
list.push_back(bracket);
|
|||
|
bracketOn = false;
|
|||
|
}
|
|||
|
else {
|
|||
|
if (word.find(splitter[0]) != std::string::npos || word.find(splitter[1]) != std::string::npos) {
|
|||
|
throw std::runtime_error("行中存在无法解析的分词,请检查是否缺少空格等分隔符.");
|
|||
|
}
|
|||
|
if (bracketOn) {
|
|||
|
bracket += word + " ";
|
|||
|
}
|
|||
|
else {
|
|||
|
list.push_back(word);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return list;
|
|||
|
}
|
|||
|
static std::string FirstWord(const std::string& str) {
|
|||
|
std::string word = str;
|
|||
|
word.erase(0, word.find_first_not_of(" \t\n\r"));
|
|||
|
word.erase(word.find_last_not_of(" \t\n\r") + 1);
|
|||
|
size_t index = word.find(' ');
|
|||
|
if (index == std::string::npos) {
|
|||
|
return word;
|
|||
|
}
|
|||
|
return word.substr(0, index);
|
|||
|
}
|
|||
|
|
|||
|
static double roundToNDecimalPlaces(double value, int n) {
|
|||
|
double factor = std::pow(10, n); // 计算 10 的 n 次方
|
|||
|
return std::round(value * factor) / factor; // 四舍五入到 n 位小数
|
|||
|
}
|
|||
|
|
|||
|
static std::string formatToString(double value,int dot) {
|
|||
|
std::stringstream ss;
|
|||
|
ss << std::fixed << std::setprecision(dot) << value;
|
|||
|
return ss.str();
|
|||
|
}
|
|||
|
|
|||
|
// 去除字符串头部和尾部的空白字符
|
|||
|
static std::string trimStartEnd(const std::string& str) {
|
|||
|
// 去除左边的空白字符
|
|||
|
size_t start = str.find_first_not_of(" \t\n\r\f\v");
|
|||
|
if (start == std::string::npos) {
|
|||
|
return ""; // 如果全是空白字符,返回空字符串
|
|||
|
}
|
|||
|
|
|||
|
// 去除右边的空白字符
|
|||
|
size_t end = str.find_last_not_of(" \t\n\r\f\v");
|
|||
|
|
|||
|
// 使用substr提取头尾去除空白后的子字符串
|
|||
|
return str.substr(start, end - start + 1);
|
|||
|
}
|
|||
|
|
|||
|
static void removeLeadingNewlines(std::string& str) {
|
|||
|
// 基本情况:如果字符串为空或第一个字符不是 '\n',停止递归
|
|||
|
if (str.empty() || str.front() != '\n') {
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
// 递归调用,删除第一个 '\n'
|
|||
|
str.erase(0, 1);
|
|||
|
removeLeadingNewlines(str);
|
|||
|
}
|
|||
|
|
|||
|
static bool IsNullOrEmptyAfterTrim(const std::string& str)
|
|||
|
{
|
|||
|
return str.empty() || str.find_first_not_of(" \t") == std::string::npos;
|
|||
|
}
|
|||
|
|
|||
|
};
|