Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:50:18

0001 #ifndef BOOST_PROPERTY_TREE_DETAIL_JSON_PARSER_NARROW_ENCODING_HPP
0002 #define BOOST_PROPERTY_TREE_DETAIL_JSON_PARSER_NARROW_ENCODING_HPP
0003 
0004 #include <boost/assert.hpp>
0005 #include <boost/range/iterator_range_core.hpp>
0006 
0007 #include <utility>
0008 
0009 namespace boost { namespace property_tree {
0010     namespace json_parser { namespace detail
0011 {
0012 
0013     struct external_ascii_superset_encoding
0014     {
0015         typedef char external_char;
0016 
0017         bool is_nl(char c) const { return c == '\n'; }
0018         bool is_ws(char c) const {
0019             return c == ' ' || c == '\t' || c == '\n' || c == '\r';
0020         }
0021 
0022         bool is_minus(char c) const { return c == '-'; }
0023         bool is_plusminus(char c) const { return c == '+' || c == '-'; }
0024         bool is_dot(char c) const { return c == '.'; }
0025         bool is_eE(char c) const { return c == 'e' || c == 'E'; }
0026         bool is_0(char c) const { return c == '0'; }
0027         bool is_digit(char c) const { return c >= '0' && c <= '9'; }
0028         bool is_digit0(char c) const { return c >= '1' && c <= '9'; }
0029 
0030         bool is_quote(char c) const { return c == '"'; }
0031         bool is_backslash(char c) const { return c == '\\'; }
0032         bool is_slash(char c) const { return c == '/'; }
0033 
0034         bool is_comma(char c) const { return c == ','; }
0035         bool is_open_bracket(char c) const { return c == '['; }
0036         bool is_close_bracket(char c) const { return c == ']'; }
0037         bool is_colon(char c) const { return c == ':'; }
0038         bool is_open_brace(char c) const { return c == '{'; }
0039         bool is_close_brace(char c) const { return c == '}'; }
0040 
0041         bool is_a(char c) const { return c == 'a'; }
0042         bool is_b(char c) const { return c == 'b'; }
0043         bool is_e(char c) const { return c == 'e'; }
0044         bool is_f(char c) const { return c == 'f'; }
0045         bool is_l(char c) const { return c == 'l'; }
0046         bool is_n(char c) const { return c == 'n'; }
0047         bool is_r(char c) const { return c == 'r'; }
0048         bool is_s(char c) const { return c == 's'; }
0049         bool is_t(char c) const { return c == 't'; }
0050         bool is_u(char c) const { return c == 'u'; }
0051 
0052         int decode_hexdigit(char c) {
0053             if (c >= '0' && c <= '9') return c - '0';
0054             if (c >= 'A' && c <= 'F') return c - 'A' + 10;
0055             if (c >= 'a' && c <= 'f') return c - 'a' + 10;
0056             return -1;
0057         }
0058     };
0059 
0060     struct utf8_utf8_encoding : external_ascii_superset_encoding
0061     {
0062         typedef char internal_char;
0063 
0064         template <typename Iterator>
0065         boost::iterator_range<Iterator>
0066         to_internal(Iterator first, Iterator last) const {
0067             return boost::make_iterator_range(first, last);
0068         }
0069 
0070         char to_internal_trivial(char c) const {
0071             BOOST_ASSERT(static_cast<unsigned char>(c) <= 0x7f);
0072             return c;
0073         }
0074 
0075         template <typename Iterator, typename Sentinel,
0076                   typename EncodingErrorFn>
0077         void skip_codepoint(Iterator& cur, Sentinel end,
0078                             EncodingErrorFn error_fn) const {
0079             transcode_codepoint(cur, end, DoNothing(), error_fn);
0080         }
0081 
0082         template <typename Iterator, typename Sentinel, typename TranscodedFn,
0083                   typename EncodingErrorFn>
0084         void transcode_codepoint(Iterator& cur, Sentinel end,
0085                 TranscodedFn transcoded_fn, EncodingErrorFn error_fn) const {
0086             unsigned char c = *cur;
0087             ++cur;
0088             if (c <= 0x7f) {
0089                 // Solo byte, filter out disallowed codepoints.
0090                 if (c < 0x20) {
0091                     error_fn();
0092                 }
0093                 transcoded_fn(c);
0094                 return;
0095             }
0096             int trailing = trail_table(c);
0097             if (trailing == -1) {
0098                 // Standalone trailing byte or overly long sequence.
0099                 error_fn();
0100             }
0101             transcoded_fn(c);
0102             for (int i = 0; i < trailing; ++i) {
0103                 if (cur == end || !is_trail(*cur)) {
0104                     error_fn();
0105                 }
0106                 transcoded_fn(*cur);
0107                 ++cur;
0108             }
0109         }
0110 
0111         template <typename TranscodedFn>
0112         void feed_codepoint(unsigned codepoint,
0113                             TranscodedFn transcoded_fn) const {
0114             if (codepoint <= 0x7f) {
0115                 transcoded_fn(static_cast<char>(codepoint));
0116             } else if (codepoint <= 0x7ff) {
0117                 transcoded_fn(static_cast<char>(0xc0 | (codepoint >> 6)));
0118                 transcoded_fn(trail(codepoint));
0119             } else if (codepoint <= 0xffff) {
0120                 transcoded_fn(static_cast<char>(0xe0 | (codepoint >> 12)));
0121                 transcoded_fn(trail(codepoint >> 6));
0122                 transcoded_fn(trail(codepoint));
0123             } else if (codepoint <= 0x10ffff) {
0124                 transcoded_fn(static_cast<char>(0xf0 | (codepoint >> 18)));
0125                 transcoded_fn(trail(codepoint >> 12));
0126                 transcoded_fn(trail(codepoint >> 6));
0127                 transcoded_fn(trail(codepoint));
0128             }
0129         }
0130 
0131         template <typename Iterator, typename Sentinel>
0132         void skip_introduction(Iterator& cur, Sentinel end) const {
0133             if (cur != end && static_cast<unsigned char>(*cur) == 0xef) {
0134                 if (++cur == end) return;
0135                 if (++cur == end) return;
0136                 if (++cur == end) return;
0137             }
0138         }
0139 
0140     private:
0141         struct DoNothing {
0142             void operator ()(char) const {}
0143         };
0144 
0145         bool is_trail(unsigned char c) const {
0146             return (c & 0xc0) == 0x80;
0147         }
0148 
0149         int trail_table(unsigned char c) const {
0150             static const signed char table[] = {
0151                                  /* not a lead byte */
0152                 /* 0x10???sss */ -1, -1, -1, -1, -1, -1, -1, -1,
0153                 /* 0x110??sss */ 1, 1, 1, 1, /* 1 trailing byte */
0154                 /* 0x1110?sss */ 2, 2, /* 2 trailing bytes */
0155                 /* 0x11110sss */ 3, /* 3 trailing bytes */
0156                 /* 0x11111sss */ -1 /* 4 or 5 trailing bytes, disallowed */
0157             };
0158             return table[(c & 0x7f) >> 3];
0159         }
0160 
0161         char trail(unsigned unmasked) const {
0162             return static_cast<char>(0x80 | (unmasked & 0x3f));
0163         }
0164     };
0165 
0166 }}}}
0167 
0168 #endif