diff --git a/base/base_tests/string_utils_test.cpp b/base/base_tests/string_utils_test.cpp index be6d3cbfef..fd37d4feb9 100644 --- a/base/base_tests/string_utils_test.cpp +++ b/base/base_tests/string_utils_test.cpp @@ -365,3 +365,18 @@ UNIT_TEST(UniStringToUtf8) strings::UniString uniS = strings::MakeUniString(utf8Text); TEST_EQUAL(string(utf8Text), strings::ToUtf8(uniS), ()); } + +UNIT_TEST(StartsWith) +{ + using namespace strings; + + TEST(StartsWith(string(), ""), ()); + + string s("xyz"); + TEST(StartsWith(s, ""), ()); + TEST(StartsWith(s, "x"), ()); + TEST(StartsWith(s, "xyz"), ()); + TEST(!StartsWith(s, "xyzabc"), ()); + TEST(!StartsWith(s, "ayz"), ()); + TEST(!StartsWith(s, "axy"), ()); +} diff --git a/base/string_utils.cpp b/base/string_utils.cpp index 2419beb37a..20c5bc3d68 100644 --- a/base/string_utils.cpp +++ b/base/string_utils.cpp @@ -147,4 +147,9 @@ string ToUtf8(UniString const & s) return result; } +bool StartsWith(string const & s1, char const * s2) +{ + return (s1.compare(0, strlen(s2), s2) == 0); +} + } diff --git a/base/string_utils.hpp b/base/string_utils.hpp index 98dd14544e..f62dce1c30 100644 --- a/base/string_utils.hpp +++ b/base/string_utils.hpp @@ -238,6 +238,8 @@ inline string to_string(uint64_t i) } //@} +bool StartsWith(string const & s1, char const * s2); + /* template typename ItT::value_type JoinStrings(ItT begin, ItT end, DelimiterT const & delimiter) diff --git a/map/framework.cpp b/map/framework.cpp index 785f0c6d14..3a77681aea 100644 --- a/map/framework.cpp +++ b/map/framework.cpp @@ -1425,7 +1425,7 @@ void Framework::AddBookmarkAndSetViewport(Bookmark & bm, m2::RectD const & viewP bool Framework::SetViewportByURL(string const & url, url_api::Request & request) { - if (url.find("geo") == 0) + if (strings::StartsWith(url, "geo")) { using namespace url_scheme; @@ -1447,7 +1447,7 @@ bool Framework::SetViewportByURL(string const & url, url_api::Request & request) return true; } } - else if (url.find("ge0") == 0) + else if (strings::StartsWith(url, "ge0")) { url_api::Ge0Parser parser; if (parser.Parse(url, request)) diff --git a/map/ge0_parser.cpp b/map/ge0_parser.cpp index afee35b39d..8654d5cb9a 100644 --- a/map/ge0_parser.cpp +++ b/map/ge0_parser.cpp @@ -1,10 +1,16 @@ #include "ge0_parser.hpp" #include "url_api.hpp" + #include "../api/internal/c/api-client-internals.h" -#include "../coding/url_encode.hpp" -#include "../base/math.hpp" + #include "../indexer/mercator.hpp" +#include "../coding/url_encode.hpp" + +#include "../base/math.hpp" +#include "../base/string_utils.hpp" + + static const int ZOOM_POSITION = 6; url_api::Ge0Parser::Ge0Parser() @@ -29,7 +35,7 @@ bool url_api::Ge0Parser::Parse(string const & url, Request & request) // ge0://ZCoordba64/Name request.Clear(); - if (url.size() < 16 || url.substr(0, 6) != "ge0://") + if (url.size() < 16 || !strings::StartsWith(url, "ge0://")) return false; uint8_t const zoomI = DecodeBase64Char(url[ZOOM_POSITION]);