diff options
author | Alex Zolotarev <alex@maps.me> | 2015-11-06 10:21:41 +0300 |
---|---|---|
committer | Alex Zolotarev <alex@maps.me> | 2015-11-10 19:16:05 +0300 |
commit | 30d1880a6fe5f15add03a59cfa04411bdfda1d36 (patch) | |
tree | a1fd0c0867f7ecdade742fb5599187fb8acd0781 /indexer/feature_meta.cpp | |
parent | 7685f5327b54902e09d9caf12ad288439510af45 (diff) |
Fixed wikipedia links mwm storage and displaying in Android.
Previous solution was a really bad engineering decision (more details in https://github.com/mapsme/omim/pull/421 )
Diffstat (limited to 'indexer/feature_meta.cpp')
-rw-r--r-- | indexer/feature_meta.cpp | 85 |
1 files changed, 23 insertions, 62 deletions
diff --git a/indexer/feature_meta.cpp b/indexer/feature_meta.cpp index f709aa0391..cc41944241 100644 --- a/indexer/feature_meta.cpp +++ b/indexer/feature_meta.cpp @@ -1,72 +1,33 @@ #include "indexer/feature_meta.hpp" -namespace -{ -char HexToDec(char ch) -{ - if (ch >= '0' && ch <= '9') - return ch - '0'; - if (ch >= 'a') - ch -= 'a' - 'A'; - if (ch >= 'A' && ch <= 'F') - return ch - 'A' + 10; - return -1; -} - -string UriDecode(string const & sSrc) -{ - // This code is based on - // http://www.codeguru.com/cpp/cpp/string/conversions/article.php/c12759 - // - // Note from RFC1630: "Sequences which start with a percent - // sign but are not followed by two hexadecimal characters - // (0-9, A-F) are reserved for future extension" - - string result(sSrc.length(), 0); - auto itResult = result.begin(); - - for (auto it = sSrc.begin(); it != sSrc.end(); ++it) - { - if (*it == '%') - { - if (distance(it, sSrc.end()) > 2) - { - char dec1 = HexToDec(*(it + 1)); - char dec2 = HexToDec(*(it + 2)); - if (-1 != dec1 && -1 != dec2) - { - *itResult++ = (dec1 << 4) + dec2; - it += 2; - continue; - } - } - } - - if (*it == '_') - *itResult++ = ' '; - else - *itResult++ = *it; - } - - result.resize(distance(result.begin(), itResult)); - return result; -} -} // namespace +#include "std/algorithm.hpp" namespace feature { + string Metadata::GetWikiURL() const { - string value = this->Get(FMD_WIKIPEDIA); - string::size_type i = value.find(':'); - if (i == string::npos) - return string(); - return "https://" + value.substr(0, i) + ".wikipedia.org/wiki/" + value.substr(i + 1); + string v = this->Get(FMD_WIKIPEDIA); + if (v.empty()) + return v; + + auto const colon = v.find(':'); + if (colon == string::npos) + return v; + + // Spaces and % sign should be replaced in urls. + replace(v.begin() + colon, v.end(), ' ', '_'); + string::size_type percent, pos = colon; + string const escapedPercent("%25"); + while ((percent = v.find('%', pos)) != string::npos) + { + v.replace(percent, 1, escapedPercent); + pos = percent + escapedPercent.size(); + } + // Trying to avoid redirects by constructing the right link. + // TODO: Wikipedia article could be opened it a user's language, but need + // generator level support to check for available article languages first. + return "https://" + v.substr(0, colon) + ".m.wikipedia.org/wiki/" + v.substr(colon + 1); } -string Metadata::GetWikiTitle() const -{ - string value = this->Get(FMD_WIKIPEDIA); - return UriDecode(value); -} } // namespace feature |