summary refs log tree commit diff stats
path: root/util.h
diff options
context:
space:
mode:
Diffstat (limited to 'util.h')
-rw-r--r--util.h60
1 files changed, 60 insertions, 0 deletions
diff --git a/util.h b/util.h new file mode 100644 index 0000000..c38f624 --- /dev/null +++ b/util.h
@@ -0,0 +1,60 @@
1#ifndef UTIL_H_1F6A84F6
2#define UTIL_H_1F6A84F6
3
4#include <string>
5#include <sstream>
6#include <iterator>
7
8template <class InputIterator>
9std::string implode(
10 InputIterator first,
11 InputIterator last,
12 std::string delimiter)
13{
14 std::stringstream result;
15
16 for (InputIterator it = first; it != last; it++)
17 {
18 if (it != first)
19 {
20 result << delimiter;
21 }
22
23 result << *it;
24 }
25
26 return result.str();
27}
28
29template <class OutputIterator>
30void split(std::string input, std::string delimiter, OutputIterator out)
31{
32 while (!input.empty())
33 {
34 int divider = input.find(delimiter);
35 if (divider == std::string::npos)
36 {
37 *out = input;
38 out++;
39
40 input = "";
41 } else {
42 *out = input.substr(0, divider);
43 out++;
44
45 input = input.substr(divider+delimiter.length());
46 }
47 }
48}
49
50template <class Container>
51Container split(std::string input, std::string delimiter)
52{
53 Container result;
54
55 split(input, delimiter, std::back_inserter(result));
56
57 return result;
58}
59
60#endif /* end of include guard: UTIL_H_1F6A84F6 */