From 95868154a7249982eb965c0b8220b8dcec8ad9f0 Mon Sep 17 00:00:00 2001 From: Shane Turner Date: Mon, 28 Mar 2016 16:56:57 -0300 Subject: [PATCH] Account for character class being char or unsigned char. Avoid comparing to <= 0xFF and triggering a GCC warning. See https://svn.boost.org/trac/boost/ticket/5598 --- .../property_tree/json_parser/detail/write.hpp | 35 ++++++++++++++++++++-- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/include/boost/property_tree/json_parser/detail/write.hpp b/include/boost/property_tree/json_parser/detail/write.hpp index bba10038b..90bb570a1 100644 --- a/include/boost/property_tree/json_parser/detail/write.hpp +++ b/include/boost/property_tree/json_parser/detail/write.hpp @@ -20,6 +20,37 @@ namespace boost { namespace property_tree { namespace json_parser { + /* + * Add specialized checks for the case where the character + * class is char or unsigned char. The specialized check avoids + * comparing to <= 0xFF which triggers a GCC warning about the + * fact that the check is always true. + * + * See https://svn.boost.org/trac/boost/ticket/5598 + */ + namespace detail { + template + bool check_is_ascii(const Ch c) + { + return (c == 0x20 || c == 0x21 || (c >= 0x23 && c <= 0x2E) || + (c >= 0x30 && c <= 0x5B) || (c >= 0x5D && c <= 0xFF)); + } + + template <> + bool check_is_ascii(const char c) + { + return (c == 0x20 || c == 0x21 || (c >= 0x23 && c <= 0x2E) || + (c >= 0x30 && c <= 0x5B) || c >= 0x5D); + } + + template <> + bool check_is_ascii(const unsigned char c) + { + return (c == 0x20 || c == 0x21 || (c >= 0x23 && c <= 0x2E) || + (c >= 0x30 && c <= 0x5B) || c >= 0x5D); + } + } + // Create necessary escape sequences from illegal characters template std::basic_string create_escapes(const std::basic_string &s) @@ -30,12 +61,10 @@ namespace boost { namespace property_tree { namespace json_parser while (b != e) { typedef typename make_unsigned::type UCh; - UCh c(*b); // This assumes an ASCII superset. But so does everything in PTree. // We escape everything outside ASCII, because this code can't // handle high unicode characters. - if (c == 0x20 || c == 0x21 || (c >= 0x23 && c <= 0x2E) || - (c >= 0x30 && c <= 0x5B) || (c >= 0x5D && c <= 0xFF)) + if (detail::check_is_ascii(*b)) result += *b; else if (*b == Ch('\b')) result += Ch('\\'), result += Ch('b'); else if (*b == Ch('\f')) result += Ch('\\'), result += Ch('f');