#pragma once #include #include #include #include #include #include #include #include using namespace std; const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; class StringUtil { public: static string wstringToString(wstring wt) { wstring_convert> conv; string result = conv.to_bytes(wt); return result; } static std::wstring Unescape(const std::string& input) { std::wstring wresult; for (size_t i = 0; i < input.length(); ) { if (input[i] == '\\' && input[i + 1] == 'u') { std::string code = input.substr(i + 2, 4); wchar_t unicode = stoi(code, nullptr, 16); wresult += unicode; i += 6; } else { wresult += input[i++]; } } return wresult; } static std::string cstring2String(CString getbuf) { int iLen = WideCharToMultiByte(0, 0, getbuf, -1, NULL, 0, NULL, NULL); //Ê×ÏȼÆËãTCHAR ³¤¶È¡£ char* chRtn = new char[iLen * sizeof(char)]; //¶¨ÒåÒ»¸ö TCHAR ³¤¶È´óСµÄ CHAR ÀàÐÍ¡£ WideCharToMultiByte(0, 0, getbuf, -1, chRtn, iLen, NULL, NULL); //½«TCHAR ÀàÐ͵ÄÊý¾Ýת»»Îª CHAR ÀàÐÍ¡£ std::string str(chRtn); return str; } static std::string cstring2StringV2(CString getbuf) { std::wstring_convert> converter; std::string utf8String = converter.to_bytes(getbuf); return utf8String; } static string to_string(double val) { std::stringstream stream; stream << std::fixed << std::setprecision(2) << val; return stream.str(); } static string to_string(float val) { std::stringstream stream; stream << std::fixed << std::setprecision(2) << val; return stream.str(); } static bool isNumber(const std::string& str) { if (str.empty()) { return false; } for (char c : str) { if (!isdigit(c)) { return false; } } return true; } // doubleתΪ×Ö·û´® static string toString(double value, int precision) { std::ostringstream oss; oss << std::fixed << std::setprecision(precision) << value; return oss.str(); } static std::vector split(std::string str, std::string pattern) { std::string::size_type pos; std::vector 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; } static std::string base64_encode(const std::string& input) { std::string encoded; int val = 0, valb = -6; for (unsigned char c : input) { val = (val << 8) + c; valb += 8; while (valb >= 0) { encoded.push_back(base64_chars[(val >> valb) & 0x3F]); valb -= 6; } } if (valb > -6) encoded.push_back(base64_chars[((val << 8) >> (valb + 8)) & 0x3F]); while (encoded.size() % 4) encoded.push_back('='); return encoded; } static std::string base64_decode(const std::string& input) { std::string decoded; std::vector T(256, -1); for (int i = 0; i < 64; i++) T[base64_chars[i]] = i; int val = 0, valb = -8; for (unsigned char c : input) { if (T[c] == -1) break; val = (val << 6) + T[c]; valb += 6; if (valb >= 0) { decoded.push_back(char((val >> valb) & 0xFF)); valb -= 8; } } return decoded; } static std::string trim(const std::string& str) { if (str.empty()) { return str; } // È¥³ýǰ¿Õ¸ñ size_t start = str.find_first_not_of(" \t\n\r"); if (start == std::string::npos) { return ""; // Èç¹ûÈ«Êǿոñ£¬·µ»Ø¿Õ×Ö·û´® } // È¥³ýºó¿Õ¸ñ size_t end = str.find_last_not_of(" \t\n\r"); return str.substr(start, end - start + 1); } };