diff options
Diffstat (limited to 'src/util.h')
-rw-r--r-- | src/util.h | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src/util.h b/src/util.h new file mode 100644 index 0000000..8cdad3d --- /dev/null +++ b/src/util.h | |||
@@ -0,0 +1,39 @@ | |||
1 | #ifndef UTIL_H_4AC35025 | ||
2 | #define UTIL_H_4AC35025 | ||
3 | |||
4 | #include <string> | ||
5 | #include <iterator> | ||
6 | |||
7 | template <class OutputIterator> | ||
8 | void splitStr( | ||
9 | std::string input, | ||
10 | std::string delimiter, | ||
11 | OutputIterator out) { | ||
12 | while (!input.empty()) { | ||
13 | int divider = input.find(delimiter); | ||
14 | if (divider == std::string::npos) { | ||
15 | *out = input; | ||
16 | out++; | ||
17 | |||
18 | input = ""; | ||
19 | } else { | ||
20 | *out = input.substr(0, divider); | ||
21 | out++; | ||
22 | |||
23 | input = input.substr(divider+delimiter.length()); | ||
24 | } | ||
25 | } | ||
26 | } | ||
27 | |||
28 | template <class Container> | ||
29 | Container splitStr( | ||
30 | std::string input, | ||
31 | std::string delimiter) { | ||
32 | Container result; | ||
33 | |||
34 | splitStr(input, delimiter, std::back_inserter(result)); | ||
35 | |||
36 | return result; | ||
37 | } | ||
38 | |||
39 | #endif /* end of include guard: UTIL_H_4AC35025 */ | ||