Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/base
diff options
context:
space:
mode:
authortatiana-yan <tatiana.kondakova@gmail.com>2019-02-14 15:32:55 +0300
committermpimenov <mpimenov@users.noreply.github.com>2019-02-18 13:42:14 +0300
commit0e658ee41115f405449b7e12b9440619173a91c1 (patch)
treec9e6e9545fae8f59a5b988bb3e9c98951755ab57 /base
parent1eb4e77239abdfcb003306a2edcc6c5f29d1fbb1 (diff)
[base] Add Replace method to UniString.
Diffstat (limited to 'base')
-rw-r--r--base/base_tests/string_utils_test.cpp66
-rw-r--r--base/string_utils.hpp25
2 files changed, 88 insertions, 3 deletions
diff --git a/base/base_tests/string_utils_test.cpp b/base/base_tests/string_utils_test.cpp
index 8e3bc04977..15dc4c850f 100644
--- a/base/base_tests/string_utils_test.cpp
+++ b/base/base_tests/string_utils_test.cpp
@@ -756,3 +756,69 @@ UNIT_TEST(CSV)
std::vector<std::string> expected3;
TEST_EQUAL(target, expected3, ());
}
+
+UNIT_TEST(UniString_Replace)
+{
+ std::vector<std::string> const testStrings = {
+ "longlong",
+ "ss",
+ "samesize",
+ "sometext longlong",
+ "sometext ss",
+ "sometext samesize",
+ "longlong sometext",
+ "ss sometext",
+ "samesize sometext",
+ "longlong ss samesize",
+ "sometext longlong sometext ss samesize sometext",
+ "длинная строка",
+ "к с",
+ "такая же строка",
+ "sometext длинная строка",
+ "sometext к с",
+ "sometext такая же строка",
+ "длинная строка sometext",
+ "к с sometext",
+ "samesize sometext",
+ "длинная строка к с samesize",
+ "sometext длинная строка sometext к с такая же строка sometext"};
+
+ std::vector<std::pair<std::string, std::string>> const replacements = {
+ {"longlong", "ll"},
+ {"ss", "shortshort"},
+ {"samesize", "sizesame"},
+ {"длинная строка", "д с"},
+ {"к с", "короткая строка"},
+ {"такая же строка", "строка такая же"}};
+
+ for (auto testString : testStrings)
+ {
+ auto uniStr = strings::MakeUniString(testString);
+ for (auto const & r : replacements)
+ {
+ {
+ auto const toReplace = strings::MakeUniString(r.first);
+ auto const replacement = strings::MakeUniString(r.second);
+ auto & str = uniStr;
+ auto start = std::search(str.begin(), str.end(), toReplace.begin(), toReplace.end());
+ if (start != str.end())
+ {
+ auto end = start + toReplace.size();
+ str.Replace(start, end, replacement.begin(), replacement.end());
+ }
+ }
+ {
+ auto const toReplace = r.first;
+ auto const replacement = r.second;
+ auto & str = testString;
+ auto start = std::search(str.begin(), str.end(), toReplace.begin(), toReplace.end());
+ if (start != str.end())
+ {
+ auto end = start + toReplace.size();
+ str.replace(start, end, replacement.begin(), replacement.end());
+ }
+ }
+ }
+ TEST_EQUAL(testString, ToUtf8(uniStr), ());
+ }
+}
diff --git a/base/string_utils.hpp b/base/string_utils.hpp
index b82eafdd1a..d405d8d471 100644
--- a/base/string_utils.hpp
+++ b/base/string_utils.hpp
@@ -32,9 +32,8 @@ public:
UniString() {}
explicit UniString(size_t n, UniChar c = UniChar()) : BaseT(n, c) {}
- template <class IterT>
- UniString(IterT b, IterT e)
- : BaseT(b, e)
+ template <typename Iter>
+ UniString(Iter b, Iter e) : BaseT(b, e)
{
}
@@ -52,6 +51,26 @@ public:
result += rhs;
return result;
}
+
+ template <class Iter>
+ void Replace(iterator first, iterator last, Iter first2, Iter last2)
+ {
+ auto it = first;
+ auto it2 = first2;
+ for (; it < last && it2 < last2; ++it, ++it2)
+ *it = *it2;
+
+ if (it == last && it2 == last2)
+ return;
+
+ if (it == last)
+ {
+ insert(it, it2, last2);
+ return;
+ }
+
+ erase(it, last);
+ }
};
/// Performs full case folding for string to make it search-compatible according