Fix full_split.

GitOrigin-RevId: 0204740d8ba45faf9f2da5ddca7323c88287a160
This commit is contained in:
levlam 2018-09-22 14:24:02 +03:00
parent 9c9ee999c4
commit 8610734232
3 changed files with 50 additions and 6 deletions

View File

@ -143,7 +143,7 @@ class MapDownloadGenerateActor : public FileGenerateActor {
Result<tl_object_ptr<telegram_api::inputWebFileGeoPointLocation>> parse_conversion() { Result<tl_object_ptr<telegram_api::inputWebFileGeoPointLocation>> parse_conversion() {
auto parts = full_split(Slice(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"); return Status::Error("Wrong conversion");
} }

View File

@ -34,13 +34,17 @@ std::pair<T, T> split(T s, char delimiter = ' ') {
template <class T> template <class T>
vector<T> full_split(T s, char delimiter = ' ') { vector<T> full_split(T s, char delimiter = ' ') {
T next;
vector<T> result; vector<T> result;
while (!s.empty()) { while (true) {
std::tie(next, s) = split(s, delimiter); auto delimiter_pos = s.find(delimiter);
result.push_back(next); if (delimiter_pos == string::npos) {
} result.push_back(std::move(s));
return result; return result;
} else {
result.push_back(s.substr(0, delimiter_pos));
s = s.substr(delimiter_pos + 1);
}
}
} }
string implode(vector<string> v, char delimiter = ' '); string implode(vector<string> v, char delimiter = ' ');

View File

@ -450,3 +450,43 @@ TEST(Misc, IPAddress_get_ipv4) {
test_get_ipv4(0x04030201); test_get_ipv4(0x04030201);
test_get_ipv4(0xFFFFFFFF); test_get_ipv4(0xFFFFFFFF);
} }
static void test_split(Slice str, std::pair<Slice, Slice> 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<Slice> 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", "", ""});
}