diff options
author | Konstantin Käfer <mail@kkaefer.com> | 2014-05-08 13:42:54 +0200 |
---|---|---|
committer | Konstantin Käfer <mail@kkaefer.com> | 2014-05-08 13:42:54 +0200 |
commit | 7f4f3271665c57e9720f9efeff7371c6aac58075 (patch) | |
tree | e102ba04f9e1ee6ac7ea31f7ec62012704c1f437 /include/llmr/style/value.hpp | |
parent | 115afe3b10b2f84a48665d5810feea2b8ab50963 (diff) | |
download | qtlocation-mapboxgl-7f4f3271665c57e9720f9efeff7371c6aac58075.tar.gz |
make sure we're not converting too relaxedly
Diffstat (limited to 'include/llmr/style/value.hpp')
-rw-r--r-- | include/llmr/style/value.hpp | 45 |
1 files changed, 29 insertions, 16 deletions
diff --git a/include/llmr/style/value.hpp b/include/llmr/style/value.hpp index 051f061d38..0dc53248b7 100644 --- a/include/llmr/style/value.hpp +++ b/include/llmr/style/value.hpp @@ -14,30 +14,42 @@ Value parseValue(pbf data); namespace util { +inline int string_to_bool(std::string str) { + std::transform(str.begin(), str.end(), str.begin(), ::tolower); + if (str == "false" || str == "null" || !str.length()) return 0; + else if (str == "true") return 1; + else return -1; +} + template <typename T> struct string_to_number {}; template <> struct string_to_number<bool> { - bool operator()(std::string str) const { - std::transform(str.begin(), str.end(), str.begin(), ::tolower); - str.erase(std::remove_if(str.begin(), str.end(), ::isspace), str.end()); - return !(str == "false" || str == "null" || !str.length()); + bool operator()(const std::string &str) const { + // Converts 0 => false, 1 => true and -1 => true. + return string_to_bool(str); } }; template <> struct string_to_number<double> { - double operator()(std::string const &str) const try { return std::stod(str); } - catch (...) { return string_to_number<bool>()(str); } + double operator()(std::string const &str) const { + int val = string_to_bool(str); + return val < 0 ? std::stod(str) : val; + } }; template <> struct string_to_number<int64_t> { - int64_t operator()(std::string const &str) const try { return std::stoll(str); } - catch (...) { return string_to_number<bool>()(str); } + int64_t operator()(std::string const &str) const { + int val = string_to_bool(str); + return val < 0 ? std::stoll(str) : val; + } }; template <> struct string_to_number<uint64_t> { - uint64_t operator()(std::string const &str) const try { return std::stoull(str); } - catch (...) { return string_to_number<bool>()(str); } + uint64_t operator()(std::string const &str) const { + int val = string_to_bool(str); + return val < 0 ? std::stoull(str) : val; + } }; struct relaxed_equal_visitor { @@ -53,23 +65,24 @@ struct relaxed_equal_visitor { bool operator()(uint64_t lhs, int64_t rhs) const { return rhs < 0 ? false : lhs == uint64_t(rhs); } template <typename T, class = typename std::enable_if<std::is_arithmetic<T>::value>::type> - bool operator()(T lhs, const std::string &rhs) const { return lhs == string_to_number<T>()(rhs); } + bool operator()(T lhs, const std::string &rhs) const try { return lhs == string_to_number<T>()(rhs); } + catch(...) { return false; } template <typename T, class = typename std::enable_if<std::is_arithmetic<T>::value>::type> - bool operator()(const std::string &lhs, T rhs) const { return string_to_number<T>()(lhs) == rhs; } + bool operator()(const std::string &lhs, T rhs) const try { return string_to_number<T>()(lhs) == rhs; } + catch(...) { return false; } template <typename T0, typename T1> bool operator()(T0 lhs, T1 rhs) const { return lhs == rhs; } }; -template <typename T> struct relaxed_equal { - relaxed_equal(T const &lhs) : lhs(lhs) {} + relaxed_equal(Value const &lhs) : lhs(lhs) {} - bool operator()(T const &rhs) const { + bool operator()(Value const &rhs) const { return apply_visitor(lhs, rhs, relaxed_equal_visitor()); } - T const &lhs; + Value const &lhs; }; } } |