diff --git a/src/option_manager.cc b/src/option_manager.cc index ee329d89..8e55c2eb 100644 --- a/src/option_manager.cc +++ b/src/option_manager.cc @@ -5,13 +5,6 @@ namespace Kakoune { -String int_to_str(int value) -{ - std::ostringstream oss; - oss << value; - return oss.str(); -} - Option& OptionManager::operator[] (const String& name) { auto it = m_options.find(name); 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 split(const String& str, Character separator) +{ + auto begin = str.begin(); + auto end = str.begin(); + + std::vector 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; +} + +} diff --git a/src/string.hh b/src/string.hh index d751d9ce..64ee9992 100644 --- a/src/string.hh +++ b/src/string.hh @@ -109,6 +109,9 @@ inline String operator+(Character lhs, const String& rhs) return String(lhs) + rhs; } +String int_to_str(int value); +std::vector split(const String& str, Character separator); + } namespace std diff --git a/src/unit_tests.cc b/src/unit_tests.cc index 8fee1000..a3bfc427 100644 --- a/src/unit_tests.cc +++ b/src/unit_tests.cc @@ -45,8 +45,24 @@ void test_editor() } } +void test_string() +{ + assert(int_to_str(124) == "124"); + assert(int_to_str(-129) == "-129"); + assert(int_to_str(0) == "0"); + + assert(String("youpi ") + "matin" == "youpi matin"); + + std::vector splited = split("youpi:matin::tchou", ':'); + assert(splited[0] == "youpi"); + assert(splited[1] == "matin"); + assert(splited[2] == ""); + assert(splited[3] == "tchou"); +} + void run_unit_tests() { + test_string(); test_buffer(); test_editor(); }