-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathstringhelper.cpp
167 lines (138 loc) · 3.71 KB
/
stringhelper.cpp
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
156
157
158
159
160
161
162
163
164
165
166
167
#include "stringhelper.h"
#include <sstream>
using namespace std;
template <typename charT>
vector<basic_string<charT>> split(const basic_string<charT> &s, charT delim)
{
vector<basic_string<charT>> elems;
basic_stringstream<charT> ss(s);
basic_string<charT> item;
while (getline(ss, item, delim))
{
elems.push_back(item);
}
return elems;
}
vector<string> &split(const string &s, char delim, vector<string> &elems)
{
stringstream ss(s);
string item;
while (getline(ss, item, delim))
{
elems.push_back(item);
}
return elems;
}
vector<string> split(const string &s, char delim)
{
vector<string> elems;
return split(s, delim, elems);
}
vector<wstring> split(const wstring &s, wchar_t delim)
{
vector<wstring> elems;
wstringstream ss(s);
wstring item;
while (getline(ss, item, delim))
{
elems.push_back(item);
}
return elems;
}
string &strreplace(string &input, char oldChar, char newChar)
{
for (int i = 0; i < input.length(); i++)
{
if(input.at(i) == oldChar)
{
input.replace(i, 1, 1, newChar);
}
}
return input;
}
wstring &strreplace(wstring &input, char oldChar, char newChar)
{
for (int i = 0; i < input.length(); i++)
{
if (input.at(i) == oldChar)
{
input.replace(i, 1, 1, newChar);
}
}
return input;
}
void strSplitLines(string &input, vector<string> &v)
{
string line;
stringstream wss(input);
while(getline(wss, line, '\r'))
{
// Ignore empty lines
if(line.length() > 0)
{
v.push_back(line);
}
}
}
void replaceAll(std::string& str, const std::string& from, const std::string& to) {
if(from.empty())
return;
size_t start_pos = 0;
while((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
}
}
void replaceAll(std::wstring& str, const std::wstring& from, const std::wstring& to) {
if (from.empty())
return;
size_t start_pos = 0;
while ((start_pos = str.find(from, start_pos)) != std::string::npos) {
str.replace(start_pos, from.length(), to);
start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
}
}
void StrToUpper(string &input)
{
for (int i = 0; i < input.length(); i++)
{
input.replace(i, 1, 1, toupper(input.at(i)));
}
}
void splitFilePath(wstring &filepath, wstring &folderpath, wstring &filename, wstring &filenamenoext, wstring &ext)
{
size_t index = filepath.find_last_of(L"/\\");
folderpath = filepath.substr(0, index);
filename = filepath.substr(index + 1);
index = filename.find_last_of('.');
filenamenoext = filename.substr(0, index);
ext = filename.substr(index + 1);
}
std::string trim(const std::string& str,
const std::string& whitespace)
{
const auto strBegin = str.find_first_not_of(whitespace);
if (strBegin == std::string::npos)
return ""; // no content
const auto strEnd = str.find_last_not_of(whitespace);
const auto strRange = strEnd - strBegin + 1;
return str.substr(strBegin, strRange);
}
std::string reduce(const std::string& str,
const std::string& fill,
const std::string& whitespace)
{
// trim first
auto result = trim(str, whitespace);
// replace sub ranges
auto beginSpace = result.find_first_of(whitespace);
while (beginSpace != std::string::npos)
{
const auto endSpace = result.find_first_not_of(whitespace, beginSpace);
const auto range = endSpace - beginSpace;
result.replace(beginSpace, range, fill);
const auto newStart = beginSpace + fill.length();
beginSpace = result.find_first_of(whitespace, newStart);
}
return result;
}