Cleanup strings::Trim.

Signed-off-by: Viktor Govako <viktor.govako@gmail.com>
This commit is contained in:
Viktor Govako 2022-06-07 09:36:56 +03:00
parent 2c04522154
commit 9b6fc18905
6 changed files with 37 additions and 51 deletions

View file

@ -1248,18 +1248,15 @@ void TestTrim(std::string const & str, std::string const & expected)
UNIT_TEST(Trim)
{
std::string const kStrWithoutSpaces = "string";
std::string strWithLeftSpaces = " " + kStrWithoutSpaces;
TEST_EQUAL(strings::TrimLeft(strWithLeftSpaces), kStrWithoutSpaces, ());
std::string strWithRightSpaces = kStrWithoutSpaces + " ";
TEST_EQUAL(strings::TrimRight(strWithRightSpaces), kStrWithoutSpaces, ());
std::string str = "string";
TestTrim("", "");
TestTrim(" ", "");
TestTrim(kStrWithoutSpaces, kStrWithoutSpaces);
TestTrim(" " + kStrWithoutSpaces, kStrWithoutSpaces);
TestTrim(kStrWithoutSpaces + " ", kStrWithoutSpaces);
TestTrim(" " + kStrWithoutSpaces + " ", kStrWithoutSpaces);
TestTrim(str, str);
TestTrim(" " + str, str);
TestTrim(str + " ", str);
TestTrim(" " + str + " ", str);
strings::Trim(str, "tsgn");
TEST_EQUAL(str, "ri", ());
}

View file

@ -219,21 +219,12 @@ void AsciiToLower(std::string & s)
});
}
std::string & TrimLeft(std::string & s)
void Trim(std::string & s)
{
s.erase(s.begin(), std::find_if(s.cbegin(), s.cend(), [](auto c) { return !std::isspace(c); }));
return s;
boost::trim_if(s, ::isspace);
}
std::string & TrimRight(std::string & s)
{
s.erase(std::find_if(s.crbegin(), s.crend(), [](auto c) { return !std::isspace(c); }).base(), s.end());
return s;
}
std::string & Trim(std::string & s) { return TrimLeft(TrimRight(s)); }
std::string_view & Trim(std::string_view & sv)
void Trim(std::string_view & sv)
{
auto const beg = std::find_if(sv.cbegin(), sv.cend(), [](auto c) { return !std::isspace(c); });
if (beg != sv.end())
@ -243,13 +234,11 @@ std::string_view & Trim(std::string_view & sv)
}
else
sv = {};
return sv;
}
std::string & Trim(std::string & s, char const * anyOf)
void Trim(std::string & s, std::string_view anyOf)
{
boost::trim_if(s, boost::is_any_of(anyOf));
return s;
}
bool ReplaceFirst(std::string & str, std::string const & from, std::string const & to)

View file

@ -106,12 +106,10 @@ void AsciiToLower(std::string & s);
// All triming functions return a reference on an input string.
// They do in-place trimming. In general, it does not work for any unicode whitespace except
// ASCII U+0020 one.
std::string & TrimLeft(std::string & s);
std::string & TrimRight(std::string & s);
std::string & Trim(std::string & s);
std::string_view & Trim(std::string_view & sv);
void Trim(std::string & s);
void Trim(std::string_view & sv);
/// Remove any characters that contain in "anyOf" on left and right side of string s
std::string & Trim(std::string & s, char const * anyOf);
void Trim(std::string & s, std::string_view anyOf);
// Replace the first match of the search substring in the input with the format string.
// str - An input string

View file

@ -68,8 +68,9 @@ void OsmElement::AddTag(char const * key, char const * value)
SKIP_KEY_BY_PREFIX("official_name");
#undef SKIP_KEY_BY_PREFIX
std::string val(value);
m_tags.emplace_back(key, std::move(strings::Trim(val)));
std::string_view val(value);
strings::Trim(val);
m_tags.emplace_back(key, val);
}
void OsmElement::AddTag(std::string const & key, std::string const & value)

View file

@ -48,7 +48,7 @@ struct OsmElement
struct Tag
{
Tag() = default;
Tag(std::string const & key, std::string const & value) : m_key(key), m_value(value) {}
Tag(std::string_view key, std::string_view value) : m_key(key), m_value(value) {}
bool operator==(Tag const & other) const
{

View file

@ -164,38 +164,39 @@ MapcssRules ParseMapCSS(std::unique_ptr<Reader> reader)
auto const processFull = [&rules](std::string const & typeString,
std::string const & selectorsString)
{
ASSERT(!typeString.empty(), ());
ASSERT(!selectorsString.empty(), ());
auto const typeTokens = strings::Tokenize<std::string>(typeString, "|");
for (auto const & selector : strings::Tokenize(selectorsString, ","))
strings::Tokenize(selectorsString, ",", [&typeTokens, &rules](std::string_view selector)
{
CHECK(!selector.empty(), (selectorsString));
CHECK_EQUAL(selector[0], '[', (selectorsString));
CHECK_EQUAL(selector.back(), ']', (selectorsString));
ASSERT_EQUAL(selector.front(), '[', ());
ASSERT_EQUAL(selector.back(), ']', ());
MapcssRule rule;
auto tags = strings::Tokenize<std::string>(selector, "[");
for (auto & rawTag : tags)
strings::Tokenize(selector, "[]", [&rule](std::string_view kv)
{
strings::Trim(rawTag, "]");
CHECK(!rawTag.empty(), (selector, tags));
auto tag = strings::Tokenize<std::string>(rawTag, "=");
auto const tag = strings::Tokenize(kv, "=");
if (tag.size() == 1)
{
CHECK(!tag[0].empty(), (rawTag));
auto const forbidden = tag[0][0] == '!';
strings::Trim(tag[0], "?!");
auto const forbidden = (tag[0][0] == '!');
std::string v(tag[0]);
strings::Trim(v, "?!");
if (forbidden)
rule.m_forbiddenKeys.push_back(tag[0]);
rule.m_forbiddenKeys.push_back(std::move(v));
else
rule.m_mandatoryKeys.push_back(tag[0]);
rule.m_mandatoryKeys.push_back(std::move(v));
}
else
{
CHECK_EQUAL(tag.size(), 2, (tag));
ASSERT_EQUAL(tag.size(), 2, (tag));
rule.m_tags.emplace_back(tag[0], tag[1]);
}
}
});
rules.emplace_back(typeTokens, std::move(rule));
}
});
};
// Mapcss-mapping maps tags to types.