-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.hpp
301 lines (263 loc) · 5.94 KB
/
string.hpp
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#pragma once
#include <string>
#include <string_view>
#include <vector>
namespace Moon
{
namespace String
{
//Splits string into lines
static auto ViewLines(const std::string& str, const bool& keepEmpty = true)
{
//output
std::vector<std::string_view> lines;
if (str.empty())
return lines;
//current char
const char* c = str.c_str();
//the last endline found. Is start of string by default
const char* lastPos = c;
//the end of the string
const char* cend = c + str.size();
while (c < cend)
{
if (*c == '\r')
{
//Add the current string, if the lenght is 0 and keepEmpty is false, not add
size_t lenght = c - lastPos;
if (keepEmpty || lenght > 0)
{
lines.push_back(std::string_view(lastPos, lenght));
}
//If the next char is \n, we are at a \r\n ending. Ignore next
if (c[1] == '\n')
{
c++;
}
//Ignore current endline in the next string
lastPos = c + 1;
}
else if (*c == '\n')
{
//Add the current string, if the lenght is 0 and keepEmpty is false, not add
size_t lenght = c - lastPos;
if (keepEmpty || lenght > 0)
{
lines.push_back(std::string_view(lastPos, lenght));
}
//Ignore current endline in the next string
lastPos = c + 1;
}
//Next char
c++;
}
//Store last line
size_t lenght = c - lastPos;
if (keepEmpty || lenght > 0)
{
lines.push_back(std::string_view(lastPos, c - lastPos));
}
return lines;
}
//Splits string into lines (Makes copy)
static auto GetLines(const std::string& str, bool keepempty)
{
//View lines of the string
auto lines = ViewLines(str, keepempty);
//Buffer to copy
std::vector<std::string> str_lines;
str_lines.reserve(lines.size());
//Copy the lines
for (const std::string_view& line : lines)
{
str_lines.push_back(std::string(line));
}
return str_lines;
}
template<typename T>
static std::string Join(const std::vector<T>& strings, const std::string& separator, bool ignoreEmpty)
{
size_t totalSize = 0;
size_t count = strings.size();
for (const std::string_view& str : strings)
{
totalSize += str.size();
}
totalSize += separator.size() * strings.size();
std::string output;
output.reserve(totalSize);
if (ignoreEmpty)
{
if (strings[0].size() != 0)
output.append(strings[0]);
}
else
output.append(strings[0]);
for (size_t i = 1; i < count; ++i)
{
output.append(separator);
output.append(strings[i]);
}
return output;
}
static void Remove(std::string& str, const std::string& find)
{
size_t pos = str.find(find);
while (pos != std::string::npos)
{
str.erase(pos, find.size());
pos = str.find(find, pos+find.size());
}
}
static void ReplaceDiferentSize(std::string& str, const std::string& find, const std::string replace)
{
int diference = replace.size() - find.size();
size_t index = str.find(find);
if (index == std::string::npos) return;
std::vector<size_t> indexes;
size_t count = 0;
while (index != std::string::npos)
{
indexes.push_back(index + (diference * count));
index = str.find(find, index + find.size());
++count;
}
str.reserve(str.size() + (diference * count));
for (const size_t& pos : indexes)
{
str.erase(pos, find.size());
str.insert(pos, replace);
}
}
static void ReplaceSameSize(std::string& str, const std::string& find, const std::string& replace)
{
if (find == replace) return;
size_t pos = str.find(find);
while (pos != std::string::npos)
{
str.replace(pos, replace.size(), replace);
pos = str.find(find, pos + find.size());
}
}
static void Replace(std::string& str, const std::string& find, const std::string& replace)
{
if (str.empty()) return;
if (find.empty()) return;
if (replace.empty())
{
Remove(str, find);
}
if (find.size() == replace.size())
{
ReplaceSameSize(str, find, replace);
}
else
{
ReplaceDiferentSize(str, find, replace);
}
}
template <typename T>
static void ConvertLineEnds(std::basic_string<T>& str, const uint32_t& eol)
{
for (size_t pos = 0; pos < str.size(); pos++) {
if (str[pos] == '\r')
{
if (str[pos+1] == '\n')
{
// CRLF
if (eol == 0x0d)
{
str.erase(pos + 1, 1); // Delete the LF
}
else if (eol == 0x0a)
{
str.erase(pos, 1); // Delete the CR
}
else
{
pos++;
}
}
else {
// CR
if (eol == 0x0d0a)
{
str.insert(pos + 1, 1, '\n'); // Insert LF
pos++;
}
else if (eol == 0x0a)
{
str[pos] = '\n';
}
}
}
else if (str[pos] == '\n')
{
// LF
if (eol == 0x0d0a)
{
str.insert(pos, 1, '\r'); // Insert CR
pos++;
}
else if (eol == 0x0d)
{
str[pos] = '\r';
}
}
}
}
template <typename T>
static uint32_t EOLToInt(const std::basic_string<T>& eol)
{
if (eol.size() == 0)
{
return 0;
}
else if (eol.size() == 1)
{
return eol[0];
}
else
{
return 0x0d0a;
}
}
static const char* IntToEol(const uint32_t& eol)
{
switch(eol)
{
case 0x0a:
return "\n";
break;
case 0x0d:
return "\r";
break;
default:
return "\r\n";
break;
}
}
static bool CheckEndLineCRLF(const std::string_view& str)
{
if (str.empty())
return true;
size_t crPos = str.find("\r");
size_t lfPos = str.find("\n");
//npos
if (crPos == lfPos)
return true;
while (crPos != std::string::npos || lfPos != std::string::npos)
{
if (lfPos != (crPos + 1))
return false;
crPos = str.find("\r", crPos + 1);
lfPos = str.find("\n", lfPos + 1);
}
return true;
}
static bool CheckEndLineLF(const std::string_view& str)
{
return std::string::npos != str.find("\r");
}
}
}