diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2012-05-29 05:19:50 +0000 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2012-05-29 05:19:50 +0000 |
| commit | 62202a46c113b06cccf9196804f88c923eae8cc3 (patch) | |
| tree | 9d5a43c4f11071969d914e4dfc56998653290311 /src/string.cc | |
| parent | 96c440fcaa72113e974b4bf467f708a64e0b0ac4 (diff) | |
Add some string helpers and unit tests
functions int_to_str(int) and split(const String&, Character),
plus corresponding unit tests
Diffstat (limited to 'src/string.cc')
| -rw-r--r-- | src/string.cc | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/src/string.cc b/src/string.cc new file mode 100644 index 00000000..30654256 --- /dev/null +++ b/src/string.cc @@ -0,0 +1,46 @@ +#include "string.hh" + +namespace Kakoune +{ + +String int_to_str(int value) +{ + const bool negative = value < 0; + if (negative) + value = -value; + + char buffer[16]; + size_t pos = sizeof(buffer); + buffer[--pos] = 0; + do + { + buffer[--pos] = '0' + (value % 10); + value /= 10; + } + while (value); + + if (negative) + buffer[--pos] = '-'; + + return String(buffer + pos); +} + +std::vector<String> split(const String& str, Character separator) +{ + auto begin = str.begin(); + auto end = str.begin(); + + std::vector<String> res; + while (end != str.end()) + { + while (end != str.end() and *end != separator) + ++end; + res.push_back(String(begin, end)); + if (end == str.end()) + break; + begin = ++end; + } + return res; +} + +} |
