Files
MapLibreParticleLab/src/mbgl/util/token.hpp
Bart Louwers 71f6da2722 Add clang-format (#1086)
Co-authored-by: mwilsnd <mwilsnd@gmail.com>
2023-05-01 16:57:37 +02:00

51 lines
1.3 KiB
C++

#pragma once
#include <map>
#include <string>
#include <algorithm>
#include <optional>
namespace mbgl {
namespace util {
const static std::string tokenReservedChars = "{}";
// Replaces {tokens} in a string by calling the lookup function.
template <typename Lookup>
std::string replaceTokens(const std::string &source, const Lookup &lookup) {
std::string result;
result.reserve(source.size());
auto pos = source.begin();
const auto end = source.end();
while (pos != end) {
auto brace = std::find(pos, end, '{');
result.append(pos, brace);
pos = brace;
if (pos != end) {
for (brace++; brace != end && tokenReservedChars.find(*brace) == std::string::npos; brace++)
;
if (brace != end && *brace == '}') {
std::string key{pos + 1, brace};
if (std::optional<std::string> replacement = lookup(key)) {
result.append(*replacement);
} else {
result.append("{");
result.append(key);
result.append("}");
}
pos = brace + 1;
} else {
result.append(pos, brace);
pos = brace;
}
}
}
return result;
}
} // end namespace util
} // end namespace mbgl