From e18aa99081bc1f89ffe80573391555adcf395619 Mon Sep 17 00:00:00 2001 From: Maxim Pimenov Date: Mon, 26 Feb 2018 18:13:33 +0300 Subject: [PATCH] [search] Fixed PrependCity. --- search/ranker.cpp | 2 +- search/result.cpp | 14 ++++++++++---- search/result.hpp | 2 +- search/search_tests/results_tests.cpp | 25 ++++++++++++++++++++++++- 4 files changed, 36 insertions(+), 7 deletions(-) diff --git a/search/ranker.cpp b/search/ranker.cpp index 18a4c2d7d4..6e203751c6 100644 --- a/search/ranker.cpp +++ b/search/ranker.cpp @@ -438,7 +438,7 @@ Result Ranker::MakeResult(RankerResult const & rankerResult, bool needAddress, m_localities.GetLocality(res.GetFeatureCenter(), [&](LocalityItem const & item) { string city; if (item.GetSpecifiedOrDefaultName(m_localeCode, city)) - res.AppendCity(city); + res.PrependCity(city); }); } diff --git a/search/result.cpp b/search/result.cpp index 51ccddcc36..e8790c4b20 100644 --- a/search/result.cpp +++ b/search/result.cpp @@ -3,6 +3,8 @@ #include "search/common.hpp" #include "search/geometry_utils.hpp" +#include "base/string_utils.hpp" + #include #include @@ -132,11 +134,15 @@ pair const & Result::GetHighlightRange(size_t idx) const return m_hightlightRanges[idx]; } -void Result::AppendCity(string const & name) +void Result::PrependCity(string const & name) { - // Prepend only if city is absent in region (mwm) name. - if (m_address.find(name) == string::npos) - m_address = name + ", " + m_address; + // It is expected that if |m_address| is not empty, + // it starts with the region name. Avoid duplication + // in the case where this region name coincides with + // the city name and prepend otherwise. + strings::SimpleTokenizer tok(m_address, ","); + if (tok && *tok != name) + m_address = Join(name, m_address); } string Result::ToStringForStats() const diff --git a/search/result.hpp b/search/result.hpp index e35ad3d01e..a0926def4c 100644 --- a/search/result.hpp +++ b/search/result.hpp @@ -108,7 +108,7 @@ public: std::pair const & GetHighlightRange(size_t idx) const; size_t GetHighlightRangesCount() const { return m_hightlightRanges.size(); } - void AppendCity(string const & name); + void PrependCity(string const & name); int32_t GetPositionInResults() const { return m_positionInResults; } void SetPositionInResults(int32_t pos) { m_positionInResults = pos; } diff --git a/search/search_tests/results_tests.cpp b/search/search_tests/results_tests.cpp index 21d092ffd0..9aaf1ccd8d 100644 --- a/search/search_tests/results_tests.cpp +++ b/search/search_tests/results_tests.cpp @@ -6,7 +6,7 @@ namespace search { -UNIT_TEST(Sorting_Test) +UNIT_TEST(Results_Sorting) { Results r; MwmSet::MwmId id; @@ -34,4 +34,27 @@ UNIT_TEST(Sorting_Test) TEST_EQUAL(result.GetPositionInResults(), std::distance(r.begin(), it), ()); } } + +UNIT_TEST(Result_PrependCity) +{ + FeatureID const fid; + Result::Metadata const meta; + + { + Result r(fid, m2::PointD::Zero(), "" /* str */, "Moscow, Russia" /* address */, + "" /* featureTypeName */, 0 /* featureType */, meta); + + r.PrependCity("Moscow"); + TEST_EQUAL(r.GetAddress(), "Moscow, Russia", ()); + } + + { + Result r(fid, m2::PointD::Zero(), "улица Михася Лынькова" /* str */, + "Минская область, Беларусь" /* address */, "" /* featureTypeName */, + 0 /* featureType */, meta); + + r.PrependCity("Минск"); + TEST_EQUAL(r.GetAddress(), "Минск, Минская область, Беларусь", ()); + } +} } // namespace search