From 8610734232fc232660cf4a4a1e15d40d19b8a80e Mon Sep 17 00:00:00 2001 From: levlam Date: Sat, 22 Sep 2018 14:24:02 +0300 Subject: [PATCH] Fix full_split. GitOrigin-RevId: 0204740d8ba45faf9f2da5ddca7323c88287a160 --- td/telegram/files/FileGenerateManager.cpp | 2 +- tdutils/td/utils/misc.h | 14 +++++--- tdutils/test/misc.cpp | 40 +++++++++++++++++++++++ 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/td/telegram/files/FileGenerateManager.cpp b/td/telegram/files/FileGenerateManager.cpp index b00fc357..b61f5362 100644 --- a/td/telegram/files/FileGenerateManager.cpp +++ b/td/telegram/files/FileGenerateManager.cpp @@ -143,7 +143,7 @@ class MapDownloadGenerateActor : public FileGenerateActor { Result> parse_conversion() { auto parts = full_split(Slice(conversion_), '#'); - if (parts.size() != 8 || !parts[0].empty() || parts[1] != "map") { + if (parts.size() != 9 || !parts[0].empty() || parts[1] != "map" || !parts[8].empty()) { return Status::Error("Wrong conversion"); } diff --git a/tdutils/td/utils/misc.h b/tdutils/td/utils/misc.h index b3b869ae..22f049ab 100644 --- a/tdutils/td/utils/misc.h +++ b/tdutils/td/utils/misc.h @@ -34,13 +34,17 @@ std::pair split(T s, char delimiter = ' ') { template vector full_split(T s, char delimiter = ' ') { - T next; vector result; - while (!s.empty()) { - std::tie(next, s) = split(s, delimiter); - result.push_back(next); + while (true) { + auto delimiter_pos = s.find(delimiter); + if (delimiter_pos == string::npos) { + result.push_back(std::move(s)); + return result; + } else { + result.push_back(s.substr(0, delimiter_pos)); + s = s.substr(delimiter_pos + 1); + } } - return result; } string implode(vector v, char delimiter = ' '); diff --git a/tdutils/test/misc.cpp b/tdutils/test/misc.cpp index 1ac2174e..8f687adc 100644 --- a/tdutils/test/misc.cpp +++ b/tdutils/test/misc.cpp @@ -450,3 +450,43 @@ TEST(Misc, IPAddress_get_ipv4) { test_get_ipv4(0x04030201); test_get_ipv4(0xFFFFFFFF); } + +static void test_split(Slice str, std::pair expected) { + ASSERT_EQ(expected, td::split(str)); +} + +TEST(Misc, split) { + test_split("", {"", ""}); + test_split(" ", {"", ""}); + test_split("abcdef", {"abcdef", ""}); + test_split("abc def", {"abc", "def"}); + test_split("a bcdef", {"a", "bcdef"}); + test_split(" abcdef", {"", "abcdef"}); + test_split("abcdef ", {"abcdef", ""}); + test_split("ab cd ef", {"ab", "cd ef"}); + test_split("ab cdef ", {"ab", "cdef "}); + test_split(" abcd ef", {"", "abcd ef"}); + test_split(" abcdef ", {"", "abcdef "}); +} + + +static void test_full_split(Slice str, vector expected) { + ASSERT_EQ(expected, td::full_split(str)); +} + +TEST(Misc, full_split) { + test_full_split("", {""}); + test_full_split(" ", {"", ""}); + test_full_split(" ", {"", "", ""}); + test_full_split("abcdef", {"abcdef"}); + test_full_split("abc def", {"abc", "def"}); + test_full_split("a bcdef", {"a", "bcdef"}); + test_full_split(" abcdef", {"", "abcdef"}); + test_full_split("abcdef ", {"abcdef", ""}); + test_full_split("ab cd ef", {"ab", "cd", "ef"}); + test_full_split("ab cdef ", {"ab", "cdef", ""}); + test_full_split(" abcd ef", {"", "abcd", "ef"}); + test_full_split(" abcdef ", {"", "abcdef", ""}); + test_full_split(" ab cd ef ", {"", "ab", "cd", "ef", ""}); + test_full_split(" ab cd ef ", {"", "", "ab", "", "cd", "", "ef", "", ""}); +}