|
1 | 1 | #include "StringHelper.h" |
2 | 2 | #include <regex> |
3 | 3 |
|
4 | | -std::string StringHelper::ReplaceAll(const std::string& str, const std::string& search, const std::string& replace) |
| 4 | +std::string StringHelper::ReplaceAll(const std::string &str, const std::string &search, const std::string &replace) |
5 | 5 | { |
6 | | - return std::regex_replace(str, std::regex(search), replace); |
| 6 | + return std::regex_replace(str, std::regex(search), replace); |
7 | 7 | } |
8 | 8 |
|
9 | | -std::wstring StringHelper::ReplaceAll(const std::wstring& wstr, const std::wstring& search, const std::wstring& replace) |
| 9 | +std::wstring StringHelper::ReplaceAll(const std::wstring &wstr, const std::wstring &search, const std::wstring &replace) |
10 | 10 | { |
11 | | - return std::regex_replace(wstr, std::wregex(search), replace); |
| 11 | + return std::regex_replace(wstr, std::wregex(search), replace); |
12 | 12 | } |
13 | 13 |
|
14 | | -std::wstring StringHelper::ToWstring(const std::string& str, UINT codePage) |
| 14 | +std::wstring StringHelper::ToWstring(const std::string &str, UINT codePage) |
15 | 15 | { |
16 | | - std::wstring wstr; |
17 | | - |
18 | | - if (!str.empty()) |
19 | | - { |
20 | | - auto required = ::MultiByteToWideChar(codePage, 0, str.data(), static_cast<int>(str.size()), NULL, 0); |
21 | | - if (0 != required) |
22 | | - { |
23 | | - wstr.resize(required); |
24 | | - |
25 | | - auto converted = ::MultiByteToWideChar(codePage, 0, str.data(), static_cast<int>(str.size()), &wstr[0], static_cast<int>(wstr.capacity())); |
26 | | - if (0 == converted) |
27 | | - { |
28 | | - wstr.clear(); |
29 | | - } |
30 | | - } |
31 | | - } |
32 | | - |
33 | | - return wstr; |
| 16 | + std::wstring wstr; |
| 17 | + |
| 18 | + if (!str.empty()) |
| 19 | + { |
| 20 | + auto required = ::MultiByteToWideChar(codePage, 0, str.data(), static_cast<int>(str.size()), NULL, 0); |
| 21 | + if (0 != required) |
| 22 | + { |
| 23 | + wstr.resize(required); |
| 24 | + |
| 25 | + auto converted = ::MultiByteToWideChar(codePage, 0, str.data(), static_cast<int>(str.size()), &wstr[0], static_cast<int>(wstr.capacity())); |
| 26 | + if (0 == converted) |
| 27 | + { |
| 28 | + wstr.clear(); |
| 29 | + } |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + return wstr; |
| 34 | +} |
| 35 | + |
| 36 | +std::string StringHelper::ToString(const std::wstring &wstr, UINT codePage) |
| 37 | +{ |
| 38 | + std::string str; |
| 39 | + if (!wstr.empty()) |
| 40 | + { |
| 41 | + auto required = ::WideCharToMultiByte(codePage, 0, wstr.data(), static_cast<int>(wstr.size()), NULL, 0, NULL, NULL); |
| 42 | + if (0 != required) |
| 43 | + { |
| 44 | + str.resize(required); |
| 45 | + |
| 46 | + auto converted = ::WideCharToMultiByte(codePage, 0, wstr.data(), static_cast<int>(wstr.size()), &str[0], static_cast<int>(str.capacity()), NULL, NULL); |
| 47 | + if (0 == converted) |
| 48 | + { |
| 49 | + str.clear(); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return str; |
| 55 | +} |
| 56 | + |
| 57 | +std::vector<std::string> StringHelper::Split(const std::string &input, const std::string &delim) |
| 58 | +{ |
| 59 | + // Vector is created on stack and copied on return |
| 60 | + std::vector<std::string> tokens; |
| 61 | + |
| 62 | + // Skip delimiters at beginning. |
| 63 | + auto lastPos = input.find_first_not_of(delim, 0); |
| 64 | + // Find first "non-delimiter". |
| 65 | + auto pos = input.find_first_of(delim, lastPos); |
| 66 | + |
| 67 | + while (pos != std::string::npos || lastPos != std::string::npos) |
| 68 | + { |
| 69 | + // Found a token, add it to the vector. |
| 70 | + tokens.push_back(input.substr(lastPos, pos - lastPos)); |
| 71 | + // Skip delimiters. Note the "not_of" |
| 72 | + lastPos = input.find_first_not_of(delim, pos); |
| 73 | + // Find next "non-delimiter" |
| 74 | + pos = input.find_first_of(delim, lastPos); |
| 75 | + } |
| 76 | + return tokens; |
34 | 77 | } |
35 | 78 |
|
36 | | -std::string StringHelper::ToString(const std::wstring& wstr, UINT codePage) |
| 79 | +std::vector<std::wstring> StringHelper::Split(const std::wstring &input, const std::wstring &delim) |
37 | 80 | { |
38 | | - std::string str; |
39 | | - if (!wstr.empty()) |
40 | | - { |
41 | | - auto required = ::WideCharToMultiByte(codePage, 0, wstr.data(), static_cast<int>(wstr.size()), NULL, 0, NULL, NULL); |
42 | | - if (0 != required) |
43 | | - { |
44 | | - str.resize(required); |
45 | | - |
46 | | - auto converted = ::WideCharToMultiByte(codePage, 0, wstr.data(), static_cast<int>(wstr.size()), &str[0], static_cast<int>(str.capacity()), NULL, NULL); |
47 | | - if (0 == converted) |
48 | | - { |
49 | | - str.clear(); |
50 | | - } |
51 | | - } |
52 | | - } |
53 | | - |
54 | | - return str; |
| 81 | + // Vector is created on stack and copied on return |
| 82 | + std::vector<std::wstring> tokens; |
| 83 | + |
| 84 | + // Skip delimiters at beginning. |
| 85 | + auto lastPos = input.find_first_not_of(delim, 0); |
| 86 | + // Find first "non-delimiter". |
| 87 | + auto pos = input.find_first_of(delim, lastPos); |
| 88 | + |
| 89 | + while (pos != std::wstring::npos || lastPos != std::wstring::npos) |
| 90 | + { |
| 91 | + // Found a token, add it to the vector. |
| 92 | + tokens.push_back(input.substr(lastPos, pos - lastPos)); |
| 93 | + // Skip delimiters. Note the "not_of" |
| 94 | + lastPos = input.find_first_not_of(delim, pos); |
| 95 | + // Find next "non-delimiter" |
| 96 | + pos = input.find_first_of(delim, lastPos); |
| 97 | + } |
| 98 | + return tokens; |
| 99 | +} |
| 100 | + |
| 101 | +bool StringHelper::Contains(const std::string &input, const std::string &search, bool ignorecase) |
| 102 | +{ |
| 103 | + return Contains(ToWstring(input), ToWstring(search), ignorecase); |
| 104 | +} |
| 105 | + |
| 106 | +bool StringHelper::Contains(const std::wstring &input, const std::wstring &search, bool ignorecase) |
| 107 | +{ |
| 108 | + std::wstring lower_input = input; |
| 109 | + std::wstring lower_search = search; |
| 110 | + if (ignorecase) |
| 111 | + { |
| 112 | + ToLower(lower_input); |
| 113 | + ToLower(lower_search); |
| 114 | + } |
| 115 | + |
| 116 | + return lower_input.find(lower_search) != std::wstring::npos; |
55 | 117 | } |
56 | 118 |
|
57 | | -std::vector<std::string> StringHelper::Split(const std::string& input, const std::string& delim) |
| 119 | +void StringHelper::ToLower(std::string &input) |
58 | 120 | { |
59 | | - //Vector is created on stack and copied on return |
60 | | - std::vector<std::string> tokens; |
61 | | - |
62 | | - // Skip delimiters at beginning. |
63 | | - auto lastPos = input.find_first_not_of(delim, 0); |
64 | | - // Find first "non-delimiter". |
65 | | - auto pos = input.find_first_of(delim, lastPos); |
66 | | - |
67 | | - while (pos != std::string::npos || lastPos != std::string::npos) |
68 | | - { |
69 | | - // Found a token, add it to the vector. |
70 | | - tokens.push_back(input.substr(lastPos, pos - lastPos)); |
71 | | - // Skip delimiters. Note the "not_of" |
72 | | - lastPos = input.find_first_not_of(delim, pos); |
73 | | - // Find next "non-delimiter" |
74 | | - pos = input.find_first_of(delim, lastPos); |
75 | | - } |
76 | | - return tokens; |
| 121 | + auto s = ToWstring(input); |
| 122 | + ToLower(s); |
| 123 | + input = ToString(s); |
77 | 124 | } |
78 | 125 |
|
79 | | -std::vector<std::wstring> StringHelper::Split(const std::wstring& input, const std::wstring& delim) |
| 126 | +void StringHelper::ToLower(std::wstring &input) |
80 | 127 | { |
81 | | - //Vector is created on stack and copied on return |
82 | | - std::vector<std::wstring> tokens; |
83 | | - |
84 | | - // Skip delimiters at beginning. |
85 | | - auto lastPos = input.find_first_not_of(delim, 0); |
86 | | - // Find first "non-delimiter". |
87 | | - auto pos = input.find_first_of(delim, lastPos); |
88 | | - |
89 | | - while (pos != std::wstring::npos || lastPos != std::wstring::npos) |
90 | | - { |
91 | | - // Found a token, add it to the vector. |
92 | | - tokens.push_back(input.substr(lastPos, pos - lastPos)); |
93 | | - // Skip delimiters. Note the "not_of" |
94 | | - lastPos = input.find_first_not_of(delim, pos); |
95 | | - // Find next "non-delimiter" |
96 | | - pos = input.find_first_of(delim, lastPos); |
97 | | - } |
98 | | - return tokens; |
| 128 | + std::transform(input.begin(), input.end(), input.begin(), ::towlower); |
99 | 129 | } |
0 commit comments