admin
2025-04-08 5c9991be21f57781573f04961ec511ac2938ea3d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#pragma once
#include <afx.h>
#include <string>
#include <locale>  
#include <codecvt>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
using namespace std;
 
const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
 
class StringUtil {
 
public:
    static string wstringToString(wstring wt) {
        wstring_convert<codecvt_utf8<wchar_t>> 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<std::codecvt_utf8<wchar_t>> 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<std::string> 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;
    }
    
    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<int> 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);
    }
 
};