diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2018-03-27 15:28:43 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2018-03-27 15:28:43 -0400 |
commit | ef708dd1307c7b2146eb3ab518b3c932dd8b6996 (patch) | |
tree | ec4499a581b12e7d724b745cd797f003342486cb | |
parent | 1ba9210ca8cd39bde458957c2c244f0c6f8e273f (diff) | |
download | hkutil-ef708dd1307c7b2146eb3ab518b3c932dd8b6996.tar.gz hkutil-ef708dd1307c7b2146eb3ab518b3c932dd8b6996.tar.bz2 hkutil-ef708dd1307c7b2146eb3ab518b3c932dd8b6996.zip |
Added string utils (uppercase, split, implode)
-rw-r--r-- | hkutil/string.h | 87 |
1 files changed, 87 insertions, 0 deletions
diff --git a/hkutil/string.h b/hkutil/string.h new file mode 100644 index 0000000..f8264ac --- /dev/null +++ b/hkutil/string.h | |||
@@ -0,0 +1,87 @@ | |||
1 | #ifndef UTIL_H_CED7A66D | ||
2 | #define UTIL_H_CED7A66D | ||
3 | |||
4 | #include <algorithm> | ||
5 | #include <string> | ||
6 | #include <iterator> | ||
7 | #include <cctype> | ||
8 | #include <sstream> | ||
9 | |||
10 | namespace hatkirby { | ||
11 | |||
12 | inline std::string uppercase(std::string in) | ||
13 | { | ||
14 | std::string result; | ||
15 | |||
16 | std::transform( | ||
17 | std::begin(in), | ||
18 | std::end(in), | ||
19 | std::back_inserter(result), | ||
20 | [] (char ch) | ||
21 | { | ||
22 | return std::toupper(ch); | ||
23 | }); | ||
24 | |||
25 | return result; | ||
26 | } | ||
27 | |||
28 | template <class InputIterator> | ||
29 | std::string implode( | ||
30 | InputIterator first, | ||
31 | InputIterator last, | ||
32 | std::string delimiter) | ||
33 | { | ||
34 | std::stringstream result; | ||
35 | |||
36 | for (InputIterator it = first; it != last; it++) | ||
37 | { | ||
38 | if (it != first) | ||
39 | { | ||
40 | result << delimiter; | ||
41 | } | ||
42 | |||
43 | result << *it; | ||
44 | } | ||
45 | |||
46 | return result.str(); | ||
47 | } | ||
48 | |||
49 | template <class OutputIterator> | ||
50 | void split( | ||
51 | std::string input, | ||
52 | std::string delimiter, | ||
53 | OutputIterator out) | ||
54 | { | ||
55 | while (!input.empty()) | ||
56 | { | ||
57 | int divider = input.find(delimiter); | ||
58 | if (divider == std::string::npos) | ||
59 | { | ||
60 | *out = input; | ||
61 | out++; | ||
62 | |||
63 | input = ""; | ||
64 | } else { | ||
65 | *out = input.substr(0, divider); | ||
66 | out++; | ||
67 | |||
68 | input = input.substr(divider+delimiter.length()); | ||
69 | } | ||
70 | } | ||
71 | } | ||
72 | |||
73 | template <class Container> | ||
74 | Container split( | ||
75 | std::string input, | ||
76 | std::string delimiter) | ||
77 | { | ||
78 | Container result; | ||
79 | |||
80 | split(input, delimiter, std::back_inserter(result)); | ||
81 | |||
82 | return result; | ||
83 | } | ||
84 | |||
85 | }; | ||
86 | |||
87 | #endif /* end of include guard: UTIL_H_CED7A66D */ | ||