diff options
author | Ilya Zverev <zverik@textual.ru> | 2015-09-25 13:27:32 +0300 |
---|---|---|
committer | Ilya Zverev <zverik@textual.ru> | 2015-09-25 13:27:32 +0300 |
commit | a373e6214299a33bd460ff0cfe56dde3cbe6e78b (patch) | |
tree | 165610b33254d7fac19ecac2c5664ceba19e3ad7 /generator | |
parent | 1f78f10516683ac4e3b3941d8b15b63a70947abe (diff) |
[generator] Wikipedia tag fixes and tests
Diffstat (limited to 'generator')
-rw-r--r-- | generator/generator_tests/metadata_test.cpp | 23 | ||||
-rw-r--r-- | generator/osm2meta.hpp | 26 |
2 files changed, 33 insertions, 16 deletions
diff --git a/generator/generator_tests/metadata_test.cpp b/generator/generator_tests/metadata_test.cpp index a9d043115c..ddec481b72 100644 --- a/generator/generator_tests/metadata_test.cpp +++ b/generator/generator_tests/metadata_test.cpp @@ -129,6 +129,29 @@ UNIT_TEST(Metadata_ValidateAndFormat_ele) params.GetMetadata().Drop(feature::Metadata::FMD_ELE); } +UNIT_TEST(Metadata_ValidateAndFormat_wikipedia) +{ + FeatureParams params; + MetadataTagProcessor p(params); + string const lanaWoodUrlEncoded = "%D0%9B%D0%B0%D0%BD%D0%B0_%D0%92%D1%83%D0%B4"; + + p("wikipedia", "ru:Лана Вуд"); + TEST_EQUAL(params.GetMetadata().Get(feature::Metadata::FMD_WIKIPEDIA), "ru:" + lanaWoodUrlEncoded, ("ru:")); + params.GetMetadata().Drop(feature::Metadata::FMD_WIKIPEDIA); + + p("wikipedia", "https://ru.wikipedia.org/wiki/" + lanaWoodUrlEncoded); + TEST_EQUAL(params.GetMetadata().Get(feature::Metadata::FMD_WIKIPEDIA), "ru:" + lanaWoodUrlEncoded, ("https:")); + params.GetMetadata().Drop(feature::Metadata::FMD_WIKIPEDIA); + + p("wikipedia", "Test"); + TEST(params.GetMetadata().Empty(), ("Test")); + + p("wikipedia", "https://en.wikipedia.org/wiki/"); + TEST(params.GetMetadata().Empty(), ("Null wiki")); + + p("wikipedia", "http://ru.google.com/wiki/wutlol"); + TEST(params.GetMetadata().Empty(), ("Google")); +} UNIT_TEST(Metadata_ReadWrite_Intermediate) { diff --git a/generator/osm2meta.hpp b/generator/osm2meta.hpp index 74cb910cfe..e37acb207c 100644 --- a/generator/osm2meta.hpp +++ b/generator/osm2meta.hpp @@ -196,7 +196,7 @@ protected: escaped.fill('0'); escaped << hex; - for (string::const_iterator i = value.begin(), n = value.end(); i != n; ++i) + for (string::const_iterator i = value.cbegin(), n = value.cend(); i != n; ++i) { string::value_type c = (*i); @@ -216,30 +216,24 @@ protected: string ValidateAndFormat_wikipedia(string const & v) const { - // Shortest string: "lg:aa" - if (v.length() < 5) - return v; - - // Find prefix before ':' - int i = 0; - while (i + 2 < v.length() && i < 10 && v[i] != ':') - i++; - if (v[i] != ':') + // Find prefix before ':', shortest case: "lg:aa" + string::size_type i = v.find(':'); + if (i == string::npos || i < 2 || i + 2 > v.length()) return string(); // URL encode lang:title, so URL can be reconstructed faster if (i <= 3 || v.substr(0, i) == "be-x-old") return v.substr(0, i + 1) + url_encode(v.substr(i + 1)); - if (v[i+1] == '/' && i + 27 < v.length()) + string::size_type const minUrlPartLength = string("//be.wikipedia.org/wiki/AB").length(); + if (v[i+1] == '/' && i + minUrlPartLength < v.length()) { // Convert URL to "lang:title" i += 3; - int j = i; - while (j < v.length() && v[j] != '.') - j++; - if (v.substr(j, 20) == ".wikipedia.org/wiki/") - return v.substr(i, j - i) + ":" + v.substr(j + 20); + string::size_type const j = v.find('.'); + string const wikiUrlPart = ".wikipedia.org/wiki/"; + if (j != string::npos && v.substr(j, wikiUrlPart.length()) == wikiUrlPart) + return v.substr(i, j - i) + ":" + v.substr(j + wikiUrlPart.length()); } return string(); } |