-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstring_util_split.h
46 lines (34 loc) · 1 KB
/
string_util_split.h
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
#ifndef STRING_UTIL_SPLIT_H
#define STRING_UTIL_SPLIT_H
#include <string>
#include <vector>
#include <stdexcept>
namespace string_util {
template<typename CharT, typename Traits, typename Alloc>
std::vector<std::basic_string<CharT, Traits, Alloc> >
split(const std::basic_string<CharT, Traits, Alloc>& s,
const CharT* delimiter) {
using _String = std::basic_string<CharT, Traits, Alloc>;
auto delimiter_size = _String(delimiter).size();
if (delimiter_size == 0) {
throw std::invalid_argument("empty delimiter");
}
std::vector<_String> results;
typename _String::size_type pos = 0;
while (true) {
typename _String::size_type next = s.find(delimiter, pos);
if (next == _String::npos) {
results.push_back(s.substr(pos));
break;
}
results.push_back(s.substr(pos, next - pos));
pos = next + delimiter_size;
}
return results;
}
inline std::vector<std::string>
split(const char* s, const char* delimiter) {
return split(std::string(s), delimiter);
}
}
#endif