VCarContainer/ai_matrix/Utils/StringUtil.h

75 lines
2.1 KiB
C++

//
// Created by matrixai on 4/2/24.
//
#ifndef TRAIN_RFID_LINUX_STRINGUTIL_H
#define TRAIN_RFID_LINUX_STRINGUTIL_H
#include <string>
#include <vector>
#include <mutex>
#include <set>
#include <sstream>
#include <bits/stdc++.h>
namespace ai_matrix
{
class StringUtil final
{
public:
static StringUtil *getins();
// 字符串分割函数
std::vector<std::string> split(std::string str, std::string pattern);
// 去掉字符串前后的空格
std::string trim(const std::string& str);
// 替换string中所有指定字符串
std::string replace_all_distinct(std::string str, const std::string old_value, const std::string new_value);
//
std::vector<float> SplitAnchor(std::string &strLine, const std::set<char> &setDelimiters);
// float 转 string
std::string getStringFromFloat(float f);
// bool 转 string
std::string getStringFromBool(bool b);
// string 转 int
int string2int(const std::string s, bool &b);
// 判断字符串中是否含有 字母和数字 之外的字符
bool containsNonAlphaNum(const std::string &str);
// 判断字符串中是否仅含有数字
// 检查函数
bool is_digits(const std::string& str);
private:
StringUtil() = default;
StringUtil(const StringUtil &) = delete;
StringUtil(StringUtil &&) = delete;
StringUtil &operator=(const StringUtil &) = delete;
StringUtil &operator=(StringUtil &&) = delete;
~StringUtil() = default;
//定义一个嵌套类,负责释放内存,操作系统自动完成,不用担心内存泄露
class GarbageCollector
{
public:
~GarbageCollector()
{
if (StringUtil::ins)
{
delete StringUtil::ins;
StringUtil::ins = nullptr;
}
}
};
static GarbageCollector gc;
static StringUtil *ins;
static std::mutex mx; //锁,保证线程安全
};
}
#endif //TRAIN_RFID_LINUX_STRINGUTIL_H