diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2016-12-06 14:44:55 +0100 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2016-12-07 10:46:43 +0100 |
commit | dc1bda435fa1345737604767ee71859377d41530 (patch) | |
tree | f8d4207e70383513efc765280dcc78bdf43efd84 /src | |
parent | cc6e5c9df9b7b42eba9cdb51bea529c1d5c3a497 (diff) | |
download | qtlocation-mapboxgl-dc1bda435fa1345737604767ee71859377d41530.tar.gz |
[core] add util::isURL() for checking whether a string starts with a URL scheme
Diffstat (limited to 'src')
-rw-r--r-- | src/mbgl/util/url.cpp | 35 | ||||
-rw-r--r-- | src/mbgl/util/url.hpp | 1 |
2 files changed, 34 insertions, 2 deletions
diff --git a/src/mbgl/util/url.cpp b/src/mbgl/util/url.cpp index bf6fc70ff5..9831bc9038 100644 --- a/src/mbgl/util/url.cpp +++ b/src/mbgl/util/url.cpp @@ -1,12 +1,30 @@ #include <mbgl/util/url.hpp> -#include <cctype> #include <iomanip> #include <sstream> #include <string> #include <cstdlib> #include <algorithm> + +namespace { + +// std::alnum etc. suffer from locale-dependence. + +inline bool isAlphaCharacter(char c) { + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); +} + +inline bool isAlphaNumericCharacter(char c) { + return isAlphaCharacter(c) || (c >= '0' && c <= '9'); +} + +inline bool isSchemeCharacter(char c) { + return isAlphaNumericCharacter(c) || c == '-' || c == '+' || c == '.'; +} + +} // namespace + namespace mbgl { namespace util { @@ -17,7 +35,7 @@ std::string percentEncode(const std::string& input) { encoded << std::hex; for (auto c : input) { - if (std::isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') { + if (isAlphaNumericCharacter(c) || c == '-' || c == '_' || c == '.' || c == '~') { encoded << c; } else { encoded << '%' << std::setw(2) << int(c); @@ -47,5 +65,18 @@ std::string percentDecode(const std::string& input) { return decoded; } +// Checks whether the input string contains ://, and the part before it is all alphanumeric ASCII. +bool isURL(const std::string& input) { + auto it = input.begin(); + // First character has to be alphabetic + if (it == input.end() || !isAlphaCharacter(*it++)) return false; + // The remaining characters of the scheme can be alphanumeric, or be one of +.- + while (it != input.end() && isSchemeCharacter(*it)) ++it; + // Check that :// follows + return (it != input.end() && *it++ == ':') && + (it != input.end() && *it++ == '/') && + (it != input.end() && *it++ == '/'); +} + } // namespace util } // namespace mbgl diff --git a/src/mbgl/util/url.hpp b/src/mbgl/util/url.hpp index b9c66261df..2d18f7476a 100644 --- a/src/mbgl/util/url.hpp +++ b/src/mbgl/util/url.hpp @@ -8,6 +8,7 @@ namespace util { std::string percentEncode(const std::string&); std::string percentDecode(const std::string&); +bool isURL(const std::string&); } // namespace util } // namespace mbgl |