VTrain_BothSides/ai_matrix/Utils/StringUtil.cpp

172 lines
4.6 KiB
C++
Raw Normal View History

2025-01-09 02:02:52 +00:00
//
// Created by matrixai on 4/2/24.
//
#include "StringUtil.h"
namespace ai_matrix
{
StringUtil *StringUtil::ins = nullptr;
StringUtil::GarbageCollector StringUtil::gc;
std::mutex StringUtil::mx;
StringUtil *StringUtil::getins()
{
//双层锁,确保线程安全
if (ins == nullptr)
{
std::lock_guard<std::mutex> guard(mx); //防止异常发生不能解锁
if (ins == nullptr)
{
ins = new StringUtil();
}
}
return ins;
}
/**
*
* @param str
* @param pattern
* @return vector<std::string>
*/
std::vector<std::string> StringUtil::split(std::string str, std::string pattern)
{
std::string::size_type pos;
std::vector<std::string> result;
str += pattern; //扩展字符串以方便操作
int size = str.size();
for (int i = 0; i < size; i++)
{
pos = str.find(pattern, i);
if (pos < size)
{
std::string s = str.substr(i, pos - i);
result.push_back(s);
i = pos + pattern.size() - 1;
}
}
return result;
}
/**
*
* @param str
* @return
*/
std::string StringUtil::trim(const std::string& str) {
std::string temp = str;
temp.erase(0, temp.find_first_not_of(" \t\n\r\f\v")); // 去除开头的空格
temp.erase(temp.find_last_not_of(" \t\n\r\f\v") + 1); // 去除末尾的空格
return temp;
}
/**
*
* @param str
* @param old_value
* @param new_value
* @return
*/
std::string StringUtil::replace_all_distinct(std::string str, const std::string old_value, const std::string new_value)
{
for (std::string::size_type pos(0); pos != std::string::npos; pos += new_value.length()) {
if ((pos = str.find(old_value, pos)) != std::string::npos)
str.replace(pos, old_value.length(), new_value);
else break;
}
return str;
}
/**
* Anchor行分割
* inParam : std::string &strLine Anchor一行内容
* : const std::set<char> &setDelimiters
* outParam: N/A
* return :
*/
std::vector<float> StringUtil::SplitAnchor(std::string &strLine, const std::set<char> &setDelimiters)
{
std::vector<float> result;
if (strLine.empty())
{
return result;
}
char const *pch = strLine.c_str();
char const *start = pch;
for (; *pch; ++pch)
{
if (setDelimiters.find(*pch) != setDelimiters.end())
{
if (start != pch)
{
std::string tmp(start, pch);
result.push_back(atof(tmp.c_str())); //浮点数
}
start = pch + 1;
}
}
result.push_back(atof(start)); //最后一个内容
return result;
}
/**
* float string
* @param f
* @return
*/
std::string StringUtil::getStringFromFloat(float f)
{
std::ostringstream buffer;
buffer << f;
return buffer.str();
}
/**
* bool string
* @param b
* @return
*/
std::string StringUtil::getStringFromBool(bool b)
{
std::ostringstream buffer;
buffer << b;
return buffer.str();
}
/**
* string int
* ()
* @param s
* @param b
* @return
*/
int StringUtil::string2int(const std::string s, bool &b) {
try {
b = true;
return std::stoi(this->trim(s));
} catch (...) {
b = false;
return 0;
}
}
/**
*
* @param str
* @return
*/
bool StringUtil::containsNonAlphaNum(const std::string &str) {
for (char c : str) {
if (!std::isalnum(c) && !std::isspace(c)) { // 如果不是字母或数字
return true; // 返回true表示存在非字母数字字符
}
}
return false; // 遍历完成未发现非字母数字字符返回false
}
bool StringUtil::is_digits(const std::string &str) {
return all_of(str.begin(), str.end(), ::isdigit);
}
}