Skip to content

Commit f8758fa

Browse files
committed
Added some more string functions
1 parent 71897c5 commit f8758fa

File tree

2 files changed

+123
-88
lines changed

2 files changed

+123
-88
lines changed
Lines changed: 109 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,129 @@
11
#include "StringHelper.h"
22
#include <regex>
33

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)
55
{
6-
return std::regex_replace(str, std::regex(search), replace);
6+
return std::regex_replace(str, std::regex(search), replace);
77
}
88

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)
1010
{
11-
return std::regex_replace(wstr, std::wregex(search), replace);
11+
return std::regex_replace(wstr, std::wregex(search), replace);
1212
}
1313

14-
std::wstring StringHelper::ToWstring(const std::string& str, UINT codePage)
14+
std::wstring StringHelper::ToWstring(const std::string &str, UINT codePage)
1515
{
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;
3477
}
3578

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)
3780
{
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;
55117
}
56118

57-
std::vector<std::string> StringHelper::Split(const std::string& input, const std::string& delim)
119+
void StringHelper::ToLower(std::string &input)
58120
{
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);
77124
}
78125

79-
std::vector<std::wstring> StringHelper::Split(const std::wstring& input, const std::wstring& delim)
126+
void StringHelper::ToLower(std::wstring &input)
80127
{
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);
99129
}

NppJSONViewer/UtilityLib/StringHelper.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@
66
class StringHelper
77
{
88
public:
9-
StringHelper() = default;
10-
~StringHelper() = default;
9+
StringHelper() = default;
10+
~StringHelper() = default;
1111

12-
static std::string ReplaceAll(const std::string& str, const std::string& search, const std::string& replace);
13-
static std::wstring ReplaceAll(const std::wstring& wstr, const std::wstring& search, const std::wstring& replace);
12+
static std::string ReplaceAll(const std::string &str, const std::string &search, const std::string &replace);
13+
static std::wstring ReplaceAll(const std::wstring &wstr, const std::wstring &search, const std::wstring &replace);
1414

15-
static std::wstring ToWstring(const std::string& str, UINT codePage = CP_THREAD_ACP);
16-
static std::string ToString(const std::wstring& wstr, UINT codePage = CP_THREAD_ACP);
15+
static std::wstring ToWstring(const std::string &str, UINT codePage = CP_THREAD_ACP);
16+
static std::string ToString(const std::wstring &wstr, UINT codePage = CP_THREAD_ACP);
1717

18-
static std::vector<std::string> Split(const std::string& input, const std::string& delim);
19-
static std::vector<std::wstring> Split(const std::wstring& input, const std::wstring& delim);
20-
};
18+
static std::vector<std::string> Split(const std::string &input, const std::string &delim);
19+
static std::vector<std::wstring> Split(const std::wstring &input, const std::wstring &delim);
20+
21+
static bool Contains(const std::string &input, const std::string &search, bool ignorecase = true);
22+
static bool Contains(const std::wstring &input, const std::wstring &search, bool ignorecase = true);
2123

24+
static void ToLower(std::string &input);
25+
static void ToLower(std::wstring &input);
26+
};

0 commit comments

Comments
 (0)