Ticket #5598: json_parser_write.hpp.diff

File json_parser_write.hpp.diff, 2.0 KB (added by Shane Turner <shane.turner@…>, 10 years ago)

Silence the warning from GCC about a comparison that is always true due to to the limits of the type by creating specializations of the check for char and unsigned char.

  • boost/property_tree/detail/json_parser_write.hpp

    old new  
    2020namespace boost { namespace property_tree { namespace json_parser
    2121{
    2222
     23    /*
     24     * Add specialized checks for the case where the character
     25     * class is char or unsigned char. The specialized check avoids
     26     * comparing to <= 0xFF which triggers a GCC warning about the
     27     * fact that the check is always true.
     28     *
     29     * See https://svn.boost.org/trac/boost/ticket/5598
     30     */
     31    namespace detail {
     32      template <class Ch>
     33      bool check_is_ascii(const Ch c)
     34      {
     35        return (c == 0x20 || c == 0x21 || (c >= 0x23 && c <= 0x2E) ||
     36               (c >= 0x30 && c <= 0x5B) || (c >= 0x5D && c <= 0xFF));
     37      }
     38
     39      template <>
     40      bool check_is_ascii(const char c)
     41      {
     42        return (c == 0x20 || c == 0x21 || (c >= 0x23 && c <= 0x2E) ||
     43               (c >= 0x30 && c <= 0x5B) || c >= 0x5D);
     44      }
     45
     46      template <>
     47      bool check_is_ascii(const unsigned char c)
     48      {
     49        return (c == 0x20 || c == 0x21 || (c >= 0x23 && c <= 0x2E) ||
     50               (c >= 0x30 && c <= 0x5B) || c >= 0x5D);
     51      }
     52    }
    2353    // Create necessary escape sequences from illegal characters
    2454    template<class Ch>
    2555    std::basic_string<Ch> create_escapes(const std::basic_string<Ch> &s)
     
    3262            // This assumes an ASCII superset. But so does everything in PTree.
    3363            // We escape everything outside ASCII, because this code can't
    3464            // handle high unicode characters.
    35             if (*b == 0x20 || *b == 0x21 || (*b >= 0x23 && *b <= 0x2E) ||
    36                 (*b >= 0x30 && *b <= 0x5B) || (*b >= 0x5D && *b <= 0xFF))
     65            if (detail::check_is_ascii(*b))
    3766                result += *b;
    3867            else if (*b == Ch('\b')) result += Ch('\\'), result += Ch('b');
    3968            else if (*b == Ch('\f')) result += Ch('\\'), result += Ch('f');