diff options
| author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2017-01-09 14:01:51 -0500 |
|---|---|---|
| committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2017-01-09 14:01:51 -0500 |
| commit | 46b7119fab32b05923dbf9cda7cd6ce62f572846 (patch) | |
| tree | 31ac6c281869085e5dbc6b210886c554d4c52cad /util.h | |
| download | cadence-46b7119fab32b05923dbf9cda7cd6ce62f572846.tar.gz cadence-46b7119fab32b05923dbf9cda7cd6ce62f572846.tar.bz2 cadence-46b7119fab32b05923dbf9cda7cd6ce62f572846.zip | |
Initial commit
Diffstat (limited to 'util.h')
| -rw-r--r-- | util.h | 68 |
1 files changed, 68 insertions, 0 deletions
| diff --git a/util.h b/util.h new file mode 100644 index 0000000..256456f --- /dev/null +++ b/util.h | |||
| @@ -0,0 +1,68 @@ | |||
| 1 | #ifndef UTIL_H_CED7A66D | ||
| 2 | #define UTIL_H_CED7A66D | ||
| 3 | |||
| 4 | namespace cadence { | ||
| 5 | |||
| 6 | inline std::string uppercase(std::string in) | ||
| 7 | { | ||
| 8 | std::string result; | ||
| 9 | std::transform(std::begin(in), std::end(in), std::back_inserter(result), [] (char ch) | ||
| 10 | { | ||
| 11 | return std::toupper(ch); | ||
| 12 | }); | ||
| 13 | |||
| 14 | return result; | ||
| 15 | } | ||
| 16 | |||
| 17 | template <class InputIterator> | ||
| 18 | std::string implode(InputIterator first, InputIterator last, std::string delimiter) | ||
| 19 | { | ||
| 20 | std::stringstream result; | ||
| 21 | |||
| 22 | for (InputIterator it = first; it != last; it++) | ||
| 23 | { | ||
| 24 | if (it != first) | ||
| 25 | { | ||
| 26 | result << delimiter; | ||
| 27 | } | ||
| 28 | |||
| 29 | result << *it; | ||
| 30 | } | ||
| 31 | |||
| 32 | return result.str(); | ||
| 33 | } | ||
| 34 | |||
| 35 | template <class OutputIterator> | ||
| 36 | void split(std::string input, std::string delimiter, OutputIterator out) | ||
| 37 | { | ||
| 38 | while (!input.empty()) | ||
| 39 | { | ||
| 40 | int divider = input.find(delimiter); | ||
| 41 | if (divider == std::string::npos) | ||
| 42 | { | ||
| 43 | *out = input; | ||
| 44 | out++; | ||
| 45 | |||
| 46 | input = ""; | ||
| 47 | } else { | ||
| 48 | *out = input.substr(0, divider); | ||
| 49 | out++; | ||
| 50 | |||
| 51 | input = input.substr(divider+delimiter.length()); | ||
| 52 | } | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | template <class Container> | ||
| 57 | Container split(std::string input, std::string delimiter) | ||
| 58 | { | ||
| 59 | Container result; | ||
| 60 | |||
| 61 | split(input, delimiter, std::back_inserter(result)); | ||
| 62 | |||
| 63 | return result; | ||
| 64 | } | ||
| 65 | |||
| 66 | }; | ||
| 67 | |||
| 68 | #endif /* end of include guard: UTIL_H_CED7A66D */ | ||
