Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-11 08:14:26

0001 //
0002 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
0003 // Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
0004 //
0005 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0006 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0007 //
0008 // Official repository: https://github.com/boostorg/json
0009 //
0010 
0011 #ifndef BOOST_JSON_BASIC_PARSER_IMPL_HPP
0012 #define BOOST_JSON_BASIC_PARSER_IMPL_HPP
0013 
0014 #include <boost/json/detail/config.hpp>
0015 #include <boost/json/basic_parser.hpp>
0016 #include <boost/json/error.hpp>
0017 #include <boost/json/detail/buffer.hpp>
0018 #include <boost/json/detail/charconv/from_chars.hpp>
0019 #include <boost/json/detail/sse2.hpp>
0020 #include <boost/mp11/algorithm.hpp>
0021 #include <boost/mp11/integral.hpp>
0022 #include <cmath>
0023 #include <limits>
0024 #include <cstring>
0025 
0026 #ifdef _MSC_VER
0027 #pragma warning(push)
0028 #pragma warning(disable: 4702) // unreachable code
0029 #pragma warning(disable: 4127) // conditional expression is constant
0030 #endif
0031 
0032 /*  This file must be manually included to get the
0033     function template definitions for basic_parser.
0034 */
0035 
0036 /*  Reference:
0037 
0038     https://www.json.org/
0039 
0040     RFC 7159: The JavaScript Object Notation (JSON) Data Interchange Format
0041     https://tools.ietf.org/html/rfc7159
0042 
0043     https://ampl.com/netlib/fp/dtoa.c
0044 */
0045 
0046 #ifndef BOOST_JSON_DOCS
0047 
0048 namespace boost {
0049 namespace json {
0050 namespace detail {
0051 
0052 inline
0053 double
0054 pow10(int exp) noexcept
0055 {
0056     static double const tab[618] = {
0057                         1e-308, 1e-307, 1e-306, 1e-305, 1e-304, 1e-303, 1e-302, 1e-301,
0058 
0059         1e-300, 1e-299, 1e-298, 1e-297, 1e-296, 1e-295, 1e-294, 1e-293, 1e-292, 1e-291,
0060         1e-290, 1e-289, 1e-288, 1e-287, 1e-286, 1e-285, 1e-284, 1e-283, 1e-282, 1e-281,
0061         1e-280, 1e-279, 1e-278, 1e-277, 1e-276, 1e-275, 1e-274, 1e-273, 1e-272, 1e-271,
0062         1e-270, 1e-269, 1e-268, 1e-267, 1e-266, 1e-265, 1e-264, 1e-263, 1e-262, 1e-261,
0063         1e-260, 1e-259, 1e-258, 1e-257, 1e-256, 1e-255, 1e-254, 1e-253, 1e-252, 1e-251,
0064         1e-250, 1e-249, 1e-248, 1e-247, 1e-246, 1e-245, 1e-244, 1e-243, 1e-242, 1e-241,
0065         1e-240, 1e-239, 1e-238, 1e-237, 1e-236, 1e-235, 1e-234, 1e-233, 1e-232, 1e-231,
0066         1e-230, 1e-229, 1e-228, 1e-227, 1e-226, 1e-225, 1e-224, 1e-223, 1e-222, 1e-221,
0067         1e-220, 1e-219, 1e-218, 1e-217, 1e-216, 1e-215, 1e-214, 1e-213, 1e-212, 1e-211,
0068         1e-210, 1e-209, 1e-208, 1e-207, 1e-206, 1e-205, 1e-204, 1e-203, 1e-202, 1e-201,
0069 
0070         1e-200, 1e-199, 1e-198, 1e-197, 1e-196, 1e-195, 1e-194, 1e-193, 1e-192, 1e-191,
0071         1e-190, 1e-189, 1e-188, 1e-187, 1e-186, 1e-185, 1e-184, 1e-183, 1e-182, 1e-181,
0072         1e-180, 1e-179, 1e-178, 1e-177, 1e-176, 1e-175, 1e-174, 1e-173, 1e-172, 1e-171,
0073         1e-170, 1e-169, 1e-168, 1e-167, 1e-166, 1e-165, 1e-164, 1e-163, 1e-162, 1e-161,
0074         1e-160, 1e-159, 1e-158, 1e-157, 1e-156, 1e-155, 1e-154, 1e-153, 1e-152, 1e-151,
0075         1e-150, 1e-149, 1e-148, 1e-147, 1e-146, 1e-145, 1e-144, 1e-143, 1e-142, 1e-141,
0076         1e-140, 1e-139, 1e-138, 1e-137, 1e-136, 1e-135, 1e-134, 1e-133, 1e-132, 1e-131,
0077         1e-130, 1e-129, 1e-128, 1e-127, 1e-126, 1e-125, 1e-124, 1e-123, 1e-122, 1e-121,
0078         1e-120, 1e-119, 1e-118, 1e-117, 1e-116, 1e-115, 1e-114, 1e-113, 1e-112, 1e-111,
0079         1e-110, 1e-109, 1e-108, 1e-107, 1e-106, 1e-105, 1e-104, 1e-103, 1e-102, 1e-101,
0080 
0081         1e-100, 1e-099, 1e-098, 1e-097, 1e-096, 1e-095, 1e-094, 1e-093, 1e-092, 1e-091,
0082         1e-090, 1e-089, 1e-088, 1e-087, 1e-086, 1e-085, 1e-084, 1e-083, 1e-082, 1e-081,
0083         1e-080, 1e-079, 1e-078, 1e-077, 1e-076, 1e-075, 1e-074, 1e-073, 1e-072, 1e-071,
0084         1e-070, 1e-069, 1e-068, 1e-067, 1e-066, 1e-065, 1e-064, 1e-063, 1e-062, 1e-061,
0085         1e-060, 1e-059, 1e-058, 1e-057, 1e-056, 1e-055, 1e-054, 1e-053, 1e-052, 1e-051,
0086         1e-050, 1e-049, 1e-048, 1e-047, 1e-046, 1e-045, 1e-044, 1e-043, 1e-042, 1e-041,
0087         1e-040, 1e-039, 1e-038, 1e-037, 1e-036, 1e-035, 1e-034, 1e-033, 1e-032, 1e-031,
0088         1e-030, 1e-029, 1e-028, 1e-027, 1e-026, 1e-025, 1e-024, 1e-023, 1e-022, 1e-021,
0089         1e-020, 1e-019, 1e-018, 1e-017, 1e-016, 1e-015, 1e-014, 1e-013, 1e-012, 1e-011,
0090         1e-010, 1e-009, 1e-008, 1e-007, 1e-006, 1e-005, 1e-004, 1e-003, 1e-002, 1e-001,
0091 
0092         1e+000, 1e+001, 1e+002, 1e+003, 1e+004, 1e+005, 1e+006, 1e+007, 1e+008, 1e+009,
0093         1e+010, 1e+011, 1e+012, 1e+013, 1e+014, 1e+015, 1e+016, 1e+017, 1e+018, 1e+019,
0094         1e+020, 1e+021, 1e+022, 1e+023, 1e+024, 1e+025, 1e+026, 1e+027, 1e+028, 1e+029,
0095         1e+030, 1e+031, 1e+032, 1e+033, 1e+034, 1e+035, 1e+036, 1e+037, 1e+038, 1e+039,
0096         1e+040, 1e+041, 1e+042, 1e+043, 1e+044, 1e+045, 1e+046, 1e+047, 1e+048, 1e+049,
0097         1e+050, 1e+051, 1e+052, 1e+053, 1e+054, 1e+055, 1e+056, 1e+057, 1e+058, 1e+059,
0098         1e+060, 1e+061, 1e+062, 1e+063, 1e+064, 1e+065, 1e+066, 1e+067, 1e+068, 1e+069,
0099         1e+070, 1e+071, 1e+072, 1e+073, 1e+074, 1e+075, 1e+076, 1e+077, 1e+078, 1e+079,
0100         1e+080, 1e+081, 1e+082, 1e+083, 1e+084, 1e+085, 1e+086, 1e+087, 1e+088, 1e+089,
0101         1e+090, 1e+091, 1e+092, 1e+093, 1e+094, 1e+095, 1e+096, 1e+097, 1e+098, 1e+099,
0102 
0103         1e+100, 1e+101, 1e+102, 1e+103, 1e+104, 1e+105, 1e+106, 1e+107, 1e+108, 1e+109,
0104         1e+110, 1e+111, 1e+112, 1e+113, 1e+114, 1e+115, 1e+116, 1e+117, 1e+118, 1e+119,
0105         1e+120, 1e+121, 1e+122, 1e+123, 1e+124, 1e+125, 1e+126, 1e+127, 1e+128, 1e+129,
0106         1e+130, 1e+131, 1e+132, 1e+133, 1e+134, 1e+135, 1e+136, 1e+137, 1e+138, 1e+139,
0107         1e+140, 1e+141, 1e+142, 1e+143, 1e+144, 1e+145, 1e+146, 1e+147, 1e+148, 1e+149,
0108         1e+150, 1e+151, 1e+152, 1e+153, 1e+154, 1e+155, 1e+156, 1e+157, 1e+158, 1e+159,
0109         1e+160, 1e+161, 1e+162, 1e+163, 1e+164, 1e+165, 1e+166, 1e+167, 1e+168, 1e+169,
0110         1e+170, 1e+171, 1e+172, 1e+173, 1e+174, 1e+175, 1e+176, 1e+177, 1e+178, 1e+179,
0111         1e+180, 1e+181, 1e+182, 1e+183, 1e+184, 1e+185, 1e+186, 1e+187, 1e+188, 1e+189,
0112         1e+190, 1e+191, 1e+192, 1e+193, 1e+194, 1e+195, 1e+196, 1e+197, 1e+198, 1e+199,
0113 
0114         1e+200, 1e+201, 1e+202, 1e+203, 1e+204, 1e+205, 1e+206, 1e+207, 1e+208, 1e+209,
0115         1e+210, 1e+211, 1e+212, 1e+213, 1e+214, 1e+215, 1e+216, 1e+217, 1e+218, 1e+219,
0116         1e+220, 1e+221, 1e+222, 1e+223, 1e+224, 1e+225, 1e+226, 1e+227, 1e+228, 1e+229,
0117         1e+230, 1e+231, 1e+232, 1e+233, 1e+234, 1e+235, 1e+236, 1e+237, 1e+238, 1e+239,
0118         1e+240, 1e+241, 1e+242, 1e+243, 1e+244, 1e+245, 1e+246, 1e+247, 1e+248, 1e+249,
0119         1e+250, 1e+251, 1e+252, 1e+253, 1e+254, 1e+255, 1e+256, 1e+257, 1e+258, 1e+259,
0120         1e+260, 1e+261, 1e+262, 1e+263, 1e+264, 1e+265, 1e+266, 1e+267, 1e+268, 1e+269,
0121         1e+270, 1e+271, 1e+272, 1e+273, 1e+274, 1e+275, 1e+276, 1e+277, 1e+278, 1e+279,
0122         1e+280, 1e+281, 1e+282, 1e+283, 1e+284, 1e+285, 1e+286, 1e+287, 1e+288, 1e+289,
0123         1e+290, 1e+291, 1e+292, 1e+293, 1e+294, 1e+295, 1e+296, 1e+297, 1e+298, 1e+299,
0124 
0125         1e+300, 1e+301, 1e+302, 1e+303, 1e+304, 1e+305, 1e+306, 1e+307, 1e+308 };
0126 
0127     if( exp > 308 )
0128     {
0129         return std::numeric_limits<double>::infinity();
0130     }
0131     else if( exp < -308 )
0132     {
0133         // due to the way pow10 is used by dec_to_float,
0134         // we can afford to return 0.0 here
0135         return 0.0;
0136     }
0137     else
0138     {
0139         exp += 308;
0140         BOOST_ASSERT(exp >= 0 && exp < 618);
0141         return tab[exp];
0142     }
0143 }
0144 
0145 inline
0146 double
0147 dec_to_float(
0148     std::uint64_t m,
0149     std::int32_t e,
0150     bool neg) noexcept
0151 {
0152     // convert to double explicitly to silence warnings
0153     double x = static_cast<double>(m);
0154     if(neg)
0155         x = -x;
0156 
0157     if(e < -305)
0158     {
0159         x *= 1e-305 ;
0160         e += 305;
0161     }
0162 
0163     if(e >= -22 && e < 0)
0164         return x / pow10(-e);
0165 
0166     return x * pow10(e);
0167 }
0168 
0169 inline
0170 bool
0171 is_control(char c) noexcept
0172 {
0173     return static_cast<unsigned char>(c) < 32;
0174 }
0175 
0176 inline
0177 int
0178 hex_digit(unsigned char c) noexcept
0179 {
0180     // by Peter Dimov
0181     if( c >= '0' && c <= '9' )
0182         return c - '0';
0183     c &= ~0x20;
0184     if( c >= 'A' && c <= 'F' )
0185         return 10 + c - 'A';
0186     return -1;
0187 }
0188 
0189 enum json_literal
0190 {
0191     null_literal = 0,
0192     true_literal,
0193     false_literal,
0194     infinity_literal,
0195     neg_infinity_literal,
0196     nan_literal,
0197     resume_literal = -1
0198 };
0199 
0200 } // detail
0201 
0202 //----------------------------------------------------------
0203 
0204 template< class Handler >
0205 template< bool StackEmpty_, char First_ >
0206 struct basic_parser<Handler>::
0207 parse_number_helper
0208 {
0209     basic_parser* parser;
0210     char const* p;
0211 
0212     template< std::size_t N >
0213     char const*
0214     operator()( mp11::mp_size_t<N> ) const
0215     {
0216         return parser->parse_number(
0217             p,
0218             std::integral_constant<bool, StackEmpty_>(),
0219             std::integral_constant<char, First_>(),
0220             std::integral_constant<
0221                 number_precision, static_cast<number_precision>(N)>() );
0222     }
0223 };
0224 
0225 //----------------------------------------------------------
0226 
0227 template<class Handler>
0228 void
0229 basic_parser<Handler>::
0230 reserve()
0231 {
0232     if(BOOST_JSON_LIKELY(
0233         ! st_.empty()))
0234         return;
0235     // Reserve the largest stack we need,
0236     // to avoid reallocation during suspend.
0237     st_.reserve(
0238         sizeof(state) + // document parsing state
0239         (sizeof(state) +
0240             sizeof(std::size_t)) * depth() + // array and object state + size
0241         sizeof(state) + // value parsing state
0242         sizeof(std::size_t) + // string size
0243         sizeof(state)); // comment state
0244 }
0245 
0246 //----------------------------------------------------------
0247 //
0248 // The sentinel value is returned by parse functions
0249 // to indicate that the parser failed, or suspended.
0250 // this is used as it is distinct from all valid values
0251 // for data in write
0252 
0253 template<class Handler>
0254 const char*
0255 basic_parser<Handler>::
0256 sentinel()
0257 {
0258     // the "+1" ensures that the returned pointer is unique even if
0259     // the given input buffer borders on this object
0260     return reinterpret_cast<
0261         const char*>(this) + 1;
0262 }
0263 
0264 template<class Handler>
0265 bool
0266 basic_parser<Handler>::
0267 incomplete(
0268     const detail::const_stream_wrapper& cs)
0269 {
0270     return cs.begin() == sentinel();
0271 }
0272 
0273 //----------------------------------------------------------
0274 //
0275 // These functions are declared with the BOOST_NOINLINE
0276 // attribute to avoid polluting the parsers hot-path.
0277 // They return the canary value to indicate suspension
0278 // or failure.
0279 
0280 template<class Handler>
0281 const char*
0282 basic_parser<Handler>::
0283 suspend_or_fail(state st)
0284 {
0285     if(BOOST_JSON_LIKELY(
0286         ! ec_ && more_))
0287     {
0288         // suspend
0289         reserve();
0290         st_.push_unchecked(st);
0291     }
0292     return sentinel();
0293 }
0294 
0295 template<class Handler>
0296 const char*
0297 basic_parser<Handler>::
0298 suspend_or_fail(
0299     state st,
0300     std::size_t n)
0301 {
0302     if(BOOST_JSON_LIKELY(
0303         ! ec_ && more_))
0304     {
0305         // suspend
0306         reserve();
0307         st_.push_unchecked(n);
0308         st_.push_unchecked(st);
0309     }
0310     return sentinel();
0311 }
0312 
0313 
0314 template<class Handler>
0315 const char*
0316 basic_parser<Handler>::
0317 fail(const char* p) noexcept
0318 {
0319     BOOST_ASSERT( p != sentinel() );
0320     end_ = p;
0321     return sentinel();
0322 }
0323 
0324 template<class Handler>
0325 const char*
0326 basic_parser<Handler>::
0327 fail(
0328     const char* p,
0329     error ev,
0330     source_location const* loc) noexcept
0331 {
0332     BOOST_ASSERT( p != sentinel() );
0333     end_ = p;
0334     ec_.assign(ev, loc);
0335     return sentinel();
0336 }
0337 
0338 template<class Handler>
0339 const char*
0340 basic_parser<Handler>::
0341 maybe_suspend(
0342     const char* p,
0343     state st)
0344 {
0345     if( p != sentinel() )
0346         end_ = p;
0347     if(BOOST_JSON_LIKELY(more_))
0348     {
0349         // suspend
0350         reserve();
0351         st_.push_unchecked(st);
0352     }
0353     return sentinel();
0354 }
0355 
0356 template<class Handler>
0357 const char*
0358 basic_parser<Handler>::
0359 maybe_suspend(
0360     const char* p,
0361     state st,
0362     std::size_t n)
0363 {
0364     BOOST_ASSERT( p != sentinel() );
0365     end_ = p;
0366     if(BOOST_JSON_LIKELY(more_))
0367     {
0368         // suspend
0369         reserve();
0370         st_.push_unchecked(n);
0371         st_.push_unchecked(st);
0372     }
0373     return sentinel();
0374 }
0375 
0376 template<class Handler>
0377 const char*
0378 basic_parser<Handler>::
0379 maybe_suspend(
0380     const char* p,
0381     state st,
0382     const number& num)
0383 {
0384     BOOST_ASSERT( p != sentinel() );
0385     end_ = p;
0386     if(BOOST_JSON_LIKELY(more_))
0387     {
0388         // suspend
0389         num_ = num;
0390         reserve();
0391         st_.push_unchecked(st);;
0392     }
0393     return sentinel();
0394 }
0395 
0396 template<class Handler>
0397 const char*
0398 basic_parser<Handler>::
0399 suspend(
0400     const char* p,
0401     state st)
0402 {
0403     BOOST_ASSERT( p != sentinel() );
0404     end_ = p;
0405     // suspend
0406     reserve();
0407     st_.push_unchecked(st);
0408     return sentinel();
0409 }
0410 
0411 template<class Handler>
0412 const char*
0413 basic_parser<Handler>::
0414 suspend(
0415     const char* p,
0416     state st,
0417     const number& num)
0418 {
0419     BOOST_ASSERT( p != sentinel() );
0420     end_ = p;
0421     // suspend
0422     num_ = num;
0423     reserve();
0424     st_.push_unchecked(st);
0425     return sentinel();
0426 }
0427 
0428 template<class Handler>
0429 template<
0430     bool StackEmpty_/*,
0431     bool Terminal_*/>
0432 const char*
0433 basic_parser<Handler>::
0434 parse_comment(const char* p,
0435     std::integral_constant<bool, StackEmpty_> stack_empty,
0436     /*std::integral_constant<bool, Terminal_>*/ bool terminal)
0437 {
0438     detail::const_stream_wrapper cs(p, end_);
0439     const char* start = cs.begin();
0440     std::size_t remain;
0441     if(! stack_empty && ! st_.empty())
0442     {
0443         state st;
0444         st_.pop(st);
0445         switch(st)
0446         {
0447             default: BOOST_JSON_UNREACHABLE();
0448             case state::com1: goto do_com1;
0449             case state::com2: goto do_com2;
0450             case state::com3: goto do_com3;
0451             case state::com4: goto do_com4;
0452         }
0453     }
0454     BOOST_ASSERT(*cs == '/');
0455     ++cs;
0456 do_com1:
0457     if(BOOST_JSON_UNLIKELY(! cs))
0458         return maybe_suspend(cs.begin(), state::com1);
0459     switch(*cs)
0460     {
0461     default:
0462         {
0463             BOOST_STATIC_CONSTEXPR source_location loc
0464                 = BOOST_CURRENT_LOCATION;
0465             return fail(cs.begin(), error::syntax, &loc);
0466         }
0467     case '/':
0468         ++cs;
0469 do_com2:
0470         // KRYSTIAN TODO: this is a mess, we have to fix this
0471         remain = cs.remain();
0472         cs = remain ? static_cast<const char*>(
0473             std::memchr(cs.begin(), '\n', remain)) : sentinel();
0474         if(! cs.begin())
0475             cs = sentinel();
0476         if(BOOST_JSON_UNLIKELY(incomplete(cs)))
0477         {
0478             // if the doc does not terminate
0479             // with a newline, treat it as the
0480             // end of the comment
0481             if(terminal && ! more_)
0482             {
0483                 if(BOOST_JSON_UNLIKELY(! h_.on_comment(
0484                     {start, cs.remain(start)}, ec_)))
0485                     return fail(cs.end());
0486                 return cs.end();
0487             }
0488             if(BOOST_JSON_UNLIKELY(! h_.on_comment_part(
0489                 {start, cs.remain(start)}, ec_)))
0490                 return fail(cs.end());
0491             if(terminal)
0492                 return suspend(cs.end(), state::com2);
0493             return maybe_suspend(cs.end(), state::com2);
0494         }
0495         break;
0496     case '*':
0497         do
0498         {
0499             ++cs;
0500 do_com3:
0501             // KRYSTIAN TODO: this is a mess, we have to fix this
0502             remain = cs.remain();
0503             cs = remain ? static_cast<const char*>(
0504                 std::memchr(cs.begin(), '*', remain)) : sentinel();
0505             if(! cs.begin())
0506                 cs = sentinel();
0507             // stopped inside a c comment
0508             if(BOOST_JSON_UNLIKELY(incomplete(cs)))
0509             {
0510                 if(BOOST_JSON_UNLIKELY(! h_.on_comment_part(
0511                     {start, cs.remain(start)}, ec_)))
0512                     return fail(cs.end());
0513                 return maybe_suspend(cs.end(), state::com3);
0514             }
0515             // found a asterisk, check if the next char is a slash
0516             ++cs;
0517 do_com4:
0518             if(BOOST_JSON_UNLIKELY(! cs))
0519             {
0520                 if(BOOST_JSON_UNLIKELY(! h_.on_comment_part(
0521                     {start, cs.used(start)}, ec_)))
0522                     return fail(cs.begin());
0523                 return maybe_suspend(cs.begin(), state::com4);
0524             }
0525         }
0526         while(*cs != '/');
0527     }
0528     ++cs;
0529     if(BOOST_JSON_UNLIKELY(! h_.on_comment(
0530         {start, cs.used(start)}, ec_)))
0531         return fail(cs.begin());
0532     return cs.begin();
0533 }
0534 
0535 template<class Handler>
0536 template<bool StackEmpty_>
0537 const char*
0538 basic_parser<Handler>::
0539 parse_document(const char* p,
0540     std::integral_constant<bool, StackEmpty_> stack_empty)
0541 {
0542     detail::const_stream_wrapper cs(p, end_);
0543     if(! stack_empty && ! st_.empty())
0544     {
0545         state st;
0546         st_.peek(st);
0547         switch(st)
0548         {
0549         default: goto do_doc2;
0550         case state::doc1:
0551                  st_.pop(st);
0552                  goto do_doc1;
0553         case state::doc3:
0554                  st_.pop(st);
0555                  goto do_doc3;
0556         case state::com1: case state::com2:
0557         case state::com3: case state::com4:
0558                  goto do_doc4;
0559         }
0560     }
0561 do_doc1:
0562     cs = detail::count_whitespace(cs.begin(), cs.end());
0563     if(BOOST_JSON_UNLIKELY(! cs))
0564         return maybe_suspend(cs.begin(), state::doc1);
0565 do_doc2:
0566     switch(+opt_.allow_comments |
0567         (opt_.allow_trailing_commas << 1) |
0568         (opt_.allow_invalid_utf8 << 2))
0569     {
0570     // no extensions
0571     default:
0572         cs = parse_value(cs.begin(), stack_empty, std::false_type(), std::false_type(), std::false_type(), opt_.allow_invalid_utf16);
0573         break;
0574     // comments
0575     case 1:
0576         cs = parse_value(cs.begin(), stack_empty, std::true_type(), std::false_type(), std::false_type(), opt_.allow_invalid_utf16);
0577         break;
0578     // trailing
0579     case 2:
0580         cs = parse_value(cs.begin(), stack_empty, std::false_type(), std::true_type(), std::false_type(), opt_.allow_invalid_utf16);
0581         break;
0582     // comments & trailing
0583     case 3:
0584         cs = parse_value(cs.begin(), stack_empty, std::true_type(), std::true_type(), std::false_type(), opt_.allow_invalid_utf16);
0585         break;
0586     // skip validation
0587     case 4:
0588         cs = parse_value(cs.begin(), stack_empty, std::false_type(), std::false_type(), std::true_type(), opt_.allow_invalid_utf16);
0589         break;
0590     // comments & skip validation
0591     case 5:
0592         cs = parse_value(cs.begin(), stack_empty, std::true_type(), std::false_type(), std::true_type(), opt_.allow_invalid_utf16);
0593         break;
0594     // trailing & skip validation
0595     case 6:
0596         cs = parse_value(cs.begin(), stack_empty, std::false_type(), std::true_type(), std::true_type(), opt_.allow_invalid_utf16);
0597         break;
0598     // comments & trailing & skip validation
0599     case 7:
0600         cs = parse_value(cs.begin(), stack_empty, std::true_type(), std::true_type(), std::true_type(), opt_.allow_invalid_utf16);
0601         break;
0602     }
0603     if(BOOST_JSON_UNLIKELY(incomplete(cs)))
0604         // the appropriate state has already been pushed into stack
0605         return sentinel();
0606 do_doc3:
0607     cs = detail::count_whitespace(cs.begin(), cs.end());
0608     if(BOOST_JSON_UNLIKELY(! cs))
0609     {
0610         if(more_)
0611             return suspend(cs.begin(), state::doc3);
0612     }
0613     else if(opt_.allow_comments && *cs == '/')
0614     {
0615 do_doc4:
0616         cs = parse_comment(cs.begin(), stack_empty, std::true_type());
0617         if(BOOST_JSON_UNLIKELY(incomplete(cs)))
0618             return sentinel();
0619         goto do_doc3;
0620     }
0621     return cs.begin();
0622 }
0623 
0624 template<class Handler>
0625 template<
0626     bool StackEmpty_,
0627     bool AllowComments_/*,
0628     bool AllowTrailing_,
0629     bool AllowBadUTF8_*/>
0630 const char*
0631 basic_parser<Handler>::
0632 parse_value(const char* p,
0633     std::integral_constant<bool, StackEmpty_> stack_empty,
0634     std::integral_constant<bool, AllowComments_> allow_comments,
0635     /*std::integral_constant<bool, AllowTrailing_>*/ bool allow_trailing,
0636     /*std::integral_constant<bool, AllowBadUTF8_>*/ bool allow_bad_utf8,
0637     bool allow_bad_utf16)
0638 {
0639     if(stack_empty || st_.empty())
0640     {
0641 loop:
0642         switch(*p)
0643         {
0644         case '0':
0645             return mp11::mp_with_index<3>(
0646                 static_cast<unsigned char>(opt_.numbers),
0647                 parse_number_helper<true, '0'>{ this, p });
0648         case '-':
0649             return mp11::mp_with_index<3>(
0650                 static_cast<unsigned char>(opt_.numbers),
0651                 parse_number_helper<true, '-'>{ this, p });
0652         case '1': case '2': case '3':
0653         case '4': case '5': case '6':
0654         case '7': case '8': case '9':
0655             return mp11::mp_with_index<3>(
0656                 static_cast<unsigned char>(opt_.numbers),
0657                 parse_number_helper<true, '+'>{ this, p });
0658         case 'n':
0659             return parse_literal( p, mp11::mp_int<detail::null_literal>() );
0660         case 't':
0661             return parse_literal( p, mp11::mp_int<detail::true_literal>() );
0662         case 'f':
0663             return parse_literal( p, mp11::mp_int<detail::false_literal>() );
0664         case 'I':
0665             if( !opt_.allow_infinity_and_nan )
0666             {
0667                 BOOST_STATIC_CONSTEXPR source_location loc
0668                     = BOOST_CURRENT_LOCATION;
0669                 return fail(p, error::syntax, &loc);
0670             }
0671             return parse_literal( p, mp11::mp_int<detail::infinity_literal>() );
0672         case 'N':
0673             if( !opt_.allow_infinity_and_nan )
0674             {
0675                 BOOST_STATIC_CONSTEXPR source_location loc
0676                     = BOOST_CURRENT_LOCATION;
0677                 return fail(p, error::syntax, &loc);
0678             }
0679             return parse_literal( p, mp11::mp_int<detail::nan_literal>() );
0680         case '"':
0681             return parse_string(p, std::true_type(), std::false_type(), allow_bad_utf8, allow_bad_utf16);
0682         case '[':
0683             return parse_array(p, std::true_type(), allow_comments, allow_trailing, allow_bad_utf8, allow_bad_utf16);
0684         case '{':
0685             return parse_object(p, std::true_type(), allow_comments, allow_trailing, allow_bad_utf8, allow_bad_utf16);
0686         case '/':
0687             if(! allow_comments)
0688             {
0689                 BOOST_STATIC_CONSTEXPR source_location loc
0690                     = BOOST_CURRENT_LOCATION;
0691                 return fail(p, error::syntax, &loc);
0692             }
0693             p = parse_comment(p, stack_empty, std::false_type());
0694             // KRYSTIAN NOTE: incomplete takes const_stream, we either
0695             // can add an overload, change the existing one to take a pointer,
0696             // or just leave it as is
0697             if(BOOST_JSON_UNLIKELY(p == sentinel()))
0698                 return maybe_suspend(p, state::val2);
0699             // intentional fallthrough
0700         case ' ':
0701         case '\t':
0702         case '\n':
0703         case '\r':
0704             p = detail::count_whitespace(p, end_);
0705             if(BOOST_JSON_UNLIKELY(p == end_))
0706                 return maybe_suspend(p, state::val1);
0707             goto loop;
0708         default:
0709             {
0710                 BOOST_STATIC_CONSTEXPR source_location loc
0711                     = BOOST_CURRENT_LOCATION;
0712                 return fail(p, error::syntax, &loc);
0713             }
0714         }
0715     }
0716     return resume_value(p, allow_comments, allow_trailing, allow_bad_utf8, allow_bad_utf16);
0717 }
0718 
0719 template<class Handler>
0720 template<
0721     bool AllowComments_/*,
0722     bool AllowTrailing_,
0723     bool AllowBadUTF8_*/>
0724 const char*
0725 basic_parser<Handler>::
0726 resume_value(const char* p,
0727     std::integral_constant<bool, AllowComments_> allow_comments,
0728     /*std::integral_constant<bool, AllowTrailing_>*/ bool allow_trailing,
0729     /*std::integral_constant<bool, AllowBadUTF8_>*/ bool allow_bad_utf8,
0730     bool allow_bad_utf16)
0731 {
0732     state st;
0733     st_.peek(st);
0734     switch(st)
0735     {
0736     default: BOOST_JSON_UNREACHABLE();
0737     case state::lit1:
0738         return parse_literal(p,  mp11::mp_int<detail::resume_literal>() );
0739 
0740     case state::str1: case state::str2:
0741     case state::str8:
0742         return parse_string(p, std::false_type(), std::false_type(), allow_bad_utf8, allow_bad_utf16);
0743 
0744     case state::arr1: case state::arr2:
0745     case state::arr3: case state::arr4:
0746     case state::arr5: case state::arr6:
0747         return parse_array(p, std::false_type(), allow_comments, allow_trailing, allow_bad_utf8, allow_bad_utf16);
0748 
0749     case state::obj1: case state::obj2:
0750     case state::obj3: case state::obj4:
0751     case state::obj5: case state::obj6:
0752     case state::obj7: case state::obj8:
0753     case state::obj9: case state::obj10:
0754     case state::obj11:
0755         return parse_object(p, std::false_type(), allow_comments, allow_trailing, allow_bad_utf8, allow_bad_utf16);
0756 
0757     case state::num1: case state::num2:
0758     case state::num3: case state::num4:
0759     case state::num5: case state::num6:
0760     case state::num7: case state::num8:
0761     case state::exp1: case state::exp2:
0762     case state::exp3:
0763         return mp11::mp_with_index<3>(
0764             static_cast<unsigned char>(opt_.numbers),
0765             parse_number_helper<false, 0>{ this, p });
0766 
0767     // KRYSTIAN NOTE: these are special cases
0768     case state::val1:
0769     {
0770         st_.pop(st);
0771         BOOST_ASSERT(st_.empty());
0772         p = detail::count_whitespace(p, end_);
0773         if(BOOST_JSON_UNLIKELY(p == end_))
0774             return maybe_suspend(p, state::val1);
0775         return parse_value(p, std::true_type(), allow_comments, allow_trailing, allow_bad_utf8, allow_bad_utf16);
0776     }
0777 
0778     case state::val2:
0779     {
0780         st_.pop(st);
0781         p = parse_comment(p, std::false_type(), std::false_type());
0782         if(BOOST_JSON_UNLIKELY(p == sentinel()))
0783             return maybe_suspend(p, state::val2);
0784         if(BOOST_JSON_UNLIKELY( p == end_ ))
0785             return maybe_suspend(p, state::val3);
0786         BOOST_ASSERT(st_.empty());
0787         return parse_value(p, std::true_type(), std::true_type(), allow_trailing, allow_bad_utf8, allow_bad_utf16);
0788     }
0789 
0790     case state::val3:
0791     {
0792         st_.pop(st);
0793         return parse_value(p, std::true_type(), std::true_type(), allow_trailing, allow_bad_utf8, allow_bad_utf16);
0794     }
0795     }
0796 }
0797 
0798 template<class Handler>
0799 template<int Literal>
0800 const char*
0801 basic_parser<Handler>::
0802 parse_literal(const char* p,
0803     std::integral_constant<int, Literal> literal)
0804 {
0805     constexpr char const* literals[] = {
0806         "null",
0807         "true",
0808         "false",
0809         "Infinity",
0810         "-Infinity",
0811         "NaN",
0812     };
0813 
0814     constexpr std::size_t literal_sizes[] = {
0815         4,
0816         4,
0817         5,
0818         8,
0819         9,
0820         3,
0821     };
0822 
0823     std::size_t cur_lit;
0824     std::size_t offset;
0825 
0826     detail::const_stream_wrapper cs(p, end_);
0827     BOOST_IF_CONSTEXPR( literal != detail::resume_literal )
0828     {
0829         BOOST_ASSERT( literal >= 0 );
0830         if(BOOST_JSON_LIKELY( cs.remain() >= literal_sizes[literal] ))
0831         {
0832             int const cmp = std::memcmp(
0833                 cs.begin(), literals[literal], literal_sizes[literal] );
0834             if( cmp != 0 )
0835             {
0836                 BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
0837                 return fail(cs.begin(), error::syntax, &loc);
0838             }
0839 
0840             BOOST_IF_CONSTEXPR( literal == detail::null_literal )
0841             {
0842                 if(BOOST_JSON_UNLIKELY(
0843                     ! h_.on_null(ec_)))
0844                     return fail(cs.begin());
0845             }
0846             else BOOST_IF_CONSTEXPR( literal == detail::true_literal )
0847             {
0848                 if(BOOST_JSON_UNLIKELY(
0849                     ! h_.on_bool(true, ec_)))
0850                     return fail(cs.begin());
0851             }
0852             else BOOST_IF_CONSTEXPR( literal == detail::false_literal )
0853             {
0854                 if(BOOST_JSON_UNLIKELY(
0855                     ! h_.on_bool(false, ec_)))
0856                     return fail(cs.begin());
0857             }
0858             else BOOST_IF_CONSTEXPR( literal == detail::infinity_literal )
0859             {
0860                 if(BOOST_JSON_UNLIKELY(
0861                     ! h_.on_double(
0862                         std::numeric_limits<double>::infinity(),
0863                         string_view(
0864                             literals[detail::infinity_literal],
0865                             literal_sizes[detail::infinity_literal]),
0866                         ec_)))
0867                     return fail(cs.begin());
0868             }
0869             else BOOST_IF_CONSTEXPR( literal == detail::neg_infinity_literal )
0870             {
0871                 if(BOOST_JSON_UNLIKELY(
0872                     ! h_.on_double(
0873                         -std::numeric_limits<double>::infinity(),
0874                         string_view(
0875                             literals[detail::neg_infinity_literal],
0876                             literal_sizes[detail::neg_infinity_literal]),
0877                         ec_)))
0878                     return fail(cs.begin());
0879             }
0880             else BOOST_IF_CONSTEXPR( literal == detail::nan_literal )
0881             {
0882                 if(BOOST_JSON_UNLIKELY(
0883                     ! h_.on_double(
0884                         std::numeric_limits<double>::quiet_NaN(),
0885                         string_view(
0886                             literals[detail::nan_literal],
0887                             literal_sizes[detail::nan_literal]),
0888                         ec_)))
0889                     return fail(cs.begin());
0890             }
0891             else
0892             {
0893                 BOOST_JSON_UNREACHABLE();
0894             }
0895 
0896             cs += literal_sizes[literal];
0897             return cs.begin();
0898         }
0899 
0900         offset = 0;
0901         cur_lit = literal;
0902     }
0903     else
0904     {
0905         state st;
0906         st_.pop(st);
0907         BOOST_ASSERT( st == state::lit1 );
0908 
0909         cur_lit = cur_lit_;
0910         offset = lit_offset_;
0911     }
0912 
0913     std::size_t const size = (std::min)(
0914         literal_sizes[cur_lit] - offset, cs.remain() );
0915     int cmp = 0;
0916     if(BOOST_JSON_LIKELY( cs.begin() ))
0917         cmp = std::memcmp( cs.begin(), literals[cur_lit] + offset, size );
0918     if( cmp != 0 )
0919     {
0920         BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
0921         return fail(cs.begin(), error::syntax, &loc);
0922     }
0923 
0924     if(BOOST_JSON_UNLIKELY( offset + size < literal_sizes[cur_lit] ))
0925     {
0926         BOOST_ASSERT( cur_lit < 256 );
0927         cur_lit_ = static_cast<unsigned char>( cur_lit );
0928         BOOST_ASSERT( offset + size < 256 );
0929         lit_offset_ = static_cast<unsigned char>( offset + size );
0930         return maybe_suspend(cs.begin() + size, state::lit1);
0931     }
0932 
0933     switch( cur_lit )
0934     {
0935     case detail::null_literal:
0936         if(BOOST_JSON_UNLIKELY(
0937             ! h_.on_null(ec_)))
0938             return fail(cs.begin());
0939         break;
0940     case detail::true_literal:
0941         if(BOOST_JSON_UNLIKELY(
0942             ! h_.on_bool(true, ec_)))
0943             return fail(cs.begin());
0944         break;
0945     case detail::false_literal:
0946         if(BOOST_JSON_UNLIKELY(
0947             ! h_.on_bool(false, ec_)))
0948             return fail(cs.begin());
0949         break;
0950     case detail::infinity_literal:
0951         if(BOOST_JSON_UNLIKELY(
0952             ! h_.on_double(
0953                 std::numeric_limits<double>::infinity(),
0954                 string_view(
0955                     literals[detail::infinity_literal],
0956                     literal_sizes[detail::infinity_literal]),
0957                 ec_)))
0958             return fail(cs.begin());
0959         break;
0960     case detail::neg_infinity_literal:
0961         if(BOOST_JSON_UNLIKELY(
0962             ! h_.on_double(
0963                 -std::numeric_limits<double>::infinity(),
0964                 string_view(
0965                     literals[detail::neg_infinity_literal],
0966                     literal_sizes[detail::neg_infinity_literal]),
0967                 ec_)))
0968             return fail(cs.begin());
0969         break;
0970     case detail::nan_literal:
0971         if(BOOST_JSON_UNLIKELY(
0972             ! h_.on_double(
0973                 std::numeric_limits<double>::quiet_NaN(),
0974                 string_view(
0975                     literals[detail::nan_literal],
0976                     literal_sizes[detail::nan_literal]),
0977                 ec_)))
0978             return fail(cs.begin());
0979         break;
0980     default: BOOST_JSON_UNREACHABLE();
0981     }
0982 
0983     cs += size;
0984     return cs.begin();
0985 }
0986 
0987 //----------------------------------------------------------
0988 
0989 template<class Handler>
0990 template<bool StackEmpty_, bool IsKey_>
0991 const char*
0992 basic_parser<Handler>::
0993 parse_string(const char* p,
0994     std::integral_constant<bool, StackEmpty_> stack_empty,
0995     std::integral_constant<bool, IsKey_> is_key,
0996     bool allow_bad_utf8,
0997     bool allow_bad_utf16)
0998 {
0999     detail::const_stream_wrapper cs(p, end_);
1000     std::size_t total;
1001     char const* start;
1002     std::size_t size;
1003     if(! stack_empty && ! st_.empty())
1004     {
1005         state st;
1006         st_.pop(st);
1007         st_.pop(total);
1008         switch(st)
1009         {
1010         default: BOOST_JSON_UNREACHABLE();
1011         case state::str2: goto do_str2;
1012         case state::str8: goto do_str8;
1013         case state::str1: break;
1014         }
1015     }
1016     else
1017     {
1018         BOOST_ASSERT(*cs == '\x22'); // '"'
1019         ++cs;
1020         total = 0;
1021     }
1022 
1023 do_str1:
1024     start = cs.begin();
1025     cs = allow_bad_utf8?
1026         detail::count_valid<true>(cs.begin(), cs.end()):
1027         detail::count_valid<false>(cs.begin(), cs.end());
1028     size = cs.used(start);
1029     if(is_key)
1030     {
1031         BOOST_ASSERT(total <= Handler::max_key_size);
1032         if(BOOST_JSON_UNLIKELY(size >
1033             Handler::max_key_size - total))
1034         {
1035             BOOST_STATIC_CONSTEXPR source_location loc
1036                 = BOOST_CURRENT_LOCATION;
1037             return fail(cs.begin(), error::key_too_large, &loc);
1038         }
1039     }
1040     else
1041     {
1042         BOOST_ASSERT(total <= Handler::max_string_size);
1043         if(BOOST_JSON_UNLIKELY(size >
1044             Handler::max_string_size - total))
1045         {
1046             BOOST_STATIC_CONSTEXPR source_location loc
1047                 = BOOST_CURRENT_LOCATION;
1048             return fail(cs.begin(), error::string_too_large, &loc);
1049         }
1050     }
1051     total += size;
1052     if(BOOST_JSON_UNLIKELY(! cs))
1053     {
1054         // call handler if the string isn't empty
1055         if(BOOST_JSON_LIKELY(size))
1056         {
1057             {
1058                 bool r = is_key?
1059                     h_.on_key_part( {start, size}, total, ec_ ):
1060                     h_.on_string_part( {start, size}, total, ec_ );
1061 
1062                 if(BOOST_JSON_UNLIKELY(!r))
1063                 {
1064                     return fail(cs.begin());
1065                 }
1066             }
1067         }
1068         return maybe_suspend(cs.begin(), state::str1, total);
1069     }
1070     // at this point all valid characters have been skipped, so any remaining
1071     // if there are any more characters, they are either escaped, or incomplete
1072     // utf8, or invalid utf8
1073     if(BOOST_JSON_UNLIKELY(*cs != '\x22')) // '"'
1074     {
1075         // sequence is invalid or incomplete
1076         if((*cs & 0x80) && !allow_bad_utf8)
1077         {
1078             seq_.save(cs.begin(), cs.remain());
1079             if(BOOST_JSON_UNLIKELY(seq_.complete()))
1080             {
1081                 BOOST_STATIC_CONSTEXPR source_location loc
1082                     = BOOST_CURRENT_LOCATION;
1083                 return fail(cs.begin(), error::syntax, &loc);
1084             }
1085             if(BOOST_JSON_LIKELY(size))
1086             {
1087                 bool const r = is_key?
1088                     h_.on_key_part( {start, size}, total, ec_ ):
1089                     h_.on_string_part( {start, size}, total, ec_ );
1090                 if(BOOST_JSON_UNLIKELY( !r ))
1091                     return fail( cs.begin() );
1092             }
1093             return maybe_suspend(cs.end(), state::str8, total);
1094         }
1095         else if(BOOST_JSON_LIKELY(*cs == '\\'))
1096         {
1097             // flush unescaped run from input
1098             if(BOOST_JSON_LIKELY(size))
1099             {
1100                 bool const r = is_key?
1101                     h_.on_key_part( {start, size}, total, ec_ ):
1102                     h_.on_string_part( {start, size}, total, ec_ );
1103                 if(BOOST_JSON_UNLIKELY( !r ))
1104                     return fail( cs.begin() );
1105             }
1106 do_str2:
1107             cs = parse_escaped(cs.begin(), total, stack_empty, is_key, allow_bad_utf16);
1108             if(BOOST_JSON_UNLIKELY( incomplete(cs) ))
1109                 return suspend_or_fail(state::str2, total);
1110 
1111             goto do_str1;
1112         }
1113         // illegal control
1114         BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
1115         return fail(cs.begin(), error::syntax, &loc);
1116     }
1117 
1118     {
1119         bool r = is_key?
1120             h_.on_key( {start, size}, total, ec_ ):
1121             h_.on_string( {start, size}, total, ec_ );
1122 
1123         if(BOOST_JSON_UNLIKELY(!r))
1124         {
1125             return fail(cs.begin());
1126         }
1127     }
1128 
1129     ++cs;
1130     return cs.begin();
1131 
1132 do_str8:
1133     uint8_t needed = seq_.needed();
1134     if(BOOST_JSON_UNLIKELY( !seq_.append(cs.begin(), cs.remain()) ))
1135         return maybe_suspend(cs.end(), state::str8, total);
1136     if(BOOST_JSON_UNLIKELY( !seq_.valid() ))
1137     {
1138         BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
1139         return fail(cs.begin(), error::syntax, &loc);
1140     }
1141     {
1142         bool const r = is_key?
1143             h_.on_key_part( {seq_.data(), seq_.length()}, total, ec_ ):
1144             h_.on_string_part( {seq_.data(), seq_.length()}, total, ec_ );
1145         if(BOOST_JSON_UNLIKELY( !r ))
1146             return fail( cs.begin() );
1147     }
1148     cs += needed;
1149     goto do_str1;
1150 }
1151 
1152 template<class Handler>
1153 template<bool StackEmpty_>
1154 const char*
1155 basic_parser<Handler>::
1156 parse_escaped(
1157     const char* p,
1158     std::size_t& total,
1159     std::integral_constant<bool, StackEmpty_> stack_empty,
1160     bool is_key,
1161     bool allow_bad_utf16)
1162 {
1163     constexpr unsigned urc = 0xFFFD; // Unicode replacement character
1164     auto const ev_too_large = is_key?
1165         error::key_too_large : error::string_too_large;
1166     auto const max_size = is_key?
1167         Handler::max_key_size : Handler::max_string_size;
1168     int digit;
1169 
1170     //---------------------------------------------------------------
1171     //
1172     // To handle escapes, a local temporary buffer accumulates
1173     // the unescaped result. The algorithm attempts to fill the
1174     // buffer to capacity before invoking the handler.
1175     // In some cases the temporary buffer needs to be flushed
1176     // before it is full:
1177     // * When the closing double quote is seen
1178     // * When there in no more input (and more is expected later)
1179     // A goal of the algorithm is to call the handler as few times
1180     // as possible. Thus, when the first escape is encountered,
1181     // the algorithm attempts to fill the temporary buffer first.
1182     //
1183     detail::buffer<BOOST_JSON_STACK_BUFFER_SIZE> temp;
1184 
1185     // Unescaped JSON is never larger than its escaped version.
1186     // To efficiently process only what will fit in the temporary buffer,
1187     // the size of the input stream is temporarily "clipped" to the size
1188     // of the temporary buffer.
1189     // handle escaped character
1190     detail::clipped_const_stream cs(p, end_);
1191     cs.clip(temp.max_size());
1192 
1193     if(! stack_empty && ! st_.empty())
1194     {
1195         state st;
1196         st_.pop(st);
1197         switch(st)
1198         {
1199         default: BOOST_JSON_UNREACHABLE();
1200         case state::str3: goto do_str3;
1201         case state::str4: goto do_str4;
1202         case state::str5: goto do_str5;
1203         case state::str6: goto do_str6;
1204         case state::str7: goto do_str7;
1205         case state::sur1: goto do_sur1;
1206         case state::sur2: goto do_sur2;
1207         case state::sur3: goto do_sur3;
1208         case state::sur4: goto do_sur4;
1209         case state::sur5: goto do_sur5;
1210         case state::sur6: goto do_sur6;
1211         }
1212     }
1213 
1214     while(true)
1215     {
1216         BOOST_ASSERT( temp.capacity() );
1217         BOOST_ASSERT(*cs == '\\');
1218         ++cs;
1219 do_str3:
1220         if(BOOST_JSON_UNLIKELY(! cs))
1221         {
1222             if(BOOST_JSON_LIKELY(! temp.empty()))
1223             {
1224                 BOOST_ASSERT(total <= max_size);
1225                 if(BOOST_JSON_UNLIKELY(
1226                     temp.size() > max_size - total))
1227                 {
1228                     BOOST_STATIC_CONSTEXPR source_location loc
1229                         = BOOST_CURRENT_LOCATION;
1230                     return fail(cs.begin(), ev_too_large, &loc);
1231                 }
1232                 total += temp.size();
1233                 {
1234                     bool r = is_key
1235                         ? h_.on_key_part(temp.get(), total, ec_)
1236                         : h_.on_string_part(temp.get(), total, ec_);
1237 
1238                     if(BOOST_JSON_UNLIKELY(!r))
1239                     {
1240                         return fail(cs.begin());
1241                     }
1242                 }
1243                 temp.clear();
1244             }
1245             cs.clip(temp.max_size());
1246             if(BOOST_JSON_UNLIKELY(! cs))
1247                 return maybe_suspend(cs.begin(), state::str3);
1248         }
1249         switch(*cs)
1250         {
1251         default:
1252             {
1253                 BOOST_STATIC_CONSTEXPR source_location loc
1254                     = BOOST_CURRENT_LOCATION;
1255                 return fail(cs.begin(), error::syntax, &loc);
1256             }
1257         case '\x22': // '"'
1258             temp.push_back('\x22');
1259             ++cs;
1260             break;
1261         case '\\':
1262             temp.push_back('\\');
1263             ++cs;
1264             break;
1265         case '/':
1266             temp.push_back('/');
1267             ++cs;
1268             break;
1269         case 'b':
1270             temp.push_back('\x08');
1271             ++cs;
1272             break;
1273         case 'f':
1274             temp.push_back('\x0c');
1275             ++cs;
1276             break;
1277         case 'n':
1278             temp.push_back('\x0a');
1279             ++cs;
1280             break;
1281         case 'r':
1282             temp.push_back('\x0d');
1283             ++cs;
1284             break;
1285         case 't':
1286             temp.push_back('\x09');
1287             ++cs;
1288             break;
1289         case 'u':
1290             // utf16 escape
1291             //
1292             // fast path only when the buffer
1293             // is large enough for 2 surrogates
1294             if(BOOST_JSON_LIKELY(cs.remain() > 10))
1295             {
1296                 // KRYSTIAN TODO: this could be done
1297                 // with fewer instructions
1298                 digit = detail::load_little_endian<4>(
1299                     cs.begin() + 1);
1300                 int d4 = detail::hex_digit(static_cast<
1301                     unsigned char>(digit >> 24));
1302                 int d3 = detail::hex_digit(static_cast<
1303                     unsigned char>(digit >> 16));
1304                 int d2 = detail::hex_digit(static_cast<
1305                     unsigned char>(digit >> 8));
1306                 int d1 = detail::hex_digit(static_cast<
1307                     unsigned char>(digit));
1308                 if(BOOST_JSON_UNLIKELY(
1309                     (d1 | d2 | d3 | d4) == -1))
1310                 {
1311                     if(d1 != -1)
1312                         ++cs;
1313                     if(d2 != -1)
1314                         ++cs;
1315                     if(d3 != -1)
1316                         ++cs;
1317                     BOOST_STATIC_CONSTEXPR source_location loc
1318                         = BOOST_CURRENT_LOCATION;
1319                     return fail(cs.begin(), error::expected_hex_digit, &loc);
1320                 }
1321                 // 32 bit unicode scalar value
1322                 unsigned u1 =
1323                     (d1 << 12) + (d2 << 8) +
1324                     (d3 << 4) + d4;
1325                 // valid unicode scalar values are
1326                 // [0, D7FF] and [E000, 10FFFF]
1327                 // values within this range are valid utf-8
1328                 // code points and invalid leading surrogates.
1329                 if(BOOST_JSON_LIKELY(
1330                     u1 < 0xd800 || u1 > 0xdfff))
1331                 {
1332                     cs += 5;
1333                     temp.append_utf8(u1);
1334                     break;
1335                 }
1336                 if(BOOST_JSON_UNLIKELY(u1 > 0xdbff))
1337                 {
1338                     // If it's an illegal leading surrogate and
1339                     // the parser does not allow it, return an error.
1340                     if(!allow_bad_utf16)
1341                     {
1342                         BOOST_STATIC_CONSTEXPR source_location loc
1343                             = BOOST_CURRENT_LOCATION;
1344                         return fail(cs.begin(), error::illegal_leading_surrogate,
1345                             &loc);
1346                     }
1347                     // Otherwise, append the Unicode replacement character
1348                     else
1349                     {
1350                         cs += 5;
1351                         temp.append_utf8(urc);
1352                         break;
1353                     }
1354                 }
1355                 cs += 5;
1356                 // KRYSTIAN TODO: this can be a two byte load
1357                 // and a single comparison. We lose error information,
1358                 // but it's faster.
1359                 if(BOOST_JSON_UNLIKELY(*cs != '\\'))
1360                 {
1361                     // If the next character is not a backslash and
1362                     // the parser does not allow it, return a syntax error.
1363                     if(!allow_bad_utf16)
1364                     {
1365                         BOOST_STATIC_CONSTEXPR source_location loc
1366                             = BOOST_CURRENT_LOCATION;
1367                         return fail(cs.begin(), error::syntax, &loc);
1368                     }
1369                     // Otherwise, append the Unicode replacement character since
1370                     // the first code point is a valid leading surrogate
1371                     else
1372                     {
1373                         temp.append_utf8(urc);
1374                         break;
1375                     }
1376                 }
1377                 ++cs;
1378                 if(BOOST_JSON_UNLIKELY(*cs != 'u'))
1379                 {
1380                     if (!allow_bad_utf16)
1381                     {
1382                         BOOST_STATIC_CONSTEXPR source_location loc
1383                             = BOOST_CURRENT_LOCATION;
1384                         return fail(cs.begin(), error::syntax, &loc);
1385                     }
1386                     // Otherwise, append the Unicode replacement character since
1387                     // the first code point is a valid leading surrogate
1388                     else
1389                     {
1390                         temp.append_utf8(urc);
1391                         goto do_str3;
1392                     }
1393                 }
1394                 ++cs;
1395                 digit = detail::load_little_endian<4>(cs.begin());
1396                 d4 = detail::hex_digit(static_cast<
1397                     unsigned char>(digit >> 24));
1398                 d3 = detail::hex_digit(static_cast<
1399                     unsigned char>(digit >> 16));
1400                 d2 = detail::hex_digit(static_cast<
1401                     unsigned char>(digit >> 8));
1402                 d1 = detail::hex_digit(static_cast<
1403                     unsigned char>(digit));
1404                 if(BOOST_JSON_UNLIKELY(
1405                     (d1 | d2 | d3 | d4) == -1))
1406                 {
1407                     if(d1 != -1)
1408                         ++cs;
1409                     if(d2 != -1)
1410                         ++cs;
1411                     if(d3 != -1)
1412                         ++cs;
1413                     BOOST_STATIC_CONSTEXPR source_location loc
1414                         = BOOST_CURRENT_LOCATION;
1415                     return fail(cs.begin(), error::expected_hex_digit, &loc);
1416                 }
1417                 unsigned u2 =
1418                     (d1 << 12) + (d2 << 8) +
1419                     (d3 << 4) + d4;
1420                 // Check if the second code point is a valid trailing surrogate.
1421                 // Valid trailing surrogates are [DC00, DFFF]
1422                 if(BOOST_JSON_UNLIKELY(
1423                     u2 < 0xdc00 || u2 > 0xdfff))
1424                 {
1425                     // If not valid and the parser does not allow it, return an error.
1426                     if(!allow_bad_utf16)
1427                     {
1428                         BOOST_STATIC_CONSTEXPR source_location loc
1429                             = BOOST_CURRENT_LOCATION;
1430                         return fail(cs.begin(), error::illegal_trailing_surrogate,
1431                             &loc);
1432                     }
1433                     // Append the replacement character for the
1434                     // first leading surrogate.
1435                     cs += 4;
1436                     temp.append_utf8(urc);
1437                     // Check if the second code point is a
1438                     // valid unicode scalar value (invalid leading
1439                     // or trailing surrogate)
1440                     if (u2 < 0xd800 || u2 > 0xdbff)
1441                     {
1442                         temp.append_utf8(u2);
1443                         break;
1444                     }
1445                     // If it is a valid leading surrogate
1446                     else
1447                     {
1448                         u1_ = u2;
1449                         goto do_sur1;
1450                     }
1451                 }
1452                 cs += 4;
1453                 // Calculate the Unicode code point from the surrogate pair and
1454                 // append the UTF-8 representation.
1455                 unsigned cp =
1456                     ((u1 - 0xd800) << 10) +
1457                     ((u2 - 0xdc00)) +
1458                         0x10000;
1459                 // utf-16 surrogate pair
1460                 temp.append_utf8(cp);
1461                 break;
1462             }
1463             // flush
1464             if(BOOST_JSON_LIKELY(! temp.empty()))
1465             {
1466                 BOOST_ASSERT(total <= max_size);
1467                 if(BOOST_JSON_UNLIKELY(
1468                     temp.size() > max_size - total))
1469                 {
1470                     BOOST_STATIC_CONSTEXPR source_location loc
1471                         = BOOST_CURRENT_LOCATION;
1472                     return fail(cs.begin(), ev_too_large, &loc);
1473                 }
1474                 total += temp.size();
1475                 {
1476                     bool r = is_key
1477                         ? h_.on_key_part(temp.get(), total, ec_)
1478                         : h_.on_string_part(temp.get(), total, ec_);
1479 
1480                     if(BOOST_JSON_UNLIKELY(!r))
1481                     {
1482                         return fail(cs.begin());
1483                     }
1484                 }
1485                 temp.clear();
1486                 cs.clip(temp.max_size());
1487             }
1488             ++cs;
1489             // utf-16 escape
1490     do_str4:
1491             if(BOOST_JSON_UNLIKELY(! cs))
1492                 return maybe_suspend(cs.begin(), state::str4);
1493             digit = detail::hex_digit(*cs);
1494             if(BOOST_JSON_UNLIKELY(digit == -1))
1495             {
1496                 BOOST_STATIC_CONSTEXPR source_location loc
1497                     = BOOST_CURRENT_LOCATION;
1498                 return fail(cs.begin(), error::expected_hex_digit, &loc);
1499             }
1500             ++cs;
1501             u1_ = digit << 12;
1502     do_str5:
1503             if(BOOST_JSON_UNLIKELY(! cs))
1504                 return maybe_suspend(cs.begin(), state::str5);
1505             digit = detail::hex_digit(*cs);
1506             if(BOOST_JSON_UNLIKELY(digit == -1))
1507             {
1508                 BOOST_STATIC_CONSTEXPR source_location loc
1509                     = BOOST_CURRENT_LOCATION;
1510                 return fail(cs.begin(), error::expected_hex_digit, &loc);
1511             }
1512             ++cs;
1513             u1_ += digit << 8;
1514     do_str6:
1515             if(BOOST_JSON_UNLIKELY(! cs))
1516                 return maybe_suspend(cs.begin(), state::str6);
1517             digit = detail::hex_digit(*cs);
1518             if(BOOST_JSON_UNLIKELY(digit == -1))
1519             {
1520                 BOOST_STATIC_CONSTEXPR source_location loc
1521                     = BOOST_CURRENT_LOCATION;
1522                 return fail(cs.begin(), error::expected_hex_digit, &loc);
1523             }
1524             ++cs;
1525             u1_ += digit << 4;
1526     do_str7:
1527             if(BOOST_JSON_UNLIKELY(! cs))
1528                 return maybe_suspend(cs.begin(), state::str7);
1529             digit = detail::hex_digit(*cs);
1530             if(BOOST_JSON_UNLIKELY(digit == -1))
1531             {
1532                 BOOST_STATIC_CONSTEXPR source_location loc
1533                     = BOOST_CURRENT_LOCATION;
1534                 return fail(cs.begin(), error::expected_hex_digit, &loc);
1535             }
1536             ++cs;
1537             u1_ += digit;
1538             if(BOOST_JSON_LIKELY(
1539                 u1_ < 0xd800 || u1_ > 0xdfff))
1540             {
1541                 BOOST_ASSERT(temp.empty());
1542                 // utf-8 codepoint
1543                 temp.append_utf8(u1_);
1544                 break;
1545             }
1546             if(BOOST_JSON_UNLIKELY(u1_ > 0xdbff))
1547             {
1548                 // If it's an illegal leading surrogate and
1549                 // the parser does not allow it, return an error.
1550                 if(!allow_bad_utf16)
1551                 {
1552                     BOOST_STATIC_CONSTEXPR source_location loc
1553                         = BOOST_CURRENT_LOCATION;
1554                     return fail(cs.begin(), error::illegal_leading_surrogate, &loc);
1555                 }
1556                 // Otherwise, append the Unicode replacement character
1557                 else
1558                 {
1559                     BOOST_ASSERT(temp.empty());
1560                     temp.append_utf8(urc);
1561                     break;
1562                 }
1563             }
1564     do_sur1:
1565             if(BOOST_JSON_UNLIKELY(! cs))
1566                 return maybe_suspend(cs.begin(), state::sur1);
1567             if(BOOST_JSON_UNLIKELY(*cs != '\\'))
1568             {
1569                 // If the next character is not a backslash and
1570                 // the parser does not allow it, return a syntax error.
1571                 if(!allow_bad_utf16)
1572                 {
1573                     BOOST_STATIC_CONSTEXPR source_location loc
1574                         = BOOST_CURRENT_LOCATION;
1575                     return fail(cs.begin(), error::syntax, &loc);
1576                 }
1577                 // Otherwise, append the Unicode replacement character since
1578                 // the first code point is a valid leading surrogate
1579                 else
1580                 {
1581                     temp.append_utf8(urc);
1582                     break;
1583                 }
1584             }
1585             ++cs;
1586     do_sur2:
1587             if(BOOST_JSON_UNLIKELY(! cs))
1588                 return maybe_suspend(cs.begin(), state::sur2);
1589             if(BOOST_JSON_UNLIKELY(*cs != 'u'))
1590             {
1591                 if (!allow_bad_utf16)
1592                 {
1593                     BOOST_STATIC_CONSTEXPR source_location loc
1594                         = BOOST_CURRENT_LOCATION;
1595                     return fail(cs.begin(), error::syntax, &loc);
1596                 }
1597                 // Otherwise, append the Unicode replacement character since
1598                 // the first code point is a valid leading surrogate
1599                 else
1600                 {
1601                     temp.append_utf8(urc);
1602                     goto do_str3;
1603                 }
1604             }
1605             ++cs;
1606     do_sur3:
1607             if(BOOST_JSON_UNLIKELY(! cs))
1608                 return maybe_suspend(cs.begin(), state::sur3);
1609             digit = detail::hex_digit(*cs);
1610             if(BOOST_JSON_UNLIKELY(digit == -1))
1611             {
1612                 BOOST_STATIC_CONSTEXPR source_location loc
1613                     = BOOST_CURRENT_LOCATION;
1614                 return fail(cs.begin(), error::expected_hex_digit, &loc);
1615             }
1616             ++cs;
1617             u2_ = digit << 12;
1618     do_sur4:
1619             if(BOOST_JSON_UNLIKELY(! cs))
1620                 return maybe_suspend(cs.begin(), state::sur4);
1621             digit = detail::hex_digit(*cs);
1622             if(BOOST_JSON_UNLIKELY(digit == -1))
1623             {
1624                 BOOST_STATIC_CONSTEXPR source_location loc
1625                     = BOOST_CURRENT_LOCATION;
1626                 return fail(cs.begin(), error::expected_hex_digit, &loc);
1627             }
1628             ++cs;
1629             u2_ += digit << 8;
1630     do_sur5:
1631             if(BOOST_JSON_UNLIKELY(! cs))
1632                 return maybe_suspend(cs.begin(), state::sur5);
1633             digit = detail::hex_digit(*cs);
1634             if(BOOST_JSON_UNLIKELY(digit == -1))
1635             {
1636                 BOOST_STATIC_CONSTEXPR source_location loc
1637                     = BOOST_CURRENT_LOCATION;
1638                 return fail(cs.begin(), error::expected_hex_digit, &loc);
1639             }
1640             ++cs;
1641             u2_ += digit << 4;
1642     do_sur6:
1643             if(BOOST_JSON_UNLIKELY(! cs))
1644                 return maybe_suspend(cs.begin(), state::sur6);
1645             digit = detail::hex_digit(*cs);
1646             if(BOOST_JSON_UNLIKELY(digit == -1))
1647             {
1648                 BOOST_STATIC_CONSTEXPR source_location loc
1649                     = BOOST_CURRENT_LOCATION;
1650                 return fail(cs.begin(), error::expected_hex_digit, &loc);
1651             }
1652             ++cs;
1653             u2_ += digit;
1654             // Check if the second code point is a valid trailing surrogate.
1655             // Valid trailing surrogates are [DC00, DFFF]
1656             if(BOOST_JSON_UNLIKELY(
1657                 u2_ < 0xdc00 || u2_ > 0xdfff))
1658             {
1659                 // If not valid and the parser does not allow it, return an error.
1660                 if(!allow_bad_utf16)
1661                 {
1662                     BOOST_STATIC_CONSTEXPR source_location loc
1663                         = BOOST_CURRENT_LOCATION;
1664                     return fail(cs.begin(), error::illegal_trailing_surrogate, &loc);
1665                 }
1666                 // Append the replacement character for the
1667                 // first leading surrogate.
1668                 temp.append_utf8(urc);
1669                 // Check if the second code point is a
1670                 // valid unicode scalar value (invalid leading
1671                 // or trailing surrogate)
1672                 if (u2_ < 0xd800 || u2_ > 0xdbff)
1673                 {
1674                     temp.append_utf8(u2_);
1675                     break;
1676                 }
1677                 // If it is a valid leading surrogate
1678                 else
1679                 {
1680                     u1_ = u2_;
1681                     goto do_sur1;
1682                 }
1683             }
1684             // Calculate the Unicode code point from the surrogate pair and
1685             // append the UTF-8 representation.
1686             unsigned cp =
1687                 ((u1_ - 0xd800) << 10) +
1688                 ((u2_ - 0xdc00)) +
1689                     0x10000;
1690             // utf-16 surrogate pair
1691             temp.append_utf8(cp);
1692         }
1693 
1694         // flush
1695         if(BOOST_JSON_UNLIKELY( !cs ) || *cs != '\\')
1696             break;
1697     }
1698 
1699     if(BOOST_JSON_LIKELY( temp.size() ))
1700     {
1701         BOOST_ASSERT(total <= max_size);
1702         if(BOOST_JSON_UNLIKELY( temp.size() > max_size - total ))
1703         {
1704             BOOST_STATIC_CONSTEXPR source_location loc
1705                 = BOOST_CURRENT_LOCATION;
1706             return fail(cs.begin(), ev_too_large, &loc);
1707         }
1708 
1709         total += temp.size();
1710         bool const r = is_key
1711             ? h_.on_key_part(temp.get(), total, ec_)
1712             : h_.on_string_part(temp.get(), total, ec_);
1713         if(BOOST_JSON_UNLIKELY( !r ))
1714             return fail( cs.begin() );
1715     }
1716 
1717     return cs.begin();
1718 }
1719 
1720 //----------------------------------------------------------
1721 
1722 template<class Handler>
1723 template<
1724     bool StackEmpty_,
1725     bool AllowComments_/*,
1726     bool AllowTrailing_,
1727     bool AllowBadUTF8_*/>
1728 const char*
1729 basic_parser<Handler>::
1730 parse_object(const char* p,
1731     std::integral_constant<bool, StackEmpty_> stack_empty,
1732     std::integral_constant<bool, AllowComments_> allow_comments,
1733     /*std::integral_constant<bool, AllowTrailing_>*/ bool allow_trailing,
1734     /*std::integral_constant<bool, AllowBadUTF8_>*/ bool allow_bad_utf8,
1735     bool allow_bad_utf16)
1736 {
1737     detail::const_stream_wrapper cs(p, end_);
1738     std::size_t size;
1739     if(! stack_empty && ! st_.empty())
1740     {
1741         // resume
1742         state st;
1743         st_.pop(st);
1744         st_.pop(size);
1745         switch(st)
1746         {
1747         default: BOOST_JSON_UNREACHABLE();
1748         case state::obj1: goto do_obj1;
1749         case state::obj2: goto do_obj2;
1750         case state::obj3: goto do_obj3;
1751         case state::obj4: goto do_obj4;
1752         case state::obj5: goto do_obj5;
1753         case state::obj6: goto do_obj6;
1754         case state::obj7: goto do_obj7;
1755         case state::obj8: goto do_obj8;
1756         case state::obj9: goto do_obj9;
1757         case state::obj10: goto do_obj10;
1758         case state::obj11: goto do_obj11;
1759         }
1760     }
1761     BOOST_ASSERT(*cs == '{');
1762     size = 0;
1763     if(BOOST_JSON_UNLIKELY(! depth_))
1764     {
1765         BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
1766         return fail(cs.begin(), error::too_deep, &loc);
1767     }
1768     --depth_;
1769     if(BOOST_JSON_UNLIKELY(
1770         ! h_.on_object_begin(ec_)))
1771         return fail(cs.begin());
1772     ++cs;
1773     // object:
1774     //     '{' *ws '}'
1775     //     '{' *ws string *ws ':' *ws value *ws *[ ',' *ws string *ws ':' *ws value *ws ] '}'
1776 do_obj1:
1777     cs = detail::count_whitespace(cs.begin(), cs.end());
1778     if(BOOST_JSON_UNLIKELY(! cs))
1779         return maybe_suspend(cs.begin(), state::obj1, size);
1780     if(BOOST_JSON_LIKELY(*cs != '}'))
1781     {
1782         if(BOOST_JSON_UNLIKELY(*cs != '\x22'))
1783         {
1784             if(allow_comments && *cs == '/')
1785             {
1786 do_obj2:
1787                 cs = parse_comment(cs.begin(), stack_empty, std::false_type());
1788                 if(BOOST_JSON_UNLIKELY(incomplete(cs)))
1789                     return suspend_or_fail(state::obj2, size);
1790                 goto do_obj1;
1791             }
1792             BOOST_STATIC_CONSTEXPR source_location loc
1793                 = BOOST_CURRENT_LOCATION;
1794             return fail(cs.begin(), error::syntax, &loc);
1795         }
1796 loop:
1797         if(BOOST_JSON_UNLIKELY(++size >
1798             Handler::max_object_size))
1799         {
1800             BOOST_STATIC_CONSTEXPR source_location loc
1801                 = BOOST_CURRENT_LOCATION;
1802             return fail(cs.begin(), error::object_too_large, &loc);
1803         }
1804 do_obj3:
1805         cs = parse_string(cs.begin(), stack_empty, std::true_type(), allow_bad_utf8, allow_bad_utf16);
1806         if(BOOST_JSON_UNLIKELY(incomplete(cs)))
1807             return suspend_or_fail(state::obj3, size);
1808 do_obj4:
1809         cs = detail::count_whitespace(cs.begin(), cs.end());
1810         if(BOOST_JSON_UNLIKELY(! cs))
1811             return maybe_suspend(cs.begin(), state::obj4, size);
1812         if(BOOST_JSON_UNLIKELY(*cs != ':'))
1813         {
1814             if(allow_comments && *cs == '/')
1815             {
1816 do_obj5:
1817                 cs = parse_comment(cs.begin(), stack_empty, std::false_type());
1818                 if(BOOST_JSON_UNLIKELY(incomplete(cs)))
1819                     return suspend_or_fail(state::obj5, size);
1820                 goto do_obj4;
1821             }
1822             BOOST_STATIC_CONSTEXPR source_location loc
1823                 = BOOST_CURRENT_LOCATION;
1824             return fail(cs.begin(), error::syntax, &loc);
1825         }
1826         ++cs;
1827 do_obj6:
1828         cs = detail::count_whitespace(cs.begin(), cs.end());
1829         if(BOOST_JSON_UNLIKELY(! cs))
1830             return maybe_suspend(cs.begin(), state::obj6, size);
1831 do_obj7:
1832         cs = parse_value(cs.begin(), stack_empty, allow_comments, allow_trailing, allow_bad_utf8, allow_bad_utf16);
1833         if(BOOST_JSON_UNLIKELY(incomplete(cs)))
1834             return suspend_or_fail(state::obj7, size);
1835 do_obj8:
1836         cs = detail::count_whitespace(cs.begin(), cs.end());
1837         if(BOOST_JSON_UNLIKELY(! cs))
1838             return maybe_suspend(cs.begin(), state::obj8, size);
1839         if(BOOST_JSON_LIKELY(*cs == ','))
1840         {
1841             ++cs;
1842 do_obj9:
1843             cs = detail::count_whitespace(cs.begin(), cs.end());
1844             if(BOOST_JSON_UNLIKELY(! cs))
1845                 return maybe_suspend(cs.begin(), state::obj9, size);
1846 
1847             // loop for next element
1848             if(BOOST_JSON_LIKELY(*cs == '\x22'))
1849                 goto loop;
1850             if(! allow_trailing || *cs != '}')
1851             {
1852                 if(allow_comments && *cs == '/')
1853                 {
1854 do_obj10:
1855                     cs = parse_comment(cs.begin(), stack_empty, std::false_type());
1856                     if(BOOST_JSON_UNLIKELY(incomplete(cs)))
1857                         return suspend_or_fail(state::obj10, size);
1858                     goto do_obj9;
1859                 }
1860                 BOOST_STATIC_CONSTEXPR source_location loc
1861                     = BOOST_CURRENT_LOCATION;
1862                 return fail(cs.begin(), error::syntax, &loc);
1863             }
1864         }
1865         else if(BOOST_JSON_UNLIKELY(*cs != '}'))
1866         {
1867             if(allow_comments && *cs == '/')
1868             {
1869 do_obj11:
1870                 cs = parse_comment(cs.begin(), stack_empty, std::false_type());
1871                 if(BOOST_JSON_UNLIKELY(incomplete(cs)))
1872                     return suspend_or_fail(state::obj11, size);
1873                 goto do_obj8;
1874             }
1875             BOOST_STATIC_CONSTEXPR source_location loc
1876                 = BOOST_CURRENT_LOCATION;
1877             return fail(cs.begin(), error::syntax, &loc);
1878         }
1879         // got closing brace, fall through
1880     }
1881     if(BOOST_JSON_UNLIKELY(
1882         ! h_.on_object_end(size, ec_)))
1883         return fail(cs.begin());
1884     ++depth_;
1885     ++cs;
1886     return cs.begin();
1887 }
1888 
1889 //----------------------------------------------------------
1890 
1891 template<class Handler>
1892 template<
1893     bool StackEmpty_,
1894     bool AllowComments_/*,
1895     bool AllowTrailing_,
1896     bool AllowBadUTF8_*/>
1897 const char*
1898 basic_parser<Handler>::
1899 parse_array(const char* p,
1900     std::integral_constant<bool, StackEmpty_> stack_empty,
1901     std::integral_constant<bool, AllowComments_> allow_comments,
1902     /*std::integral_constant<bool, AllowTrailing_>*/ bool allow_trailing,
1903     /*std::integral_constant<bool, AllowBadUTF8_>*/ bool allow_bad_utf8,
1904     bool allow_bad_utf16)
1905 {
1906     detail::const_stream_wrapper cs(p, end_);
1907     std::size_t size;
1908     if(! stack_empty && ! st_.empty())
1909     {
1910         // resume
1911         state st;
1912         st_.pop(st);
1913         st_.pop(size);
1914         switch(st)
1915         {
1916         default: BOOST_JSON_UNREACHABLE();
1917         case state::arr1: goto do_arr1;
1918         case state::arr2: goto do_arr2;
1919         case state::arr3: goto do_arr3;
1920         case state::arr4: goto do_arr4;
1921         case state::arr5: goto do_arr5;
1922         case state::arr6: goto do_arr6;
1923         }
1924     }
1925     BOOST_ASSERT(*cs == '[');
1926     size = 0;
1927     if(BOOST_JSON_UNLIKELY(! depth_))
1928     {
1929         BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
1930         return fail(cs.begin(), error::too_deep, &loc);
1931     }
1932     --depth_;
1933     if(BOOST_JSON_UNLIKELY(
1934         ! h_.on_array_begin(ec_)))
1935         return fail(cs.begin());
1936     ++cs;
1937     // array:
1938     //     '[' *ws ']'
1939     //     '[' *ws value *ws *[ ',' *ws value *ws ] ']'
1940 do_arr1:
1941     cs = detail::count_whitespace(cs.begin(), cs.end());
1942     if(BOOST_JSON_UNLIKELY(! cs))
1943         return maybe_suspend(cs.begin(), state::arr1, size);
1944     if(BOOST_JSON_LIKELY(*cs != ']'))
1945     {
1946 loop:
1947         if(allow_comments && *cs == '/')
1948         {
1949 do_arr2:
1950             cs = parse_comment(cs.begin(), stack_empty, std::false_type());
1951             if(BOOST_JSON_UNLIKELY(incomplete(cs)))
1952                 return suspend_or_fail(state::arr2, size);
1953             goto do_arr1;
1954         }
1955         if(BOOST_JSON_UNLIKELY(++size >
1956             Handler::max_array_size))
1957         {
1958             BOOST_STATIC_CONSTEXPR source_location loc
1959                 = BOOST_CURRENT_LOCATION;
1960             return fail(cs.begin(), error::array_too_large, &loc);
1961         }
1962 do_arr3:
1963         // array is not empty, value required
1964         cs = parse_value(cs.begin(), stack_empty, allow_comments, allow_trailing, allow_bad_utf8, allow_bad_utf16);
1965         if(BOOST_JSON_UNLIKELY(incomplete(cs)))
1966             return suspend_or_fail(state::arr3, size);
1967 do_arr4:
1968         cs = detail::count_whitespace(cs.begin(), cs.end());
1969         if(BOOST_JSON_UNLIKELY(! cs))
1970             return maybe_suspend(cs.begin(), state::arr4, size);
1971         if(BOOST_JSON_LIKELY(*cs == ','))
1972         {
1973             ++cs;
1974 do_arr5:
1975             cs = detail::count_whitespace(cs.begin(), cs.end());
1976             if(BOOST_JSON_UNLIKELY(! cs))
1977                 return maybe_suspend(cs.begin(), state::arr5, size);
1978             // loop for next element
1979             if(! allow_trailing || *cs != ']')
1980                 goto loop;
1981         }
1982         else if(BOOST_JSON_UNLIKELY(*cs != ']'))
1983         {
1984             if(allow_comments && *cs == '/')
1985             {
1986 do_arr6:
1987                 cs = parse_comment(cs.begin(), stack_empty, std::false_type());
1988                 if(BOOST_JSON_UNLIKELY(incomplete(cs)))
1989                     return suspend_or_fail(state::arr6, size);
1990                 goto do_arr4;
1991             }
1992             BOOST_STATIC_CONSTEXPR source_location loc
1993                 = BOOST_CURRENT_LOCATION;
1994             return fail(cs.begin(), error::syntax, &loc);
1995         }
1996         // got closing bracket; fall through
1997     }
1998     if(BOOST_JSON_UNLIKELY(
1999         ! h_.on_array_end(size, ec_)))
2000         return fail(cs.begin());
2001     ++depth_;
2002     ++cs;
2003     return cs.begin();
2004 }
2005 
2006 //----------------------------------------------------------
2007 
2008 template<class Handler>
2009 template<bool StackEmpty_, char First_, number_precision Numbers_>
2010 const char*
2011 basic_parser<Handler>::
2012 parse_number(const char* p,
2013     std::integral_constant<bool, StackEmpty_> stack_empty,
2014     std::integral_constant<char, First_> first,
2015     std::integral_constant<number_precision, Numbers_> mode)
2016 {
2017     constexpr bool precise_parsing = mode == number_precision::precise;
2018     constexpr bool no_parsing = mode == number_precision::none;
2019 
2020     // only one of these will be true if we are not resuming
2021     // if negative then !zero_first && !nonzero_first
2022     // if zero_first then !nonzero_first && !negative
2023     // if nonzero_first then !zero_first && !negative
2024     bool const negative = first == '-';
2025     bool const zero_first = first == '0';
2026     bool const nonzero_first = first == '+';
2027     detail::const_stream_wrapper cs(p, end_);
2028     number num;
2029     const char* begin = cs.begin();
2030     if(stack_empty || st_.empty())
2031     {
2032         num.bias = 0;
2033         num.exp = 0;
2034         num.frac = false;
2035         num_buf_.clear();
2036 
2037         //----------------------------------
2038         //
2039         // '-'
2040         // leading minus sign
2041         //
2042         BOOST_ASSERT(cs);
2043         if(negative)
2044             ++cs;
2045 
2046         num.neg = negative;
2047         num.frac = false;
2048         num.exp = 0;
2049         num.bias = 0;
2050 
2051         // fast path
2052         if( cs.remain() >= 16 + 1 + 16 ) // digits . digits
2053         {
2054             int n1;
2055 
2056             if( nonzero_first ||
2057                 (negative && *cs != '0') )
2058             {
2059                 n1 = detail::count_digits( cs.begin() );
2060                 BOOST_ASSERT(n1 >= 0 && n1 <= 16);
2061 
2062                 if( negative && n1 == 0 && opt_.allow_infinity_and_nan )
2063                 {
2064                     return parse_literal(
2065                         p - 1, mp11::mp_int<detail::neg_infinity_literal>());
2066                 }
2067 
2068                 if( ! nonzero_first && n1 == 0 )
2069                 {
2070                     // digit required
2071                     BOOST_STATIC_CONSTEXPR source_location loc
2072                         = BOOST_CURRENT_LOCATION;
2073                     return fail(cs.begin(), error::syntax, &loc);
2074                 }
2075 
2076                 BOOST_IF_CONSTEXPR( !no_parsing )
2077                     num.mant = detail::parse_unsigned( 0, cs.begin(), n1 );
2078                 else
2079                     num.mant = 0;
2080 
2081                 cs += n1;
2082 
2083                 // integer or floating-point with
2084                 // >= 16 leading digits
2085                 if( n1 == 16 )
2086                 {
2087                     goto do_num2;
2088                 }
2089             }
2090             else
2091             {
2092                 // 0. floating-point or 0e integer
2093                 num.mant = 0;
2094                 n1 = 0;
2095                 ++cs;
2096             }
2097 
2098             {
2099                 const char c = *cs;
2100                 if(c != '.')
2101                 {
2102                     if((c | 32) == 'e')
2103                     {
2104                         ++cs;
2105                         goto do_exp1;
2106                     }
2107                     BOOST_IF_CONSTEXPR( negative && !no_parsing )
2108                         num.mant = ~num.mant + 1;
2109                     goto finish_signed;
2110                 }
2111             }
2112 
2113             // floating-point number
2114 
2115             ++cs;
2116 
2117             int n2 = detail::count_digits( cs.begin() );
2118             BOOST_ASSERT(n2 >= 0 && n2 <= 16);
2119 
2120             if( n2 == 0 )
2121             {
2122                 // digit required
2123                 BOOST_STATIC_CONSTEXPR source_location loc
2124                     = BOOST_CURRENT_LOCATION;
2125                 return fail(cs.begin(), error::syntax, &loc);
2126             }
2127 
2128             // floating-point mantissa overflow
2129             if( n1 + n2 >= 19 )
2130             {
2131                 goto do_num7;
2132             }
2133 
2134             BOOST_IF_CONSTEXPR( !no_parsing )
2135                 num.mant = detail::parse_unsigned( num.mant, cs.begin(), n2 );
2136 
2137             BOOST_ASSERT(num.bias == 0);
2138 
2139             num.bias -= n2;
2140 
2141             cs += n2;
2142 
2143             char ch = *cs;
2144 
2145             if( (ch | 32) == 'e' )
2146             {
2147                 ++cs;
2148                 goto do_exp1;
2149             }
2150             else if( ch >= '0' && ch <= '9' )
2151             {
2152                 goto do_num8;
2153             }
2154 
2155             goto finish_dub;
2156         }
2157     }
2158     else
2159     {
2160         num = num_;
2161         state st;
2162         st_.pop(st);
2163         switch(st)
2164         {
2165         default: BOOST_JSON_UNREACHABLE();
2166         case state::num1: goto do_num1;
2167         case state::num2: goto do_num2;
2168         case state::num3: goto do_num3;
2169         case state::num4: goto do_num4;
2170         case state::num5: goto do_num5;
2171         case state::num6: goto do_num6;
2172         case state::num7: goto do_num7;
2173         case state::num8: goto do_num8;
2174         case state::exp1: goto do_exp1;
2175         case state::exp2: goto do_exp2;
2176         case state::exp3: goto do_exp3;
2177         }
2178     }
2179 
2180     //----------------------------------
2181     //
2182     // DIGIT
2183     // first digit
2184     //
2185 do_num1:
2186     if(zero_first || nonzero_first ||
2187         BOOST_JSON_LIKELY(cs))
2188     {
2189         char const c = *cs;
2190         if(zero_first)
2191         {
2192             ++cs;
2193             num.mant = 0;
2194             goto do_num6;
2195         }
2196         else if(nonzero_first || BOOST_JSON_LIKELY(
2197             c >= '1' && c <= '9'))
2198         {
2199             ++cs;
2200             num.mant = c - '0';
2201         }
2202         else if(BOOST_JSON_UNLIKELY(
2203             c == '0'))
2204         {
2205             ++cs;
2206             num.mant = 0;
2207             goto do_num6;
2208         }
2209         else if( (negative || num.neg) && opt_.allow_infinity_and_nan )
2210         {
2211             st_.push(state::lit1);
2212             cur_lit_ = detail::neg_infinity_literal;
2213             lit_offset_ = 1;
2214             return parse_literal(
2215                 cs.begin(), mp11::mp_int<detail::resume_literal>() );
2216         }
2217         else
2218         {
2219             BOOST_STATIC_CONSTEXPR source_location loc
2220                 = BOOST_CURRENT_LOCATION;
2221             return fail(cs.begin(), error::syntax, &loc);
2222         }
2223     }
2224     else
2225     {
2226         if(BOOST_JSON_UNLIKELY(
2227             ! h_.on_number_part(
2228                 {begin, cs.used(begin)}, ec_)))
2229             return fail(cs.begin());
2230 
2231         BOOST_IF_CONSTEXPR( precise_parsing )
2232             num_buf_.append( begin, cs.used(begin) );
2233         return maybe_suspend(
2234             cs.begin(), state::num1, num);
2235     }
2236 
2237     //----------------------------------
2238     //
2239     // 1*DIGIT
2240     // significant digits left of decimal
2241     //
2242 do_num2:
2243     if(negative || (!stack_empty && num.neg))
2244     {
2245         for(;;)
2246         {
2247             if(BOOST_JSON_UNLIKELY(! cs))
2248             {
2249                 if(BOOST_JSON_UNLIKELY(more_))
2250                 {
2251                     if(BOOST_JSON_UNLIKELY(
2252                         ! h_.on_number_part(
2253                             {begin, cs.used(begin)}, ec_)))
2254                         return fail(cs.begin());
2255 
2256                     BOOST_IF_CONSTEXPR( precise_parsing )
2257                         num_buf_.append( begin, cs.used(begin) );
2258                     return suspend(cs.begin(), state::num2, num);
2259                 }
2260                 goto finish_int;
2261             }
2262             char const c = *cs;
2263             if(BOOST_JSON_LIKELY(
2264                 c >= '0' && c <= '9'))
2265             {
2266                 ++cs;
2267                 //              9223372036854775808 INT64_MIN
2268                 if( num.mant  > 922337203685477580 || (
2269                     num.mant == 922337203685477580 && c > '8'))
2270                     break;
2271                 BOOST_IF_CONSTEXPR( !no_parsing )
2272                     num.mant = 10 * num.mant + ( c - '0' );
2273                 continue;
2274             }
2275             goto do_num6; // [.eE]
2276         }
2277     }
2278     else
2279     {
2280         for(;;)
2281         {
2282             if(BOOST_JSON_UNLIKELY(! cs))
2283             {
2284                 if(BOOST_JSON_UNLIKELY(more_))
2285                 {
2286                     if(BOOST_JSON_UNLIKELY(
2287                         ! h_.on_number_part(
2288                             {begin, cs.used(begin)}, ec_)))
2289                         return fail(cs.begin());
2290 
2291                     BOOST_IF_CONSTEXPR( precise_parsing )
2292                         num_buf_.append( begin, cs.used(begin) );
2293                     return suspend(cs.begin(), state::num2, num);
2294                 }
2295                 goto finish_int;
2296             }
2297             char const c = *cs;
2298             if(BOOST_JSON_LIKELY(
2299                 c >= '0' && c <= '9'))
2300             {
2301                 ++cs;
2302                 //              18446744073709551615 UINT64_MAX
2303                 if( num.mant  > 1844674407370955161 || (
2304                     num.mant == 1844674407370955161 && c > '5'))
2305                     break;
2306                 BOOST_IF_CONSTEXPR( !no_parsing )
2307                     num.mant = 10 * num.mant + ( c - '0' );
2308             }
2309             else
2310             {
2311                 goto do_num6; // [.eE]
2312             }
2313         }
2314     }
2315     ++num.bias;
2316 
2317     //----------------------------------
2318     //
2319     // 1*DIGIT
2320     // non-significant digits left of decimal
2321     //
2322 do_num3:
2323     for(;;)
2324     {
2325         if(BOOST_JSON_UNLIKELY(! cs))
2326         {
2327             if(BOOST_JSON_UNLIKELY(more_))
2328             {
2329                 if(BOOST_JSON_UNLIKELY(
2330                     ! h_.on_number_part(
2331                         {begin, cs.used(begin)}, ec_)))
2332                     return fail(cs.begin());
2333 
2334                 BOOST_IF_CONSTEXPR( precise_parsing )
2335                     num_buf_.append( begin, cs.used(begin) );
2336                 return suspend(cs.begin(), state::num3, num);
2337             }
2338             goto finish_dub;
2339         }
2340         char const c = *cs;
2341         if(BOOST_JSON_UNLIKELY(
2342             c >= '0' && c <= '9'))
2343         {
2344             if(BOOST_JSON_UNLIKELY( num.bias + 1 == INT_MAX ))
2345             {
2346                 BOOST_STATIC_CONSTEXPR source_location loc
2347                     = BOOST_CURRENT_LOCATION;
2348                 return fail(cs.begin(), error::exponent_overflow, &loc);
2349             }
2350             ++cs;
2351             ++num.bias;
2352         }
2353         else if(BOOST_JSON_LIKELY(
2354             c == '.'))
2355         {
2356             ++cs;
2357             break;
2358         }
2359         else if((c | 32) == 'e')
2360         {
2361             ++cs;
2362             goto do_exp1;
2363         }
2364         else
2365         {
2366             goto finish_dub;
2367         }
2368     }
2369 
2370     //----------------------------------
2371     //
2372     // DIGIT
2373     // first non-significant digit
2374     // to the right of decimal
2375     //
2376 do_num4:
2377     {
2378         if(BOOST_JSON_UNLIKELY(! cs))
2379         {
2380             if(BOOST_JSON_UNLIKELY(
2381                 ! h_.on_number_part(
2382                     {begin, cs.used(begin)}, ec_)))
2383                 return fail(cs.begin());
2384 
2385             BOOST_IF_CONSTEXPR( precise_parsing )
2386                 num_buf_.append( begin, cs.used(begin) );
2387             return maybe_suspend(
2388                 cs.begin(), state::num4, num);
2389         }
2390         char const c = *cs;
2391         if(BOOST_JSON_LIKELY(
2392             //static_cast<unsigned char>(c - '0') < 10))
2393             c >= '0' && c <= '9'))
2394         {
2395             ++cs;
2396         }
2397         else
2398         {
2399             // digit required
2400             BOOST_STATIC_CONSTEXPR source_location loc
2401                 = BOOST_CURRENT_LOCATION;
2402             return fail(cs.begin(), error::syntax, &loc);
2403         }
2404     }
2405 
2406     //----------------------------------
2407     //
2408     // 1*DIGIT
2409     // non-significant digits
2410     // to the right of decimal
2411     //
2412 do_num5:
2413     for(;;)
2414     {
2415         if(BOOST_JSON_UNLIKELY(! cs))
2416         {
2417             if(BOOST_JSON_UNLIKELY(more_))
2418             {
2419                 if(BOOST_JSON_UNLIKELY(
2420                     ! h_.on_number_part(
2421                         {begin, cs.used(begin)}, ec_)))
2422                     return fail(cs.begin());
2423 
2424                 BOOST_IF_CONSTEXPR( precise_parsing )
2425                     num_buf_.append( begin, cs.used(begin) );
2426                 return suspend(cs.begin(), state::num5, num);
2427             }
2428             goto finish_dub;
2429         }
2430         char const c = *cs;
2431         if(BOOST_JSON_LIKELY(
2432             c >= '0' && c <= '9'))
2433         {
2434             ++cs;
2435         }
2436         else if((c | 32) == 'e')
2437         {
2438             ++cs;
2439             goto do_exp1;
2440         }
2441         else
2442         {
2443             goto finish_dub;
2444         }
2445     }
2446 
2447     //----------------------------------
2448     //
2449     // [.eE]
2450     //
2451 do_num6:
2452     {
2453         if(BOOST_JSON_UNLIKELY(! cs))
2454         {
2455             if(BOOST_JSON_UNLIKELY(more_))
2456             {
2457                 if(BOOST_JSON_UNLIKELY(
2458                     ! h_.on_number_part(
2459                         {begin, cs.used(begin)}, ec_)))
2460                     return fail(cs.begin());
2461 
2462                 BOOST_IF_CONSTEXPR( precise_parsing )
2463                     num_buf_.append( begin, cs.used(begin) );
2464                 return suspend(cs.begin(), state::num6, num);
2465             }
2466             goto finish_int;
2467         }
2468         char const c = *cs;
2469         if(BOOST_JSON_LIKELY(
2470             c == '.'))
2471         {
2472             ++cs;
2473         }
2474         else if((c | 32) == 'e')
2475         {
2476             ++cs;
2477             goto do_exp1;
2478         }
2479         else
2480         {
2481             goto finish_int;
2482         }
2483     }
2484 
2485     //----------------------------------
2486     //
2487     // DIGIT
2488     // first significant digit
2489     // to the right of decimal
2490     //
2491 do_num7:
2492     {
2493         if(BOOST_JSON_UNLIKELY(! cs))
2494         {
2495             if(BOOST_JSON_UNLIKELY(more_))
2496             {
2497                 if(BOOST_JSON_UNLIKELY(
2498                     ! h_.on_number_part(
2499                         {begin, cs.used(begin)}, ec_)))
2500                     return fail(cs.begin());
2501 
2502                 BOOST_IF_CONSTEXPR( precise_parsing )
2503                     num_buf_.append( begin, cs.used(begin) );
2504                 return suspend(cs.begin(), state::num7, num);
2505             }
2506             // digit required
2507             BOOST_STATIC_CONSTEXPR source_location loc
2508                 = BOOST_CURRENT_LOCATION;
2509             return fail(cs.begin(), error::syntax, &loc);
2510         }
2511         char const c = *cs;
2512         if(BOOST_JSON_UNLIKELY(
2513             c < '0' || c > '9'))
2514         {
2515             // digit required
2516             BOOST_STATIC_CONSTEXPR source_location loc
2517                 = BOOST_CURRENT_LOCATION;
2518             return fail(cs.begin(), error::syntax, &loc);
2519         }
2520     }
2521 
2522     //----------------------------------
2523     //
2524     // 1*DIGIT
2525     // significant digits
2526     // to the right of decimal
2527     //
2528 do_num8:
2529     for(;;)
2530     {
2531         if(BOOST_JSON_UNLIKELY(! cs))
2532         {
2533             if(BOOST_JSON_UNLIKELY(more_))
2534             {
2535                 if(BOOST_JSON_UNLIKELY(
2536                     ! h_.on_number_part(
2537                         {begin, cs.used(begin)}, ec_)))
2538                     return fail(cs.begin());
2539 
2540                 BOOST_IF_CONSTEXPR( precise_parsing )
2541                     num_buf_.append( begin, cs.used(begin) );
2542                 return suspend(cs.begin(), state::num8, num);
2543             }
2544             goto finish_dub;
2545         }
2546         char const c = *cs;
2547         if(BOOST_JSON_LIKELY(
2548             c >= '0' && c <= '9'))
2549         {
2550             ++cs;
2551             if(!no_parsing && BOOST_JSON_LIKELY(
2552                 num.mant <= 9007199254740991)) // 2^53-1
2553             {
2554                 if(BOOST_JSON_UNLIKELY( num.bias - 1 == INT_MIN ))
2555                 {
2556                     BOOST_STATIC_CONSTEXPR source_location loc
2557                         = BOOST_CURRENT_LOCATION;
2558                     return fail(cs.begin(), error::exponent_overflow, &loc);
2559                 }
2560                 --num.bias;
2561                 num.mant = 10 * num.mant + ( c - '0' );
2562             }
2563             else
2564             {
2565                 goto do_num5;
2566             }
2567         }
2568         else if((c | 32) == 'e')
2569         {
2570             ++cs;
2571             goto do_exp1;
2572         }
2573         else
2574         {
2575             goto finish_dub;
2576         }
2577     }
2578 
2579     //----------------------------------
2580     //
2581     // *[+-]
2582     //
2583 do_exp1:
2584     if(BOOST_JSON_UNLIKELY(! cs))
2585     {
2586         if(BOOST_JSON_UNLIKELY(
2587             ! h_.on_number_part(
2588                 {begin, cs.used(begin)}, ec_)))
2589             return fail(cs.begin());
2590 
2591         BOOST_IF_CONSTEXPR( precise_parsing )
2592             num_buf_.append( begin, cs.used(begin) );
2593         return maybe_suspend(
2594             cs.begin(), state::exp1, num);
2595     }
2596     if(*cs == '+')
2597     {
2598         ++cs;
2599     }
2600     else if(*cs == '-')
2601     {
2602         ++cs;
2603         num.frac = true;
2604     }
2605 
2606     //----------------------------------
2607     //
2608     // DIGIT
2609     // first digit of the exponent
2610     //
2611 do_exp2:
2612     {
2613         if(BOOST_JSON_UNLIKELY(! cs))
2614         {
2615             if(BOOST_JSON_UNLIKELY(more_))
2616             {
2617                 if(BOOST_JSON_UNLIKELY(
2618                     ! h_.on_number_part(
2619                         {begin, cs.used(begin)}, ec_)))
2620                     return fail(cs.begin());
2621 
2622                 BOOST_IF_CONSTEXPR( precise_parsing )
2623                     num_buf_.append( begin, cs.used(begin) );
2624                 return suspend(cs.begin(), state::exp2, num);
2625             }
2626             // digit required
2627             BOOST_STATIC_CONSTEXPR source_location loc
2628                 = BOOST_CURRENT_LOCATION;
2629             return fail(cs.begin(), error::syntax, &loc);
2630         }
2631         char const c = *cs;
2632         if(BOOST_JSON_UNLIKELY(
2633             c < '0' || c > '9'))
2634         {
2635             // digit required
2636             BOOST_STATIC_CONSTEXPR source_location loc
2637                 = BOOST_CURRENT_LOCATION;
2638             return fail(cs.begin(), error::syntax, &loc);
2639         }
2640         ++cs;
2641         num.exp = c - '0';
2642     }
2643 
2644     //----------------------------------
2645     //
2646     // 1*DIGIT
2647     // subsequent digits in the exponent
2648     //
2649 do_exp3:
2650     for(;;)
2651     {
2652         if(BOOST_JSON_UNLIKELY(! cs))
2653         {
2654             if(BOOST_JSON_UNLIKELY(more_))
2655             {
2656                 if(BOOST_JSON_UNLIKELY(
2657                     ! h_.on_number_part(
2658                         {begin, cs.used(begin)}, ec_)))
2659                     return fail(cs.begin());
2660 
2661                 BOOST_IF_CONSTEXPR( precise_parsing )
2662                     num_buf_.append( begin, cs.used(begin) );
2663                 return suspend(cs.begin(), state::exp3, num);
2664             }
2665         }
2666         else
2667         {
2668             char const c = *cs;
2669             if(BOOST_JSON_LIKELY( c >= '0' && c <= '9' ))
2670             {
2671                 if(BOOST_JSON_UNLIKELY(
2672                 //              2147483647 INT_MAX
2673                     num.exp  >  214748364 ||
2674                     (num.exp == 214748364 && c > '7')
2675                 ))
2676                     num.exp = INT_MAX;
2677                 else BOOST_IF_CONSTEXPR( !no_parsing )
2678                     num.exp = 10 * num.exp + ( c - '0' );
2679 
2680                 ++cs;
2681                 continue;
2682             }
2683         }
2684         BOOST_ASSERT(num.exp >= 0);
2685         if ( num.frac )
2686         {
2687             if(BOOST_JSON_UNLIKELY( num.bias < (INT_MIN + num.exp) ))
2688             {
2689                 // if exponent overflowed, bias is a very large negative
2690                 // number, and mantissa isn't zero, then we cannot parse the
2691                 // number correctly
2692                 if(BOOST_JSON_UNLIKELY(
2693                     (num.exp == INT_MAX) &&
2694                     (num.bias < 0) &&
2695                     (num.exp + num.bias < 308) &&
2696                     num.mant ))
2697                 {
2698                     BOOST_STATIC_CONSTEXPR source_location loc
2699                         = BOOST_CURRENT_LOCATION;
2700                     return fail(cs.begin(), error::exponent_overflow, &loc);
2701                 }
2702 
2703                 num.bias = 0;
2704                 num.exp = INT_MAX;
2705             }
2706         }
2707         else if (BOOST_JSON_UNLIKELY( num.bias > (INT_MAX - num.exp) ))
2708         {
2709             // if exponent overflowed, bias is a very large positive number,
2710             // and mantissa isn't zero, then we cannot parse the
2711             // number correctly
2712             if(BOOST_JSON_UNLIKELY(
2713                 (num.exp == INT_MAX) &&
2714                 (num.bias > 0) &&
2715                 (num.exp - num.bias < 308) &&
2716                 num.mant ))
2717             {
2718                 BOOST_STATIC_CONSTEXPR source_location loc
2719                     = BOOST_CURRENT_LOCATION;
2720                 return fail(cs.begin(), error::exponent_overflow, &loc);
2721             }
2722 
2723             num.bias = 0;
2724             num.exp = INT_MAX;
2725         }
2726         goto finish_dub;
2727     }
2728 
2729 finish_int:
2730     if(negative || (!stack_empty && num.neg))
2731     {
2732         if(BOOST_JSON_UNLIKELY(
2733             ! h_.on_int64(static_cast<
2734                 int64_t>(~num.mant + 1), {begin, cs.used(begin)}, ec_)))
2735             return fail(cs.begin());
2736         return cs.begin();
2737     }
2738     if(num.mant <= INT64_MAX)
2739     {
2740 finish_signed:
2741         if(BOOST_JSON_UNLIKELY(
2742             ! h_.on_int64(static_cast<
2743                 int64_t>(num.mant), {begin, cs.used(begin)}, ec_)))
2744             return fail(cs.begin());
2745         return cs.begin();
2746     }
2747     if(BOOST_JSON_UNLIKELY(
2748         ! h_.on_uint64(num.mant, {begin, cs.used(begin)}, ec_)))
2749         return fail(cs.begin());
2750     return cs.begin();
2751 finish_dub:
2752     double d;
2753     std::size_t const size = cs.used(begin);
2754     BOOST_ASSERT( !num_buf_.size() || precise_parsing );
2755     BOOST_IF_CONSTEXPR( precise_parsing )
2756     {
2757         char const* data = begin;
2758         std::size_t full_size = size;
2759          // if we previously suspended or if the current input ends with the
2760          // number, we need to copy the current part of the number to the
2761          // temporary buffer
2762         if(BOOST_JSON_UNLIKELY( num_buf_.size() ))
2763         {
2764             data = num_buf_.append( begin, size );
2765             full_size = num_buf_.size();
2766         }
2767         auto const err = detail::charconv::from_chars(
2768             data, data + full_size, d );
2769         BOOST_ASSERT( err.ec != std::errc::invalid_argument );
2770         BOOST_ASSERT( err.ptr == data + full_size );
2771         (void)err;
2772     }
2773     else BOOST_IF_CONSTEXPR( no_parsing )
2774         d = 0;
2775     else
2776         d = detail::dec_to_float(
2777             num.mant,
2778             num.bias + (num.frac ?
2779                 -num.exp : num.exp),
2780             num.neg);
2781     if(BOOST_JSON_UNLIKELY(
2782         ! h_.on_double(d, {begin, size}, ec_)))
2783         return fail(cs.begin());
2784     return cs.begin();
2785 }
2786 
2787 //----------------------------------------------------------
2788 
2789 template<class Handler>
2790 template<class... Args>
2791 basic_parser<Handler>::
2792 basic_parser(
2793     parse_options const& opt,
2794     Args&&... args)
2795     : h_(std::forward<Args>(args)...)
2796     , opt_(opt)
2797 {
2798 }
2799 
2800 //----------------------------------------------------------
2801 
2802 template<class Handler>
2803 void
2804 basic_parser<Handler>::
2805 reset() noexcept
2806 {
2807     ec_ = {};
2808     st_.clear();
2809     more_ = true;
2810     done_ = false;
2811     clean_ = true;
2812     num_buf_.clear();
2813 }
2814 
2815 template<class Handler>
2816 void
2817 basic_parser<Handler>::
2818 fail(system::error_code ec) noexcept
2819 {
2820     if(! ec)
2821     {
2822         // assign an arbitrary
2823         // error code to prevent UB
2824         BOOST_JSON_FAIL(ec_, error::incomplete);
2825     }
2826     else
2827     {
2828         ec_ = ec;
2829     }
2830     done_ = false;
2831 }
2832 
2833 //----------------------------------------------------------
2834 
2835 template<class Handler>
2836 std::size_t
2837 basic_parser<Handler>::
2838 write_some(
2839     bool more,
2840     char const* data,
2841     std::size_t size,
2842     system::error_code& ec)
2843 {
2844     // see if we exited via exception
2845     // on the last call to write_some
2846     if(! clean_)
2847     {
2848         // prevent UB
2849         if(! ec_)
2850         {
2851             BOOST_JSON_FAIL(ec_, error::exception);
2852         }
2853     }
2854     if(ec_)
2855     {
2856         // error is sticky
2857         ec = ec_;
2858         return 0;
2859     }
2860     clean_ = false;
2861     more_ = more;
2862     end_ = data + size;
2863     const char* p;
2864     if(BOOST_JSON_LIKELY(st_.empty()))
2865     {
2866         // first time
2867         depth_ = opt_.max_depth;
2868         if(BOOST_JSON_UNLIKELY(
2869             ! h_.on_document_begin(ec_)))
2870         {
2871             ec = ec_;
2872             return 0;
2873         }
2874         p = parse_document(data, std::true_type());
2875     }
2876     else
2877     {
2878         p = parse_document(data, std::false_type());
2879     }
2880 
2881     if(BOOST_JSON_LIKELY(p != sentinel()))
2882     {
2883         BOOST_ASSERT(! ec_);
2884         if(! done_)
2885         {
2886             done_ = true;
2887             h_.on_document_end(ec_);
2888         }
2889     }
2890     else
2891     {
2892         if(! ec_)
2893         {
2894             if(! more_)
2895             {
2896                 BOOST_JSON_FAIL(ec_, error::incomplete);
2897             }
2898             else if(! st_.empty())
2899             {
2900                 // consume as much trailing whitespace in
2901                 // the JSON document as possible, but still
2902                 // consider the parse complete
2903                 state st;
2904                 st_.peek(st);
2905                 if( st == state::doc3 &&
2906                     ! done_)
2907                 {
2908                     done_ = true;
2909                     h_.on_document_end(ec_);
2910                 }
2911             }
2912         }
2913         p = end_;
2914     }
2915     ec = ec_;
2916     clean_ = true;
2917     return p - data;
2918 }
2919 
2920 template<class Handler>
2921 std::size_t
2922 basic_parser<Handler>::
2923 write_some(
2924     bool more,
2925     char const* data,
2926     std::size_t size,
2927     std::error_code& ec)
2928 {
2929     system::error_code jec;
2930     std::size_t const result = write_some(more, data, size, jec);
2931     ec = jec;
2932     return result;
2933 }
2934 
2935 #endif
2936 
2937 } // namespace json
2938 } // namespace boost
2939 
2940 #ifdef _MSC_VER
2941 #pragma warning(pop)
2942 #endif
2943 
2944 #endif