Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
Replace regex usage with basic string manipulation
Browse files Browse the repository at this point in the history
Apparently we can’t rely on the existence of <regex> yet.

Fixes #919.
  • Loading branch information
1ec5 authored and jfirebaugh committed Mar 6, 2015
1 parent 41c0137 commit a6c8a76
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/mbgl/util/mapbox.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <mbgl/util/mapbox.hpp>

#include <stdexcept>
#include <regex>

namespace mbgl {
namespace util {
Expand Down Expand Up @@ -44,8 +43,23 @@ std::string normalizeTileURL(const std::string& url, const std::string& sourceUR
if (sourceURL.empty() || sourceURL.compare(0, mapbox.length(), mapbox) != 0)
return url;

static std::regex extension_re("\\.((?:png|jpg)\\d*)(?=$|\\?)");
return std::regex_replace(url, extension_re, std::string("{ratio}.$1"));
std::string::size_type queryIdx = url.rfind("?");
// Trim off the right end but never touch anything before the extension dot.
std::string urlSansParams((queryIdx == std::string::npos) ? url : url.substr(0, queryIdx));

while (!urlSansParams.empty() && isdigit(urlSansParams.back()))
urlSansParams.pop_back();

std::string::size_type extensionIdx = urlSansParams.length() - 4;
if (extensionIdx <= 0)
return url;
std::string extension = urlSansParams.substr(extensionIdx);
if (extension.compare(".png") != 0 && extension.compare(".jpg") != 0)
return url;

std::string normalizedURL(url);
normalizedURL.insert(extensionIdx, "{ratio}");
return normalizedURL;
}

}
Expand Down

0 comments on commit a6c8a76

Please sign in to comment.