123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- //
- // Created by xcbosa on 2023/1/31.
- //
- #include "strop.h"
- #include <chrono>
- #include <functional>
- #include <random>
- namespace xc {
- namespace utils {
- vector<string> split(const string& str, const string& delim) {
- vector<string> res;
- if ("" == str) return res;
- char *strs = new char[str.length() + 1];
- strcpy(strs, str.c_str());
- char *d = new char[delim.length() + 1];
- strcpy(d, delim.c_str());
- char *p = strtok(strs, d);
- while (p) {
- string s = p;
- res.push_back(s);
- p = strtok(NULL, d);
- }
- delete[] strs;
- delete[] d;
- return res;
- }
- string replace_all(string& src, const string& old_value, const string& new_value) {
- for (string::size_type pos(0); pos != string::npos; pos += new_value.length()) {
- if ((pos = src.find(old_value, pos)) != string::npos) {
- src.replace(pos, old_value.length(), new_value);
- }
- else break;
- }
- return src;
- }
- string fixStringTransfer(string& src) {
- string modify = src;
- replace_all(modify, "\"", "\\\"");
- replace_all(modify, "\r", "\\\r");
- replace_all(modify, "\n", "\\\n");
- return modify;
- }
- string& trim(string &s) {
- if (s.empty()) { return s; }
- s.erase(0,s.find_first_not_of(" "));
- s.erase(s.find_last_not_of(" ") + 1);
- return s;
- }
- string uppercase(string s) {
- ostringstream oss;
- for (char ch : s) {
- oss << (char)::toupper(ch);
- }
- return oss.str();
- }
- int to_int(string s, bool &isSuccess) {
- try {
- isSuccess = true;
- return stoi(s);
- } catch (...) {
- isSuccess = false;
- }
- }
- int to_int(string s, int defaultValue) {
- try {
- return stoi(s);
- } catch (...) {
- return defaultValue;
- }
- }
- bool is_in(int value, int minIncluded, int maxExcluded) {
- return value >= minIncluded && value < maxExcluded;
- }
- string create_uuid() {
- ostringstream stream;
- auto random_seed = std::chrono::system_clock::now().time_since_epoch().count();
- std::mt19937 seed_engine(random_seed);
- std::uniform_int_distribution<std::size_t> random_gen;
- std::size_t value = random_gen(seed_engine);
- stream << hex << value;
- return stream.str();
- }
- } // xc
- } // utils
|