LCOV - code coverage report
Current view: top level - json - basic_parser_impl.hpp (source / functions) Coverage Total Hit Missed
Test: coverage_remapped.info Lines: 99.2 % 1280 1270 10
Test Date: 2026-11-18 06:06:47 Functions: 35.5 % 4431 1571 2860

           TLA  Line data    Source code
       1                 : //
       2                 : // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
       3                 : // Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
       4                 : //
       5                 : // Distributed under the Boost Software License, Version 1.0. (See accompanying
       6                 : // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
       7                 : //
       8                 : // Official repository: https://github.com/boostorg/json
       9                 : //
      10                 : 
      11                 : #ifndef BOOST_JSON_BASIC_PARSER_IMPL_HPP
      12                 : #define BOOST_JSON_BASIC_PARSER_IMPL_HPP
      13                 : 
      14                 : #include <boost/json/detail/config.hpp>
      15                 : #include <boost/json/detail/literals.hpp>
      16                 : #include <boost/json/basic_parser.hpp>
      17                 : #include <boost/json/error.hpp>
      18                 : #include <boost/json/detail/buffer.hpp>
      19                 : #include <boost/json/detail/charconv/from_chars.hpp>
      20                 : #include <boost/json/detail/sse2.hpp>
      21                 : #include <boost/mp11/algorithm.hpp>
      22                 : #include <boost/mp11/integral.hpp>
      23                 : #include <cmath>
      24                 : #include <limits>
      25                 : #include <cstring>
      26                 : 
      27                 : #ifdef _MSC_VER
      28                 : #pragma warning(push)
      29                 : #pragma warning(disable: 4702) // unreachable code
      30                 : #pragma warning(disable: 4127) // conditional expression is constant
      31                 : #endif
      32                 : 
      33                 : /*  This file must be manually included to get the
      34                 :     function template definitions for basic_parser.
      35                 : */
      36                 : 
      37                 : /*  Reference:
      38                 : 
      39                 :     https://www.json.org/
      40                 : 
      41                 :     RFC 7159: The JavaScript Object Notation (JSON) Data Interchange Format
      42                 :     https://tools.ietf.org/html/rfc7159
      43                 : 
      44                 :     https://ampl.com/netlib/fp/dtoa.c
      45                 : */
      46                 : 
      47                 : #ifndef BOOST_JSON_DOCS
      48                 : 
      49                 : namespace boost {
      50                 : namespace json {
      51                 : namespace detail {
      52                 : 
      53                 : inline
      54                 : double
      55 HIT     1035607 : pow10(int exp) noexcept
      56                 : {
      57                 :     static double const tab[618] = {
      58                 :                         1e-308, 1e-307, 1e-306, 1e-305, 1e-304, 1e-303, 1e-302, 1e-301,
      59                 : 
      60                 :         1e-300, 1e-299, 1e-298, 1e-297, 1e-296, 1e-295, 1e-294, 1e-293, 1e-292, 1e-291,
      61                 :         1e-290, 1e-289, 1e-288, 1e-287, 1e-286, 1e-285, 1e-284, 1e-283, 1e-282, 1e-281,
      62                 :         1e-280, 1e-279, 1e-278, 1e-277, 1e-276, 1e-275, 1e-274, 1e-273, 1e-272, 1e-271,
      63                 :         1e-270, 1e-269, 1e-268, 1e-267, 1e-266, 1e-265, 1e-264, 1e-263, 1e-262, 1e-261,
      64                 :         1e-260, 1e-259, 1e-258, 1e-257, 1e-256, 1e-255, 1e-254, 1e-253, 1e-252, 1e-251,
      65                 :         1e-250, 1e-249, 1e-248, 1e-247, 1e-246, 1e-245, 1e-244, 1e-243, 1e-242, 1e-241,
      66                 :         1e-240, 1e-239, 1e-238, 1e-237, 1e-236, 1e-235, 1e-234, 1e-233, 1e-232, 1e-231,
      67                 :         1e-230, 1e-229, 1e-228, 1e-227, 1e-226, 1e-225, 1e-224, 1e-223, 1e-222, 1e-221,
      68                 :         1e-220, 1e-219, 1e-218, 1e-217, 1e-216, 1e-215, 1e-214, 1e-213, 1e-212, 1e-211,
      69                 :         1e-210, 1e-209, 1e-208, 1e-207, 1e-206, 1e-205, 1e-204, 1e-203, 1e-202, 1e-201,
      70                 : 
      71                 :         1e-200, 1e-199, 1e-198, 1e-197, 1e-196, 1e-195, 1e-194, 1e-193, 1e-192, 1e-191,
      72                 :         1e-190, 1e-189, 1e-188, 1e-187, 1e-186, 1e-185, 1e-184, 1e-183, 1e-182, 1e-181,
      73                 :         1e-180, 1e-179, 1e-178, 1e-177, 1e-176, 1e-175, 1e-174, 1e-173, 1e-172, 1e-171,
      74                 :         1e-170, 1e-169, 1e-168, 1e-167, 1e-166, 1e-165, 1e-164, 1e-163, 1e-162, 1e-161,
      75                 :         1e-160, 1e-159, 1e-158, 1e-157, 1e-156, 1e-155, 1e-154, 1e-153, 1e-152, 1e-151,
      76                 :         1e-150, 1e-149, 1e-148, 1e-147, 1e-146, 1e-145, 1e-144, 1e-143, 1e-142, 1e-141,
      77                 :         1e-140, 1e-139, 1e-138, 1e-137, 1e-136, 1e-135, 1e-134, 1e-133, 1e-132, 1e-131,
      78                 :         1e-130, 1e-129, 1e-128, 1e-127, 1e-126, 1e-125, 1e-124, 1e-123, 1e-122, 1e-121,
      79                 :         1e-120, 1e-119, 1e-118, 1e-117, 1e-116, 1e-115, 1e-114, 1e-113, 1e-112, 1e-111,
      80                 :         1e-110, 1e-109, 1e-108, 1e-107, 1e-106, 1e-105, 1e-104, 1e-103, 1e-102, 1e-101,
      81                 : 
      82                 :         1e-100, 1e-099, 1e-098, 1e-097, 1e-096, 1e-095, 1e-094, 1e-093, 1e-092, 1e-091,
      83                 :         1e-090, 1e-089, 1e-088, 1e-087, 1e-086, 1e-085, 1e-084, 1e-083, 1e-082, 1e-081,
      84                 :         1e-080, 1e-079, 1e-078, 1e-077, 1e-076, 1e-075, 1e-074, 1e-073, 1e-072, 1e-071,
      85                 :         1e-070, 1e-069, 1e-068, 1e-067, 1e-066, 1e-065, 1e-064, 1e-063, 1e-062, 1e-061,
      86                 :         1e-060, 1e-059, 1e-058, 1e-057, 1e-056, 1e-055, 1e-054, 1e-053, 1e-052, 1e-051,
      87                 :         1e-050, 1e-049, 1e-048, 1e-047, 1e-046, 1e-045, 1e-044, 1e-043, 1e-042, 1e-041,
      88                 :         1e-040, 1e-039, 1e-038, 1e-037, 1e-036, 1e-035, 1e-034, 1e-033, 1e-032, 1e-031,
      89                 :         1e-030, 1e-029, 1e-028, 1e-027, 1e-026, 1e-025, 1e-024, 1e-023, 1e-022, 1e-021,
      90                 :         1e-020, 1e-019, 1e-018, 1e-017, 1e-016, 1e-015, 1e-014, 1e-013, 1e-012, 1e-011,
      91                 :         1e-010, 1e-009, 1e-008, 1e-007, 1e-006, 1e-005, 1e-004, 1e-003, 1e-002, 1e-001,
      92                 : 
      93                 :         1e+000, 1e+001, 1e+002, 1e+003, 1e+004, 1e+005, 1e+006, 1e+007, 1e+008, 1e+009,
      94                 :         1e+010, 1e+011, 1e+012, 1e+013, 1e+014, 1e+015, 1e+016, 1e+017, 1e+018, 1e+019,
      95                 :         1e+020, 1e+021, 1e+022, 1e+023, 1e+024, 1e+025, 1e+026, 1e+027, 1e+028, 1e+029,
      96                 :         1e+030, 1e+031, 1e+032, 1e+033, 1e+034, 1e+035, 1e+036, 1e+037, 1e+038, 1e+039,
      97                 :         1e+040, 1e+041, 1e+042, 1e+043, 1e+044, 1e+045, 1e+046, 1e+047, 1e+048, 1e+049,
      98                 :         1e+050, 1e+051, 1e+052, 1e+053, 1e+054, 1e+055, 1e+056, 1e+057, 1e+058, 1e+059,
      99                 :         1e+060, 1e+061, 1e+062, 1e+063, 1e+064, 1e+065, 1e+066, 1e+067, 1e+068, 1e+069,
     100                 :         1e+070, 1e+071, 1e+072, 1e+073, 1e+074, 1e+075, 1e+076, 1e+077, 1e+078, 1e+079,
     101                 :         1e+080, 1e+081, 1e+082, 1e+083, 1e+084, 1e+085, 1e+086, 1e+087, 1e+088, 1e+089,
     102                 :         1e+090, 1e+091, 1e+092, 1e+093, 1e+094, 1e+095, 1e+096, 1e+097, 1e+098, 1e+099,
     103                 : 
     104                 :         1e+100, 1e+101, 1e+102, 1e+103, 1e+104, 1e+105, 1e+106, 1e+107, 1e+108, 1e+109,
     105                 :         1e+110, 1e+111, 1e+112, 1e+113, 1e+114, 1e+115, 1e+116, 1e+117, 1e+118, 1e+119,
     106                 :         1e+120, 1e+121, 1e+122, 1e+123, 1e+124, 1e+125, 1e+126, 1e+127, 1e+128, 1e+129,
     107                 :         1e+130, 1e+131, 1e+132, 1e+133, 1e+134, 1e+135, 1e+136, 1e+137, 1e+138, 1e+139,
     108                 :         1e+140, 1e+141, 1e+142, 1e+143, 1e+144, 1e+145, 1e+146, 1e+147, 1e+148, 1e+149,
     109                 :         1e+150, 1e+151, 1e+152, 1e+153, 1e+154, 1e+155, 1e+156, 1e+157, 1e+158, 1e+159,
     110                 :         1e+160, 1e+161, 1e+162, 1e+163, 1e+164, 1e+165, 1e+166, 1e+167, 1e+168, 1e+169,
     111                 :         1e+170, 1e+171, 1e+172, 1e+173, 1e+174, 1e+175, 1e+176, 1e+177, 1e+178, 1e+179,
     112                 :         1e+180, 1e+181, 1e+182, 1e+183, 1e+184, 1e+185, 1e+186, 1e+187, 1e+188, 1e+189,
     113                 :         1e+190, 1e+191, 1e+192, 1e+193, 1e+194, 1e+195, 1e+196, 1e+197, 1e+198, 1e+199,
     114                 : 
     115                 :         1e+200, 1e+201, 1e+202, 1e+203, 1e+204, 1e+205, 1e+206, 1e+207, 1e+208, 1e+209,
     116                 :         1e+210, 1e+211, 1e+212, 1e+213, 1e+214, 1e+215, 1e+216, 1e+217, 1e+218, 1e+219,
     117                 :         1e+220, 1e+221, 1e+222, 1e+223, 1e+224, 1e+225, 1e+226, 1e+227, 1e+228, 1e+229,
     118                 :         1e+230, 1e+231, 1e+232, 1e+233, 1e+234, 1e+235, 1e+236, 1e+237, 1e+238, 1e+239,
     119                 :         1e+240, 1e+241, 1e+242, 1e+243, 1e+244, 1e+245, 1e+246, 1e+247, 1e+248, 1e+249,
     120                 :         1e+250, 1e+251, 1e+252, 1e+253, 1e+254, 1e+255, 1e+256, 1e+257, 1e+258, 1e+259,
     121                 :         1e+260, 1e+261, 1e+262, 1e+263, 1e+264, 1e+265, 1e+266, 1e+267, 1e+268, 1e+269,
     122                 :         1e+270, 1e+271, 1e+272, 1e+273, 1e+274, 1e+275, 1e+276, 1e+277, 1e+278, 1e+279,
     123                 :         1e+280, 1e+281, 1e+282, 1e+283, 1e+284, 1e+285, 1e+286, 1e+287, 1e+288, 1e+289,
     124                 :         1e+290, 1e+291, 1e+292, 1e+293, 1e+294, 1e+295, 1e+296, 1e+297, 1e+298, 1e+299,
     125                 : 
     126                 :         1e+300, 1e+301, 1e+302, 1e+303, 1e+304, 1e+305, 1e+306, 1e+307, 1e+308 };
     127                 : 
     128         1035607 :     if( exp > 308 )
     129                 :     {
     130             455 :         return std::numeric_limits<double>::infinity();
     131                 :     }
     132         1035152 :     else if( exp < -308 )
     133                 :     {
     134                 :         // due to the way pow10 is used by dec_to_float,
     135                 :         // we can afford to return 0.0 here
     136             151 :         return 0.0;
     137                 :     }
     138                 :     else
     139                 :     {
     140         1035001 :         exp += 308;
     141         1035001 :         BOOST_ASSERT(exp >= 0 && exp < 618);
     142         1035001 :         return tab[exp];
     143                 :     }
     144                 : }
     145                 : 
     146                 : inline
     147                 : double
     148         1043157 : dec_to_float(
     149                 :     std::uint64_t m,
     150                 :     std::int32_t e,
     151                 :     bool neg) noexcept
     152                 : {
     153                 :     // convert to double explicitly to silence warnings
     154         1043157 :     double x = static_cast<double>(m);
     155         1043157 :     if(neg)
     156           17069 :         x = -x;
     157                 : 
     158                 :     // pow10 returns infinity for large exponents,
     159                 :     // and zero times infinity is NaN
     160         1043157 :     if(m == 0)
     161            7550 :         return x;
     162                 : 
     163         1035607 :     if(e < -305)
     164                 :     {
     165            5269 :         x *= 1e-305 ;
     166            5269 :         e += 305;
     167                 :     }
     168                 : 
     169         1035607 :     if(e >= -22 && e < 0)
     170           57637 :         return x / pow10(-e);
     171                 : 
     172          977970 :     return x * pow10(e);
     173                 : }
     174                 : 
     175                 : inline
     176                 : bool
     177                 : is_control(char c) noexcept
     178                 : {
     179                 :     return static_cast<unsigned char>(c) < 32;
     180                 : }
     181                 : 
     182                 : inline
     183                 : int
     184           66931 : hex_digit(unsigned char c) noexcept
     185                 : {
     186                 :     // by Peter Dimov
     187           66931 :     if( c >= '0' && c <= '9' )
     188           35759 :         return c - '0';
     189           31172 :     c &= ~0x20;
     190           31172 :     if( c >= 'A' && c <= 'F' )
     191           30562 :         return 10 + c - 'A';
     192             610 :     return -1;
     193                 : }
     194                 : 
     195                 : } // detail
     196                 : 
     197                 : //----------------------------------------------------------
     198                 : 
     199                 : template< class Handler >
     200                 : template< bool StackEmpty_, char First_ >
     201                 : struct basic_parser<Handler>::
     202                 : parse_number_helper
     203                 : {
     204                 :     basic_parser* parser;
     205                 :     char const* p;
     206                 : 
     207                 :     template< std::size_t N >
     208                 :     char const*
     209        10516087 :     operator()( mp11::mp_size_t<N> ) const
     210                 :     {
     211        21026669 :         return parser->parse_number(
     212        10516087 :             p,
     213                 :             std::integral_constant<bool, StackEmpty_>(),
     214                 :             std::integral_constant<char, First_>(),
     215                 :             std::integral_constant<
     216        10510583 :                 number_precision, static_cast<number_precision>(N)>() );
     217                 :     }
     218                 : };
     219                 : 
     220                 : //----------------------------------------------------------
     221                 : 
     222                 : template<class Handler>
     223                 : void
     224         8602653 : basic_parser<Handler>::
     225                 : reserve()
     226                 : {
     227         8602653 :     if(BOOST_JSON_LIKELY(
     228                 :         ! st_.empty()))
     229           37574 :         return;
     230                 :     // Reserve the largest stack we need,
     231                 :     // to avoid reallocation during suspend.
     232        17130158 :     st_.reserve(
     233                 :         sizeof(state) + // document parsing state
     234                 :         (sizeof(state) +
     235         8565079 :             sizeof(std::size_t)) * depth() + // array and object state + size
     236                 :         sizeof(state) + // value parsing state
     237                 :         sizeof(std::size_t) + // string size
     238                 :         sizeof(state)); // comment state
     239                 : }
     240                 : 
     241                 : //----------------------------------------------------------
     242                 : //
     243                 : // The sentinel value is returned by parse functions
     244                 : // to indicate that the parser failed, or suspended.
     245                 : // this is used as it is distinct from all valid values
     246                 : // for data in write
     247                 : 
     248                 : template<class Handler>
     249                 : const char*
     250        47302321 : basic_parser<Handler>::
     251                 : sentinel()
     252                 : {
     253                 :     // the "+1" ensures that the returned pointer is unique even if
     254                 :     // the given input buffer borders on this object
     255                 :     return reinterpret_cast<
     256        47302321 :         const char*>(this) + 1;
     257                 : }
     258                 : 
     259                 : template<class Handler>
     260                 : bool
     261        10852366 : basic_parser<Handler>::
     262                 : incomplete(
     263                 :     const detail::const_stream_wrapper& cs)
     264                 : {
     265        10852366 :     return cs.begin() == sentinel();
     266                 : }
     267                 : 
     268                 : //----------------------------------------------------------
     269                 : //
     270                 : // These functions are declared with the BOOST_NOINLINE
     271                 : // attribute to avoid polluting the parsers hot-path.
     272                 : // They return the canary value to indicate suspension
     273                 : // or failure.
     274                 : 
     275                 : template<class Handler>
     276                 : const char*
     277                 : basic_parser<Handler>::
     278                 : suspend_or_fail(state st)
     279                 : {
     280                 :     if(BOOST_JSON_LIKELY(
     281                 :         ! ec_ && more_))
     282                 :     {
     283                 :         // suspend
     284                 :         reserve();
     285                 :         st_.push_unchecked(st);
     286                 :     }
     287                 :     return sentinel();
     288                 : }
     289                 : 
     290                 : template<class Handler>
     291                 : const char*
     292           56215 : basic_parser<Handler>::
     293                 : suspend_or_fail(
     294                 :     state st,
     295                 :     std::size_t n)
     296                 : {
     297           56215 :     if(BOOST_JSON_LIKELY(
     298                 :         ! ec_ && more_))
     299                 :     {
     300                 :         // suspend
     301           35897 :         reserve();
     302           35897 :         st_.push_unchecked(n);
     303           35897 :         st_.push_unchecked(st);
     304                 :     }
     305           56215 :     return sentinel();
     306                 : }
     307                 : 
     308                 : 
     309                 : template<class Handler>
     310                 : const char*
     311           19076 : basic_parser<Handler>::
     312                 : fail(const char* p) noexcept
     313                 : {
     314           19076 :     BOOST_ASSERT( p != sentinel() );
     315           19076 :     end_ = p;
     316           19076 :     return sentinel();
     317                 : }
     318                 : 
     319                 : template<class Handler>
     320                 : const char*
     321            7781 : basic_parser<Handler>::
     322                 : fail(
     323                 :     const char* p,
     324                 :     error ev,
     325                 :     source_location const* loc) noexcept
     326                 : {
     327            7781 :     BOOST_ASSERT( p != sentinel() );
     328            7781 :     end_ = p;
     329            7781 :     ec_.assign(ev, loc);
     330            7781 :     return sentinel();
     331                 : }
     332                 : 
     333                 : template<class Handler>
     334                 : const char*
     335           11450 : basic_parser<Handler>::
     336                 : maybe_suspend(
     337                 :     const char* p,
     338                 :     state st)
     339                 : {
     340           11450 :     if( p != sentinel() )
     341            9511 :         end_ = p;
     342           11450 :     if(BOOST_JSON_LIKELY(more_))
     343                 :     {
     344                 :         // suspend
     345           11176 :         reserve();
     346           11176 :         st_.push_unchecked(st);
     347                 :     }
     348           11450 :     return sentinel();
     349                 : }
     350                 : 
     351                 : template<class Handler>
     352                 : const char*
     353           41284 : basic_parser<Handler>::
     354                 : maybe_suspend(
     355                 :     const char* p,
     356                 :     state st,
     357                 :     std::size_t n)
     358                 : {
     359           41284 :     BOOST_ASSERT( p != sentinel() );
     360           41284 :     end_ = p;
     361           41284 :     if(BOOST_JSON_LIKELY(more_))
     362                 :     {
     363                 :         // suspend
     364           40876 :         reserve();
     365           40876 :         st_.push_unchecked(n);
     366           40876 :         st_.push_unchecked(st);
     367                 :     }
     368           41284 :     return sentinel();
     369                 : }
     370                 : 
     371                 : template<class Handler>
     372                 : const char*
     373            1132 : basic_parser<Handler>::
     374                 : maybe_suspend(
     375                 :     const char* p,
     376                 :     state st,
     377                 :     const number& num)
     378                 : {
     379            1132 :     BOOST_ASSERT( p != sentinel() );
     380            1132 :     end_ = p;
     381            1132 :     if(BOOST_JSON_LIKELY(more_))
     382                 :     {
     383                 :         // suspend
     384            1132 :         num_ = num;
     385            1132 :         reserve();
     386            1132 :         st_.push_unchecked(st);;
     387                 :     }
     388            1132 :     return sentinel();
     389                 : }
     390                 : 
     391                 : template<class Handler>
     392                 : const char*
     393           88669 : basic_parser<Handler>::
     394                 : suspend(
     395                 :     const char* p,
     396                 :     state st)
     397                 : {
     398           88669 :     BOOST_ASSERT( p != sentinel() );
     399           88669 :     end_ = p;
     400                 :     // suspend
     401           88669 :     reserve();
     402           88669 :     st_.push_unchecked(st);
     403           88669 :     return sentinel();
     404                 : }
     405                 : 
     406                 : template<class Handler>
     407                 : const char*
     408         8424903 : basic_parser<Handler>::
     409                 : suspend(
     410                 :     const char* p,
     411                 :     state st,
     412                 :     const number& num)
     413                 : {
     414         8424903 :     BOOST_ASSERT( p != sentinel() );
     415         8424903 :     end_ = p;
     416                 :     // suspend
     417         8424903 :     num_ = num;
     418         8424903 :     reserve();
     419         8424903 :     st_.push_unchecked(st);
     420         8424903 :     return sentinel();
     421                 : }
     422                 : 
     423                 : template<class Handler>
     424                 : template<
     425                 :     bool StackEmpty_/*,
     426                 :     bool Terminal_*/>
     427                 : const char*
     428           21829 : basic_parser<Handler>::
     429                 : parse_comment(const char* p,
     430                 :     std::integral_constant<bool, StackEmpty_> stack_empty,
     431                 :     /*std::integral_constant<bool, Terminal_>*/ bool terminal)
     432                 : {
     433           21829 :     detail::const_stream_wrapper cs(p, end_);
     434           21829 :     const char* start = cs.begin();
     435                 :     std::size_t remain;
     436           21829 :     if(! stack_empty && ! st_.empty())
     437                 :     {
     438                 :         state st;
     439            3576 :         st_.pop(st);
     440            3576 :         switch(st)
     441                 :         {
     442                 :             default: BOOST_JSON_UNREACHABLE(); // LCOV_EXCL_LINE
     443             541 :             case state::com1: goto do_com1;
     444            2381 :             case state::com2: goto do_com2;
     445             438 :             case state::com3: goto do_com3;
     446             216 :             case state::com4: goto do_com4;
     447                 :         }
     448                 :     }
     449           18253 :     BOOST_ASSERT(*cs == '/');
     450           18253 :     ++cs;
     451           18794 : do_com1:
     452           18794 :     if(BOOST_JSON_UNLIKELY(! cs))
     453             558 :         return maybe_suspend(cs.begin(), state::com1);
     454           18236 :     switch(*cs)
     455                 :     {
     456               5 :     default:
     457                 :         {
     458                 :             BOOST_STATIC_CONSTEXPR source_location loc
     459                 :                 = BOOST_CURRENT_LOCATION;
     460               5 :             return fail(cs.begin(), error::syntax, &loc);
     461                 :         }
     462           10547 :     case '/':
     463           10547 :         ++cs;
     464           12928 : do_com2:
     465                 :         // KRYSTIAN TODO: this is a mess, we have to fix this
     466           12928 :         remain = cs.remain();
     467           25856 :         cs = remain ? static_cast<const char*>(
     468           12928 :             std::memchr(cs.begin(), '\n', remain)) : sentinel();
     469           12928 :         if(! cs.begin())
     470            2197 :             cs = sentinel();
     471           12928 :         if(BOOST_JSON_UNLIKELY(incomplete(cs)))
     472                 :         {
     473                 :             // if the doc does not terminate
     474                 :             // with a newline, treat it as the
     475                 :             // end of the comment
     476            2632 :             if(terminal && ! more_)
     477                 :             {
     478              39 :                 if(BOOST_JSON_UNLIKELY(! h_.on_comment(
     479                 :                     {start, cs.remain(start)}, ec_)))
     480               2 :                     return fail(cs.end());
     481              35 :                 return cs.end();
     482                 :             }
     483            2593 :             if(BOOST_JSON_UNLIKELY(! h_.on_comment_part(
     484                 :                 {start, cs.remain(start)}, ec_)))
     485              96 :                 return fail(cs.end());
     486            2401 :             if(terminal)
     487             106 :                 return suspend(cs.end(), state::com2);
     488            2295 :             return maybe_suspend(cs.end(), state::com2);
     489                 :         }
     490           10296 :         break;
     491            1684 :     case '*':
     492                 :         do
     493                 :         {
     494            9368 :             ++cs;
     495            9806 : do_com3:
     496                 :             // KRYSTIAN TODO: this is a mess, we have to fix this
     497            9806 :             remain = cs.remain();
     498           19612 :             cs = remain ? static_cast<const char*>(
     499            9806 :                 std::memchr(cs.begin(), '*', remain)) : sentinel();
     500            9806 :             if(! cs.begin())
     501             242 :                 cs = sentinel();
     502                 :             // stopped inside a c comment
     503            9806 :             if(BOOST_JSON_UNLIKELY(incomplete(cs)))
     504                 :             {
     505             503 :                 if(BOOST_JSON_UNLIKELY(! h_.on_comment_part(
     506                 :                     {start, cs.remain(start)}, ec_)))
     507              30 :                     return fail(cs.end());
     508             443 :                 return maybe_suspend(cs.end(), state::com3);
     509                 :             }
     510                 :             // found a asterisk, check if the next char is a slash
     511            9303 :             ++cs;
     512            9519 : do_com4:
     513            9519 :             if(BOOST_JSON_UNLIKELY(! cs))
     514                 :             {
     515             259 :                 if(BOOST_JSON_UNLIKELY(! h_.on_comment_part(
     516                 :                     {start, cs.used(start)}, ec_)))
     517              18 :                     return fail(cs.begin());
     518             223 :                 return maybe_suspend(cs.begin(), state::com4);
     519                 :             }
     520                 :         }
     521            9260 :         while(*cs != '/');
     522                 :     }
     523           17872 :     ++cs;
     524           17872 :     if(BOOST_JSON_UNLIKELY(! h_.on_comment(
     525                 :         {start, cs.used(start)}, ec_)))
     526             968 :         return fail(cs.begin());
     527           15936 :     return cs.begin();
     528                 : }
     529                 : 
     530                 : template<class Handler>
     531                 : template<bool StackEmpty_>
     532                 : const char*
     533        10710741 : basic_parser<Handler>::
     534                 : parse_document(const char* p,
     535                 :     std::integral_constant<bool, StackEmpty_> stack_empty)
     536                 : {
     537        10710741 :     detail::const_stream_wrapper cs(p, end_);
     538        10710741 :     if(! stack_empty && ! st_.empty())
     539                 :     {
     540                 :         state st;
     541         8561597 :         st_.peek(st);
     542         8561597 :         switch(st)
     543                 :         {
     544         8475520 :         default: goto do_doc2;
     545             601 :         case state::doc1:
     546             601 :                  st_.pop(st);
     547             601 :                  goto do_doc1;
     548           85256 :         case state::doc3:
     549           85256 :                  st_.pop(st);
     550           85256 :                  goto do_doc3;
     551             220 :         case state::com1: case state::com2:
     552                 :         case state::com3: case state::com4:
     553             220 :                  goto do_doc4;
     554                 :         }
     555                 :     }
     556         2149144 : do_doc1:
     557         2149745 :     cs = detail::count_whitespace(cs.begin(), cs.end());
     558         2149745 :     if(BOOST_JSON_UNLIKELY(! cs))
     559             633 :         return maybe_suspend(cs.begin(), state::doc1);
     560         2149112 : do_doc2:
     561        10624632 :     switch(+opt_.allow_comments |
     562        10624632 :         (opt_.allow_trailing_commas << 1) |
     563        10624632 :         (opt_.allow_invalid_utf8 << 2))
     564                 :     {
     565                 :     // no extensions
     566        10600807 :     default:
     567        10600807 :         cs = parse_value(cs.begin(), stack_empty, std::false_type(), std::false_type(), std::false_type(), opt_.allow_invalid_utf16);
     568        10585585 :         break;
     569                 :     // comments
     570           13694 :     case 1:
     571           13694 :         cs = parse_value(cs.begin(), stack_empty, std::true_type(), std::false_type(), std::false_type(), opt_.allow_invalid_utf16);
     572           11426 :         break;
     573                 :     // trailing
     574            6710 :     case 2:
     575            6710 :         cs = parse_value(cs.begin(), stack_empty, std::false_type(), std::true_type(), std::false_type(), opt_.allow_invalid_utf16);
     576            5117 :         break;
     577                 :     // comments & trailing
     578             761 :     case 3:
     579             761 :         cs = parse_value(cs.begin(), stack_empty, std::true_type(), std::true_type(), std::false_type(), opt_.allow_invalid_utf16);
     580             761 :         break;
     581                 :     // skip validation
     582             760 :     case 4:
     583             760 :         cs = parse_value(cs.begin(), stack_empty, std::false_type(), std::false_type(), std::true_type(), opt_.allow_invalid_utf16);
     584             760 :         break;
     585                 :     // comments & skip validation
     586             760 :     case 5:
     587             760 :         cs = parse_value(cs.begin(), stack_empty, std::true_type(), std::false_type(), std::true_type(), opt_.allow_invalid_utf16);
     588             760 :         break;
     589                 :     // trailing & skip validation
     590             760 :     case 6:
     591             760 :         cs = parse_value(cs.begin(), stack_empty, std::false_type(), std::true_type(), std::true_type(), opt_.allow_invalid_utf16);
     592             760 :         break;
     593                 :     // comments & trailing & skip validation
     594             380 :     case 7:
     595             380 :         cs = parse_value(cs.begin(), stack_empty, std::true_type(), std::true_type(), std::true_type(), opt_.allow_invalid_utf16);
     596             380 :         break;
     597                 :     }
     598        10605549 :     if(BOOST_JSON_UNLIKELY(incomplete(cs)))
     599                 :         // the appropriate state has already been pushed into stack
     600         8502821 :         return sentinel();
     601         2102728 : do_doc3:
     602         2188316 :     cs = detail::count_whitespace(cs.begin(), cs.end());
     603         2188316 :     if(BOOST_JSON_UNLIKELY(! cs))
     604                 :     {
     605         2185718 :         if(more_)
     606           88563 :             return suspend(cs.begin(), state::doc3);
     607                 :     }
     608            2598 :     else if(opt_.allow_comments && *cs == '/')
     609                 :     {
     610             536 : do_doc4:
     611             756 :         cs = parse_comment(cs.begin(), stack_empty, std::true_type());
     612             671 :         if(BOOST_JSON_UNLIKELY(incomplete(cs)))
     613             339 :             return sentinel();
     614             332 :         goto do_doc3;
     615                 :     }
     616         2099217 :     return cs.begin();
     617                 : }
     618                 : 
     619                 : template<class Handler>
     620                 : template<
     621                 :     bool StackEmpty_,
     622                 :     bool AllowComments_/*,
     623                 :     bool AllowTrailing_,
     624                 :     bool AllowBadUTF8_*/>
     625                 : const char*
     626        10742427 : basic_parser<Handler>::
     627                 : parse_value(const char* p,
     628                 :     std::integral_constant<bool, StackEmpty_> stack_empty,
     629                 :     std::integral_constant<bool, AllowComments_> allow_comments,
     630                 :     /*std::integral_constant<bool, AllowTrailing_>*/ bool allow_trailing,
     631                 :     /*std::integral_constant<bool, AllowBadUTF8_>*/ bool allow_bad_utf8,
     632                 :     bool allow_bad_utf16)
     633                 : {
     634        10742427 :     if(stack_empty || st_.empty())
     635                 :     {
     636         2248489 : loop:
     637         2253405 :         switch(*p)
     638                 :         {
     639           22761 :         case '0':
     640           22761 :             return mp11::mp_with_index<3>(
     641           22761 :                 static_cast<unsigned char>(opt_.numbers),
     642           22100 :                 parse_number_helper<true, '0'>{ this, p });
     643           25185 :         case '-':
     644           25185 :             return mp11::mp_with_index<3>(
     645           25185 :                 static_cast<unsigned char>(opt_.numbers),
     646           24073 :                 parse_number_helper<true, '-'>{ this, p });
     647         2042109 :         case '1': case '2': case '3':
     648                 :         case '4': case '5': case '6':
     649                 :         case '7': case '8': case '9':
     650         2042109 :             return mp11::mp_with_index<3>(
     651         2042109 :                 static_cast<unsigned char>(opt_.numbers),
     652         2039233 :                 parse_number_helper<true, '+'>{ this, p });
     653           11381 :         case 'n':
     654           11381 :             return parse_literal( p, detail::literals_c<detail::literals::null>() );
     655             664 :         case 't':
     656             664 :             return parse_literal( p, detail::literals_c<detail::literals::true_>() );
     657             722 :         case 'f':
     658             722 :             return parse_literal( p, detail::literals_c<detail::literals::false_>() );
     659             681 :         case 'I':
     660             681 :             if( !opt_.allow_infinity_and_nan )
     661                 :             {
     662                 :                 BOOST_STATIC_CONSTEXPR source_location loc
     663                 :                     = BOOST_CURRENT_LOCATION;
     664              24 :                 return fail(p, error::syntax, &loc);
     665                 :             }
     666             657 :             return parse_literal( p, detail::literals_c<detail::literals::infinity>() );
     667             231 :         case 'N':
     668             231 :             if( !opt_.allow_infinity_and_nan )
     669                 :             {
     670                 :                 BOOST_STATIC_CONSTEXPR source_location loc
     671                 :                     = BOOST_CURRENT_LOCATION;
     672              30 :                 return fail(p, error::syntax, &loc);
     673                 :             }
     674             201 :             return parse_literal(p, detail::literals_c<detail::literals::nan>() );
     675           47587 :         case '"':
     676           47587 :             return parse_string(p, std::true_type(), std::false_type(), allow_bad_utf8, allow_bad_utf16);
     677           20677 :         case '[':
     678           20677 :             return parse_array(p, std::true_type(), allow_comments, allow_trailing, allow_bad_utf8, allow_bad_utf16);
     679           74138 :         case '{':
     680           74138 :             return parse_object(p, std::true_type(), allow_comments, allow_trailing, allow_bad_utf8, allow_bad_utf16);
     681            6148 :         case '/':
     682            6148 :             if(! allow_comments)
     683                 :             {
     684                 :                 BOOST_STATIC_CONSTEXPR source_location loc
     685                 :                     = BOOST_CURRENT_LOCATION;
     686             284 :                 return fail(p, error::syntax, &loc);
     687                 :             }
     688            5864 :             p = parse_comment(p, stack_empty, std::false_type());
     689                 :             // KRYSTIAN NOTE: incomplete takes const_stream, we either
     690                 :             // can add an overload, change the existing one to take a pointer,
     691                 :             // or just leave it as is
     692            5625 :             if(BOOST_JSON_UNLIKELY(p == sentinel()))
     693             606 :                 return maybe_suspend(p, state::val2);
     694                 :             BOOST_FALLTHROUGH;
     695                 :         case ' ':
     696                 :         case '\t':
     697                 :         case '\n':
     698                 :         case '\r':
     699            5035 :             p = detail::count_whitespace(p, end_);
     700            5035 :             if(BOOST_JSON_UNLIKELY(p == end_))
     701             119 :                 return maybe_suspend(p, state::val1);
     702            4916 :             goto loop;
     703            1105 :         default:
     704                 :             {
     705                 :                 BOOST_STATIC_CONSTEXPR source_location loc
     706                 :                     = BOOST_CURRENT_LOCATION;
     707            1105 :                 return fail(p, error::syntax, &loc);
     708                 :             }
     709                 :         }
     710                 :     }
     711         8493938 :     return resume_value(p, allow_comments, allow_trailing, allow_bad_utf8, allow_bad_utf16);
     712                 : }
     713                 : 
     714                 : template<class Handler>
     715                 : template<
     716                 :     bool AllowComments_/*,
     717                 :     bool AllowTrailing_,
     718                 :     bool AllowBadUTF8_*/>
     719                 : const char*
     720         8493938 : basic_parser<Handler>::
     721                 : resume_value(const char* p,
     722                 :     std::integral_constant<bool, AllowComments_> allow_comments,
     723                 :     /*std::integral_constant<bool, AllowTrailing_>*/ bool allow_trailing,
     724                 :     /*std::integral_constant<bool, AllowBadUTF8_>*/ bool allow_bad_utf8,
     725                 :     bool allow_bad_utf16)
     726                 : {
     727                 :     state st;
     728         8493938 :     st_.peek(st);
     729         8493938 :     switch(st)
     730                 :     {
     731                 :     default: BOOST_JSON_UNREACHABLE(); // LCOV_EXCL_LINE
     732            1924 :     case state::lit1:
     733            1924 :         return parse_literal(p,  detail::literals_c<detail::literals::resume>() );
     734                 : 
     735           23268 :     case state::str1: case state::str2:
     736                 :     case state::str8:
     737           23268 :         return parse_string(p, std::false_type(), std::false_type(), allow_bad_utf8, allow_bad_utf16);
     738                 : 
     739            5780 :     case state::arr1: case state::arr2:
     740                 :     case state::arr3: case state::arr4:
     741                 :     case state::arr5: case state::arr6:
     742            5780 :         return parse_array(p, std::false_type(), allow_comments, allow_trailing, allow_bad_utf8, allow_bad_utf16);
     743                 : 
     744           35074 :     case state::obj1: case state::obj2:
     745                 :     case state::obj3: case state::obj4:
     746                 :     case state::obj5: case state::obj6:
     747                 :     case state::obj7: case state::obj8:
     748                 :     case state::obj9: case state::obj10:
     749                 :     case state::obj11:
     750           35074 :         return parse_object(p, std::false_type(), allow_comments, allow_trailing, allow_bad_utf8, allow_bad_utf16);
     751                 : 
     752         8426032 :     case state::num1: case state::num2:
     753                 :     case state::num3: case state::num4:
     754                 :     case state::num5: case state::num6:
     755                 :     case state::num7: case state::num8:
     756                 :     case state::exp1: case state::exp2:
     757                 :     case state::exp3:
     758         8426032 :         return mp11::mp_with_index<3>(
     759         8426032 :             static_cast<unsigned char>(opt_.numbers),
     760         8425177 :             parse_number_helper<false, 0>{ this, p });
     761                 : 
     762                 :     // KRYSTIAN NOTE: these are special cases
     763             112 :     case state::val1:
     764                 :     {
     765             112 :         st_.pop(st);
     766             112 :         BOOST_ASSERT(st_.empty());
     767             112 :         p = detail::count_whitespace(p, end_);
     768             112 :         if(BOOST_JSON_UNLIKELY(p == end_))
     769               4 :             return maybe_suspend(p, state::val1);
     770             108 :         return parse_value(p, std::true_type(), allow_comments, allow_trailing, allow_bad_utf8, allow_bad_utf16);
     771                 :     }
     772                 : 
     773            1677 :     case state::val2:
     774                 :     {
     775            1677 :         st_.pop(st);
     776            1677 :         p = parse_comment(p, std::false_type(), std::false_type());
     777            1657 :         if(BOOST_JSON_UNLIKELY(p == sentinel()))
     778            1333 :             return maybe_suspend(p, state::val2);
     779             324 :         if(BOOST_JSON_UNLIKELY( p == end_ ))
     780              81 :             return maybe_suspend(p, state::val3);
     781             243 :         BOOST_ASSERT(st_.empty());
     782             243 :         return parse_value(p, std::true_type(), std::true_type(), allow_trailing, allow_bad_utf8, allow_bad_utf16);
     783                 :     }
     784                 : 
     785              71 :     case state::val3:
     786                 :     {
     787              71 :         st_.pop(st);
     788              71 :         if(BOOST_JSON_UNLIKELY(p == end_))
     789               1 :             return maybe_suspend(p, state::val1);
     790              70 :         return parse_value(p, std::true_type(), std::true_type(), allow_trailing, allow_bad_utf8, allow_bad_utf16);
     791                 :     }
     792                 :     }
     793                 : }
     794                 : 
     795                 : template<class Handler>
     796                 : template<class Literal>
     797                 : const char*
     798           16565 : basic_parser<Handler>::
     799                 : parse_literal(const char* p, Literal)
     800                 : {
     801                 :     using L = detail::literals;
     802                 : 
     803                 :     std::size_t cur_lit;
     804                 :     std::size_t offset;
     805                 : 
     806           16565 :     detail::const_stream_wrapper cs(p, end_);
     807                 :     BOOST_IF_CONSTEXPR( Literal::value != L::resume )
     808                 :     {
     809           13634 :         constexpr std::size_t index = literal_index(Literal::value);
     810           13634 :         constexpr char const* literal = detail::literal_strings[index];
     811           13634 :         constexpr std::size_t sz = detail::literal_sizes[index];
     812                 : 
     813           13634 :         if(BOOST_JSON_LIKELY( cs.remain() >= sz ))
     814                 :         {
     815           11984 :             int const cmp = std::memcmp(cs.begin(), literal, sz);
     816           11984 :             if( cmp != 0 )
     817                 :             {
     818                 :                 BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
     819             197 :                 return fail(cs.begin(), error::syntax, &loc);
     820                 :             }
     821                 : 
     822                 :             BOOST_IF_CONSTEXPR( Literal::value == L::null )
     823                 :             {
     824           10789 :                 if(BOOST_JSON_UNLIKELY(
     825                 :                     ! h_.on_null(ec_)))
     826             161 :                     return fail(cs.begin());
     827                 :             }
     828                 :             else BOOST_IF_CONSTEXPR( Literal::value == L::true_ )
     829                 :             {
     830             384 :                 if(BOOST_JSON_UNLIKELY(
     831                 :                     ! h_.on_bool(true, ec_)))
     832              14 :                     return fail(cs.begin());
     833                 :             }
     834                 :             else BOOST_IF_CONSTEXPR( Literal::value == L::false_ )
     835                 :             {
     836             406 :                 if(BOOST_JSON_UNLIKELY(
     837                 :                     ! h_.on_bool(false, ec_)))
     838              13 :                     return fail(cs.begin());
     839                 :             }
     840                 :             else BOOST_IF_CONSTEXPR( Literal::value == L::infinity )
     841                 :             {
     842             103 :                 if(BOOST_JSON_UNLIKELY(
     843                 :                     ! h_.on_double(
     844                 :                         std::numeric_limits<double>::infinity(),
     845                 :                         string_view(literal, sz),
     846                 :                         ec_)))
     847              13 :                     return fail(cs.begin());
     848                 :             }
     849                 :             else BOOST_IF_CONSTEXPR( Literal::value == L::neg_infinity )
     850                 :             {
     851               9 :                 if(BOOST_JSON_UNLIKELY(
     852                 :                     ! h_.on_double(
     853                 :                         -std::numeric_limits<double>::infinity(),
     854                 :                         string_view(literal, sz),
     855                 :                         ec_)))
     856               1 :                     return fail(cs.begin());
     857                 :             }
     858                 :             else BOOST_IF_CONSTEXPR( Literal::value == L::nan )
     859                 :             {
     860              96 :                 if(BOOST_JSON_UNLIKELY(
     861                 :                     ! h_.on_double(
     862                 :                         std::numeric_limits<double>::quiet_NaN(),
     863                 :                         string_view(literal, sz),
     864                 :                         ec_)))
     865              12 :                     return fail(cs.begin());
     866                 :             }
     867                 :             else
     868                 :             {
     869                 :                 BOOST_JSON_UNREACHABLE(); // LCOV_EXCL_LINE
     870                 :             }
     871                 : 
     872           11363 :             cs += sz;
     873           11363 :             return cs.begin();
     874                 :         }
     875                 : 
     876            1650 :         offset = 0;
     877            1650 :         cur_lit = index;
     878                 :     }
     879                 :     else
     880                 :     {
     881                 :         state st;
     882            2931 :         st_.pop(st);
     883            2931 :         BOOST_ASSERT( st == state::lit1 );
     884                 : 
     885            2931 :         cur_lit = cur_lit_;
     886            2931 :         offset = lit_offset_;
     887                 :     }
     888                 : 
     889            4581 :     std::size_t const lit_size = detail::literal_sizes[cur_lit];
     890            4581 :     std::size_t const size = (std::min)( lit_size - offset, cs.remain() );
     891            4581 :     int cmp = 0;
     892            4581 :     if(BOOST_JSON_LIKELY( cs.begin() ))
     893            4580 :         cmp = std::memcmp(
     894            4580 :             cs.begin(), detail::literal_strings[cur_lit] + offset, size );
     895            4581 :     if( cmp != 0 )
     896                 :     {
     897                 :         BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
     898             699 :         return fail(cs.begin(), error::syntax, &loc);
     899                 :     }
     900                 : 
     901            3882 :     if(BOOST_JSON_UNLIKELY( offset + size < lit_size ))
     902                 :     {
     903            1990 :         BOOST_ASSERT( cur_lit < 256 );
     904            1990 :         cur_lit_ = static_cast<unsigned char>( cur_lit );
     905            1990 :         BOOST_ASSERT( offset + size < 256 );
     906            1990 :         lit_offset_ = static_cast<unsigned char>( offset + size );
     907            1990 :         return maybe_suspend(cs.begin() + size, state::lit1);
     908                 :     }
     909                 : 
     910            1892 :     switch( static_cast<L>(cur_lit) )
     911                 :     {
     912             472 :     case L::null:
     913             472 :         if(BOOST_JSON_UNLIKELY(
     914                 :             ! h_.on_null(ec_)))
     915              61 :             return fail(cs.begin());
     916             351 :         break;
     917             152 :     case L::true_:
     918             152 :         if(BOOST_JSON_UNLIKELY(
     919                 :             ! h_.on_bool(true, ec_)))
     920              22 :             return fail(cs.begin());
     921             109 :         break;
     922             198 :     case L::false_:
     923             198 :         if(BOOST_JSON_UNLIKELY(
     924                 :             ! h_.on_bool(false, ec_)))
     925              28 :             return fail(cs.begin());
     926             142 :         break;
     927             308 :     case L::infinity:
     928             308 :         if(BOOST_JSON_UNLIKELY(
     929                 :             ! h_.on_double(
     930                 :                 std::numeric_limits<double>::infinity(),
     931                 :                 string_view(
     932                 :                     detail::literal_strings[ literal_index(L::infinity) ],
     933                 :                     detail::literal_sizes[ literal_index(L::infinity) ]),
     934                 :                 ec_)))
     935              49 :             return fail(cs.begin());
     936             210 :         break;
     937             686 :     case L::neg_infinity:
     938             686 :         if(BOOST_JSON_UNLIKELY(
     939                 :             ! h_.on_double(
     940                 :                 -std::numeric_limits<double>::infinity(),
     941                 :                 string_view(
     942                 :                     detail::literal_strings[ literal_index(L::neg_infinity) ],
     943                 :                     detail::literal_sizes[ literal_index(L::neg_infinity) ]),
     944                 :                 ec_)))
     945             102 :             return fail(cs.begin());
     946             482 :         break;
     947              76 :     case L::nan:
     948              76 :         if(BOOST_JSON_UNLIKELY(
     949                 :             ! h_.on_double(
     950                 :                 std::numeric_limits<double>::quiet_NaN(),
     951                 :                 string_view(
     952                 :                     detail::literal_strings[ literal_index(L::nan) ],
     953                 :                     detail::literal_sizes[ literal_index(L::nan) ]),
     954                 :                 ec_)))
     955              12 :             return fail(cs.begin());
     956              52 :         break;
     957                 :     default: BOOST_JSON_UNREACHABLE(); // LCOV_EXCL_LINE
     958                 :     }
     959                 : 
     960            1346 :     cs += size;
     961            1346 :     return cs.begin();
     962                 : }
     963                 : 
     964                 : //----------------------------------------------------------
     965                 : 
     966                 : template<class Handler>
     967                 : template<bool StackEmpty_, bool IsKey_>
     968                 : const char*
     969          164348 : basic_parser<Handler>::
     970                 : parse_string(const char* p,
     971                 :     std::integral_constant<bool, StackEmpty_> stack_empty,
     972                 :     std::integral_constant<bool, IsKey_> is_key,
     973                 :     bool allow_bad_utf8,
     974                 :     bool allow_bad_utf16)
     975                 : {
     976          164348 :     detail::const_stream_wrapper cs(p, end_);
     977                 :     std::size_t total;
     978                 :     char const* start;
     979                 :     std::size_t size;
     980          164348 :     if(! stack_empty && ! st_.empty())
     981                 :     {
     982                 :         state st;
     983           35919 :         st_.pop(st);
     984           35919 :         st_.pop(total);
     985           35919 :         switch(st)
     986                 :         {
     987                 :         default: BOOST_JSON_UNREACHABLE(); // LCOV_EXCL_LINE
     988            3149 :         case state::str2: goto do_str2;
     989            1868 :         case state::str8: goto do_str8;
     990           30902 :         case state::str1: break;
     991                 :         }
     992                 :     }
     993                 :     else
     994                 :     {
     995          128429 :         BOOST_ASSERT(*cs == '\x22'); // '"'
     996          128429 :         ++cs;
     997          128429 :         total = 0;
     998                 :     }
     999                 : 
    1000          167824 : do_str1:
    1001          167824 :     start = cs.begin();
    1002          335648 :     cs = allow_bad_utf8?
    1003            2177 :         detail::count_valid<true>(cs.begin(), cs.end()):
    1004          165647 :         detail::count_valid<false>(cs.begin(), cs.end());
    1005          167824 :     size = cs.used(start);
    1006          167824 :     if(is_key)
    1007                 :     {
    1008           46712 :         BOOST_ASSERT(total <= Handler::max_key_size);
    1009           94023 :         if(BOOST_JSON_UNLIKELY(size >
    1010                 :             Handler::max_key_size - total))
    1011                 :         {
    1012                 :             BOOST_STATIC_CONSTEXPR source_location loc
    1013                 :                 = BOOST_CURRENT_LOCATION;
    1014               3 :             return fail(cs.begin(), error::key_too_large, &loc);
    1015                 :         }
    1016                 :     }
    1017                 :     else
    1018                 :     {
    1019           38330 :         BOOST_ASSERT(total <= Handler::max_string_size);
    1020           73801 :         if(BOOST_JSON_UNLIKELY(size >
    1021                 :             Handler::max_string_size - total))
    1022                 :         {
    1023                 :             BOOST_STATIC_CONSTEXPR source_location loc
    1024                 :                 = BOOST_CURRENT_LOCATION;
    1025               3 :             return fail(cs.begin(), error::string_too_large, &loc);
    1026                 :         }
    1027                 :     }
    1028          167818 :     total += size;
    1029          167818 :     if(BOOST_JSON_UNLIKELY(! cs))
    1030                 :     {
    1031                 :         // call handler if the string isn't empty
    1032           33236 :         if(BOOST_JSON_LIKELY(size))
    1033                 :         {
    1034                 :             {
    1035           30068 :                 bool r = is_key?
    1036           12406 :                     h_.on_key_part( {start, size}, total, ec_ ):
    1037           18996 :                     h_.on_string_part( {start, size}, total, ec_ );
    1038                 : 
    1039           28960 :                 if(BOOST_JSON_UNLIKELY(!r))
    1040                 :                 {
    1041            1110 :                     return fail(cs.begin());
    1042                 :                 }
    1043                 :             }
    1044                 :         }
    1045           31018 :         return maybe_suspend(cs.begin(), state::str1, total);
    1046                 :     }
    1047                 :     // at this point all valid characters have been skipped, so any remaining
    1048                 :     // if there are any more characters, they are either escaped, or incomplete
    1049                 :     // utf8, or invalid utf8
    1050          134582 :     if(BOOST_JSON_UNLIKELY(*cs != '\x22')) // '"'
    1051                 :     {
    1052                 :         // sequence is invalid or incomplete
    1053           15081 :         if((*cs & 0x80) && !allow_bad_utf8)
    1054                 :         {
    1055            3472 :             seq_.save(cs.begin(), cs.remain());
    1056            3472 :             if(BOOST_JSON_UNLIKELY(seq_.complete()))
    1057                 :             {
    1058                 :                 BOOST_STATIC_CONSTEXPR source_location loc
    1059                 :                     = BOOST_CURRENT_LOCATION;
    1060            1557 :                 return fail(cs.begin(), error::syntax, &loc);
    1061                 :             }
    1062            1915 :             if(BOOST_JSON_LIKELY(size))
    1063                 :             {
    1064             245 :                 bool const r = is_key?
    1065              22 :                     h_.on_key_part( {start, size}, total, ec_ ):
    1066             245 :                     h_.on_string_part( {start, size}, total, ec_ );
    1067             223 :                 if(BOOST_JSON_UNLIKELY( !r ))
    1068              22 :                     return fail( cs.begin() );
    1069                 :             }
    1070            1871 :             return maybe_suspend(cs.end(), state::str8, total);
    1071                 :         }
    1072           11609 :         else if(BOOST_JSON_LIKELY(*cs == '\\'))
    1073                 :         {
    1074                 :             // flush unescaped run from input
    1075           11500 :             if(BOOST_JSON_LIKELY(size))
    1076                 :             {
    1077            4250 :                 bool const r = is_key?
    1078            1226 :                     h_.on_key_part( {start, size}, total, ec_ ):
    1079            3554 :                     h_.on_string_part( {start, size}, total, ec_ );
    1080            3766 :                 if(BOOST_JSON_UNLIKELY( !r ))
    1081             484 :                     return fail( cs.begin() );
    1082                 :             }
    1083            7250 : do_str2:
    1084           13681 :             cs = parse_escaped(cs.begin(), total, stack_empty, is_key, allow_bad_utf16);
    1085           12719 :             if(BOOST_JSON_UNLIKELY( incomplete(cs) ))
    1086            5476 :                 return suspend_or_fail(state::str2, total);
    1087                 : 
    1088            7243 :             goto do_str1;
    1089                 :         }
    1090                 :         // illegal control
    1091                 :         BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
    1092             109 :         return fail(cs.begin(), error::syntax, &loc);
    1093                 :     }
    1094                 : 
    1095                 :     {
    1096          119501 :         bool r = is_key?
    1097           84525 :             h_.on_key( {start, size}, total, ec_ ):
    1098           41985 :             h_.on_string( {start, size}, total, ec_ );
    1099                 : 
    1100          115127 :         if(BOOST_JSON_UNLIKELY(!r))
    1101                 :         {
    1102            4318 :             return fail(cs.begin());
    1103                 :         }
    1104                 :     }
    1105                 : 
    1106          110809 :     ++cs;
    1107          110809 :     return cs.begin();
    1108                 : 
    1109            1868 : do_str8:
    1110            1868 :     uint8_t needed = seq_.needed();
    1111            1868 :     if(BOOST_JSON_UNLIKELY( !seq_.append(cs.begin(), cs.remain()) ))
    1112               4 :         return maybe_suspend(cs.end(), state::str8, total);
    1113            1864 :     if(BOOST_JSON_UNLIKELY( !seq_.valid() ))
    1114                 :     {
    1115                 :         BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
    1116             210 :         return fail(cs.begin(), error::syntax, &loc);
    1117                 :     }
    1118                 :     {
    1119            1654 :         std::size_t const n = seq_.length();
    1120                 :         bool r;
    1121            1654 :         if(is_key)
    1122                 :         {
    1123               2 :             BOOST_ASSERT(total <= Handler::max_key_size);
    1124               2 :             if(BOOST_JSON_UNLIKELY(n >
    1125                 :                 Handler::max_key_size - total))
    1126                 :             {
    1127                 :                 BOOST_STATIC_CONSTEXPR source_location loc
    1128                 :                     = BOOST_CURRENT_LOCATION;
    1129               1 :                 return fail(cs.begin(), error::key_too_large, &loc);
    1130                 :             }
    1131               1 :             total += n;
    1132               1 :             r = h_.on_key_part( {seq_.data(), n}, total, ec_ );
    1133                 :         }
    1134                 :         else
    1135                 :         {
    1136               1 :             BOOST_ASSERT(total <= Handler::max_string_size);
    1137            1652 :             if(BOOST_JSON_UNLIKELY(n >
    1138                 :                 Handler::max_string_size - total))
    1139                 :             {
    1140                 :                 BOOST_STATIC_CONSTEXPR source_location loc
    1141                 :                     = BOOST_CURRENT_LOCATION;
    1142               1 :                 return fail(cs.begin(), error::string_too_large, &loc);
    1143                 :             }
    1144            1651 :             total += n;
    1145            1651 :             r = h_.on_string_part( {seq_.data(), n}, total, ec_ );
    1146                 :         }
    1147            1451 :         if(BOOST_JSON_UNLIKELY( !r ))
    1148             201 :             return fail( cs.begin() );
    1149                 :     }
    1150            1250 :     cs += needed;
    1151            1250 :     goto do_str1;
    1152                 : }
    1153                 : 
    1154                 : template<class Handler>
    1155                 : template<bool StackEmpty_>
    1156                 : const char*
    1157           13681 : basic_parser<Handler>::
    1158                 : parse_escaped(
    1159                 :     const char* p,
    1160                 :     std::size_t& total,
    1161                 :     std::integral_constant<bool, StackEmpty_> stack_empty,
    1162                 :     bool is_key,
    1163                 :     bool allow_bad_utf16)
    1164                 : {
    1165           13681 :     constexpr unsigned urc = 0xFFFD; // Unicode replacement character
    1166           13681 :     auto const ev_too_large = is_key?
    1167                 :         error::key_too_large : error::string_too_large;
    1168           13681 :     auto const max_size = is_key?
    1169                 :         Handler::max_key_size : Handler::max_string_size;
    1170                 :     int digit;
    1171                 : 
    1172                 :     //---------------------------------------------------------------
    1173                 :     //
    1174                 :     // To handle escapes, a local temporary buffer accumulates
    1175                 :     // the unescaped result. The algorithm attempts to fill the
    1176                 :     // buffer to capacity before invoking the handler.
    1177                 :     // In some cases the temporary buffer needs to be flushed
    1178                 :     // before it is full:
    1179                 :     // * When the closing double quote is seen
    1180                 :     // * When there in no more input (and more is expected later)
    1181                 :     // A goal of the algorithm is to call the handler as few times
    1182                 :     // as possible. Thus, when the first escape is encountered,
    1183                 :     // the algorithm attempts to fill the temporary buffer first.
    1184                 :     //
    1185           13681 :     detail::buffer<BOOST_JSON_STACK_BUFFER_SIZE> temp;
    1186                 : 
    1187                 :     // Unescaped JSON is never larger than its escaped version.
    1188                 :     // To efficiently process only what will fit in the temporary buffer,
    1189                 :     // the size of the input stream is temporarily "clipped" to the size
    1190                 :     // of the temporary buffer.
    1191                 :     // handle escaped character
    1192           13681 :     detail::clipped_const_stream cs(p, end_);
    1193           13681 :     cs.clip(temp.max_size());
    1194                 : 
    1195           13681 :     if(! stack_empty && ! st_.empty())
    1196                 :     {
    1197                 :         state st;
    1198            3149 :         st_.pop(st);
    1199            3149 :         switch(st)
    1200                 :         {
    1201                 :         default: BOOST_JSON_UNREACHABLE(); // LCOV_EXCL_LINE
    1202             528 :         case state::str3: goto do_str3;
    1203             392 :         case state::str4: goto do_str4;
    1204             390 :         case state::str5: goto do_str5;
    1205             389 :         case state::str6: goto do_str6;
    1206             386 :         case state::str7: goto do_str7;
    1207             232 :         case state::sur1: goto do_sur1;
    1208             188 :         case state::sur2: goto do_sur2;
    1209             164 :         case state::sur3: goto do_sur3;
    1210             162 :         case state::sur4: goto do_sur4;
    1211             160 :         case state::sur5: goto do_sur5;
    1212             158 :         case state::sur6: goto do_sur6;
    1213                 :         }
    1214                 :     }
    1215                 : 
    1216            3783 :     while(true)
    1217                 :     {
    1218           14315 :         BOOST_ASSERT( temp.capacity() );
    1219           14315 :         BOOST_ASSERT(*cs == '\\');
    1220           14315 :         ++cs;
    1221           15174 : do_str3:
    1222           15379 :         if(BOOST_JSON_UNLIKELY(! cs))
    1223                 :         {
    1224             562 :             if(BOOST_JSON_LIKELY(! temp.empty()))
    1225                 :             {
    1226               1 :                 BOOST_ASSERT(total <= max_size);
    1227             101 :                 if(BOOST_JSON_UNLIKELY(
    1228                 :                     temp.size() > max_size - total))
    1229                 :                 {
    1230                 :                     BOOST_STATIC_CONSTEXPR source_location loc
    1231                 :                         = BOOST_CURRENT_LOCATION;
    1232               1 :                     return fail(cs.begin(), ev_too_large, &loc);
    1233                 :                 }
    1234             100 :                 total += temp.size();
    1235                 :                 {
    1236              91 :                     bool r = is_key
    1237             100 :                         ? h_.on_key_part(temp.get(), total, ec_)
    1238             100 :                         : h_.on_string_part(temp.get(), total, ec_);
    1239                 : 
    1240              91 :                     if(BOOST_JSON_UNLIKELY(!r))
    1241                 :                     {
    1242               9 :                         return fail(cs.begin());
    1243                 :                     }
    1244                 :                 }
    1245              82 :                 temp.clear();
    1246                 :             }
    1247             543 :             cs.clip(temp.max_size());
    1248             543 :             if(BOOST_JSON_UNLIKELY(! cs))
    1249             543 :                 return maybe_suspend(cs.begin(), state::str3);
    1250                 :         }
    1251           14817 :         switch(*cs)
    1252                 :         {
    1253             191 :         default:
    1254                 :             {
    1255                 :                 BOOST_STATIC_CONSTEXPR source_location loc
    1256                 :                     = BOOST_CURRENT_LOCATION;
    1257             191 :                 return fail(cs.begin(), error::syntax, &loc);
    1258                 :             }
    1259             265 :         case '\x22': // '"'
    1260             265 :             temp.push_back('\x22');
    1261             265 :             ++cs;
    1262             265 :             break;
    1263             178 :         case '\\':
    1264             178 :             temp.push_back('\\');
    1265             178 :             ++cs;
    1266             178 :             break;
    1267              96 :         case '/':
    1268              96 :             temp.push_back('/');
    1269              96 :             ++cs;
    1270              96 :             break;
    1271             112 :         case 'b':
    1272             112 :             temp.push_back('\x08');
    1273             112 :             ++cs;
    1274             112 :             break;
    1275             108 :         case 'f':
    1276             108 :             temp.push_back('\x0c');
    1277             108 :             ++cs;
    1278             108 :             break;
    1279            1766 :         case 'n':
    1280            1766 :             temp.push_back('\x0a');
    1281            1766 :             ++cs;
    1282            1766 :             break;
    1283             146 :         case 'r':
    1284             146 :             temp.push_back('\x0d');
    1285             146 :             ++cs;
    1286             146 :             break;
    1287             266 :         case 't':
    1288             266 :             temp.push_back('\x09');
    1289             266 :             ++cs;
    1290             266 :             break;
    1291           11689 :         case 'u':
    1292                 :             // utf16 escape
    1293                 :             //
    1294                 :             // fast path only when the buffer
    1295                 :             // is large enough for 2 surrogates
    1296           11689 :             if(BOOST_JSON_LIKELY(cs.remain() > 10))
    1297                 :             {
    1298                 :                 // KRYSTIAN TODO: this could be done
    1299                 :                 // with fewer instructions
    1300           11394 :                 digit = detail::load_little_endian<4>(
    1301            5697 :                     cs.begin() + 1);
    1302            5697 :                 int d4 = detail::hex_digit(static_cast<
    1303            5697 :                     unsigned char>(digit >> 24));
    1304            5697 :                 int d3 = detail::hex_digit(static_cast<
    1305            5697 :                     unsigned char>(digit >> 16));
    1306            5697 :                 int d2 = detail::hex_digit(static_cast<
    1307            5697 :                     unsigned char>(digit >> 8));
    1308            5697 :                 int d1 = detail::hex_digit(static_cast<
    1309                 :                     unsigned char>(digit));
    1310            5697 :                 if(BOOST_JSON_UNLIKELY(
    1311                 :                     (d1 | d2 | d3 | d4) == -1))
    1312                 :                 {
    1313              60 :                     if(d1 != -1)
    1314              45 :                         ++cs;
    1315              60 :                     if(d2 != -1)
    1316              30 :                         ++cs;
    1317              60 :                     if(d3 != -1)
    1318              15 :                         ++cs;
    1319                 :                     BOOST_STATIC_CONSTEXPR source_location loc
    1320                 :                         = BOOST_CURRENT_LOCATION;
    1321              60 :                     return fail(cs.begin(), error::expected_hex_digit, &loc);
    1322                 :                 }
    1323                 :                 // 32 bit unicode scalar value
    1324            5637 :                 unsigned u1 =
    1325            5637 :                     (d1 << 12) + (d2 << 8) +
    1326            5637 :                     (d3 << 4) + d4;
    1327                 :                 // valid unicode scalar values are
    1328                 :                 // [0, D7FF] and [E000, 10FFFF]
    1329                 :                 // values within this range are valid utf-8
    1330                 :                 // code points and invalid leading surrogates.
    1331            5637 :                 if(BOOST_JSON_LIKELY(
    1332                 :                     u1 < 0xd800 || u1 > 0xdfff))
    1333                 :                 {
    1334            1340 :                     cs += 5;
    1335            1340 :                     temp.append_utf8(u1);
    1336            1340 :                     break;
    1337                 :                 }
    1338            4297 :                 if(BOOST_JSON_UNLIKELY(u1 > 0xdbff))
    1339                 :                 {
    1340                 :                     // If it's an illegal leading surrogate and
    1341                 :                     // the parser does not allow it, return an error.
    1342             707 :                     if(!allow_bad_utf16)
    1343                 :                     {
    1344                 :                         BOOST_STATIC_CONSTEXPR source_location loc
    1345                 :                             = BOOST_CURRENT_LOCATION;
    1346             122 :                         return fail(cs.begin(), error::illegal_leading_surrogate,
    1347             122 :                             &loc);
    1348                 :                     }
    1349                 :                     // Otherwise, append the Unicode replacement character
    1350                 :                     else
    1351                 :                     {
    1352             585 :                         cs += 5;
    1353             585 :                         temp.append_utf8(urc);
    1354             585 :                         break;
    1355                 :                     }
    1356                 :                 }
    1357            3590 :                 cs += 5;
    1358                 :                 // KRYSTIAN TODO: this can be a two byte load
    1359                 :                 // and a single comparison. We lose error information,
    1360                 :                 // but it's faster.
    1361            3590 :                 if(BOOST_JSON_UNLIKELY(*cs != '\\'))
    1362                 :                 {
    1363                 :                     // If the next character is not a backslash and
    1364                 :                     // the parser does not allow it, return a syntax error.
    1365             156 :                     if(!allow_bad_utf16)
    1366                 :                     {
    1367                 :                         BOOST_STATIC_CONSTEXPR source_location loc
    1368                 :                             = BOOST_CURRENT_LOCATION;
    1369              15 :                         return fail(cs.begin(), error::syntax, &loc);
    1370                 :                     }
    1371                 :                     // Otherwise, append the Unicode replacement character since
    1372                 :                     // the first code point is a valid leading surrogate
    1373                 :                     else
    1374                 :                     {
    1375             141 :                         temp.append_utf8(urc);
    1376             141 :                         break;
    1377                 :                     }
    1378                 :                 }
    1379            3434 :                 ++cs;
    1380            3434 :                 if(BOOST_JSON_UNLIKELY(*cs != 'u'))
    1381                 :                 {
    1382             220 :                     if (!allow_bad_utf16)
    1383                 :                     {
    1384                 :                         BOOST_STATIC_CONSTEXPR source_location loc
    1385                 :                             = BOOST_CURRENT_LOCATION;
    1386              15 :                         return fail(cs.begin(), error::syntax, &loc);
    1387                 :                     }
    1388                 :                     // Otherwise, append the Unicode replacement character since
    1389                 :                     // the first code point is a valid leading surrogate
    1390                 :                     else
    1391                 :                     {
    1392             205 :                         temp.append_utf8(urc);
    1393             205 :                         goto do_str3;
    1394                 :                     }
    1395                 :                 }
    1396            3214 :                 ++cs;
    1397            3214 :                 digit = detail::load_little_endian<4>(cs.begin());
    1398            3214 :                 d4 = detail::hex_digit(static_cast<
    1399            3214 :                     unsigned char>(digit >> 24));
    1400            3214 :                 d3 = detail::hex_digit(static_cast<
    1401            3214 :                     unsigned char>(digit >> 16));
    1402            3214 :                 d2 = detail::hex_digit(static_cast<
    1403            3214 :                     unsigned char>(digit >> 8));
    1404            3214 :                 d1 = detail::hex_digit(static_cast<
    1405                 :                     unsigned char>(digit));
    1406            3214 :                 if(BOOST_JSON_UNLIKELY(
    1407                 :                     (d1 | d2 | d3 | d4) == -1))
    1408                 :                 {
    1409              90 :                     if(d1 != -1)
    1410              75 :                         ++cs;
    1411              90 :                     if(d2 != -1)
    1412              45 :                         ++cs;
    1413              90 :                     if(d3 != -1)
    1414              15 :                         ++cs;
    1415                 :                     BOOST_STATIC_CONSTEXPR source_location loc
    1416                 :                         = BOOST_CURRENT_LOCATION;
    1417              90 :                     return fail(cs.begin(), error::expected_hex_digit, &loc);
    1418                 :                 }
    1419            3124 :                 unsigned u2 =
    1420            3124 :                     (d1 << 12) + (d2 << 8) +
    1421            3124 :                     (d3 << 4) + d4;
    1422                 :                 // Check if the second code point is a valid trailing surrogate.
    1423                 :                 // Valid trailing surrogates are [DC00, DFFF]
    1424            3124 :                 if(BOOST_JSON_UNLIKELY(
    1425                 :                     u2 < 0xdc00 || u2 > 0xdfff))
    1426                 :                 {
    1427                 :                     // If not valid and the parser does not allow it, return an error.
    1428            1353 :                     if(!allow_bad_utf16)
    1429                 :                     {
    1430                 :                         BOOST_STATIC_CONSTEXPR source_location loc
    1431                 :                             = BOOST_CURRENT_LOCATION;
    1432             136 :                         return fail(cs.begin(), error::illegal_trailing_surrogate,
    1433             136 :                             &loc);
    1434                 :                     }
    1435                 :                     // Append the replacement character for the
    1436                 :                     // first leading surrogate.
    1437            1217 :                     cs += 4;
    1438            1217 :                     temp.append_utf8(urc);
    1439                 :                     // Check if the second code point is a
    1440                 :                     // valid unicode scalar value (invalid leading
    1441                 :                     // or trailing surrogate)
    1442            1217 :                     if (u2 < 0xd800 || u2 > 0xdbff)
    1443                 :                     {
    1444             524 :                         temp.append_utf8(u2);
    1445             524 :                         break;
    1446                 :                     }
    1447                 :                     // If it is a valid leading surrogate
    1448                 :                     else
    1449                 :                     {
    1450             693 :                         u1_ = u2;
    1451             693 :                         goto do_sur1;
    1452                 :                     }
    1453                 :                 }
    1454            1771 :                 cs += 4;
    1455                 :                 // Calculate the Unicode code point from the surrogate pair and
    1456                 :                 // append the UTF-8 representation.
    1457            1771 :                 unsigned cp =
    1458            1771 :                     ((u1 - 0xd800) << 10) +
    1459                 :                     ((u2 - 0xdc00)) +
    1460                 :                         0x10000;
    1461                 :                 // utf-16 surrogate pair
    1462            1771 :                 temp.append_utf8(cp);
    1463            1771 :                 break;
    1464                 :             }
    1465                 :             // flush
    1466            5992 :             if(BOOST_JSON_LIKELY(! temp.empty()))
    1467                 :             {
    1468               4 :                 BOOST_ASSERT(total <= max_size);
    1469            1723 :                 if(BOOST_JSON_UNLIKELY(
    1470                 :                     temp.size() > max_size - total))
    1471                 :                 {
    1472                 :                     BOOST_STATIC_CONSTEXPR source_location loc
    1473                 :                         = BOOST_CURRENT_LOCATION;
    1474               1 :                     return fail(cs.begin(), ev_too_large, &loc);
    1475                 :                 }
    1476            1722 :                 total += temp.size();
    1477                 :                 {
    1478            1582 :                     bool r = is_key
    1479            1722 :                         ? h_.on_key_part(temp.get(), total, ec_)
    1480            1722 :                         : h_.on_string_part(temp.get(), total, ec_);
    1481                 : 
    1482            1582 :                     if(BOOST_JSON_UNLIKELY(!r))
    1483                 :                     {
    1484             140 :                         return fail(cs.begin());
    1485                 :                     }
    1486                 :                 }
    1487            1442 :                 temp.clear();
    1488            1442 :                 cs.clip(temp.max_size());
    1489                 :             }
    1490            5711 :             ++cs;
    1491                 :             // utf-16 escape
    1492            6103 :     do_str4:
    1493            6103 :             if(BOOST_JSON_UNLIKELY(! cs))
    1494             392 :                 return maybe_suspend(cs.begin(), state::str4);
    1495            5711 :             digit = detail::hex_digit(*cs);
    1496            5711 :             if(BOOST_JSON_UNLIKELY(digit == -1))
    1497                 :             {
    1498                 :                 BOOST_STATIC_CONSTEXPR source_location loc
    1499                 :                     = BOOST_CURRENT_LOCATION;
    1500              50 :                 return fail(cs.begin(), error::expected_hex_digit, &loc);
    1501                 :             }
    1502            5661 :             ++cs;
    1503            5661 :             u1_ = digit << 12;
    1504            6051 :     do_str5:
    1505            6051 :             if(BOOST_JSON_UNLIKELY(! cs))
    1506             390 :                 return maybe_suspend(cs.begin(), state::str5);
    1507            5661 :             digit = detail::hex_digit(*cs);
    1508            5661 :             if(BOOST_JSON_UNLIKELY(digit == -1))
    1509                 :             {
    1510                 :                 BOOST_STATIC_CONSTEXPR source_location loc
    1511                 :                     = BOOST_CURRENT_LOCATION;
    1512              20 :                 return fail(cs.begin(), error::expected_hex_digit, &loc);
    1513                 :             }
    1514            5641 :             ++cs;
    1515            5641 :             u1_ += digit << 8;
    1516            6030 :     do_str6:
    1517            6030 :             if(BOOST_JSON_UNLIKELY(! cs))
    1518             389 :                 return maybe_suspend(cs.begin(), state::str6);
    1519            5641 :             digit = detail::hex_digit(*cs);
    1520            5641 :             if(BOOST_JSON_UNLIKELY(digit == -1))
    1521                 :             {
    1522                 :                 BOOST_STATIC_CONSTEXPR source_location loc
    1523                 :                     = BOOST_CURRENT_LOCATION;
    1524              20 :                 return fail(cs.begin(), error::expected_hex_digit, &loc);
    1525                 :             }
    1526            5621 :             ++cs;
    1527            5621 :             u1_ += digit << 4;
    1528            6007 :     do_str7:
    1529            6007 :             if(BOOST_JSON_UNLIKELY(! cs))
    1530             386 :                 return maybe_suspend(cs.begin(), state::str7);
    1531            5621 :             digit = detail::hex_digit(*cs);
    1532            5621 :             if(BOOST_JSON_UNLIKELY(digit == -1))
    1533                 :             {
    1534                 :                 BOOST_STATIC_CONSTEXPR source_location loc
    1535                 :                     = BOOST_CURRENT_LOCATION;
    1536              35 :                 return fail(cs.begin(), error::expected_hex_digit, &loc);
    1537                 :             }
    1538            5586 :             ++cs;
    1539            5586 :             u1_ += digit;
    1540            5586 :             if(BOOST_JSON_LIKELY(
    1541                 :                 u1_ < 0xd800 || u1_ > 0xdfff))
    1542                 :             {
    1543            1434 :                 BOOST_ASSERT(temp.empty());
    1544                 :                 // utf-8 codepoint
    1545            1434 :                 temp.append_utf8(u1_);
    1546            1434 :                 break;
    1547                 :             }
    1548            4152 :             if(BOOST_JSON_UNLIKELY(u1_ > 0xdbff))
    1549                 :             {
    1550                 :                 // If it's an illegal leading surrogate and
    1551                 :                 // the parser does not allow it, return an error.
    1552            1585 :                 if(!allow_bad_utf16)
    1553                 :                 {
    1554                 :                     BOOST_STATIC_CONSTEXPR source_location loc
    1555                 :                         = BOOST_CURRENT_LOCATION;
    1556             209 :                     return fail(cs.begin(), error::illegal_leading_surrogate, &loc);
    1557                 :                 }
    1558                 :                 // Otherwise, append the Unicode replacement character
    1559                 :                 else
    1560                 :                 {
    1561            1376 :                     BOOST_ASSERT(temp.empty());
    1562            1376 :                     temp.append_utf8(urc);
    1563            1376 :                     break;
    1564                 :                 }
    1565                 :             }
    1566            2567 :     do_sur1:
    1567            3792 :             if(BOOST_JSON_UNLIKELY(! cs))
    1568             232 :                 return maybe_suspend(cs.begin(), state::sur1);
    1569            3560 :             if(BOOST_JSON_UNLIKELY(*cs != '\\'))
    1570                 :             {
    1571                 :                 // If the next character is not a backslash and
    1572                 :                 // the parser does not allow it, return a syntax error.
    1573             952 :                 if(!allow_bad_utf16)
    1574                 :                 {
    1575                 :                     BOOST_STATIC_CONSTEXPR source_location loc
    1576                 :                         = BOOST_CURRENT_LOCATION;
    1577             149 :                     return fail(cs.begin(), error::syntax, &loc);
    1578                 :                 }
    1579                 :                 // Otherwise, append the Unicode replacement character since
    1580                 :                 // the first code point is a valid leading surrogate
    1581                 :                 else
    1582                 :                 {
    1583             803 :                     temp.append_utf8(urc);
    1584             803 :                     break;
    1585                 :                 }
    1586                 :             }
    1587            2608 :             ++cs;
    1588            2796 :     do_sur2:
    1589            2796 :             if(BOOST_JSON_UNLIKELY(! cs))
    1590             188 :                 return maybe_suspend(cs.begin(), state::sur2);
    1591            2608 :             if(BOOST_JSON_UNLIKELY(*cs != 'u'))
    1592                 :             {
    1593             396 :                 if (!allow_bad_utf16)
    1594                 :                 {
    1595                 :                     BOOST_STATIC_CONSTEXPR source_location loc
    1596                 :                         = BOOST_CURRENT_LOCATION;
    1597              65 :                     return fail(cs.begin(), error::syntax, &loc);
    1598                 :                 }
    1599                 :                 // Otherwise, append the Unicode replacement character since
    1600                 :                 // the first code point is a valid leading surrogate
    1601                 :                 else
    1602                 :                 {
    1603             331 :                     temp.append_utf8(urc);
    1604             331 :                     goto do_str3;
    1605                 :                 }
    1606                 :             }
    1607            2212 :             ++cs;
    1608            2376 :     do_sur3:
    1609            2376 :             if(BOOST_JSON_UNLIKELY(! cs))
    1610             164 :                 return maybe_suspend(cs.begin(), state::sur3);
    1611            2212 :             digit = detail::hex_digit(*cs);
    1612            2212 :             if(BOOST_JSON_UNLIKELY(digit == -1))
    1613                 :             {
    1614                 :                 BOOST_STATIC_CONSTEXPR source_location loc
    1615                 :                     = BOOST_CURRENT_LOCATION;
    1616              35 :                 return fail(cs.begin(), error::expected_hex_digit, &loc);
    1617                 :             }
    1618            2177 :             ++cs;
    1619            2177 :             u2_ = digit << 12;
    1620            2339 :     do_sur4:
    1621            2339 :             if(BOOST_JSON_UNLIKELY(! cs))
    1622             162 :                 return maybe_suspend(cs.begin(), state::sur4);
    1623            2177 :             digit = detail::hex_digit(*cs);
    1624            2177 :             if(BOOST_JSON_UNLIKELY(digit == -1))
    1625                 :             {
    1626                 :                 BOOST_STATIC_CONSTEXPR source_location loc
    1627                 :                     = BOOST_CURRENT_LOCATION;
    1628              35 :                 return fail(cs.begin(), error::expected_hex_digit, &loc);
    1629                 :             }
    1630            2142 :             ++cs;
    1631            2142 :             u2_ += digit << 8;
    1632            2302 :     do_sur5:
    1633            2302 :             if(BOOST_JSON_UNLIKELY(! cs))
    1634             160 :                 return maybe_suspend(cs.begin(), state::sur5);
    1635            2142 :             digit = detail::hex_digit(*cs);
    1636            2142 :             if(BOOST_JSON_UNLIKELY(digit == -1))
    1637                 :             {
    1638                 :                 BOOST_STATIC_CONSTEXPR source_location loc
    1639                 :                     = BOOST_CURRENT_LOCATION;
    1640              20 :                 return fail(cs.begin(), error::expected_hex_digit, &loc);
    1641                 :             }
    1642            2122 :             ++cs;
    1643            2122 :             u2_ += digit << 4;
    1644            2280 :     do_sur6:
    1645            2280 :             if(BOOST_JSON_UNLIKELY(! cs))
    1646             158 :                 return maybe_suspend(cs.begin(), state::sur6);
    1647            2122 :             digit = detail::hex_digit(*cs);
    1648            2122 :             if(BOOST_JSON_UNLIKELY(digit == -1))
    1649                 :             {
    1650                 :                 BOOST_STATIC_CONSTEXPR source_location loc
    1651                 :                     = BOOST_CURRENT_LOCATION;
    1652              20 :                 return fail(cs.begin(), error::expected_hex_digit, &loc);
    1653                 :             }
    1654            2102 :             ++cs;
    1655            2102 :             u2_ += digit;
    1656                 :             // Check if the second code point is a valid trailing surrogate.
    1657                 :             // Valid trailing surrogates are [DC00, DFFF]
    1658            2102 :             if(BOOST_JSON_UNLIKELY(
    1659                 :                 u2_ < 0xdc00 || u2_ > 0xdfff))
    1660                 :             {
    1661                 :                 // If not valid and the parser does not allow it, return an error.
    1662             580 :                 if(!allow_bad_utf16)
    1663                 :                 {
    1664                 :                     BOOST_STATIC_CONSTEXPR source_location loc
    1665                 :                         = BOOST_CURRENT_LOCATION;
    1666              60 :                     return fail(cs.begin(), error::illegal_trailing_surrogate, &loc);
    1667                 :                 }
    1668                 :                 // Append the replacement character for the
    1669                 :                 // first leading surrogate.
    1670             520 :                 temp.append_utf8(urc);
    1671                 :                 // Check if the second code point is a
    1672                 :                 // valid unicode scalar value (invalid leading
    1673                 :                 // or trailing surrogate)
    1674             520 :                 if (u2_ < 0xd800 || u2_ > 0xdbff)
    1675                 :                 {
    1676             220 :                     temp.append_utf8(u2_);
    1677             220 :                     break;
    1678                 :                 }
    1679                 :                 // If it is a valid leading surrogate
    1680                 :                 else
    1681                 :                 {
    1682             300 :                     u1_ = u2_;
    1683             300 :                     goto do_sur1;
    1684                 :                 }
    1685                 :             }
    1686                 :             // Calculate the Unicode code point from the surrogate pair and
    1687                 :             // append the UTF-8 representation.
    1688            1522 :             unsigned cp =
    1689            1522 :                 ((u1_ - 0xd800) << 10) +
    1690            1522 :                 ((u2_ - 0xdc00)) +
    1691                 :                     0x10000;
    1692                 :             // utf-16 surrogate pair
    1693            1522 :             temp.append_utf8(cp);
    1694                 :         }
    1695                 : 
    1696                 :         // flush
    1697           12653 :         if(BOOST_JSON_UNLIKELY( !cs ) || *cs != '\\')
    1698            8870 :             break;
    1699                 :     }
    1700                 : 
    1701            8870 :     if(BOOST_JSON_LIKELY( temp.size() ))
    1702                 :     {
    1703             434 :         BOOST_ASSERT(total <= max_size);
    1704            8870 :         if(BOOST_JSON_UNLIKELY( temp.size() > max_size - total ))
    1705                 :         {
    1706                 :             BOOST_STATIC_CONSTEXPR source_location loc
    1707                 :                 = BOOST_CURRENT_LOCATION;
    1708               1 :             return fail(cs.begin(), ev_too_large, &loc);
    1709                 :         }
    1710                 : 
    1711            8869 :         total += temp.size();
    1712            8056 :         bool const r = is_key
    1713            8869 :             ? h_.on_key_part(temp.get(), total, ec_)
    1714            8152 :             : h_.on_string_part(temp.get(), total, ec_);
    1715            8056 :         if(BOOST_JSON_UNLIKELY( !r ))
    1716             813 :             return fail( cs.begin() );
    1717                 :     }
    1718                 : 
    1719            7243 :     return cs.begin();
    1720                 : }
    1721                 : 
    1722                 : //----------------------------------------------------------
    1723                 : 
    1724                 : template<class Handler>
    1725                 : template<
    1726                 :     bool StackEmpty_,
    1727                 :     bool AllowComments_/*,
    1728                 :     bool AllowTrailing_,
    1729                 :     bool AllowBadUTF8_*/>
    1730                 : const char*
    1731          109212 : basic_parser<Handler>::
    1732                 : parse_object(const char* p,
    1733                 :     std::integral_constant<bool, StackEmpty_> stack_empty,
    1734                 :     std::integral_constant<bool, AllowComments_> allow_comments,
    1735                 :     /*std::integral_constant<bool, AllowTrailing_>*/ bool allow_trailing,
    1736                 :     /*std::integral_constant<bool, AllowBadUTF8_>*/ bool allow_bad_utf8,
    1737                 :     bool allow_bad_utf16)
    1738                 : {
    1739          109212 :     detail::const_stream_wrapper cs(p, end_);
    1740                 :     std::size_t size;
    1741          109212 :     if(! stack_empty && ! st_.empty())
    1742                 :     {
    1743                 :         // resume
    1744                 :         state st;
    1745           35074 :         st_.pop(st);
    1746           35074 :         st_.pop(size);
    1747           35074 :         switch(st)
    1748                 :         {
    1749                 :         default: BOOST_JSON_UNREACHABLE(); // LCOV_EXCL_LINE
    1750            1596 :         case state::obj1: goto do_obj1;
    1751             235 :         case state::obj2: goto do_obj2;
    1752           12651 :         case state::obj3: goto do_obj3;
    1753            1692 :         case state::obj4: goto do_obj4;
    1754             251 :         case state::obj5: goto do_obj5;
    1755            1593 :         case state::obj6: goto do_obj6;
    1756           15453 :         case state::obj7: goto do_obj7;
    1757             427 :         case state::obj8: goto do_obj8;
    1758             661 :         case state::obj9: goto do_obj9;
    1759             181 :         case state::obj10: goto do_obj10;
    1760             334 :         case state::obj11: goto do_obj11;
    1761                 :         }
    1762                 :     }
    1763           74138 :     BOOST_ASSERT(*cs == '{');
    1764           74138 :     size = 0;
    1765           74138 :     if(BOOST_JSON_UNLIKELY(! depth_))
    1766                 :     {
    1767                 :         BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
    1768               3 :         return fail(cs.begin(), error::too_deep, &loc);
    1769                 :     }
    1770           74135 :     --depth_;
    1771           74135 :     if(BOOST_JSON_UNLIKELY(
    1772                 :         ! h_.on_object_begin(ec_)))
    1773            2040 :         return fail(cs.begin());
    1774           70057 :     ++cs;
    1775                 :     // object:
    1776                 :     //     '{' *ws '}'
    1777                 :     //     '{' *ws string *ws ':' *ws value *ws *[ ',' *ws string *ws ':' *ws value *ws ] '}'
    1778           73624 : do_obj1:
    1779           73624 :     cs = detail::count_whitespace(cs.begin(), cs.end());
    1780           73624 :     if(BOOST_JSON_UNLIKELY(! cs))
    1781            1630 :         return maybe_suspend(cs.begin(), state::obj1, size);
    1782           71994 :     if(BOOST_JSON_LIKELY(*cs != '}'))
    1783                 :     {
    1784           69095 :         if(BOOST_JSON_UNLIKELY(*cs != '\x22'))
    1785                 :         {
    1786            2411 :             if(allow_comments && *cs == '/')
    1787                 :             {
    1788            2139 : do_obj2:
    1789            2374 :                 cs = parse_comment(cs.begin(), stack_empty, std::false_type());
    1790            2290 :                 if(BOOST_JSON_UNLIKELY(incomplete(cs)))
    1791             319 :                     return suspend_or_fail(state::obj2, size);
    1792            1971 :                 goto do_obj1;
    1793                 :             }
    1794                 :             BOOST_STATIC_CONSTEXPR source_location loc
    1795                 :                 = BOOST_CURRENT_LOCATION;
    1796             272 :             return fail(cs.begin(), error::syntax, &loc);
    1797                 :         }
    1798           66684 : loop:
    1799           80843 :         if(BOOST_JSON_UNLIKELY(++size >
    1800                 :             Handler::max_object_size))
    1801                 :         {
    1802                 :             BOOST_STATIC_CONSTEXPR source_location loc
    1803                 :                 = BOOST_CURRENT_LOCATION;
    1804               1 :             return fail(cs.begin(), error::object_too_large, &loc);
    1805                 :         }
    1806           80842 : do_obj3:
    1807           93493 :         cs = parse_string(cs.begin(), stack_empty, std::true_type(), allow_bad_utf8, allow_bad_utf16);
    1808           90537 :         if(BOOST_JSON_UNLIKELY(incomplete(cs)))
    1809           15596 :             return suspend_or_fail(state::obj3, size);
    1810           74941 : do_obj4:
    1811           79104 :         cs = detail::count_whitespace(cs.begin(), cs.end());
    1812           79104 :         if(BOOST_JSON_UNLIKELY(! cs))
    1813            1707 :             return maybe_suspend(cs.begin(), state::obj4, size);
    1814           77397 :         if(BOOST_JSON_UNLIKELY(*cs != ':'))
    1815                 :         {
    1816            2925 :             if(allow_comments && *cs == '/')
    1817                 :             {
    1818            2779 : do_obj5:
    1819            3030 :                 cs = parse_comment(cs.begin(), stack_empty, std::false_type());
    1820            2876 :                 if(BOOST_JSON_UNLIKELY(incomplete(cs)))
    1821             405 :                     return suspend_or_fail(state::obj5, size);
    1822            2471 :                 goto do_obj4;
    1823                 :             }
    1824                 :             BOOST_STATIC_CONSTEXPR source_location loc
    1825                 :                 = BOOST_CURRENT_LOCATION;
    1826             146 :             return fail(cs.begin(), error::syntax, &loc);
    1827                 :         }
    1828           74472 :         ++cs;
    1829           76065 : do_obj6:
    1830           76065 :         cs = detail::count_whitespace(cs.begin(), cs.end());
    1831           76065 :         if(BOOST_JSON_UNLIKELY(! cs))
    1832            1624 :             return maybe_suspend(cs.begin(), state::obj6, size);
    1833           74441 : do_obj7:
    1834           89894 :         cs = parse_value(cs.begin(), stack_empty, allow_comments, allow_trailing, allow_bad_utf8, allow_bad_utf16);
    1835           82545 :         if(BOOST_JSON_UNLIKELY(incomplete(cs)))
    1836           23598 :             return suspend_or_fail(state::obj7, size);
    1837           58947 : do_obj8:
    1838           61210 :         cs = detail::count_whitespace(cs.begin(), cs.end());
    1839           61210 :         if(BOOST_JSON_UNLIKELY(! cs))
    1840             442 :             return maybe_suspend(cs.begin(), state::obj8, size);
    1841           60768 :         if(BOOST_JSON_LIKELY(*cs == ','))
    1842                 :         {
    1843           17799 :             ++cs;
    1844           19715 : do_obj9:
    1845           19715 :             cs = detail::count_whitespace(cs.begin(), cs.end());
    1846           19715 :             if(BOOST_JSON_UNLIKELY(! cs))
    1847             691 :                 return maybe_suspend(cs.begin(), state::obj9, size);
    1848                 : 
    1849                 :             // loop for next element
    1850           19024 :             if(BOOST_JSON_LIKELY(*cs == '\x22'))
    1851           14159 :                 goto loop;
    1852            4865 :             if(! allow_trailing || *cs != '}')
    1853                 :             {
    1854            1644 :                 if(allow_comments && *cs == '/')
    1855                 :                 {
    1856            1433 : do_obj10:
    1857            1614 :                     cs = parse_comment(cs.begin(), stack_empty, std::false_type());
    1858            1525 :                     if(BOOST_JSON_UNLIKELY(incomplete(cs)))
    1859             270 :                         return suspend_or_fail(state::obj10, size);
    1860            1255 :                     goto do_obj9;
    1861                 :                 }
    1862                 :                 BOOST_STATIC_CONSTEXPR source_location loc
    1863                 :                     = BOOST_CURRENT_LOCATION;
    1864             211 :                 return fail(cs.begin(), error::syntax, &loc);
    1865                 :             }
    1866                 :         }
    1867           42969 :         else if(BOOST_JSON_UNLIKELY(*cs != '}'))
    1868                 :         {
    1869            2325 :             if(allow_comments && *cs == '/')
    1870                 :             {
    1871            2172 : do_obj11:
    1872            2506 :                 cs = parse_comment(cs.begin(), stack_empty, std::false_type());
    1873            2338 :                 if(BOOST_JSON_UNLIKELY(incomplete(cs)))
    1874             502 :                     return suspend_or_fail(state::obj11, size);
    1875            1836 :                 goto do_obj8;
    1876                 :             }
    1877                 :             BOOST_STATIC_CONSTEXPR source_location loc
    1878                 :                 = BOOST_CURRENT_LOCATION;
    1879             153 :             return fail(cs.begin(), error::syntax, &loc);
    1880                 :         }
    1881                 :         // got closing brace, fall through
    1882                 :     }
    1883           46764 :     if(BOOST_JSON_UNLIKELY(
    1884                 :         ! h_.on_object_end(size, ec_)))
    1885            1502 :         return fail(cs.begin());
    1886           43721 :     ++depth_;
    1887           43721 :     ++cs;
    1888           43721 :     return cs.begin();
    1889                 : }
    1890                 : 
    1891                 : //----------------------------------------------------------
    1892                 : 
    1893                 : template<class Handler>
    1894                 : template<
    1895                 :     bool StackEmpty_,
    1896                 :     bool AllowComments_/*,
    1897                 :     bool AllowTrailing_,
    1898                 :     bool AllowBadUTF8_*/>
    1899                 : const char*
    1900           26457 : basic_parser<Handler>::
    1901                 : parse_array(const char* p,
    1902                 :     std::integral_constant<bool, StackEmpty_> stack_empty,
    1903                 :     std::integral_constant<bool, AllowComments_> allow_comments,
    1904                 :     /*std::integral_constant<bool, AllowTrailing_>*/ bool allow_trailing,
    1905                 :     /*std::integral_constant<bool, AllowBadUTF8_>*/ bool allow_bad_utf8,
    1906                 :     bool allow_bad_utf16)
    1907                 : {
    1908           26457 :     detail::const_stream_wrapper cs(p, end_);
    1909                 :     std::size_t size;
    1910           26457 :     if(! stack_empty && ! st_.empty())
    1911                 :     {
    1912                 :         // resume
    1913                 :         state st;
    1914            5780 :         st_.pop(st);
    1915            5780 :         st_.pop(size);
    1916            5780 :         switch(st)
    1917                 :         {
    1918                 :         default: BOOST_JSON_UNREACHABLE(); // LCOV_EXCL_LINE
    1919            1061 :         case state::arr1: goto do_arr1;
    1920             384 :         case state::arr2: goto do_arr2;
    1921            2965 :         case state::arr3: goto do_arr3;
    1922             396 :         case state::arr4: goto do_arr4;
    1923             680 :         case state::arr5: goto do_arr5;
    1924             294 :         case state::arr6: goto do_arr6;
    1925                 :         }
    1926                 :     }
    1927           20677 :     BOOST_ASSERT(*cs == '[');
    1928           20677 :     size = 0;
    1929           20677 :     if(BOOST_JSON_UNLIKELY(! depth_))
    1930                 :     {
    1931                 :         BOOST_STATIC_CONSTEXPR source_location loc = BOOST_CURRENT_LOCATION;
    1932              34 :         return fail(cs.begin(), error::too_deep, &loc);
    1933                 :     }
    1934           20643 :     --depth_;
    1935           20643 :     if(BOOST_JSON_UNLIKELY(
    1936                 :         ! h_.on_array_begin(ec_)))
    1937             812 :         return fail(cs.begin());
    1938           19025 :     ++cs;
    1939                 :     // array:
    1940                 :     //     '[' *ws ']'
    1941                 :     //     '[' *ws value *ws *[ ',' *ws value *ws ] ']'
    1942           21619 : do_arr1:
    1943           21619 :     cs = detail::count_whitespace(cs.begin(), cs.end());
    1944           21619 :     if(BOOST_JSON_UNLIKELY(! cs))
    1945            1082 :         return maybe_suspend(cs.begin(), state::arr1, size);
    1946           20537 :     if(BOOST_JSON_LIKELY(*cs != ']'))
    1947                 :     {
    1948           18782 : loop:
    1949           26305 :         if(allow_comments && *cs == '/')
    1950                 :         {
    1951            1789 : do_arr2:
    1952            2173 :             cs = parse_comment(cs.begin(), stack_empty, std::false_type());
    1953            2045 :             if(BOOST_JSON_UNLIKELY(incomplete(cs)))
    1954             512 :                 return suspend_or_fail(state::arr2, size);
    1955            1533 :             goto do_arr1;
    1956                 :         }
    1957           24516 :         if(BOOST_JSON_UNLIKELY(++size >
    1958                 :             Handler::max_array_size))
    1959                 :         {
    1960                 :             BOOST_STATIC_CONSTEXPR source_location loc
    1961                 :                 = BOOST_CURRENT_LOCATION;
    1962               1 :             return fail(cs.begin(), error::array_too_large, &loc);
    1963                 :         }
    1964           24515 : do_arr3:
    1965                 :         // array is not empty, value required
    1966           27480 :         cs = parse_value(cs.begin(), stack_empty, allow_comments, allow_trailing, allow_bad_utf8, allow_bad_utf16);
    1967           24849 :         if(BOOST_JSON_UNLIKELY(incomplete(cs)))
    1968            9079 :             return suspend_or_fail(state::arr3, size);
    1969           15770 : do_arr4:
    1970           17396 :         cs = detail::count_whitespace(cs.begin(), cs.end());
    1971           17396 :         if(BOOST_JSON_UNLIKELY(! cs))
    1972             505 :             return maybe_suspend(cs.begin(), state::arr4, size);
    1973           16891 :         if(BOOST_JSON_LIKELY(*cs == ','))
    1974                 :         {
    1975            9236 :             ++cs;
    1976            9916 : do_arr5:
    1977            9916 :             cs = detail::count_whitespace(cs.begin(), cs.end());
    1978            9916 :             if(BOOST_JSON_UNLIKELY(! cs))
    1979             710 :                 return maybe_suspend(cs.begin(), state::arr5, size);
    1980                 :             // loop for next element
    1981            9206 :             if(! allow_trailing || *cs != ']')
    1982            7523 :                 goto loop;
    1983                 :         }
    1984            7655 :         else if(BOOST_JSON_UNLIKELY(*cs != ']'))
    1985                 :         {
    1986            1969 :             if(allow_comments && *cs == '/')
    1987                 :             {
    1988            1541 : do_arr6:
    1989            1835 :                 cs = parse_comment(cs.begin(), stack_empty, std::false_type());
    1990            1688 :                 if(BOOST_JSON_UNLIKELY(incomplete(cs)))
    1991             458 :                     return suspend_or_fail(state::arr6, size);
    1992            1230 :                 goto do_arr4;
    1993                 :             }
    1994                 :             BOOST_STATIC_CONSTEXPR source_location loc
    1995                 :                 = BOOST_CURRENT_LOCATION;
    1996             428 :             return fail(cs.begin(), error::syntax, &loc);
    1997                 :         }
    1998                 :         // got closing bracket; fall through
    1999                 :     }
    2000            9124 :     if(BOOST_JSON_UNLIKELY(
    2001                 :         ! h_.on_array_end(size, ec_)))
    2002             549 :         return fail(cs.begin());
    2003            7996 :     ++depth_;
    2004            7996 :     ++cs;
    2005            7996 :     return cs.begin();
    2006                 : }
    2007                 : 
    2008                 : //----------------------------------------------------------
    2009                 : 
    2010                 : template<class Handler>
    2011                 : template<bool StackEmpty_, char First_, number_precision Numbers_>
    2012                 : const char*
    2013        10516087 : basic_parser<Handler>::
    2014                 : parse_number(const char* p,
    2015                 :     std::integral_constant<bool, StackEmpty_> stack_empty,
    2016                 :     std::integral_constant<char, First_> first,
    2017                 :     std::integral_constant<number_precision, Numbers_> mode)
    2018                 : {
    2019        10516087 :     constexpr bool precise_parsing = mode == number_precision::precise;
    2020        10516087 :     constexpr bool no_parsing = mode == number_precision::none;
    2021                 : 
    2022                 :     // only one of these will be true if we are not resuming
    2023                 :     // if negative then !zero_first && !nonzero_first
    2024                 :     // if zero_first then !nonzero_first && !negative
    2025                 :     // if nonzero_first then !zero_first && !negative
    2026        10516087 :     bool const negative = first == '-';
    2027        10516087 :     bool const zero_first = first == '0';
    2028        10516087 :     bool const nonzero_first = first == '+';
    2029        10516087 :     detail::const_stream_wrapper cs(p, end_);
    2030                 :     number num;
    2031        10516087 :     const char* begin = cs.begin();
    2032        10516087 :     if(stack_empty || st_.empty())
    2033                 :     {
    2034         2090055 :         num.bias = 0;
    2035         2090055 :         num.exp = 0;
    2036         2090055 :         num.frac = false;
    2037         2090055 :         num_buf_.clear();
    2038                 : 
    2039                 :         //----------------------------------
    2040                 :         //
    2041                 :         // '-'
    2042                 :         // leading minus sign
    2043                 :         //
    2044         2090055 :         BOOST_ASSERT(cs);
    2045                 :         if(negative)
    2046           25185 :             ++cs;
    2047                 : 
    2048         2090055 :         num.neg = negative;
    2049         2090055 :         num.frac = false;
    2050         2090055 :         num.exp = 0;
    2051         2090055 :         num.bias = 0;
    2052                 : 
    2053                 :         // fast path
    2054         2090055 :         if( cs.remain() >= 16 + 1 + 16 ) // digits . digits
    2055                 :         {
    2056                 :             int n1;
    2057                 : 
    2058            9989 :             if( nonzero_first ||
    2059            9989 :                 (negative && *cs != '0') )
    2060                 :             {
    2061         2007319 :                 n1 = detail::count_digits( cs.begin() );
    2062         2007319 :                 BOOST_ASSERT(n1 >= 0 && n1 <= 16);
    2063                 : 
    2064            1837 :                 if( negative && n1 == 0 && opt_.allow_infinity_and_nan )
    2065                 :                 {
    2066               9 :                     return parse_literal(
    2067                 :                         p - 1,
    2068               8 :                         detail::literals_c<detail::literals::neg_infinity>());
    2069                 :                 }
    2070                 : 
    2071            1828 :                 if( ! nonzero_first && n1 == 0 )
    2072                 :                 {
    2073                 :                     // digit required
    2074                 :                     BOOST_STATIC_CONSTEXPR source_location loc
    2075                 :                         = BOOST_CURRENT_LOCATION;
    2076               2 :                     return fail(cs.begin(), error::syntax, &loc);
    2077                 :                 }
    2078                 : 
    2079                 :                 BOOST_IF_CONSTEXPR( !no_parsing )
    2080         2006460 :                     num.mant = detail::parse_unsigned( 0, cs.begin(), n1 );
    2081                 :                 else
    2082             848 :                     num.mant = 0;
    2083                 : 
    2084         2007308 :                 cs += n1;
    2085                 : 
    2086                 :                 // integer or floating-point with
    2087                 :                 // >= 16 leading digits
    2088         2007308 :                 if( n1 == 16 )
    2089                 :                 {
    2090         2001425 :                     goto do_num2;
    2091                 :                 }
    2092                 :             }
    2093                 :             else
    2094                 :             {
    2095                 :                 // 0. floating-point or 0e integer
    2096           21187 :                 num.mant = 0;
    2097           21187 :                 n1 = 0;
    2098           21187 :                 ++cs;
    2099                 :             }
    2100                 : 
    2101                 :             {
    2102           27070 :                 const char c = *cs;
    2103           27070 :                 if(c != '.')
    2104                 :                 {
    2105            9871 :                     if((c | 32) == 'e')
    2106                 :                     {
    2107            6576 :                         ++cs;
    2108            6576 :                         goto do_exp1;
    2109                 :                     }
    2110                 :                     BOOST_IF_CONSTEXPR( negative && !no_parsing )
    2111              20 :                         num.mant = ~num.mant + 1;
    2112            3295 :                     goto finish_signed;
    2113                 :                 }
    2114                 :             }
    2115                 : 
    2116                 :             // floating-point number
    2117                 : 
    2118           17199 :             ++cs;
    2119                 : 
    2120           17199 :             int n2 = detail::count_digits( cs.begin() );
    2121           17199 :             BOOST_ASSERT(n2 >= 0 && n2 <= 16);
    2122                 : 
    2123           17199 :             if( n2 == 0 )
    2124                 :             {
    2125                 :                 // digit required
    2126                 :                 BOOST_STATIC_CONSTEXPR source_location loc
    2127                 :                     = BOOST_CURRENT_LOCATION;
    2128               3 :                 return fail(cs.begin(), error::syntax, &loc);
    2129                 :             }
    2130                 : 
    2131                 :             // floating-point mantissa overflow
    2132           17196 :             if( n1 + n2 >= 19 )
    2133                 :             {
    2134             122 :                 goto do_num7;
    2135                 :             }
    2136                 : 
    2137                 :             BOOST_IF_CONSTEXPR( !no_parsing )
    2138           12855 :                 num.mant = detail::parse_unsigned( num.mant, cs.begin(), n2 );
    2139                 : 
    2140           17074 :             BOOST_ASSERT(num.bias == 0);
    2141                 : 
    2142           17074 :             num.bias -= n2;
    2143                 : 
    2144           17074 :             cs += n2;
    2145                 : 
    2146           17074 :             char ch = *cs;
    2147                 : 
    2148           17074 :             if( (ch | 32) == 'e' )
    2149                 :             {
    2150             110 :                 ++cs;
    2151             110 :                 goto do_exp1;
    2152                 :             }
    2153           16964 :             else if( ch >= '0' && ch <= '9' )
    2154                 :             {
    2155           10017 :                 goto do_num8;
    2156                 :             }
    2157                 : 
    2158            6947 :             goto finish_dub;
    2159                 :         }
    2160                 :     }
    2161                 :     else
    2162                 :     {
    2163         8426032 :         num = num_;
    2164                 :         state st;
    2165         8426032 :         st_.pop(st);
    2166         8426032 :         switch(st)
    2167                 :         {
    2168                 :         default: BOOST_JSON_UNREACHABLE(); // LCOV_EXCL_LINE
    2169             603 :         case state::num1: goto do_num1;
    2170            6473 :         case state::num2: goto do_num2;
    2171         4195112 :         case state::num3: goto do_num3;
    2172              52 :         case state::num4: goto do_num4;
    2173            4537 :         case state::num5: goto do_num5;
    2174             668 :         case state::num6: goto do_num6;
    2175             618 :         case state::num7: goto do_num7;
    2176         4205248 :         case state::num8: goto do_num8;
    2177             477 :         case state::exp1: goto do_exp1;
    2178             139 :         case state::exp2: goto do_exp2;
    2179           12105 :         case state::exp3: goto do_exp3;
    2180                 :         }
    2181                 :     }
    2182                 : 
    2183                 :     //----------------------------------
    2184                 :     //
    2185                 :     // DIGIT
    2186                 :     // first digit
    2187                 :     //
    2188           62152 : do_num1:
    2189           15799 :     if(zero_first || nonzero_first ||
    2190           15799 :         BOOST_JSON_LIKELY(cs))
    2191                 :     {
    2192           61429 :         char const c = *cs;
    2193                 :         if(zero_first)
    2194                 :         {
    2195            9726 :             ++cs;
    2196            9726 :             num.mant = 0;
    2197            9726 :             goto do_num6;
    2198                 :         }
    2199           15076 :         else if(nonzero_first || BOOST_JSON_LIKELY(
    2200                 :             c >= '1' && c <= '9'))
    2201                 :         {
    2202           42874 :             ++cs;
    2203           42874 :             num.mant = c - '0';
    2204                 :         }
    2205            8829 :         else if(BOOST_JSON_UNLIKELY(
    2206                 :             c == '0'))
    2207                 :         {
    2208            7634 :             ++cs;
    2209            7634 :             num.mant = 0;
    2210            7634 :             goto do_num6;
    2211                 :         }
    2212            1195 :         else if( (negative || num.neg) && opt_.allow_infinity_and_nan )
    2213                 :         {
    2214            1007 :             st_.push(state::lit1);
    2215            1007 :             cur_lit_ = literal_index(detail::literals::neg_infinity);
    2216            1007 :             lit_offset_ = 1;
    2217            1007 :             return parse_literal(
    2218             961 :                 cs.begin(), detail::literals_c<detail::literals::resume>() );
    2219                 :         }
    2220                 :         else
    2221                 :         {
    2222                 :             BOOST_STATIC_CONSTEXPR source_location loc
    2223                 :                 = BOOST_CURRENT_LOCATION;
    2224             188 :             return fail(cs.begin(), error::syntax, &loc);
    2225                 :         }
    2226                 :     }
    2227                 :     else
    2228                 :     {
    2229             723 :         if(BOOST_JSON_UNLIKELY(
    2230                 :             ! h_.on_number_part(
    2231                 :                 {begin, cs.used(begin)}, ec_)))
    2232              60 :             return fail(cs.begin());
    2233                 : 
    2234                 :         BOOST_IF_CONSTEXPR( precise_parsing )
    2235 MIS           0 :             num_buf_.append( begin, cs.used(begin) );
    2236 HIT         603 :         return maybe_suspend(
    2237             603 :             cs.begin(), state::num1, num);
    2238                 :     }
    2239                 : 
    2240                 :     //----------------------------------
    2241                 :     //
    2242                 :     // 1*DIGIT
    2243                 :     // significant digits left of decimal
    2244                 :     //
    2245         2050772 : do_num2:
    2246         2044514 :     if(negative || (!stack_empty && num.neg))
    2247                 :     {
    2248           22400 :         for(;;)
    2249                 :         {
    2250           30225 :             if(BOOST_JSON_UNLIKELY(! cs))
    2251                 :             {
    2252            1921 :                 if(BOOST_JSON_UNLIKELY(more_))
    2253                 :                 {
    2254            1469 :                     if(BOOST_JSON_UNLIKELY(
    2255                 :                         ! h_.on_number_part(
    2256                 :                             {begin, cs.used(begin)}, ec_)))
    2257              69 :                         return fail(cs.begin());
    2258                 : 
    2259                 :                     BOOST_IF_CONSTEXPR( precise_parsing )
    2260 MIS           0 :                         num_buf_.append( begin, cs.used(begin) );
    2261 HIT        1331 :                     return suspend(cs.begin(), state::num2, num);
    2262                 :                 }
    2263             452 :                 goto finish_int;
    2264                 :             }
    2265           28304 :             char const c = *cs;
    2266           28304 :             if(BOOST_JSON_LIKELY(
    2267                 :                 c >= '0' && c <= '9'))
    2268                 :             {
    2269           23223 :                 ++cs;
    2270                 :                 //              9223372036854775808 INT64_MIN
    2271           23223 :                 if( num.mant  > 922337203685477580 || (
    2272           22508 :                     num.mant == 922337203685477580 && c > '8'))
    2273                 :                     break;
    2274                 :                 BOOST_IF_CONSTEXPR( !no_parsing )
    2275           22141 :                     num.mant = 10 * num.mant + ( c - '0' );
    2276           22400 :                 continue;
    2277                 :             }
    2278            5081 :             goto do_num6; // [.eE]
    2279                 :         }
    2280                 :     }
    2281                 :     else
    2282                 :     {
    2283         6889563 :         for(;;)
    2284                 :         {
    2285         8932510 :             if(BOOST_JSON_UNLIKELY(! cs))
    2286                 :             {
    2287            6601 :                 if(BOOST_JSON_UNLIKELY(more_))
    2288                 :                 {
    2289            5988 :                     if(BOOST_JSON_UNLIKELY(
    2290                 :                         ! h_.on_number_part(
    2291                 :                             {begin, cs.used(begin)}, ec_)))
    2292             426 :                         return fail(cs.begin());
    2293                 : 
    2294                 :                     BOOST_IF_CONSTEXPR( precise_parsing )
    2295 MIS           0 :                         num_buf_.append( begin, cs.used(begin) );
    2296 HIT        5142 :                     return suspend(cs.begin(), state::num2, num);
    2297                 :                 }
    2298             613 :                 goto finish_int;
    2299                 :             }
    2300         8925909 :             char const c = *cs;
    2301         8925909 :             if(BOOST_JSON_LIKELY(
    2302                 :                 c >= '0' && c <= '9'))
    2303                 :             {
    2304         6893607 :                 ++cs;
    2305                 :                 //              18446744073709551615 UINT64_MAX
    2306         6893607 :                 if( num.mant  > 1844674407370955161 || (
    2307         6889985 :                     num.mant == 1844674407370955161 && c > '5'))
    2308                 :                     break;
    2309                 :                 BOOST_IF_CONSTEXPR( !no_parsing )
    2310         6883965 :                     num.mant = 10 * num.mant + ( c - '0' );
    2311                 :             }
    2312                 :             else
    2313                 :             {
    2314         2032302 :                 goto do_num6; // [.eE]
    2315                 :             }
    2316                 :         }
    2317                 :     }
    2318            4867 :     ++num.bias;
    2319                 : 
    2320                 :     //----------------------------------
    2321                 :     //
    2322                 :     // 1*DIGIT
    2323                 :     // non-significant digits left of decimal
    2324                 :     //
    2325         4199979 : do_num3:
    2326      4294978560 :     for(;;)
    2327                 :     {
    2328      4299178539 :         if(BOOST_JSON_UNLIKELY(! cs))
    2329                 :         {
    2330         4195839 :             if(BOOST_JSON_UNLIKELY(more_))
    2331                 :             {
    2332         4195206 :                 if(BOOST_JSON_UNLIKELY(
    2333                 :                     ! h_.on_number_part(
    2334                 :                         {begin, cs.used(begin)}, ec_)))
    2335              47 :                     return fail(cs.begin());
    2336                 : 
    2337                 :                 BOOST_IF_CONSTEXPR( precise_parsing )
    2338 MIS           0 :                     num_buf_.append( begin, cs.used(begin) );
    2339 HIT     4195112 :                 return suspend(cs.begin(), state::num3, num);
    2340                 :             }
    2341             633 :             goto finish_dub;
    2342                 :         }
    2343      4294982700 :         char const c = *cs;
    2344      4294982700 :         if(BOOST_JSON_UNLIKELY(
    2345                 :             c >= '0' && c <= '9'))
    2346                 :         {
    2347      4294978561 :             if(BOOST_JSON_UNLIKELY( num.bias + 1 == INT_MAX ))
    2348                 :             {
    2349                 :                 BOOST_STATIC_CONSTEXPR source_location loc
    2350                 :                     = BOOST_CURRENT_LOCATION;
    2351               1 :                 return fail(cs.begin(), error::exponent_overflow, &loc);
    2352                 :             }
    2353      4294978560 :             ++cs;
    2354      4294978560 :             ++num.bias;
    2355                 :         }
    2356            4139 :         else if(BOOST_JSON_LIKELY(
    2357                 :             c == '.'))
    2358                 :         {
    2359            2028 :             ++cs;
    2360            2028 :             break;
    2361                 :         }
    2362            2111 :         else if((c | 32) == 'e')
    2363                 :         {
    2364             762 :             ++cs;
    2365             762 :             goto do_exp1;
    2366                 :         }
    2367                 :         else
    2368                 :         {
    2369            1349 :             goto finish_dub;
    2370                 :         }
    2371                 :     }
    2372                 : 
    2373                 :     //----------------------------------
    2374                 :     //
    2375                 :     // DIGIT
    2376                 :     // first non-significant digit
    2377                 :     // to the right of decimal
    2378                 :     //
    2379            2080 : do_num4:
    2380                 :     {
    2381            2080 :         if(BOOST_JSON_UNLIKELY(! cs))
    2382                 :         {
    2383              64 :             if(BOOST_JSON_UNLIKELY(
    2384                 :                 ! h_.on_number_part(
    2385                 :                     {begin, cs.used(begin)}, ec_)))
    2386               6 :                 return fail(cs.begin());
    2387                 : 
    2388                 :             BOOST_IF_CONSTEXPR( precise_parsing )
    2389 MIS           0 :                 num_buf_.append( begin, cs.used(begin) );
    2390 HIT          52 :             return maybe_suspend(
    2391              52 :                 cs.begin(), state::num4, num);
    2392                 :         }
    2393            2016 :         char const c = *cs;
    2394            2016 :         if(BOOST_JSON_LIKELY(
    2395                 :             //static_cast<unsigned char>(c - '0') < 10))
    2396                 :             c >= '0' && c <= '9'))
    2397                 :         {
    2398            1949 :             ++cs;
    2399                 :         }
    2400                 :         else
    2401                 :         {
    2402                 :             // digit required
    2403                 :             BOOST_STATIC_CONSTEXPR source_location loc
    2404                 :                 = BOOST_CURRENT_LOCATION;
    2405              67 :             return fail(cs.begin(), error::syntax, &loc);
    2406                 :         }
    2407                 :     }
    2408                 : 
    2409                 :     //----------------------------------
    2410                 :     //
    2411                 :     // 1*DIGIT
    2412                 :     // non-significant digits
    2413                 :     // to the right of decimal
    2414                 :     //
    2415         2013470 : do_num5:
    2416        37889832 :     for(;;)
    2417                 :     {
    2418        39903302 :         if(BOOST_JSON_UNLIKELY(! cs))
    2419                 :         {
    2420            6112 :             if(BOOST_JSON_UNLIKELY(more_))
    2421                 :             {
    2422            4577 :                 if(BOOST_JSON_UNLIKELY(
    2423                 :                     ! h_.on_number_part(
    2424                 :                         {begin, cs.used(begin)}, ec_)))
    2425              20 :                     return fail(cs.begin());
    2426                 : 
    2427                 :                 BOOST_IF_CONSTEXPR( precise_parsing )
    2428 MIS           0 :                     num_buf_.append( begin, cs.used(begin) );
    2429 HIT        4537 :                 return suspend(cs.begin(), state::num5, num);
    2430                 :             }
    2431            1535 :             goto finish_dub;
    2432                 :         }
    2433        39897190 :         char const c = *cs;
    2434        39897190 :         if(BOOST_JSON_LIKELY(
    2435                 :             c >= '0' && c <= '9'))
    2436                 :         {
    2437        37889832 :             ++cs;
    2438                 :         }
    2439         2007358 :         else if((c | 32) == 'e')
    2440                 :         {
    2441         2003236 :             ++cs;
    2442         2003236 :             goto do_exp1;
    2443                 :         }
    2444                 :         else
    2445                 :         {
    2446            4122 :             goto finish_dub;
    2447                 :         }
    2448                 :     }
    2449                 : 
    2450                 :     //----------------------------------
    2451                 :     //
    2452                 :     // [.eE]
    2453                 :     //
    2454         2055411 : do_num6:
    2455                 :     {
    2456         2055411 :         if(BOOST_JSON_UNLIKELY(! cs))
    2457                 :         {
    2458             800 :             if(BOOST_JSON_UNLIKELY(more_))
    2459                 :             {
    2460             753 :                 if(BOOST_JSON_UNLIKELY(
    2461                 :                     ! h_.on_number_part(
    2462                 :                         {begin, cs.used(begin)}, ec_)))
    2463              42 :                     return fail(cs.begin());
    2464                 : 
    2465                 :                 BOOST_IF_CONSTEXPR( precise_parsing )
    2466               2 :                     num_buf_.append( begin, cs.used(begin) );
    2467             669 :                 return suspend(cs.begin(), state::num6, num);
    2468                 :             }
    2469              47 :             goto finish_int;
    2470                 :         }
    2471         2054611 :         char const c = *cs;
    2472         2054611 :         if(BOOST_JSON_LIKELY(
    2473                 :             c == '.'))
    2474                 :         {
    2475         2016099 :             ++cs;
    2476                 :         }
    2477           38512 :         else if((c | 32) == 'e')
    2478                 :         {
    2479            9153 :             ++cs;
    2480            9153 :             goto do_exp1;
    2481                 :         }
    2482                 :         else
    2483                 :         {
    2484           29359 :             goto finish_int;
    2485                 :         }
    2486                 :     }
    2487                 : 
    2488                 :     //----------------------------------
    2489                 :     //
    2490                 :     // DIGIT
    2491                 :     // first significant digit
    2492                 :     // to the right of decimal
    2493                 :     //
    2494         2016839 : do_num7:
    2495                 :     {
    2496         2016839 :         if(BOOST_JSON_UNLIKELY(! cs))
    2497                 :         {
    2498             693 :             if(BOOST_JSON_UNLIKELY(more_))
    2499                 :             {
    2500             689 :                 if(BOOST_JSON_UNLIKELY(
    2501                 :                     ! h_.on_number_part(
    2502                 :                         {begin, cs.used(begin)}, ec_)))
    2503              35 :                     return fail(cs.begin());
    2504                 : 
    2505                 :                 BOOST_IF_CONSTEXPR( precise_parsing )
    2506 MIS           0 :                     num_buf_.append( begin, cs.used(begin) );
    2507 HIT         619 :                 return suspend(cs.begin(), state::num7, num);
    2508                 :             }
    2509                 :             // digit required
    2510                 :             BOOST_STATIC_CONSTEXPR source_location loc
    2511                 :                 = BOOST_CURRENT_LOCATION;
    2512               4 :             return fail(cs.begin(), error::syntax, &loc);
    2513                 :         }
    2514         2016146 :         char const c = *cs;
    2515         2016146 :         if(BOOST_JSON_UNLIKELY(
    2516                 :             c < '0' || c > '9'))
    2517                 :         {
    2518                 :             // digit required
    2519                 :             BOOST_STATIC_CONSTEXPR source_location loc
    2520                 :                 = BOOST_CURRENT_LOCATION;
    2521             169 :             return fail(cs.begin(), error::syntax, &loc);
    2522                 :         }
    2523                 :     }
    2524                 : 
    2525                 :     //----------------------------------
    2526                 :     //
    2527                 :     // 1*DIGIT
    2528                 :     // significant digits
    2529                 :     // to the right of decimal
    2530                 :     //
    2531         6228742 : do_num8:
    2532      4298245220 :     for(;;)
    2533                 :     {
    2534      4304476462 :         if(BOOST_JSON_UNLIKELY(! cs))
    2535                 :         {
    2536         4207120 :             if(BOOST_JSON_UNLIKELY(more_))
    2537                 :             {
    2538         4205384 :                 if(BOOST_JSON_UNLIKELY(
    2539                 :                     ! h_.on_number_part(
    2540                 :                         {begin, cs.used(begin)}, ec_)))
    2541              67 :                     return fail(cs.begin());
    2542                 : 
    2543                 :                 BOOST_IF_CONSTEXPR( precise_parsing )
    2544               2 :                     num_buf_.append( begin, cs.used(begin) );
    2545         4205249 :                 return suspend(cs.begin(), state::num8, num);
    2546                 :             }
    2547            1736 :             goto finish_dub;
    2548                 :         }
    2549      4300269342 :         char const c = *cs;
    2550      4300269342 :         if(BOOST_JSON_LIKELY(
    2551                 :             c >= '0' && c <= '9'))
    2552                 :         {
    2553      4300252205 :             ++cs;
    2554      4300247112 :             if(!no_parsing && BOOST_JSON_LIKELY(
    2555                 :                 num.mant <= 9007199254740991)) // 2^53-1
    2556                 :             {
    2557      4298245221 :                 if(BOOST_JSON_UNLIKELY( num.bias - 1 == INT_MIN ))
    2558                 :                 {
    2559                 :                     BOOST_STATIC_CONSTEXPR source_location loc
    2560                 :                         = BOOST_CURRENT_LOCATION;
    2561               1 :                     return fail(cs.begin(), error::exponent_overflow, &loc);
    2562                 :                 }
    2563      4298245220 :                 --num.bias;
    2564      4298245220 :                 num.mant = 10 * num.mant + ( c - '0' );
    2565                 :             }
    2566                 :             else
    2567                 :             {
    2568         2006984 :                 goto do_num5;
    2569                 :             }
    2570                 :         }
    2571           17137 :         else if((c | 32) == 'e')
    2572                 :         {
    2573           11134 :             ++cs;
    2574           11134 :             goto do_exp1;
    2575                 :         }
    2576                 :         else
    2577                 :         {
    2578            6003 :             goto finish_dub;
    2579                 :         }
    2580                 :     }
    2581                 : 
    2582                 :     //----------------------------------
    2583                 :     //
    2584                 :     // *[+-]
    2585                 :     //
    2586         2031448 : do_exp1:
    2587         2031448 :     if(BOOST_JSON_UNLIKELY(! cs))
    2588                 :     {
    2589             575 :         if(BOOST_JSON_UNLIKELY(
    2590                 :             ! h_.on_number_part(
    2591                 :                 {begin, cs.used(begin)}, ec_)))
    2592              49 :             return fail(cs.begin());
    2593                 : 
    2594                 :         BOOST_IF_CONSTEXPR( precise_parsing )
    2595 MIS           0 :             num_buf_.append( begin, cs.used(begin) );
    2596 HIT         477 :         return maybe_suspend(
    2597             477 :             cs.begin(), state::exp1, num);
    2598                 :     }
    2599         2030873 :     if(*cs == '+')
    2600                 :     {
    2601            2364 :         ++cs;
    2602                 :     }
    2603         2028509 :     else if(*cs == '-')
    2604                 :     {
    2605         1001936 :         ++cs;
    2606         1001936 :         num.frac = true;
    2607                 :     }
    2608                 : 
    2609                 :     //----------------------------------
    2610                 :     //
    2611                 :     // DIGIT
    2612                 :     // first digit of the exponent
    2613                 :     //
    2614         1026573 : do_exp2:
    2615                 :     {
    2616         2031012 :         if(BOOST_JSON_UNLIKELY(! cs))
    2617                 :         {
    2618             188 :             if(BOOST_JSON_UNLIKELY(more_))
    2619                 :             {
    2620             179 :                 if(BOOST_JSON_UNLIKELY(
    2621                 :                     ! h_.on_number_part(
    2622                 :                         {begin, cs.used(begin)}, ec_)))
    2623              20 :                     return fail(cs.begin());
    2624                 : 
    2625                 :                 BOOST_IF_CONSTEXPR( precise_parsing )
    2626 MIS           0 :                     num_buf_.append( begin, cs.used(begin) );
    2627 HIT         139 :                 return suspend(cs.begin(), state::exp2, num);
    2628                 :             }
    2629                 :             // digit required
    2630                 :             BOOST_STATIC_CONSTEXPR source_location loc
    2631                 :                 = BOOST_CURRENT_LOCATION;
    2632               9 :             return fail(cs.begin(), error::syntax, &loc);
    2633                 :         }
    2634         2030824 :         char const c = *cs;
    2635         2030824 :         if(BOOST_JSON_UNLIKELY(
    2636                 :             c < '0' || c > '9'))
    2637                 :         {
    2638                 :             // digit required
    2639                 :             BOOST_STATIC_CONSTEXPR source_location loc
    2640                 :                 = BOOST_CURRENT_LOCATION;
    2641             508 :             return fail(cs.begin(), error::syntax, &loc);
    2642                 :         }
    2643         2030316 :         ++cs;
    2644         2030316 :         num.exp = c - '0';
    2645                 :     }
    2646                 : 
    2647                 :     //----------------------------------
    2648                 :     //
    2649                 :     // 1*DIGIT
    2650                 :     // subsequent digits in the exponent
    2651                 :     //
    2652         2042421 : do_exp3:
    2653         5468163 :     for(;;)
    2654                 :     {
    2655         7510584 :         if(BOOST_JSON_UNLIKELY(! cs))
    2656                 :         {
    2657         2020838 :             if(BOOST_JSON_UNLIKELY(more_))
    2658                 :             {
    2659           12277 :                 if(BOOST_JSON_UNLIKELY(
    2660                 :                     ! h_.on_number_part(
    2661                 :                         {begin, cs.used(begin)}, ec_)))
    2662              86 :                     return fail(cs.begin());
    2663                 : 
    2664                 :                 BOOST_IF_CONSTEXPR( precise_parsing )
    2665 MIS           0 :                     num_buf_.append( begin, cs.used(begin) );
    2666 HIT       12105 :                 return suspend(cs.begin(), state::exp3, num);
    2667                 :             }
    2668                 :         }
    2669                 :         else
    2670                 :         {
    2671         5489746 :             char const c = *cs;
    2672         5489746 :             if(BOOST_JSON_LIKELY( c >= '0' && c <= '9' ))
    2673                 :             {
    2674         5468163 :                 if(BOOST_JSON_UNLIKELY(
    2675                 :                 //              2147483647 INT_MAX
    2676                 :                     num.exp  >  214748364 ||
    2677                 :                     (num.exp == 214748364 && c > '7')
    2678                 :                 ))
    2679            3855 :                     num.exp = INT_MAX;
    2680                 :                 else BOOST_IF_CONSTEXPR( !no_parsing )
    2681         4923456 :                     num.exp = 10 * num.exp + ( c - '0' );
    2682                 : 
    2683         5468163 :                 ++cs;
    2684         5468163 :                 continue;
    2685                 :             }
    2686                 :         }
    2687         2030144 :         BOOST_ASSERT(num.exp >= 0);
    2688         2030144 :         if ( num.frac )
    2689                 :         {
    2690         1001800 :             if(BOOST_JSON_UNLIKELY( num.bias < (INT_MIN + num.exp) ))
    2691                 :             {
    2692                 :                 // if exponent overflowed, bias is a very large negative
    2693                 :                 // number, and mantissa isn't zero, then we cannot parse the
    2694                 :                 // number correctly
    2695              92 :                 if(BOOST_JSON_UNLIKELY(
    2696                 :                     (num.exp == INT_MAX) &&
    2697                 :                     (num.bias < 0) &&
    2698                 :                     (num.exp + num.bias < 308) &&
    2699                 :                     num.mant ))
    2700                 :                 {
    2701                 :                     BOOST_STATIC_CONSTEXPR source_location loc
    2702                 :                         = BOOST_CURRENT_LOCATION;
    2703               1 :                     return fail(cs.begin(), error::exponent_overflow, &loc);
    2704                 :                 }
    2705                 : 
    2706              91 :                 num.bias = 0;
    2707              91 :                 num.exp = INT_MAX;
    2708                 :             }
    2709                 :         }
    2710         1028344 :         else if (BOOST_JSON_UNLIKELY( num.bias > (INT_MAX - num.exp) ))
    2711                 :         {
    2712                 :             // if exponent overflowed, bias is a very large positive number,
    2713                 :             // and mantissa isn't zero, then we cannot parse the
    2714                 :             // number correctly
    2715             194 :             if(BOOST_JSON_UNLIKELY(
    2716                 :                 (num.exp == INT_MAX) &&
    2717                 :                 (num.bias > 0) &&
    2718                 :                 (num.exp - num.bias < 308) &&
    2719                 :                 num.mant ))
    2720                 :             {
    2721                 :                 BOOST_STATIC_CONSTEXPR source_location loc
    2722                 :                     = BOOST_CURRENT_LOCATION;
    2723               1 :                 return fail(cs.begin(), error::exponent_overflow, &loc);
    2724                 :             }
    2725                 : 
    2726             193 :             num.bias = 0;
    2727             193 :             num.exp = INT_MAX;
    2728                 :         }
    2729         2030142 :         goto finish_dub;
    2730                 :     }
    2731                 : 
    2732           30471 : finish_int:
    2733           28542 :     if(negative || (!stack_empty && num.neg))
    2734                 :     {
    2735            2531 :         if(BOOST_JSON_UNLIKELY(
    2736                 :             ! h_.on_int64(static_cast<
    2737                 :                 int64_t>(~num.mant + 1), {begin, cs.used(begin)}, ec_)))
    2738             310 :             return fail(cs.begin());
    2739            1913 :         return cs.begin();
    2740                 :     }
    2741           27940 :     if(num.mant <= INT64_MAX)
    2742                 :     {
    2743           27639 : finish_signed:
    2744           30914 :         if(BOOST_JSON_UNLIKELY(
    2745                 :             ! h_.on_int64(static_cast<
    2746                 :                 int64_t>(num.mant), {begin, cs.used(begin)}, ec_)))
    2747            2267 :             return fail(cs.begin());
    2748           26387 :         return cs.begin();
    2749                 :     }
    2750             321 :     if(BOOST_JSON_UNLIKELY(
    2751                 :         ! h_.on_uint64(num.mant, {begin, cs.used(begin)}, ec_)))
    2752              35 :         return fail(cs.begin());
    2753             254 :     return cs.begin();
    2754         2052467 : finish_dub:
    2755                 :     double d;
    2756         2052467 :     std::size_t const size = cs.used(begin);
    2757         2052467 :     BOOST_ASSERT( !num_buf_.size() || precise_parsing );
    2758                 :     BOOST_IF_CONSTEXPR( precise_parsing )
    2759                 :     {
    2760         1000052 :         char const* data = begin;
    2761         1000052 :         std::size_t full_size = size;
    2762                 :          // if we previously suspended or if the current input ends with the
    2763                 :          // number, we need to copy the current part of the number to the
    2764                 :          // temporary buffer
    2765         1000052 :         if(BOOST_JSON_UNLIKELY( num_buf_.size() ))
    2766                 :         {
    2767               1 :             data = num_buf_.append( begin, size );
    2768               1 :             full_size = num_buf_.size();
    2769                 :         }
    2770         1000052 :         auto const err = detail::charconv::from_chars(
    2771                 :             data, data + full_size, d );
    2772         1000052 :         BOOST_ASSERT( err.ec != std::errc::invalid_argument );
    2773         1000052 :         BOOST_ASSERT( err.ptr == data + full_size );
    2774                 :         (void)err;
    2775                 :     }
    2776                 :     else BOOST_IF_CONSTEXPR( no_parsing )
    2777            9258 :         d = 0;
    2778                 :     else
    2779         1043157 :         d = detail::dec_to_float(
    2780                 :             num.mant,
    2781          541045 :             num.bias + (num.frac ?
    2782          502112 :                 -num.exp : num.exp),
    2783         1043157 :             num.neg);
    2784         2052467 :     if(BOOST_JSON_UNLIKELY(
    2785                 :         ! h_.on_double(d, {begin, size}, ec_)))
    2786            1935 :         return fail(cs.begin());
    2787         2048597 :     return cs.begin();
    2788                 : }
    2789                 : 
    2790                 : //----------------------------------------------------------
    2791                 : 
    2792                 : template<class Handler>
    2793                 : template<class... Args>
    2794         2165012 : basic_parser<Handler>::
    2795                 : basic_parser(
    2796                 :     parse_options const& opt,
    2797                 :     Args&&... args)
    2798         2165003 :     : h_(std::forward<Args>(args)...)
    2799         2165012 :     , opt_(opt)
    2800                 : {
    2801         2165012 : }
    2802                 : 
    2803                 : //----------------------------------------------------------
    2804                 : 
    2805                 : template<class Handler>
    2806                 : void
    2807         4153167 : basic_parser<Handler>::
    2808                 : reset() noexcept
    2809                 : {
    2810         4153167 :     ec_ = {};
    2811         4153167 :     st_.clear();
    2812         4153167 :     more_ = true;
    2813         4153167 :     done_ = false;
    2814         4153167 :     clean_ = true;
    2815         4153167 :     num_buf_.clear();
    2816         4153167 : }
    2817                 : 
    2818                 : template<class Handler>
    2819                 : void
    2820              18 : basic_parser<Handler>::
    2821                 : fail(system::error_code ec) noexcept
    2822                 : {
    2823              18 :     if(! ec)
    2824                 :     {
    2825                 :         // assign an arbitrary
    2826                 :         // error code to prevent UB
    2827               1 :         BOOST_JSON_FAIL(ec_, error::incomplete);
    2828                 :     }
    2829                 :     else
    2830                 :     {
    2831              17 :         ec_ = ec;
    2832                 :     }
    2833              18 :     done_ = false;
    2834              18 : }
    2835                 : 
    2836                 : //----------------------------------------------------------
    2837                 : 
    2838                 : template<class Handler>
    2839                 : std::size_t
    2840        10726603 : basic_parser<Handler>::
    2841                 : write_some(
    2842                 :     bool more,
    2843                 :     char const* data,
    2844                 :     std::size_t size,
    2845                 :     system::error_code& ec)
    2846                 : {
    2847                 :     // see if we exited via exception
    2848                 :     // on the last call to write_some
    2849        10726603 :     if(! clean_)
    2850                 :     {
    2851                 :         // prevent UB
    2852               1 :         if(! ec_)
    2853                 :         {
    2854               1 :             BOOST_JSON_FAIL(ec_, error::exception);
    2855                 :         }
    2856                 :     }
    2857        10726603 :     if(ec_)
    2858                 :     {
    2859                 :         // error is sticky
    2860               5 :         ec = ec_;
    2861               5 :         return 0;
    2862                 :     }
    2863        10726598 :     clean_ = false;
    2864        10726598 :     more_ = more;
    2865        10726598 :     end_ = data + size;
    2866                 :     const char* p;
    2867        10726598 :     if(BOOST_JSON_LIKELY(st_.empty()))
    2868                 :     {
    2869                 :         // first time
    2870         2165001 :         depth_ = opt_.max_depth;
    2871         2165001 :         if(BOOST_JSON_UNLIKELY(
    2872                 :             ! h_.on_document_begin(ec_)))
    2873                 :         {
    2874            7928 :             ec = ec_;
    2875            7928 :             return 0;
    2876                 :         }
    2877         2149144 :         p = parse_document(data, std::true_type());
    2878                 :     }
    2879                 :     else
    2880                 :     {
    2881         8561597 :         p = parse_document(data, std::false_type());
    2882                 :     }
    2883                 : 
    2884        10691573 :     if(BOOST_JSON_LIKELY(p != sentinel()))
    2885                 :     {
    2886         2099217 :         BOOST_ASSERT(! ec_);
    2887         2099217 :         if(! done_)
    2888                 :         {
    2889         2031790 :             done_ = true;
    2890         2031790 :             h_.on_document_end(ec_);
    2891                 :         }
    2892                 :     }
    2893                 :     else
    2894                 :     {
    2895         8592356 :         if(! ec_)
    2896                 :         {
    2897         8565499 :             if(! more_)
    2898                 :             {
    2899             590 :                 BOOST_JSON_FAIL(ec_, error::incomplete);
    2900                 :             }
    2901         8564909 :             else if(! st_.empty())
    2902                 :             {
    2903                 :                 // consume as much trailing whitespace in
    2904                 :                 // the JSON document as possible, but still
    2905                 :                 // consider the parse complete
    2906                 :                 state st;
    2907         8564909 :                 st_.peek(st);
    2908         8564909 :                 if( st == state::doc3 &&
    2909           88563 :                     ! done_)
    2910                 :                 {
    2911           70736 :                     done_ = true;
    2912           70736 :                     h_.on_document_end(ec_);
    2913                 :                 }
    2914                 :             }
    2915                 :         }
    2916         8590712 :         p = end_;
    2917                 :     }
    2918        10685937 :     ec = ec_;
    2919        10685937 :     clean_ = true;
    2920        10685937 :     return p - data;
    2921                 : }
    2922                 : 
    2923                 : template<class Handler>
    2924                 : std::size_t
    2925               1 : basic_parser<Handler>::
    2926                 : write_some(
    2927                 :     bool more,
    2928                 :     char const* data,
    2929                 :     std::size_t size,
    2930                 :     std::error_code& ec)
    2931                 : {
    2932               1 :     system::error_code jec;
    2933               1 :     std::size_t const result = write_some(more, data, size, jec);
    2934               1 :     ec = jec;
    2935               1 :     return result;
    2936                 : }
    2937                 : 
    2938                 : #endif
    2939                 : 
    2940                 : } // namespace json
    2941                 : } // namespace boost
    2942                 : 
    2943                 : #ifdef _MSC_VER
    2944                 : #pragma warning(pop)
    2945                 : #endif
    2946                 : 
    2947                 : #endif
        

Generated by: LCOV version 2.3