forked from organicmaps/organicmaps
[coding] Add StringUtf8Multilang::RemoveString.
This commit is contained in:
parent
4a4c8f750a
commit
a0edccb21d
3 changed files with 85 additions and 0 deletions
|
@ -194,3 +194,61 @@ UNIT_TEST(MultilangString_ForEachLanguage)
|
|||
TEST(!s.ForEachLanguage(corruptedLanguages, fn), ());
|
||||
TEST_EQUAL(testAccumulator.size(), 0, ());
|
||||
}
|
||||
|
||||
UNIT_TEST(MultilangString_RemoveString)
|
||||
{
|
||||
auto testRemove = [](vector<pair<uint8_t, string>> const & strings,
|
||||
set<uint8_t> const & codesToRemove) {
|
||||
StringUtf8Multilang str;
|
||||
for (auto const & s : strings)
|
||||
str.AddString(s.first, s.second);
|
||||
|
||||
string tmp;
|
||||
for (auto const & s : strings)
|
||||
{
|
||||
TEST(str.HasString(s.first), ());
|
||||
TEST(str.GetString(s.first, tmp), ());
|
||||
TEST_EQUAL(tmp, s.second, ());
|
||||
}
|
||||
|
||||
for (auto c : codesToRemove)
|
||||
str.RemoveString(c);
|
||||
|
||||
for (auto const & s : strings)
|
||||
{
|
||||
if (codesToRemove.find(s.first) == codesToRemove.end())
|
||||
{
|
||||
TEST(str.HasString(s.first), ());
|
||||
TEST(str.GetString(s.first, tmp), ());
|
||||
TEST_EQUAL(tmp, s.second, ());
|
||||
}
|
||||
else
|
||||
{
|
||||
TEST(!str.HasString(s.first), ());
|
||||
}
|
||||
}
|
||||
|
||||
// No extra languages or other data damage.
|
||||
str.ForEach([&](uint8_t lang, auto const &) {
|
||||
TEST(base::FindIf(strings, [&lang](auto const & s) { return s.first == lang; }) !=
|
||||
strings.end(),
|
||||
());
|
||||
TEST(codesToRemove.find(lang) == codesToRemove.end(), ());
|
||||
});
|
||||
};
|
||||
|
||||
vector<pair<uint8_t, string>> strings = {{0, "aaa"},
|
||||
{1, "bbb"},
|
||||
{2, "ccc"},
|
||||
{9, "ddd"},
|
||||
{17, "eee"},
|
||||
{27, "fff"},
|
||||
{37, "ggg"}};
|
||||
|
||||
testRemove(strings, {0});
|
||||
testRemove(strings, {1});
|
||||
testRemove(strings, {9, 27});
|
||||
testRemove(strings, {37});
|
||||
testRemove(strings, {0, 1, 2, 9, 17, 27, 37});
|
||||
testRemove(strings, {39});
|
||||
}
|
||||
|
|
|
@ -207,6 +207,25 @@ void StringUtf8Multilang::AddString(int8_t lang, string const & utf8s)
|
|||
m_s.insert(m_s.end(), utf8s.begin(), utf8s.end());
|
||||
}
|
||||
|
||||
void StringUtf8Multilang::RemoveString(int8_t lang)
|
||||
{
|
||||
size_t i = 0;
|
||||
size_t const sz = m_s.size();
|
||||
|
||||
while (i < sz)
|
||||
{
|
||||
size_t const next = GetNextIndex(i);
|
||||
|
||||
if ((m_s[i] & 0x3F) == lang)
|
||||
{
|
||||
m_s.erase(i, next - i);
|
||||
return;
|
||||
}
|
||||
|
||||
i = next;
|
||||
}
|
||||
}
|
||||
|
||||
bool StringUtf8Multilang::GetString(int8_t lang, string & utf8s) const
|
||||
{
|
||||
if (!IsSupportedLangCode(lang))
|
||||
|
|
|
@ -120,6 +120,14 @@ public:
|
|||
AddString(l, utf8s);
|
||||
}
|
||||
|
||||
void RemoveString(int8_t lang);
|
||||
void RemoveString(std::string const & lang)
|
||||
{
|
||||
int8_t const l = GetLangIndex(lang);
|
||||
if (l >= 0)
|
||||
RemoveString(l);
|
||||
}
|
||||
|
||||
// Calls |fn| for each pair of |lang| and |utf8s| stored in this multilang string.
|
||||
template <typename Fn>
|
||||
void ForEach(Fn && fn) const
|
||||
|
|
Loading…
Add table
Reference in a new issue