From 9b6fc18905e4f13e096f6ab52993e065b303e6be Mon Sep 17 00:00:00 2001 From: Viktor Govako Date: Tue, 7 Jun 2022 09:36:56 +0300 Subject: [PATCH] Cleanup strings::Trim. Signed-off-by: Viktor Govako --- base/base_tests/string_utils_test.cpp | 19 ++++++--------- base/string_utils.cpp | 19 +++------------ base/string_utils.hpp | 8 +++--- generator/osm_element.cpp | 5 ++-- generator/osm_element.hpp | 2 +- generator/utils.cpp | 35 ++++++++++++++------------- 6 files changed, 37 insertions(+), 51 deletions(-) diff --git a/base/base_tests/string_utils_test.cpp b/base/base_tests/string_utils_test.cpp index 4bfa43f0d9..2cd881b0e5 100644 --- a/base/base_tests/string_utils_test.cpp +++ b/base/base_tests/string_utils_test.cpp @@ -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", ()); } diff --git a/base/string_utils.cpp b/base/string_utils.cpp index 11ce7de5aa..325574c3a2 100644 --- a/base/string_utils.cpp +++ b/base/string_utils.cpp @@ -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) diff --git a/base/string_utils.hpp b/base/string_utils.hpp index f1ac4fb325..866c06ea03 100644 --- a/base/string_utils.hpp +++ b/base/string_utils.hpp @@ -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 diff --git a/generator/osm_element.cpp b/generator/osm_element.cpp index e5993f9212..e30f965b19 100644 --- a/generator/osm_element.cpp +++ b/generator/osm_element.cpp @@ -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) diff --git a/generator/osm_element.hpp b/generator/osm_element.hpp index bd6ae79139..da826bca12 100644 --- a/generator/osm_element.hpp +++ b/generator/osm_element.hpp @@ -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 { diff --git a/generator/utils.cpp b/generator/utils.cpp index 21c440d0e3..546e99608d 100644 --- a/generator/utils.cpp +++ b/generator/utils.cpp @@ -164,38 +164,39 @@ MapcssRules ParseMapCSS(std::unique_ptr reader) auto const processFull = [&rules](std::string const & typeString, std::string const & selectorsString) { + ASSERT(!typeString.empty(), ()); + ASSERT(!selectorsString.empty(), ()); + auto const typeTokens = strings::Tokenize(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(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(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.