-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_utils.cpp
More file actions
81 lines (69 loc) · 2.38 KB
/
string_utils.cpp
File metadata and controls
81 lines (69 loc) · 2.38 KB
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
#include "string_utils.hpp"
std::vector<std::string> split(std::string string, const std::string& delimitter, int times_to_split) {
std::vector<std::string> split_string;
std::string::size_type i;
while (times_to_split != 0) {
if ((i = string.find(delimitter)) == std::string::npos) {
break;
}
split_string.push_back(string.substr(0, i));
string.erase(0, i+delimitter.size());
--times_to_split;
}
split_string.push_back(string);
return split_string;
}
std::vector<std::string> split(std::string string, const std::initializer_list<std::string>& delimitters, int times_to_split) {
std::vector<std::string> split_string;
std::string::size_type closest = string.length();
std::string::size_type curr_idx;
std::string_view curr_delim;
bool end = false;
while (!end && times_to_split != 0) {
end = true;
for (const std::string& d : delimitters) {
curr_idx = string.find(d);
if (curr_idx != std::string::npos && curr_idx < closest) {
closest = curr_idx;
curr_delim = d;
end = false;
}
}
if (end) {
break;
}
split_string.push_back(string.substr(0, closest));
string.erase(0, closest+curr_delim.size());
closest = string.size();
--times_to_split;
}
split_string.push_back(string);
return split_string;
}
std::vector<std::string> split(std::string string, const std::vector<std::string>& delimitters, int times_to_split) {
std::vector<std::string> split_string;
std::string::size_type closest = string.length();
std::string::size_type curr_idx;
std::string_view curr_delim;
bool end = false;
while (!end && times_to_split != 0) {
end = true;
for (const std::string& d : delimitters) {
curr_idx = string.find(d);
if (curr_idx != std::string::npos && curr_idx < closest) {
closest = curr_idx;
curr_delim = d;
end = false;
}
}
if (end) {
break;
}
split_string.push_back(string.substr(0, closest));
string.erase(0, closest+curr_delim.size());
closest = string.size();
--times_to_split;
}
split_string.push_back(string);
return split_string;
}