forked from organicmaps/organicmaps
[base] Helper functions that remove ("eat") prefixes and suffixes of strings.
This commit is contained in:
parent
585dc6b1e9
commit
97e3c4cb2c
3 changed files with 69 additions and 0 deletions
|
@ -825,6 +825,48 @@ UNIT_TEST(EndsWith)
|
|||
}
|
||||
}
|
||||
|
||||
UNIT_TEST(EatPrefix_EatSuffix)
|
||||
{
|
||||
// <original string, prefix/suffix to cut, success, string after cutting>
|
||||
std::vector<std::tuple<std::string, std::string, bool, std::string>> kPrefixTestcases = {
|
||||
{"abc", "a", true, "bc"},
|
||||
{"abc", "b", false, "abc"},
|
||||
{"abc", "", true, "abc"},
|
||||
{"abc", "abc", true, ""},
|
||||
{"abc", "abc00", false, "abc"},
|
||||
{"", "", true, ""},
|
||||
{"абв", "а", true, "бв"},
|
||||
{"абв", "б", false, "абв"},
|
||||
{"字符串", "字", true, "符串"},
|
||||
};
|
||||
|
||||
std::vector<std::tuple<std::string, std::string, bool, std::string>> kSuffixTestcases = {
|
||||
{"abc", "c", true, "ab"},
|
||||
{"abc", "b", false, "abc"},
|
||||
{"abc", "", true, "abc"},
|
||||
{"abc", "abc", true, ""},
|
||||
{"abc", "00abc", false, "abc"},
|
||||
{"", "", true, ""},
|
||||
{"абв", "в", true, "аб"},
|
||||
{"абв", "б", false, "абв"},
|
||||
{"字符串", "串", true, "字符"},
|
||||
};
|
||||
|
||||
for (auto const & [original, toCut, success, afterCutting] : kPrefixTestcases)
|
||||
{
|
||||
auto s = original;
|
||||
TEST_EQUAL(strings::EatPrefix(s, toCut), success, (original, toCut));
|
||||
TEST_EQUAL(s, afterCutting, ());
|
||||
}
|
||||
|
||||
for (auto const & [original, toCut, success, afterCutting] : kSuffixTestcases)
|
||||
{
|
||||
auto s = original;
|
||||
TEST_EQUAL(strings::EatSuffix(s, toCut), success, (original, toCut));
|
||||
TEST_EQUAL(s, afterCutting, ());
|
||||
}
|
||||
}
|
||||
|
||||
UNIT_TEST(UniString_LessAndEqualsAndNotEquals)
|
||||
{
|
||||
std::vector<strings::UniString> v;
|
||||
|
|
|
@ -325,6 +325,26 @@ bool EndsWith(std::string const & s1, std::string const & s2)
|
|||
return s1.size() >= s2.size() && s1.compare(s1.size() - s2.size(), s2.size(), s2) == 0;
|
||||
}
|
||||
|
||||
bool EatPrefix(std::string & s, std::string const & prefix)
|
||||
{
|
||||
if (!StartsWith(s, prefix))
|
||||
return false;
|
||||
|
||||
CHECK_LESS_OR_EQUAL(prefix.size(), s.size(), ());
|
||||
s = s.substr(prefix.size());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool EatSuffix(std::string & s, std::string const & suffix)
|
||||
{
|
||||
if (!EndsWith(s, suffix))
|
||||
return false;
|
||||
|
||||
CHECK_LESS_OR_EQUAL(suffix.size(), s.size(), ());
|
||||
s = s.substr(0, s.size() - suffix.size());
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string to_string_dac(double d, int dac)
|
||||
{
|
||||
dac = std::min(std::numeric_limits<double>::digits10, dac);
|
||||
|
|
|
@ -634,6 +634,13 @@ bool EndsWith(std::string const & s1, char const * s2);
|
|||
|
||||
bool EndsWith(std::string const & s1, std::string const & s2);
|
||||
|
||||
// If |s| starts with |prefix|, deletes it from |s| and returns true.
|
||||
// Otherwise, leaves |s| unmodified and returns false.
|
||||
bool EatPrefix(std::string & s, std::string const & prefix);
|
||||
// If |s| ends with |suffix|, deletes it from |s| and returns true.
|
||||
// Otherwise, leaves |s| unmodified and returns false.
|
||||
bool EatSuffix(std::string & s, std::string const & suffix);
|
||||
|
||||
/// Try to guess if it's HTML or not. No guarantee.
|
||||
bool IsHTML(std::string const & utf8);
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue