diff options
Diffstat (limited to 'src/util.h')
-rw-r--r-- | src/util.h | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/src/util.h b/src/util.h index 150a6a5..1eb2303 100644 --- a/src/util.h +++ b/src/util.h | |||
@@ -1,6 +1,9 @@ | |||
1 | #ifndef UTIL_H_E9110D4C | 1 | #ifndef UTIL_H_E9110D4C |
2 | #define UTIL_H_E9110D4C | 2 | #define UTIL_H_E9110D4C |
3 | 3 | ||
4 | #include <string> | ||
5 | #include <iterator> | ||
6 | |||
4 | template <typename Container, typename Predicate> | 7 | template <typename Container, typename Predicate> |
5 | void erase_if(Container& items, const Predicate& predicate) | 8 | void erase_if(Container& items, const Predicate& predicate) |
6 | { | 9 | { |
@@ -17,4 +20,36 @@ void erase_if(Container& items, const Predicate& predicate) | |||
17 | } | 20 | } |
18 | }; | 21 | }; |
19 | 22 | ||
23 | template <class OutputIterator> | ||
24 | void splitStr( | ||
25 | std::string input, | ||
26 | std::string delimiter, | ||
27 | OutputIterator out) { | ||
28 | while (!input.empty()) { | ||
29 | int divider = input.find(delimiter); | ||
30 | if (divider == std::string::npos) { | ||
31 | *out = input; | ||
32 | out++; | ||
33 | |||
34 | input = ""; | ||
35 | } else { | ||
36 | *out = input.substr(0, divider); | ||
37 | out++; | ||
38 | |||
39 | input = input.substr(divider+delimiter.length()); | ||
40 | } | ||
41 | } | ||
42 | } | ||
43 | |||
44 | template <class Container> | ||
45 | Container splitStr( | ||
46 | std::string input, | ||
47 | std::string delimiter) { | ||
48 | Container result; | ||
49 | |||
50 | splitStr(input, delimiter, std::back_inserter(result)); | ||
51 | |||
52 | return result; | ||
53 | } | ||
54 | |||
20 | #endif /* end of include guard: UTIL_H_E9110D4C */ | 55 | #endif /* end of include guard: UTIL_H_E9110D4C */ |