From ef708dd1307c7b2146eb3ab518b3c932dd8b6996 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 27 Mar 2018 15:28:43 -0400 Subject: Added string utils (uppercase, split, implode) --- hkutil/string.h | 87 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 hkutil/string.h 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 @@ +#ifndef UTIL_H_CED7A66D +#define UTIL_H_CED7A66D + +#include +#include +#include +#include +#include + +namespace hatkirby { + + inline std::string uppercase(std::string in) + { + std::string result; + + std::transform( + std::begin(in), + std::end(in), + std::back_inserter(result), + [] (char ch) + { + return std::toupper(ch); + }); + + return result; + } + + template + std::string implode( + InputIterator first, + InputIterator last, + std::string delimiter) + { + std::stringstream result; + + for (InputIterator it = first; it != last; it++) + { + if (it != first) + { + result << delimiter; + } + + result << *it; + } + + return result.str(); + } + + template + void split( + std::string input, + std::string delimiter, + OutputIterator out) + { + while (!input.empty()) + { + int divider = input.find(delimiter); + if (divider == std::string::npos) + { + *out = input; + out++; + + input = ""; + } else { + *out = input.substr(0, divider); + out++; + + input = input.substr(divider+delimiter.length()); + } + } + } + + template + Container split( + std::string input, + std::string delimiter) + { + Container result; + + split(input, delimiter, std::back_inserter(result)); + + return result; + } + +}; + +#endif /* end of include guard: UTIL_H_CED7A66D */ -- cgit 1.4.1