From c261d0b2a8ca2b1311e55eeae628d9a169ae31c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Tue, 19 Jul 2016 16:03:28 +0200 Subject: [core] use more const, auto and refs Cherry-picked from 40ea4247d81eccc1d48af56fe1da95438bbdefcc. --- src/mbgl/util/mapbox.cpp | 69 ++++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/mbgl/util/mapbox.cpp b/src/mbgl/util/mapbox.cpp index 28dedaf25c..b60c04a3ed 100644 --- a/src/mbgl/util/mapbox.cpp +++ b/src/mbgl/util/mapbox.cpp @@ -19,13 +19,13 @@ bool isMapboxURL(const std::string& url) { std::vector getMapboxURLPathname(const std::string& url) { std::vector pathname; - std::size_t startIndex = protocol.length(); - std::size_t end = url.find_first_of("?#"); + auto startIndex = protocol.length(); + auto end = url.find_first_of("?#"); if (end == std::string::npos) { end = url.length(); } while (startIndex < end) { - std::size_t endIndex = url.find("/", startIndex); + auto endIndex = url.find("/", startIndex); if (endIndex == std::string::npos) { endIndex = end; } @@ -52,16 +52,15 @@ std::string normalizeStyleURL(const std::string& url, const std::string& accessT return url; } - std::vector pathname = getMapboxURLPathname(url); - + const auto pathname = getMapboxURLPathname(url); if (pathname.size() < 3) { Log::Error(Event::ParseStyle, "Invalid style URL"); return url; } - std::string user = pathname[1]; - std::string id = pathname[2]; - bool isDraft = pathname.size() > 3; + const auto& user = pathname[1]; + const auto& id = pathname[2]; + const bool isDraft = pathname.size() > 3; return baseURL + "styles/v1/" + user + "/" + id + (isDraft ? "/draft" : "") + "?access_token=" + accessToken; } @@ -70,36 +69,33 @@ std::string normalizeSpriteURL(const std::string& url, const std::string& access return url; } - std::vector pathname = getMapboxURLPathname(url); - + const auto pathname = getMapboxURLPathname(url); if (pathname.size() < 3) { Log::Error(Event::ParseStyle, "Invalid sprite URL"); return url; } - std::string user = pathname[1]; - bool isDraft = pathname.size() > 3; + const auto& user = pathname[1]; + const bool isDraft = pathname.size() > 3; + + const auto& name = isDraft ? pathname[3] : pathname[2]; + const size_t index = name.find_first_of("@."); + if (index == std::string::npos) { + Log::Error(Event::ParseStyle, "Invalid sprite URL"); + return url; + } + const auto& extension = name.substr(index); - std::string id, extension; if (isDraft) { - size_t index = pathname[3].find_first_of("@."); - if (index == std::string::npos) { - Log::Error(Event::ParseStyle, "Invalid sprite URL"); - return url; - } - id = pathname[2]; - extension = pathname[3].substr(index); + const auto& id = pathname[2]; + return baseURL + "styles/v1/" + user + "/" + id + "/draft/sprite" + extension + + "?access_token=" + accessToken; + } else { - size_t index = pathname[2].find_first_of("@."); - if (index == std::string::npos) { - Log::Error(Event::ParseStyle, "Invalid sprite URL"); - return url; - } - id = pathname[2].substr(0, index); - extension = pathname[2].substr(index); + const auto& id = pathname[2].substr(0, index); + return baseURL + "styles/v1/" + user + "/" + id + "/sprite" + extension + "?access_token=" + + accessToken; } - - return baseURL + "styles/v1/" + user + "/" + id + "/" + (isDraft ? "draft/" : "") + "sprite" + extension + "?access_token=" + accessToken; } std::string normalizeGlyphsURL(const std::string& url, const std::string& accessToken) { @@ -107,16 +103,15 @@ std::string normalizeGlyphsURL(const std::string& url, const std::string& access return url; } - std::vector pathname = getMapboxURLPathname(url); - + const auto pathname = getMapboxURLPathname(url); if (pathname.size() < 4) { Log::Error(Event::ParseStyle, "Invalid glyph URL"); return url; } - std::string user = pathname[1]; - std::string fontstack = pathname[2]; - std::string range = pathname[3]; + const auto& user = pathname[1]; + const auto& fontstack = pathname[2]; + const auto& range = pathname[3]; return baseURL + "fonts/v1/" + user + "/" + fontstack + "/" + range + "?access_token=" + accessToken; } @@ -137,7 +132,7 @@ std::string canonicalizeTileURL(const std::string& url, SourceType type, uint16_ tilesetStartIdx += sizeof("/v4/") - 1; - auto tilesetEndIdx = url.find("/", tilesetStartIdx); + const auto tilesetEndIdx = url.find("/", tilesetStartIdx); if (tilesetEndIdx == std::string::npos) { return url; } @@ -154,12 +149,12 @@ std::string canonicalizeTileURL(const std::string& url, SourceType type, uint16_ basenameIdx += 1; } - auto extensionIdx = url.find(".", basenameIdx); + const auto extensionIdx = url.find(".", basenameIdx); if (extensionIdx == std::string::npos || extensionIdx == queryIdx - 1) { return url; } - auto tileset = url.substr(tilesetStartIdx, tilesetEndIdx - tilesetStartIdx); + const auto tileset = url.substr(tilesetStartIdx, tilesetEndIdx - tilesetStartIdx); auto extension = url.substr(extensionIdx + 1, queryIdx - extensionIdx - 1); #if !defined(__ANDROID__) && !defined(__APPLE__) && !defined(QT_IMAGE_DECODERS) -- cgit v1.2.1