replace {{token}} with {token}, and %d/%d/%d with {z}/{x}/{y}

refs https://github.com/mapbox/mapbox-gl-style-spec/pull/87
This commit is contained in:
Konstantin Käfer
2014-07-09 16:27:23 -07:00
parent f2ee02d3ca
commit 22c8afced5
7 changed files with 59 additions and 59 deletions

View File

@@ -10,12 +10,6 @@ typedef util::variant<bool, int64_t, uint64_t, double, std::string> Value;
std::string toString(const Value &value);
inline std::string& operator+=(std::string& out, const Value& value) {
out += toString(value);
return out;
}
Value parseValue(pbf data);
namespace util {

View File

@@ -9,40 +9,43 @@ namespace regex_impl = boost;
namespace regex_impl = std;
#endif
#include <map>
namespace llmr {
namespace util {
namespace detail {
const regex_impl::regex tokenRegex("\\{\\{(\\w+)\\}\\}");
const regex_impl::regex tokenRegex("\\{(\\w+)\\}");
const regex_impl::sregex_token_iterator tokensEnd = regex_impl::sregex_token_iterator();
}
template <typename Tokens>
std::string replaceTokens(const std::string &source, const Tokens &tokens) {
template <typename Lookup>
std::string replaceTokens(const std::string &source, const Lookup &lookup) {
std::string result;
result.reserve(source.size());
bool token = false;
for (auto token_it = regex_impl::sregex_token_iterator(source.begin(), source.end(),
detail::tokenRegex, {-1, 1});
token_it != detail::tokensEnd; ++token_it, token = !token) {
token_it != detail::tokensEnd; ++token_it, token = !token) {
if (!token_it->matched) {
continue;
}
if (token) {
const auto it_prop = tokens.find(token_it->str());
if (it_prop != tokens.end()) {
result += it_prop->second;
}
} else {
result += token_it->str();
}
result += token ? lookup(token_it->str()) : token_it->str();
}
return result;
}
template <typename T>
inline std::string replaceTokens(const std::string &source, const std::map<std::string, T> &properties) {
return replaceTokens(source, [&properties](const std::string &token) -> std::string {
const auto it_prop = properties.find(token);
return it_prop != properties.end() ? toString(it_prop->second) : "";
});
}
} // end namespace util
} // end namespace llmr