diff options
author | Ilya Zverev <zverik@textual.ru> | 2016-06-21 18:48:36 +0300 |
---|---|---|
committer | Ilya Zverev <zverik@textual.ru> | 2016-06-28 15:19:13 +0300 |
commit | 3fac0435788352bd5cdc67764d9f17b59cbdf501 (patch) | |
tree | 81eb4e3dcc0f18be3d33527a3d7338a4e98e835a /base | |
parent | fb53332984f36004d6ff4e3acbc7cb07d9fec3e9 (diff) |
[booking] Save all translations we've got
Diffstat (limited to 'base')
-rw-r--r-- | base/base_tests/string_utils_test.cpp | 14 | ||||
-rw-r--r-- | base/string_utils.cpp | 13 | ||||
-rw-r--r-- | base/string_utils.hpp | 3 |
3 files changed, 30 insertions, 0 deletions
diff --git a/base/base_tests/string_utils_test.cpp b/base/base_tests/string_utils_test.cpp index 1f1aa05bc2..49720cc45b 100644 --- a/base/base_tests/string_utils_test.cpp +++ b/base/base_tests/string_utils_test.cpp @@ -729,3 +729,17 @@ UNIT_TEST(NormalizeDigits_UniString) TEST_EQUAL(nd("a0192 "), "a0192 ", ()); TEST_EQUAL(nd("3456789"), "3456789", ()); } + +UNIT_TEST(Split) +{ + vector<string> target; + strings::Split(";Test\\;проверка;0;", ';', target); + vector<string> expected({"", "Test\\", "проверка", "0", ""}); + TEST_EQUAL(target, expected, ()); + strings::Split("and there was none", ' ', target); + vector<string> expected2({"and", "there", "", "was", "none"}); + TEST_EQUAL(target, expected2, ()); + strings::Split("", '!', target); + vector<string> expected3; + TEST_EQUAL(target, expected3, ()); +} diff --git a/base/string_utils.cpp b/base/string_utils.cpp index 7b6b808995..2a8b029f37 100644 --- a/base/string_utils.cpp +++ b/base/string_utils.cpp @@ -328,4 +328,17 @@ bool AlmostEqual(string const & str1, string const & str2, size_t mismatchedCoun return false; } +void Split(string const & s, char delimiter, vector<string> & target) +{ + target.clear(); + + // Special case: if the string is empty, return an empty array instead of {""}. + if (s.empty()) + return; + + using It = TokenizeIterator<SimpleDelimiter, string::const_iterator, true>; + for (It it(s, SimpleDelimiter(delimiter)); it; ++it) + target.push_back(*it); +} + } // namespace strings diff --git a/base/string_utils.hpp b/base/string_utils.hpp index d557706d85..e42c8188e8 100644 --- a/base/string_utils.hpp +++ b/base/string_utils.hpp @@ -306,6 +306,9 @@ void Tokenize(string const & str, char const * delims, TFunctor && f) } } +/// Splits a string by the delimiter, keeps empty parts, on an empty string returns an empty vector. +void Split(string const & s, char delimiter, vector<string> & target); + /// @return code of last symbol in string or 0 if s is empty UniChar LastUniChar(string const & s); |