diff --git a/td/telegram/files/FileGenerateManager.cpp b/td/telegram/files/FileGenerateManager.cpp index b00fc3574..b61f5362b 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 b3b869ae3..22f049ab4 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 1ac2174e2..8f687adce 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", "", ""}); +}