bufjson 1.0.1

Fast streaming JSON parser and lexer | Read JSON without allocating or copying.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
//! Scan JSON text, extracting a stream of tokens (lexical analysis).
//!
//! This module provides the traits, helpers, and type definitions needed to perform stream-oriented
//! lexical analysis on JSON text.
//!
//! The fundamental types are the enum [`Token`], which represents the type of a JSON token, and
//! the traits [`Analyzer`] (does the lexical analysis); [`Content`] (efficiently provides the
//! actual content of a token from the JSON text); and [`Error`] (describes errors encountered by
//! the lexical analyzer).
//!
//! The sub-modules provide concrete implementations of JSON tokenizers:
//!
//! - [`state`] is a lower-level module containing a simple reusable finite state machine; all the
//!   concrete lexical analyzers in this crate use this state machine for their core logic.
//! - [`fixed`] contains an implementation of [`Analyzer`] for tokenizing fixed-size in-memory
//!   buffers.
#![cfg_attr(feature = "read", doc = "- [`read`][`crate::lexical::read`]")]
#![cfg_attr(not(feature = "read"), doc = "- `read`")]
//! contains an implementation of [`Analyzer`] for tokenizing streams that can be read like a
//! `std::io::Read`. Requires the `read` feature to be enabled.
#![cfg_attr(feature = "pipe", doc = "- [`pipe`][`crate::lexical::pipe`]")]
#![cfg_attr(not(feature = "pipe"), doc = "- `pipe`")]
//! contains an implementation of [`Analyzer`] for tokenizing streams that yield chunks of input in
//! `Bytes` buffers, useful for zero-copy network programming use cases. Requires the `pipe` feature
//! to be enabled.
//!
//! # Performance
//!
//! Performance characteristics are documented on all relevant types at the trait level (this
//! module) and at the concrete implementation level (in the sub-modules).
//!
//! In all cases, allocations and copies are avoided except where it is technically infeasible. When
//! they have to be done, they are minimized.
//!
//! # Token content
//!
//! By design, the [`Content`] trait provides the literal text of all tokens appearing in the input
//! JSON, including whitespace, without any change whatsoever. This policy facilitates use cases
//! such as stream editing, where you might want to make changes to the JSON text, such as deleting
//! some JSON elements or inserting new ones, while leaving everything else unchanged.
//!
//! # Numbers
//!
//! For number tokens ([`Token::Num`]), the [`Content`] trait provides the literal content of the
//! number as it appears in the JSON text, without attempting to coerce it into a Rust numeric type.
//!
//! The reason for leaving numbers as text is that the [JSON spec][rfc] places no limits on the
//! range and precision of numbers \[1\]. Since this module aims to faithfully implement the spec at
//! the lexical level, it will recognize any valid JSON number, no matter the magnitude or
//! precision. This would not be possible if it coerced the text into a numeric type, which all have
//! their own limits on range and precision.
//!
//! \[1\]: The spec *does* urge software developers using JSON to be thoughtful about
//! interoperability and, kinda sorta, to just stay within the IEEE double-precision floating point
//! range, *a.k.a.*, `f64`. But that's not a requirement.
//!
//! # Strings
//!
//! For string tokens ([`Token::Str`]), the [`Content`] trait provides the literal content of the
//! string as it appears in the JSON text, *including* the quotation marks that surround it, without
//! attempting to expand the escape sequences.
//!
//! Escape sequences can be expanded by explicitly requesting [`Content::unescaped`] instead of
//! [`Content::literal`]. Note that getting the unescaped content will trigger an allocation if the
//! string indeed does contain at least one escape sequence, which may not be desirable in all
//! circumstances.
//!
//! Example of a string token without any escape sequences.
//!
//! ```
//! # use bufjson::lexical::{Token, fixed::FixedAnalyzer};
//! let mut lexer = FixedAnalyzer::new(&br#""foo""#[..]);
//! assert_eq!(Token::Str, lexer.next());
//! assert_eq!(r#""foo""#, lexer.content().literal()); // Note the surrounding quotes.
//! assert_eq!(r#""foo""#, lexer.content().unescaped()); // No allocation, returns same value.
//! ```
//!
//! Example of a string token containing an escape sequence.
//!
//! ```
//! # use bufjson::lexical::{Token, fixed::FixedAnalyzer};
//! let mut lexer = FixedAnalyzer::new(&br#""foo\u0020bar""#[..]);
//! assert_eq!(Token::Str, lexer.next());
//! assert_eq!(r#""foo\u0020bar""#, lexer.content().literal()); // Note the surrounding quotes.
//! assert_eq!(r#""foo bar""#, lexer.content().unescaped()); // Allocates, expands \u0020 -> ' '.
//! ```
//!
//! # Roll your own lexer
//!
//! The sub-module [`state`] provides the basic state machine for tokenizing JSON text. You can use
//! it to build your own implementation of [`Analyzer`] or any other application that needs a
//! low-level ability to identify JSON tokens that is faithful to the [JSON spec][rfc].
//!
//! For string tokens, you can use the [`unescape`] function standalone to expand escape sequences.
//!
//! [rfc]: https://datatracker.ietf.org/doc/html/rfc8259

use crate::{Buf, EqStr, IntoBuf, OrdStr, Pos, Sink, StringBuf};
#[cfg(feature = "num")]
use crate::{sink, sink::InlineSink};
use alloc::string::String;
use core::{
    borrow::Borrow,
    cmp::{Ord, Ordering},
    fmt,
    hash::{Hash, Hasher},
    ops::Deref,
};
#[cfg(feature = "num")]
use core::{num::ParseFloatError, str::FromStr};

#[cfg(feature = "num")]
macro_rules! max_decimal_digits {
    ($n:expr) => {{
        const V: u128 = if ($n as i128) < 0 {
            (!($n as u128)).wrapping_add(1)
        } else {
            $n as u128
        };
        const N: usize = if V == 0 { 1 } else { V.ilog10() as usize + 1 };

        N
    }};
}

#[cfg(feature = "num")]
macro_rules! parse_int {
    (into_buf, $b:ident, $t:ty, $sign:literal, $limit_val:expr) => {{
        let buf = $b.into_buf();
        let rem = buf.remaining();
        const N: usize = $sign + max_decimal_digits!($limit_val);
        if 0 < rem && rem <= N {
            let chunk = buf.chunk();
            if chunk.len() == rem {
                parse_int!(one_slice, chunk, $t, $sign, $limit_val);
            } else {
                let mut dst = InlineSink::<N>::new();
                sink(buf, &mut dst);
                let slice = dst.as_slice();
                parse_int!(one_slice, slice, $t, $sign, $limit_val);
            }
        }

        Err(parse_int_err(buf, $sign == 1))
    }};

    (one_slice, $slice:ident, $t:ty, $sign:literal, $limit_val:expr) => {{
        const MAX_DIGITS: usize = max_decimal_digits!($limit_val);
        if $sign == 1 && $slice[0] == b'-' {
            let digits = &$slice[1..];
            if digits.len() < MAX_DIGITS {
                parse_int!(calculate, digits, $t, negative, cannot_overflow);
            } else if digits.len() == MAX_DIGITS {
                parse_int!(calculate, digits, $t, negative, might_overflow);
            }
        } else {
            let digits = $slice;
            if digits.len() < MAX_DIGITS {
                parse_int!(calculate, digits, $t, non_negative, cannot_overflow);
            } else if digits.len() == MAX_DIGITS {
                parse_int!(calculate, digits, $t, non_negative, might_overflow);
            }
        }

        return Err(parse_int_err($slice, $sign == 1));
    }};

    (calculate, $slice:ident, $t:ty, negative, cannot_overflow) => {{
        let mut acc: $t = 0;
        let mut i = 0;
        while i < $slice.len() {
            let d = $slice[i].wrapping_sub(b'0');
            if d >= 10 {
                break;
            }
            acc = acc * 10 - d as $t;
            i += 1;
        }
        if i == $slice.len() {
            return Ok(acc);
        }
    }};

    (calculate, $slice:ident, $t:ty, negative, might_overflow) => {{
        if $slice.iter().all(|b| b.is_ascii_digit()) {
            let (head, tail) = $slice.split_at($slice.len() - 1);
            let acc = head
                .iter()
                .fold(0 as $t, |acc, &b| acc * 10 - (b - b'0') as $t);
            let d = (tail[0] - b'0') as $t;
            return if let Some(v) = acc.checked_mul(10).and_then(|a| a.checked_sub(d)) {
                Ok(v)
            } else {
                Err(NumError::Range)
            };
        }
    }};

    (calculate, $slice:ident, $t:ty, non_negative, cannot_overflow) => {{
        let mut acc: $t = 0;
        let mut i = 0;
        while i < $slice.len() {
            let d = $slice[i].wrapping_sub(b'0');
            if d >= 10 {
                break;
            }
            acc = acc * 10 + d as $t;
            i += 1;
        }
        if i == $slice.len() {
            return Ok(acc);
        }
    }};

    (calculate, $slice:ident, $t:ty, non_negative, might_overflow) => {{
        if $slice.iter().all(|b| b.is_ascii_digit()) {
            let (head, tail) = $slice.split_at($slice.len() - 1);
            let acc = head
                .iter()
                .fold(0 as $t, |acc, &b| acc * 10 + (b - b'0') as $t);
            let d = (tail[0] - b'0') as $t;
            return if let Some(v) = acc.checked_mul(10).and_then(|a| a.checked_add(d)) {
                Ok(v)
            } else {
                Err(NumError::Range)
            };
        }
    }};
}

/// Extracts a 64-bit signed integer from any sequence of bytes containing ASCII decimal digits with
/// an optional leading minus sign.
///
/// When the input is a valid JSON number token, this function will return `Ok` if the content does
/// not have a decimal point or exponent and represents an integer within the range of `i64`. For
/// other number tokens, the result is an `Err`. For any other input, the result is
/// `Err(NumError::Format)`.
///
/// # Performance considerations
///
/// Never allocates. May copy a small number of bytes on stack if the representation is
/// non-contiguous. This function is very fast.
///
/// # Examples
///
/// Parse an integer in the range of `i64`.
///
/// ```
/// use bufjson::lexical::{NumError, parse_i64};
///
/// assert_eq!(Ok(-1), parse_i64("-1"));
/// ```
///
/// An integer outside the range of `i64` cannot be parsed.
///
/// ```
/// use bufjson::lexical::{NumError, parse_i64};
///
/// assert_eq!(Err(NumError::Range), parse_i64(format!("{}", i64::MAX as u64 + 1)));
/// ```
///
/// A number value containing a decimal point is considered to be badly-formatted and cannot be
/// parsed, even if it technically represents an integer.
///
/// ```
/// use bufjson::lexical::{NumError, parse_i64};
///
/// assert_eq!(Err(NumError::Format), parse_i64("2.0"));
/// ```
///
/// A number value containing an exponent is considered to be badly-formatted and cannot be parsed,
/// even if it technically represents an integer.
///
/// ```
/// use bufjson::lexical::{NumError, parse_i64};
///
/// assert_eq!(Err(NumError::Format), parse_i64("1e3"));
/// ```
///
/// Non-numeric values cannot be parsed.
///
/// ```
/// use bufjson::lexical::{NumError, parse_i64};
///
/// assert_eq!(Err(NumError::Format), parse_i64("true"));
/// ```
#[cfg(feature = "num")]
pub fn parse_i64(literal: impl IntoBuf) -> Result<i64, NumError> {
    parse_int!(into_buf, literal, i64, 1, i64::MIN)
}

/// Extracts a 64-bit unsigned integer from any sequence of bytes containing ASCII decimal digits.
///
/// When the input is a valid JSON number token, this function will return `Ok` if the content does
/// not have a leading minus sign, a decimal point, or exponent; and represents an integer within
/// the range of `u64`. For other number tokens, the result is an `Err`. For any other input, the
/// result is `Err(NumError::Format)`.
///
/// # Performance considerations
///
/// Never allocates. May copy a small number of bytes on stack if the representation is
/// non-contiguous. This function is very fast.
///
/// # Examples
///
/// Parse an integer in the range of `u64`.
///
/// ```
/// use bufjson::lexical::{NumError, parse_u64};
///
/// assert_eq!(Ok(u64::MAX), parse_u64(format!("{}", u64::MAX)));
/// ```
///
/// An integer outside the range of `u64` cannot be parsed.
///
/// ```
/// use bufjson::lexical::{NumError, parse_u64};
///
/// assert_eq!(Err(NumError::Range), parse_u64(format!("{}", u64::MAX as u128 + 1)));
/// ```
///
/// A number value containing a decimal point is considered to be badly-formatted and cannot be
/// parsed, even if it technically represents an integer.
///
/// ```
/// use bufjson::lexical::{NumError, parse_u64};
///
/// assert_eq!(Err(NumError::Format), parse_u64("2.0"));
/// ```
///
/// A number value containing an exponent is considered to be badly-formatted and cannot be parsed,
/// even if it technically represents an integer.
///
/// ```
/// use bufjson::lexical::{NumError, parse_u64};
///
/// assert_eq!(Err(NumError::Format), parse_u64("1e3"));
/// ```
///
/// Negative integers cannot be parsed.
///
/// ```
/// use bufjson::lexical::{NumError, parse_u64};
///
/// assert_eq!(Err(NumError::Format), parse_u64("-1"));
/// ```
///
/// Non-numeric values cannot be parsed.
///
/// ```
/// use bufjson::lexical::{NumError, parse_u64};
///
/// assert_eq!(Err(NumError::Format), parse_u64("true"));
/// ```
#[cfg(feature = "num")]
pub fn parse_u64(literal: impl IntoBuf) -> Result<u64, NumError> {
    parse_int!(into_buf, literal, u64, 0, u64::MAX)
}

/// Extracts a 128-bit signed integer from any sequence of bytes containing ASCII decimal digits
/// with an optional leading minus sign.
///
/// When the input is a valid JSON number token, this function will return `Ok` if the content does
/// not have a decimal point or exponent and represents an integer within the range of `i128`. For
/// other number tokens, the result is an `Err`. For any other input, the result is
/// `Err(NumError::Format)`.
///
/// # Performance considerations
///
/// Never allocates. May copy a small number of bytes on stack if the representation is
/// non-contiguous. This function is very fast.
///
/// # Examples
///
/// Parse an integer in the range of `i128`.
///
/// ```
/// use bufjson::lexical::{NumError, parse_i128};
///
/// assert_eq!(Ok(-1), parse_i128("-1"));
/// ```
///
/// An integer outside the range of `i128` cannot be parsed.
///
/// ```
/// use bufjson::lexical::{NumError, parse_i128};
///
/// assert_eq!(Err(NumError::Range), parse_i128(format!("{}", i128::MAX as u128 + 1)));
/// ```
///
/// A number value containing a decimal point is considered to be badly-formatted and cannot be
/// parsed, even if it technically represents an integer.
///
/// ```
/// use bufjson::lexical::{NumError, parse_i128};
///
/// assert_eq!(Err(NumError::Format), parse_i128("2.0"));
/// ```
///
/// A number value containing an exponent is considered to be badly-formatted and cannot be parsed,
/// even if it technically represents an integer.
///
/// ```
/// use bufjson::lexical::{NumError, parse_i128};
///
/// assert_eq!(Err(NumError::Format), parse_i128("-100e-1"));
/// ```
///
/// Non-numeric values cannot be parsed.
///
/// ```
/// use bufjson::lexical::{NumError, parse_i128};
///
/// assert_eq!(Err(NumError::Format), parse_i128("true"));
/// ```
#[cfg(feature = "num_ext")]
pub fn parse_i128(literal: impl IntoBuf) -> Result<i128, NumError> {
    parse_int!(into_buf, literal, i128, 1, i128::MIN)
}

/// Extracts a 64-bit IEEE double-precision floating point number from any sequence of bytes that
/// is a valid JSON number token.
///
/// When the input is a valid JSON number token, this function will return `Ok` if the number value
/// in the text is within the range of an `f64`. If the content is within the range of an `f64` but
/// not representable due to precision limitations, `Ok` is returned with the nearest representable
/// `f64` value (rounded according to IEEE 754 round-to-nearest-even). For other number tokens, the
/// result is `Err(NumError::Range)`. For non-number tokens, the result is always
/// `Err(NumError::Format)`.
///
/// # Performance considerations
///
/// - Generally does not allocate, but will do so if the input bytes are non-contiguous.
/// - Unlike integers, which have a natural finite representation, floating point numbers can
///   conceptually have an infinite number of digits that have to be examined. Application writers
///   should be aware of this characteristic and may wish to set and enforce length limits on number
///   tokens before parsing the number value.
///
/// # Examples
///
/// Parse a value in the range of `f64`.
///
/// ```
/// use bufjson::lexical::parse_f64;
///
/// assert_eq!(Ok(3.14159), parse_f64("3.14159"));
/// ```
///
/// A number value that is within the range of `f64` but that cannot be represented precisely is
/// rounded to the closest representable value.
///
/// ```
/// use bufjson::lexical::parse_f64;
///
/// assert_eq!(
///     Ok(9007199254740994.0),                 // Got a number ending in `...4.0`.
///     parse_f64("9007199254740993.1"),        // Asked for a number ending in `...3.1`.
/// );
/// ```
///
/// A number that is outside the range of `f64` cannot be parsed.
///
/// ```
/// use bufjson::lexical::{NumError, parse_f64};
///
/// assert_eq!(Err(NumError::Range), parse_f64("-10e+309"));
/// ```
///
/// Non-numeric values cannot be parsed.
///
/// ```
/// use bufjson::lexical::{NumError, parse_f64};
///
/// assert_eq!(Err(NumError::Format), parse_f64("true"));
/// ```
#[cfg(feature = "num")]
pub fn parse_f64(literal: impl IntoBuf) -> Result<f64, NumError> {
    let buf = literal.into_buf();
    let rem = buf.remaining();
    let mut chunk = buf.chunk();

    #[cfg(test)]
    const MAX_INLINE_PARSE_LEN: usize = 7;

    #[cfg(not(test))]
    const MAX_INLINE_PARSE_LEN: usize = 128;

    let r = if chunk.len() == rem {
        // SAFETY: The `Buf` invariant requires that the entire sequence of bytes yielded by a
        //         `Buf` is valid UTF-8.
        let s = unsafe { str::from_utf8_unchecked(chunk) };

        f64::from_str(s)
    } else {
        let n = chunk.len();
        #[allow(unused_assignments)]
        {
            chunk = &[]; /* Drop the immutable borrow from `buf`. */
        }
        if n <= MAX_INLINE_PARSE_LEN {
            let mut dst = InlineSink::<MAX_INLINE_PARSE_LEN>::new();
            sink(buf, &mut dst);
            // SAFETY: The `Buf` invariant requires that the entire sequence of bytes yielded by a
            //         `Buf` is valid UTF-8.
            let s = unsafe { str::from_utf8_unchecked(dst.as_slice()) };

            f64::from_str(s)
        } else {
            let mut dst = Vec::new();
            sink(buf, &mut dst);
            // SAFETY: The `Buf` invariant requires that the entire sequence of bytes yielded by a
            //         `Buf` is valid UTF-8.
            let s = unsafe { str::from_utf8_unchecked(&dst) };

            f64::from_str(s)
        }
    };

    parse_f64_result(r)
}

#[cfg(feature = "num")]
pub(crate) fn parse_int_err(mut buf: impl Buf, signed: bool) -> NumError {
    let mut chunk = buf.chunk();
    if chunk.is_empty() {
        return NumError::Format;
    } else if signed && chunk[0] == b'-' {
        buf.advance(1);
        chunk = buf.chunk();
    }

    loop {
        if !chunk.iter().all(|b| b.is_ascii_digit()) {
            return NumError::Format;
        }

        buf.advance(chunk.len());
        chunk = buf.chunk();
        if chunk.is_empty() {
            return NumError::Range;
        }
    }
}

#[cfg(feature = "num")]
#[inline(always)]
fn parse_f64_result(r: Result<f64, ParseFloatError>) -> Result<f64, NumError> {
    match r {
        Ok(v) if v.is_infinite() => Err(NumError::Range),
        Ok(v) => Ok(v),
        Err(_) => Err(NumError::Format),
    }
}

pub mod fixed;
pub mod state;

#[cfg(feature = "pipe")]
#[cfg_attr(docsrs, doc(cfg(feature = "pipe")))]
pub mod pipe;

#[cfg(feature = "read")]
#[cfg_attr(docsrs, doc(cfg(feature = "read")))]
pub mod read;

/// Kind of lexical token in a JSON text, such as begin object `{`, literal `true`, or string.
///
/// This is a list of the JSON lexical token types as described in the [JSON spec][rfc]. The names
/// of enumeration members are aligned with the names as they appear in the spec.
///
/// Note that `Token` just models the token *type*, not the content. Some token types have static
/// content that never changes (*e.g.*, [`ArrBegin`] is always `'['`) while others have variable
/// content that depends on the specific JSON text being analyzed (*e.g.* [`Str`]).
///
/// [rfc]: https://datatracker.ietf.org/doc/html/rfc8259
/// [`ArrBegin`]: Token::ArrBegin
/// [`Str`]: Token::Str
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Token {
    /// The begin array token, which has the literal value `[`.
    ArrBegin,
    /// The end array token, which has the literal value `]`.
    ArrEnd,
    /// Pseudo-token representing the end of the JSON text (end of file).
    Eof,
    /// Pseudo-token representing an unrecoverable lexical error detected in the JSON text.
    Err,
    /// The value literal `false`.
    LitFalse,
    /// The value literal `null`.
    LitNull,
    /// The value literal `true`.
    LitTrue,
    /// The name separator token, which has the literal value `:`.
    NameSep,
    /// A number token such as `0`, `123.4`, or `-1.25e+6`.
    Num,
    /// The begin object token, which has the literal value `{`.
    ObjBegin,
    /// The end object token, which has the literal value `}`.
    ObjEnd,
    /// A string token, such as `""`, `"foo"`, or `"Hello,\u0020world! 🌎"`.
    Str,
    /// The value separator token, which has the literal value `,`.
    ValueSep,
    /// A maximal string of insignificant whitespace.
    White,
}

impl Token {
    /// Returns `true` for the [`Eof`] pseudo-token and `false` otherwise.
    ///
    /// Since a stream of JSON text can stop due to either the physical end of the stream
    /// ([`Token::Eof`]) or due to an unrecoverable error ([`Token::Err`]), you may find
    /// [`is_terminal`], which checks for both, to be more useful when checking for end of stream.
    ///
    /// [`Eof`]: Token::Eof
    /// [`is_terminal`]: method@Self::is_terminal
    ///
    /// # Examples
    ///
    ///# use bufjson::lexical::Token;
    /// assert!(Token::Eof.is_eof());
    ///
    /// assert!(!Token::Num.is_eof());
    /// assert!(!Token::Err.is_eof());
    #[inline(always)]
    pub const fn is_eof(&self) -> bool {
        matches!(self, Token::Eof)
    }

    /// Returns `true` for the [`Err`] pseudo-token and `false` otherwise.
    ///
    /// Since a stream of JSON text can stop due to either an unrecoverable error ([`Token::Err`])
    /// or the physical end of the stream ([`Token::Eof`]), you may find [`is_terminal`], which
    /// checks for both, to be more useful when checking for end of stream.
    ///
    /// [`Err`]: Token::Err
    /// [`is_terminal`]: method@Self::is_terminal
    ///
    /// # Examples
    ///
    ///# use bufjson::lexical::Token;
    /// assert!(Token::Err.is_err());
    ///
    /// assert!(!Token::Num.is_err());
    /// assert!(!Token::Eof.is_err());
    #[inline(always)]
    pub const fn is_err(&self) -> bool {
        matches!(self, Token::Err)
    }

    /// Returns `true` for lexical tokens that are literal JSON values and `false` otherwise.
    ///
    /// The following tokens are considered literal values:
    /// - [`LitFalse`][Token::LitFalse]
    /// - [`LitNull`][Token::LitNull]
    /// - [`LitTrue`][Token::LitTrue]
    ///
    /// # Examples
    ///
    /// ```
    /// # use bufjson::lexical::Token;
    /// assert!(Token::LitFalse.is_literal());
    /// assert!(Token::LitNull.is_literal());
    /// assert!(Token::LitTrue.is_literal());
    ///
    /// assert!(!Token::Str.is_literal());
    /// assert!(!Token::Eof.is_literal());
    /// ```
    #[inline(always)]
    pub const fn is_literal(&self) -> bool {
        matches!(self, Self::LitFalse | Self::LitNull | Self::LitTrue)
    }

    /// Returns `true` for lexical tokens that are primitive JSON values and `false` otherwise.
    ///
    /// The following tokens are considered primitive values:
    /// - [`LitFalse`][Token::LitFalse]
    /// - [`LitNull`][Token::LitNull]
    /// - [`LitTrue`][Token::LitTrue]
    /// - [`Num`][Token::Num]
    /// - [`Str`][Token::Str]
    ///
    /// # Examples
    ///
    /// ```
    /// # use bufjson::lexical::Token;
    /// assert!(Token::LitNull.is_primitive());
    /// assert!(Token::Num.is_primitive());
    /// assert!(Token::Str.is_primitive());
    ///
    /// assert!(!Token::ObjEnd.is_primitive());
    /// assert!(!Token::White.is_primitive());
    /// ```
    #[inline(always)]
    pub const fn is_primitive(&self) -> bool {
        matches!(
            self,
            Self::LitFalse | Self::LitNull | Self::LitTrue | Self::Num | Self::Str
        )
    }

    /// Returns `true` for pseudo-tokens and `false` otherwise.
    ///
    /// The following are considered pseudo-tokens:
    /// - [`Eof`][Token::Eof]
    /// - [`Err`][Token::Err]
    /// - [`White`][Token::White]
    ///
    /// # Examples
    ///
    /// ```
    /// # use bufjson::lexical::Token;
    /// assert!(Token::Eof.is_pseudo());
    /// assert!(Token::Err.is_pseudo());
    /// assert!(Token::White.is_pseudo());
    ///
    /// assert!(!Token::ArrEnd.is_pseudo());
    /// assert!(!Token::LitNull.is_pseudo());
    /// assert!(!Token::Num.is_pseudo());
    /// ```
    #[inline(always)]
    pub const fn is_pseudo(&self) -> bool {
        matches!(self, Self::Eof | Self::Err | Self::White)
    }

    /// Returns `true` for lexical tokens that are punctuation and `false` otherwise.
    ///
    /// The following tokens are considered punctuation:
    ///
    /// - [`NameSep`][Token::NameSep]
    /// - [`ValueSep`][Token::ValueSep]
    ///
    /// # Examples
    ///
    /// ```
    /// # use bufjson::lexical::Token;
    /// assert!(Token::NameSep.is_punct());
    /// assert!(Token::ValueSep.is_punct());
    ///
    /// assert!(!Token::ArrBegin.is_punct());
    /// assert!(!Token::Num.is_punct());
    /// assert!(!Token::White.is_punct());
    /// assert!(!Token::Err.is_punct());
    /// ```
    #[inline(always)]
    pub const fn is_punct(&self) -> bool {
        matches!(self, Self::NameSep | Self::ValueSep)
    }

    /// Returns `true` for lexical tokens that are structural and `false` otherwise.
    ///
    /// The following tokens are considered structural:
    ///
    /// - [`ArrBegin`][Token::ArrBegin]
    /// - [`ArrEnd`][Token::ArrEnd]
    /// - [`ObjBegin`][Token::ObjBegin]
    /// - [`ObjEnd`][Token::ObjEnd]
    ///
    /// # Examples
    ///
    /// ```
    /// # use bufjson::lexical::Token;
    /// assert!(Token::ArrBegin.is_struct());
    /// assert!(Token::ObjEnd.is_struct());
    ///
    /// assert!(!Token::Str.is_struct());
    /// assert!(!Token::ValueSep.is_struct());
    /// assert!(!Token::White.is_struct());
    /// ```
    #[inline(always)]
    pub const fn is_struct(&self) -> bool {
        matches!(
            self,
            Self::ArrBegin | Self::ArrEnd | Self::ObjBegin | Self::ObjEnd
        )
    }

    /// Returns `true` for pseudo-tokens that terminate a stream of lexical tokens and `false`
    /// otherwise.
    ///
    /// The following tokens are considered terminal:
    /// - [`Eof`][Token::Eof]
    /// - [`Err`][Token::Err]
    ///
    /// # Examples
    ///
    /// ```
    /// # use bufjson::lexical::Token;
    /// assert!(Token::Eof.is_terminal());
    /// assert!(Token::Err.is_terminal());
    ///
    /// assert!(!Token::Num.is_terminal());
    /// assert!(!Token::ObjBegin.is_terminal());
    /// assert!(!Token::White.is_terminal());
    /// ```
    #[inline(always)]
    pub const fn is_terminal(&self) -> bool {
        matches!(self, Self::Eof | Self::Err)
    }

    /// Returns the static content for lexical tokens that always have the same static text content.
    ///
    /// # Examples
    ///
    /// ```
    /// # use bufjson::lexical::Token;
    /// assert_eq!(Some("["), Token::ArrBegin.static_content());
    /// assert_eq!(Some("true"), Token::LitTrue.static_content());
    ///
    /// assert_eq!(None, Token::Str.static_content());
    /// assert_eq!(None, Token::White.static_content());
    /// ```
    #[inline(always)]
    pub const fn static_content(&self) -> Option<&'static str> {
        match self {
            Self::ArrBegin => Some("["),
            Self::ArrEnd => Some("]"),
            Self::LitFalse => Some("false"),
            Self::LitNull => Some("null"),
            Self::LitTrue => Some("true"),
            Self::NameSep => Some(":"),
            Self::ObjBegin => Some("{"),
            Self::ObjEnd => Some("}"),
            Self::ValueSep => Some(","),
            _ => None,
        }
    }
}

impl fmt::Display for Token {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let s = match self {
            Self::ArrBegin => "[",
            Self::ArrEnd => "]",
            Self::Eof => "EOF",
            Self::Err => "error",
            Self::LitFalse => "false",
            Self::LitNull => "null",
            Self::LitTrue => "true",
            Self::NameSep => ":",
            Self::Num => "number",
            Self::ObjBegin => "{",
            Self::ObjEnd => "}",
            Self::Str => "string",
            Self::ValueSep => ",",
            Self::White => "whitespace",
        };

        f.write_str(s)
    }
}

/// Result of expanding escape sequences in a JSON string token.
///
/// An `Unescaped` value is a valid UTF-8 string that is free of JSON escape sequences. It either
/// contains the literal content of a JSON token exactly as it appears in the JSON text, if the
/// token did not contain any escape sequences; or it contains the normalized version of the token
/// content with escape sequences expanded, if the token had at least one escape sequence.
/// Evidently, the latter case can only occur for string tokens.
///
/// # Example
///
/// ```
/// use bufjson::lexical::{Token, Unescaped, fixed::FixedAnalyzer};
///
/// let mut lexer = FixedAnalyzer::new(&br#"["foo\u0020bar"]"#[..]);
///
/// assert_eq!(Token::ArrBegin, lexer.next());
/// let content = lexer.content();
/// let u: Unescaped<_> = content.unescaped();
/// assert!(u.is_literal());
/// assert_eq!("[", u);
///
/// assert_eq!(Token::Str, lexer.next());
/// let content = lexer.content();
/// let u: Unescaped<_> = content.unescaped();
/// assert!(u.is_expanded());
/// assert_eq!(r#""foo bar""#, u);
/// ```
#[derive(Clone, Debug)]
pub enum Unescaped<T> {
    /// Literal content of the JSON token exactly as it appears in the JSON text.
    Literal(T),

    /// Normalized content of the JSON token with all escape sequences expanded.
    Expanded(String),
}

impl<T> Unescaped<T> {
    /// Returns the literal content if available.
    ///
    /// The return value is `Some(...)` if `self` is [`Literal`], and `None` otherwise.
    ///
    /// [`Literal`]: Self::Literal
    #[inline(always)]
    pub fn literal(&self) -> Option<&T> {
        match self {
            Self::Literal(t) => Some(t),
            Self::Expanded(_) => None,
        }
    }

    /// Returns the expanded content if available.
    ///
    /// The return value is `Some(...)` if `self` is [`Expanded`], and `None` otherwise.
    ///
    /// [`Expanded`]: Self::Expanded
    #[inline(always)]
    pub fn expanded(&self) -> Option<&str> {
        match self {
            Self::Literal(_) => None,
            Self::Expanded(e) => Some(e.as_str()),
        }
    }

    /// Returns `true` if `self` is [`Literal`], and `false` otherwise.
    ///
    /// [`Literal`]: Self::Literal
    #[inline(always)]
    pub fn is_literal(&self) -> bool {
        matches!(self, Self::Literal(_))
    }

    /// Returns `true` if `self` is [`Expanded`], and `false` otherwise.
    ///
    /// [`Expanded`]: Self::Expanded
    #[inline(always)]
    pub fn is_expanded(&self) -> bool {
        matches!(self, Self::Expanded(_))
    }
}

impl<T: IntoBuf> IntoBuf for Unescaped<T> {
    type Buf = UnescapedBuf<T::Buf>;

    #[inline]
    fn into_buf(self) -> Self::Buf {
        match self {
            Self::Literal(t) => UnescapedBuf(UnescapedBufInner::Literal(t.into_buf())),
            Self::Expanded(e) => UnescapedBuf(UnescapedBufInner::Expanded(e.into_buf())),
        }
    }
}

impl AsRef<str> for Unescaped<&str> {
    #[inline]
    fn as_ref(&self) -> &str {
        match self {
            Unescaped::Literal(t) => t,
            Unescaped::Expanded(e) => e.as_str(),
        }
    }
}

impl AsRef<[u8]> for Unescaped<&str> {
    #[inline]
    fn as_ref(&self) -> &[u8] {
        match self {
            Unescaped::Literal(t) => t.as_bytes(),
            Unescaped::Expanded(e) => e.as_bytes(),
        }
    }
}

impl Deref for Unescaped<&str> {
    type Target = str;

    #[inline]
    fn deref(&self) -> &str {
        match self {
            Unescaped::Literal(t) => t,
            Unescaped::Expanded(e) => e.as_str(),
        }
    }
}

impl Borrow<str> for Unescaped<&str> {
    #[inline]
    fn borrow(&self) -> &str {
        match self {
            Unescaped::Literal(t) => t,
            Unescaped::Expanded(e) => e.as_str(),
        }
    }
}

impl<T> fmt::Display for Unescaped<T>
where
    T: fmt::Display,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Unescaped::Literal(t) => t.fmt(f),
            Unescaped::Expanded(e) => e.fmt(f),
        }
    }
}

impl<T> Eq for Unescaped<T> where T: Eq + EqStr {}

impl<T> From<Unescaped<T>> for String
where
    String: From<T>,
{
    fn from(u: Unescaped<T>) -> Self {
        match u {
            Unescaped::Literal(t) => t.into(),
            Unescaped::Expanded(e) => e,
        }
    }
}

impl<T> Hash for Unescaped<T>
where
    T: Hash,
{
    fn hash<H: Hasher>(&self, state: &mut H) {
        match self {
            Unescaped::Literal(t) => t.hash(state),
            Unescaped::Expanded(e) => e.hash(state),
        }
    }
}

impl<T> Ord for Unescaped<T>
where
    T: Eq + Ord + EqStr + OrdStr,
    Self: Eq + PartialOrd,
{
    fn cmp(&self, other: &Unescaped<T>) -> Ordering {
        match (self, other) {
            (Unescaped::Literal(t1), Unescaped::Literal(t2)) => Ord::cmp(t1, t2),
            (Unescaped::Expanded(e1), Unescaped::Expanded(e2)) => e1.cmp(e2),
            (Unescaped::Literal(t), Unescaped::Expanded(e)) => OrdStr::cmp(t, e.as_str()),
            (Unescaped::Expanded(e), Unescaped::Literal(t)) => OrdStr::cmp(t, e.as_str()).reverse(),
        }
    }
}

impl<T> PartialEq<Unescaped<T>> for Unescaped<T>
where
    T: for<'a> PartialEq<&'a str>,
    T: PartialEq<T>,
{
    fn eq(&self, other: &Unescaped<T>) -> bool {
        match (self, other) {
            (Unescaped::Literal(t1), Unescaped::Literal(t2)) => t1 == t2,
            (Unescaped::Expanded(e1), Unescaped::Expanded(e2)) => e1 == e2,
            (Unescaped::Literal(t1), Unescaped::Expanded(e2)) => *t1 == e2.as_str(),
            (Unescaped::Expanded(e1), Unescaped::Literal(t2)) => *t2 == e1.as_str(),
        }
    }
}

impl<T> PartialEq<&str> for Unescaped<T>
where
    T: for<'a> PartialEq<&'a str>,
{
    fn eq(&self, other: &&str) -> bool {
        match self {
            Unescaped::Literal(t) => *t == *other,
            Unescaped::Expanded(e) => e == other,
        }
    }
}

impl<'a, 'b, T> PartialEq<Unescaped<T>> for &'a str
where
    T: PartialEq<&'b str>,
    'a: 'b,
{
    fn eq(&self, other: &Unescaped<T>) -> bool {
        match other {
            Unescaped::Literal(t) => *t == *self,
            Unescaped::Expanded(e) => self == e,
        }
    }
}

impl<T> PartialEq<String> for Unescaped<T>
where
    T: PartialEq<String>,
{
    fn eq(&self, other: &String) -> bool {
        match self {
            Unescaped::Literal(t) => t == other,
            Unescaped::Expanded(e) => e == other,
        }
    }
}

impl<T> PartialEq<Unescaped<T>> for String
where
    T: PartialEq<String>,
{
    fn eq(&self, other: &Unescaped<T>) -> bool {
        match other {
            Unescaped::Literal(t) => t == self,
            Unescaped::Expanded(e) => self == e,
        }
    }
}

impl<T> PartialOrd<Unescaped<T>> for Unescaped<T>
where
    T: for<'a> PartialOrd<&'a str>,
    for<'a> &'a str: PartialOrd<T>,
    T: PartialOrd<T>,
    Self: PartialEq,
{
    fn partial_cmp(&self, other: &Unescaped<T>) -> Option<Ordering> {
        match (self, other) {
            (Unescaped::Literal(t1), Unescaped::Literal(t2)) => t1.partial_cmp(t2),
            (Unescaped::Expanded(e1), Unescaped::Expanded(e2)) => e1.partial_cmp(e2),
            (Unescaped::Literal(t), Unescaped::Expanded(e)) => t.partial_cmp(&e.as_str()),
            (Unescaped::Expanded(e), Unescaped::Literal(t)) => {
                PartialOrd::<T>::partial_cmp(&e.as_str(), t)
            }
        }
    }
}

impl<T> PartialOrd<&str> for Unescaped<T>
where
    T: for<'a> PartialOrd<&'a str>,
    Self: for<'a> PartialEq<&'a str>,
{
    fn partial_cmp(&self, other: &&str) -> Option<Ordering> {
        match self {
            Unescaped::Literal(t) => t.partial_cmp(other),
            Unescaped::Expanded(e) => e.as_str().partial_cmp(*other),
        }
    }
}

impl<T> PartialOrd<Unescaped<T>> for &str
where
    Self: PartialOrd<T>,
    Self: for<'c> PartialEq<Unescaped<T>>,
{
    fn partial_cmp(&self, other: &Unescaped<T>) -> Option<Ordering> {
        match other {
            Unescaped::Literal(t) => self.partial_cmp(t),
            Unescaped::Expanded(e) => PartialOrd::<&str>::partial_cmp(self, &e.as_str()),
        }
    }
}

#[derive(Debug)]
enum UnescapedBufInner<B> {
    Literal(B),
    Expanded(StringBuf),
}

/// A [`Buf`] implementation for [`Unescaped`].
///
/// # Example
///
/// ```
/// use bufjson::{Buf, IntoBuf, lexical::{Token, UnescapedBuf, fixed::FixedAnalyzer}};
///
/// let mut lexer = FixedAnalyzer::new(&b"123.456"[..]);
///
/// assert_eq!(Token::Num, lexer.next());
///
/// let content = lexer.content();
/// let unescaped = content.unescaped();
/// let mut buf: UnescapedBuf<_> = unescaped.into_buf();
///
/// buf.advance(4);
/// assert_eq!(3, buf.remaining());
/// assert_eq!(b"456", buf.chunk());
/// ```
#[derive(Debug)]
pub struct UnescapedBuf<B>(UnescapedBufInner<B>);

impl<B: Buf> Buf for UnescapedBuf<B> {
    #[inline]
    fn advance(&mut self, n: usize) {
        match &mut self.0 {
            UnescapedBufInner::Literal(b) => b.advance(n),
            UnescapedBufInner::Expanded(e) => e.advance(n),
        }
    }

    #[inline]
    fn chunk(&self) -> &[u8] {
        match &self.0 {
            UnescapedBufInner::Literal(b) => b.chunk(),
            UnescapedBufInner::Expanded(e) => e.chunk(),
        }
    }

    #[inline]
    fn remaining(&self) -> usize {
        match &self.0 {
            UnescapedBufInner::Literal(b) => b.remaining(),
            UnescapedBufInner::Expanded(e) => e.remaining(),
        }
    }

    #[inline]
    fn try_copy_to_slice(&mut self, dst: &mut [u8]) -> Result<(), crate::BufUnderflow> {
        match &mut self.0 {
            UnescapedBufInner::Literal(b) => b.try_copy_to_slice(dst),
            UnescapedBufInner::Expanded(e) => e.try_copy_to_slice(dst),
        }
    }
}

/// An error encountered when converting the content of a JSON number token to a Rust numeric type.
#[cfg(feature = "num")]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum NumError {
    /// The JSON token content is not formatted correctly to be converted to the target numeric
    /// type.
    ///
    /// This error variant can occur if the value to convert is not a valid JSON number, *e.g.*,
    /// `true` or `"foo"`; or if the target numeric type is an integer type such as `u64` or `i128`
    /// and the value to convert contains a decimal point, *e.g.*, `1.0`, or an exponent, *e.g.*,
    /// `2E-1`, or both, *e.g.*, `2.15e+3`.
    Format,

    /// The JSON token content is formatted correctly but represents a value that is out of range
    /// of the target numeric type.
    Range,
}

/// Text content of a JSON token.
///
/// Contains the actual textual *content* of the JSON token read from the JSON text. This is in
/// distinction to [`Token`], which only indicates the *type* of the token.
///
/// For example, consider the following JSON text:
///
/// ```json
/// "foo"
/// ```
///
/// The above JSON text contains one token whose type is [`Token::Str`] and whose content is
/// `"foo"`.
pub trait Content: fmt::Debug {
    /// Type that contains the literal string of the token exactly as it appears in the JSON text.
    type Literal<'a>: IntoBuf
    where
        Self: 'a;

    /// Returns the literal content of the token exactly as it appears in the JSON text.
    ///
    /// # Static content tokens
    ///
    /// For token types with a static text content, *e.g.*, [`Token::NameSep`], the value returned
    /// is the static content, *e.g.*, `:`.
    ///
    /// # Numbers
    ///
    /// For number tokens, the value returned is the literal text of the number token.
    ///
    /// # Strings
    ///
    /// For string tokens, the value returned is the literal text of the string token *including*
    /// the opening and closing double quote (`"`) characters. Therefore, every string token has
    /// length at least two and the unquoted value can be extracted by dropping the first and last
    /// characters.
    ///
    /// Because the return value contains the entire literal string token as it appears in the JSON
    /// text, any escape sequences the string may contain are not expanded. This has the benefit
    /// of supporting the following use cases: allowing lexical analyzer implementations to minimize
    /// or eliminate allocations when returning token values; and allowing applications to observe
    /// or edit a stream of JSON tokens without making any unintended changes to the raw JSON input.
    ///
    /// Some applications need to have escape sequences expanded in order to work with normalized
    /// strings. For example, it's pretty hard to reliably do a dictionary lookup for the name
    /// `"foo"` if the literal value might be `"fo\u006f"`, `"f\u006f\u006f"`, `"\u0066oo"`, *etc.*
    /// To check if the string contains an escape sequence, use [`is_escaped`]; and to obtain the
    /// normalized value with all escape sequences expanded, use [`unescaped`].
    ///
    /// [`is_escaped`]: method@Self::is_escaped
    /// [`unescaped`]: method@Self::unescaped
    ///
    /// # Whitespace
    ///
    /// For whitespace tokens, the value returned is the literal string of whitespace characters.
    ///
    /// # End of file
    ///
    /// For the pseudo-token [`Token::Eof`], the value is the empty string.
    fn literal<'a>(&'a self) -> Self::Literal<'a>;

    /// Returns the number of bytes in the [`literal`] value.
    ///
    /// [`literal`]: method@Self::literal
    fn literal_len(&self) -> usize {
        self.literal().into_buf().remaining()
    }

    /// Indicates whether the token content contains escape sequences.
    ///
    /// This method must always return `false` for all token types except [`Token::Str`]. For
    /// [`Token::Str`], it must return `true` if the literal text of the string token contains at
    /// least one escape sequence, and `false` otherwise.
    fn is_escaped(&self) -> bool;

    /// Returns a normalized version of [`literal`] with all escape sequences in the JSON text fully
    /// expanded.
    ///
    /// For non-string tokens, and string tokens for which [`is_escaped`] returns `false`, this
    /// method returns an [`Unescaped::Literal`] containing the same value returned by [`literal`].
    ///
    /// For string tokens with one or more escape sequences, this method returns an
    /// [`Unescaped::Expanded`] containing a normalized version of the string value with the escape
    /// sequences expanded. An allocation will be triggered by this expansion, so it may be
    /// preferable to cache the value returned rather than calling this method repeatedly on the
    /// same content.
    ///
    /// As described in the [JSON spec][rfc], the following escape sequence expansions are done:
    ///
    /// | Sequence | Expands to |
    /// |-|-|
    /// | `\"` | Quotation mark, `"`, U+0022 |
    /// | `\\` | Reverse solidus, `\`, U+005c |
    /// | `\/` | Solidus, `/`, U+002f |
    /// | `\b` | Backspace, U+0008 |
    /// | `\f` | Form feed, U+000c |
    /// | `\n` | Line feed, U+000a |
    /// | `\r` | Carriage return, U+000d |
    /// | `\t` | Horizontal tab, U+0009 |
    /// | `\uXXXX` | Any Unicode character in basic multilingual plane, U+0000 through U+ffff |
    /// | `\uHHHH\uLLLL` | Unicode character outside the basic multilingual plane represented as a high/low surrogate pair |
    ///
    /// [`literal`]: method@Self::literal
    /// [`is_escaped`]: method@Self::is_escaped
    /// [rfc]: https://datatracker.ietf.org/doc/html/rfc8259
    fn unescaped<'a>(&'a self) -> Unescaped<Self::Literal<'a>>;

    /// Compares the token content to another string, expanding embedded escape sequences.
    ///
    /// For non-string tokens, and string tokens for which [`is_escaped`] returns `false`, this
    /// method returns the equivalent of `self.literal().cmp(other)`.
    ///
    /// For string tokens with one or more escape sequences, this method compares a normalized
    /// version of the string value with escape sequences expanded to `other`. It gives the same
    /// result as `self.unescaped().cmp(other)`, but is faster because it avoids the accompanying
    /// allocations and copying.
    ///
    /// Note two important details. First, no unescaping is done on `other`, which is assumed to
    /// contain a string with escape sequences fully expanded. Second, apart from expanding escape
    /// sequences, `self` is compared byte-for-byte against `other`, so if `self` contains the
    /// content of a string token, then `other` will need to begin and end with a quotation mark
    /// character, `"` (U+0022) in order to compare equal.
    ///
    /// # Performance considerations
    ///
    /// Implementations should never allocate.
    ///
    /// Since this method is marginally less efficient than comparing directly against the literal
    /// value, it is preferable to use it only when `is_escaped` returns `true`.
    ///
    /// [`is_escaped`]: method@Self::is_escaped
    #[inline(always)]
    fn unescaped_cmp(&self, other: &str) -> Ordering {
        unescaped_cmp(self.literal(), other)
    }

    /// Converts the token content to a 64-bit signed integer.
    ///
    /// For a number token, this method will return `Ok` if the content does not have a decimal
    /// point or exponent and represents an integer within the range of `i64`. For other number
    /// tokens, the result is an `Err`. For non-number tokens, the result is always
    /// `Err(NumError::Format)`.
    ///
    /// # Performance considerations
    ///
    /// Implementations should never allocate. Some implementations may copy a small number of bytes
    /// on stack if the representation is non-contiguous.
    #[cfg(feature = "num")]
    #[inline]
    fn parse_i64(&self) -> Result<i64, NumError> {
        parse_i64(self.literal())
    }

    /// Converts the token content to a 64-bit unsigned integer.
    ///
    /// For a number token, this method will return `Ok` if the content does not have a leading
    /// minus sign, a decimal point, or an exponent, and represents an integer within the range of
    /// `u64`. For other number tokens, the result is an `Err`. For non-number tokens, the result
    /// is always `Err(NumError::Format)`.
    ///
    /// # Performance considerations
    ///
    /// Implementations should never allocate. Some implementations may copy a small number of bytes
    /// on stack if the representation is non-contiguous.
    #[cfg(feature = "num")]
    #[inline]
    fn parse_u64(&self) -> Result<u64, NumError> {
        parse_u64(self.literal())
    }

    /// Converts the token content to a 128-bit signed integer.
    ///
    /// For a number token, this method will return `Ok` if the content does not have a decimal
    /// point or exponent and represents an integer within the range of `i128`. For other number
    /// tokens, the result is an `Err`. For non-number tokens, the result is always
    /// `Err(NumError::Format)`.
    ///
    /// # Performance considerations
    ///
    /// Implementations should never allocate. Some implementations may copy a small number of bytes
    /// on stack if the representation is non-contiguous.
    #[cfg(feature = "num_ext")]
    #[inline]
    fn parse_i128(&self) -> Result<i128, NumError> {
        parse_i128(self.literal())
    }

    /// Converts the token content to a 64-bit IEEE double precision floating point number.
    ///
    /// For a number token, this method will return `Ok` if the number value in the text is within
    /// the range of an `f64`. If the content is within the range of an `f64` but not representable
    /// due to precision limitations, `Ok` is returned with the nearest representable `f64` value
    /// (rounded according to IEEE 754 round-to-nearest-even). For other number tokens, the result
    /// is `Err(NumError::Range)`. For non-number tokens, the result is always
    /// `Err(NumError::Format)`.
    #[cfg(feature = "num")]
    #[inline]
    fn parse_f64(&self) -> Result<f64, NumError> {
        parse_f64(self.literal())
    }
}

/// Character or class of characters expected at the next input position of a JSON text.
///
/// This enumeration provides detail information for [`ErrorKind::UnexpectedByte`].
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Expect {
    /// Any token boundary character.
    ///
    /// One of:
    /// - `'{'` (opening brace, U+007B)
    /// - `'}'` (closing brace, U+007D)
    /// - `'['` (opening bracket, U+005B)
    /// - `']'` (closing bracket, U+005D)
    /// - `':'` (colon, U+003A)
    /// - `','` (comma, U+002C)
    /// - `' '` (space, U+0020)
    /// - `'\t'` (horizontal tab, U+0009)
    /// - `'\n'` (line feed, U+000A)
    /// - `'\r'` (carriage return, U+000D).
    Boundary,

    /// A specific character.
    Char(char),

    /// Any decimal digit character, `'0'`..`'9'` (U+0030..U+0039).
    Digit,

    /// Any decimal digit character ([`Digit`]); the dot or period character `'.'` (U+002E); one of
    /// the two exponent indicator characters 'E' (U+0045) or 'e' (U+0065); or any token boundary
    /// character ([`Boundary`]).
    ///
    /// [`Boundary`]: Expect::Boundary
    /// [`Digit`]: Expect::Digit
    DigitDotExpOrBoundary,

    /// Any decimal digit character ([`Digit`]); or one of the two exponent indicator characters 'E'
    /// (U+0045) or 'e' (U+0065); or any token boundary character ([`Boundary`]).
    ///
    /// [`Digit`]: Expect::Digit
    /// [`Boundary`]: Expect::Boundary
    DigitExpOrBoundary,

    /// Any decimal digit character ([`Digit`]) or token boundary character ([`Boundary`]).
    ///
    /// [`Digit`]: Expect::Digit
    /// [`Boundary`]: Expect::Boundary
    DigitOrBoundary,

    /// Any decimal digit character ([`Digit`]) or one of the two exponent sign characters `'+'`
    /// (U+002B) or `'-'` (U+002D).
    ///
    /// [`Digit`]: Expect::Digit
    DigitOrExpSign,

    /// The dot or period character `'.'` (U+002E); one of the two exponent indicator characters 'E'
    /// (U+0045) or 'e' (U+0065); or any token boundary character ([`Boundary`]).
    ///
    /// [`Boundary`]: Expect::Boundary
    DotExpOrBoundary,

    /// Any character that completes a short-form escape sequence or starts a Unicode escape
    /// sequence.
    ///
    /// One of:
    /// - `'"'` (double quotation mark, U+0022)
    /// - `'\\'` (reverse solidus, U+005C)
    /// - `'/'` (solidus, U+002F)
    /// - `'b'` (lowercase 'b', U+0062)
    /// - `'f'` (lowercase 'f', U+0066)
    /// - `'n'` (lowercase 'n', U+006E)
    /// - `'r'` (lowercase 'r', U+0072)
    /// - `'t'` (lowercase 't', U+0074)
    /// - `'u'` (lowercase 'u', U+0075)
    EscChar,

    /// Any character that is valid in a JSON string token, or the string token termination
    /// character `'"'` (double quotation mark, U+0022).
    ///
    /// This essentially means any valid Unicode character at or above the space `' '` (U+0020).
    StrChar,

    /// Any character that validly starts a JSON token.
    ///
    /// One of:
    ///
    /// - A boundary character ([`Boundary`])
    /// - A digit ([`Digit`])
    /// - `'"'` (double quotation mark, U+0022)
    /// - `'f'` (U+0066)
    /// - `'n'` (U+006E)
    /// - `'t'` (U+0074)
    ///
    /// [`Digit`]: Expect::Digit
    /// [`Boundary`]: Expect::Boundary
    TokenStartChar,

    /// Any hexadecimal digit character allowed in a Unicode escape sequence.
    ///
    /// One of:
    /// - A decimal digit character ([`Digit`])
    /// - An uppercase letter `'A'`..`'F'` (U+0041..U+0046)
    /// - A lowercase letter `'a'`..`'f'` (U+0061..U+0066)
    ///
    /// [`Digit`]: Expect::Digit
    UnicodeEscHexDigit,
}

impl fmt::Display for Expect {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Boundary => f.write_str("boundary character or EOF"),
            Self::Char(c) => write!(f, "character '{c}'"),
            Self::Digit => f.write_str("digit character '0'..'9'"),
            Self::DigitDotExpOrBoundary => f.write_str("digit character '0'..'9', boundary character, or EOF"),
            Self::DigitExpOrBoundary => f.write_str("digit character '0'..'9', decimal point character '.', exponent character 'E' or 'e', boundary character, or EOF"),
            Self::DigitOrBoundary => f.write_str("digit character '0'..'9', boundary character, or EOF"),
            Self::DigitOrExpSign => f.write_str("exponent sign character '+' or '-', or exponent digit character '0'..'9'"),
            Self::DotExpOrBoundary => f.write_str("decimal point character '.', 'exponent character 'E' or 'e', boundary character, or EOF"),
            Self::EscChar => f.write_str("escape sequence character '\\', '\"', '/', 'r', 'n', 't', or 'u'"),
            Self::StrChar => f.write_str("string character"),
            Self::TokenStartChar => f.write_str("token start character"),
            Self::UnicodeEscHexDigit => f.write_str("Unicode escape sequence hex digit '0'..'9', 'A'..'F', or 'a'..'f'"),
        }
    }
}

/// Category of error that can occur while tokenizing a JSON text.
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum ErrorKind {
    /// A Unicode escape sequence of the form `\uLLLL` or `\uHHHH\uLLLL` within a
    /// [string token][Token::Str] has a bad Unicode surrogate.
    BadSurrogate {
        /// The 16-bit number read from the first Unicode escape sequence.
        ///
        /// This will always be a valid Unicode surrogate code unit, either a high surrogate or a
        /// low surrogate code pair.
        first: u16,

        /// The 16-bit number read from Unicode escape sequence that immediately followed the first
        /// escape sequence (if there was one).
        ///
        /// This may be a Unicode high surrogate code unit, or it may be a valid Unicode code point,
        /// but will never be a low surrogate code unit.
        second: Option<u16>,
    },

    /// A UTF-8 continuation byte within a [string token][Token::Str] has an invalid value.
    BadUtf8ContByte {
        /// Length of the UTF-8 byte sequence.
        seq_len: u8,

        /// Zero-based offset of the invalid byte value within the sequence.
        offset: u8,

        /// Invalid byte value.
        value: u8,
    },

    /// The underlying source of JSON text (*e.g.* a file or stream) reported an error when the
    /// lexical analyzer tried to read the next block of JSON text to analyze.
    Read,

    /// An unexpected byte was encountered in a token.
    UnexpectedByte {
        /// Type of token within which the unexpected byte was encountered.
        token: Option<Token>,

        /// Character or characters expected.
        expect: Expect,

        /// The unexpected byte actually encountered.
        actual: u8,
    },

    /// The JSON text ended abruptly in the middle of an incomplete lexical token.
    UnexpectedEof(Token),
}

impl ErrorKind {
    pub(crate) fn bad_surrogate(first: u16, second: Option<u16>) -> ErrorKind {
        ErrorKind::BadSurrogate { first, second }
    }

    pub(crate) fn bad_utf8_cont_byte(seq_len: u8, offset: u8, value: u8) -> ErrorKind {
        ErrorKind::BadUtf8ContByte {
            seq_len,
            offset,
            value,
        }
    }

    pub(crate) fn expect_boundary(token: Token, actual: u8) -> ErrorKind {
        let expect = Expect::Boundary;

        ErrorKind::UnexpectedByte {
            token: Some(token),
            expect,
            actual,
        }
    }

    pub(crate) fn expect_char(token: Token, actual: u8, expect: char) -> ErrorKind {
        let expect = Expect::Char(expect);

        ErrorKind::UnexpectedByte {
            token: Some(token),
            expect,
            actual,
        }
    }

    pub(crate) fn expect_digit(actual: u8) -> ErrorKind {
        let expect = Expect::Digit;

        ErrorKind::UnexpectedByte {
            token: Some(Token::Num),
            expect,
            actual,
        }
    }

    pub(crate) fn expect_digit_dot_exp_or_boundary(actual: u8) -> ErrorKind {
        let expect = Expect::DigitDotExpOrBoundary;

        ErrorKind::UnexpectedByte {
            token: Some(Token::Num),
            expect,
            actual,
        }
    }

    pub(crate) fn expect_digit_exp_or_boundary(actual: u8) -> ErrorKind {
        let expect = Expect::DigitExpOrBoundary;

        ErrorKind::UnexpectedByte {
            token: Some(Token::Num),
            expect,
            actual,
        }
    }

    pub(crate) fn expect_digit_or_boundary(actual: u8) -> ErrorKind {
        let expect = Expect::DigitOrBoundary;

        ErrorKind::UnexpectedByte {
            token: Some(Token::Num),
            expect,
            actual,
        }
    }

    pub(crate) fn expect_dot_exp_or_boundary(actual: u8) -> ErrorKind {
        let expect = Expect::DotExpOrBoundary;

        ErrorKind::UnexpectedByte {
            token: Some(Token::Num),
            expect,
            actual,
        }
    }

    pub(crate) fn expect_esc_char(actual: u8) -> ErrorKind {
        let expect = Expect::EscChar;

        ErrorKind::UnexpectedByte {
            token: Some(Token::Str),
            expect,
            actual,
        }
    }

    pub(crate) fn expect_exp_sign_or_digit(actual: u8) -> ErrorKind {
        let expect = Expect::DigitOrExpSign;

        ErrorKind::UnexpectedByte {
            token: Some(Token::Num),
            expect,
            actual,
        }
    }

    pub(crate) fn expect_str_char(actual: u8) -> ErrorKind {
        let expect = Expect::StrChar;

        ErrorKind::UnexpectedByte {
            token: Some(Token::Str),
            expect,
            actual,
        }
    }

    pub(crate) fn expect_token_start_char(actual: u8) -> ErrorKind {
        let expect = Expect::TokenStartChar;

        ErrorKind::UnexpectedByte {
            token: None,
            expect,
            actual,
        }
    }

    pub(crate) fn expect_unicode_esc_hex_digit(actual: u8) -> ErrorKind {
        let expect = Expect::UnicodeEscHexDigit;

        ErrorKind::UnexpectedByte {
            token: Some(Token::Str),
            expect,
            actual,
        }
    }

    pub(crate) fn fmt_at(&self, f: &mut fmt::Formatter, pos: Option<&Pos>) -> fmt::Result {
        match self {
            Self::BadSurrogate {
                first: lo,
                second: None,
            } if (0xdc00..=0xdfff).contains(lo) => {
                write!(
                    f,
                    "bad Unicode escape sequence: low surrogate '\\u{lo:04X}' does not follow a high surrogate"
                )?;
            }

            Self::BadSurrogate {
                first: hi,
                second: None,
            } => {
                write!(
                    f,
                    "bad Unicode escape sequence: high surrogate '\\u{hi:04X}' not followed by low surrogate"
                )?;
            }

            Self::BadSurrogate {
                first: hi,
                second: Some(lo),
            } => {
                write!(
                    f,
                    "bad Unicode escape sequence surrogate pair: high surrogate '\\u{hi:04X}' followed by invalid low surrogate '\\u{lo:04X}'"
                )?;
            }

            Self::BadUtf8ContByte {
                seq_len,
                offset,
                value,
            } => {
                write!(
                    f,
                    "bad UTF-8 continuation byte 0x{value:02x} in {seq_len}-byte UTF-8 sequence (byte #{offset})"
                )?;
            }

            Self::Read => write!(f, "read error")?,

            Self::UnexpectedByte {
                token,
                expect,
                actual,
            } if (b' '..=0x7e).contains(actual) => {
                write!(
                    f,
                    "expected {expect} but got character '{}' (ASCII 0x{actual:02x})",
                    *actual as char
                )?;
                if let Some(t) = token {
                    write!(f, " in {t} token")?;
                }
            }

            Self::UnexpectedByte {
                token,
                expect,
                actual,
            } => {
                write!(f, "expected {expect} but got byte {actual:02x}")?;
                if let Some(t) = token {
                    write!(f, " in {t} token")?;
                }
            }

            Self::UnexpectedEof(token) => {
                write!(f, "unexpected EOF in {token} token")?;
            }
        };

        if let Some(p) = pos {
            write!(f, " at {}", *p)?;
        }

        Ok(())
    }
}

impl fmt::Display for ErrorKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.fmt_at(f, None)
    }
}

/// An error encountered during lexical analysis of JSON text.
pub trait Error: core::error::Error + Send + Sync {
    /// Returns the category of error.
    ///
    /// If the category is [`ErrorKind::Read`], the underlying I/O error is available from the
    /// [`source`] method.
    ///
    /// [`source`]: method@core::error::Error::source
    fn kind(&self) -> ErrorKind;

    /// Returns the position in the JSON text where the error was encountered.
    ///
    /// The error position returned by this method is more precise than the position returned by
    /// [`Analyzer::pos`]. This is because [`Analyzer::pos`] returns the position of the start of
    /// the token returned by [`Analyzer::next`], while this method provides the granular position
    /// where the error occurred.
    ///
    /// For example, consider the following lexically-invalid JSON text:
    ///
    /// ```json
    /// "foo
    /// ```
    ///
    /// The above text contains an unterminated string token. A lexical analyzer tokenizing this
    /// text will return:
    ///
    /// 1. [`Token::Err`] on the first call to its [`next`][`Analyzer::next`] method, since the very
    ///    first token has an error.
    /// 2. The position of the first `"` character in the text on a subsequent call to its
    ///    [`pos`][Analyzer::pos] method, because that is the position of the start of the token
    ///    returned by [`next`][Analyzer::next].
    /// 3. An `Err` result containing an `Error` whose `pos` method (this method) returns the
    ///    position immediately right of the last `o` character in the text, because this is where
    ///    the actual error, an unexpected end of file, was encountered.
    fn pos(&self) -> &Pos;
}

/// Lexical analyzer for JSON text.
///
/// Converts JSON text into a stream of lexical tokens.
pub trait Analyzer {
    /// The type that contains token content, returned by the [`content`] and [`try_content`]
    /// methods.
    ///
    /// [`content`]: method@Self::content
    /// [`try_content`]: method@Self::try_content
    type Content: Content;

    /// The type that reports errors during the lexical analysis process, returned by the [`err`]
    /// and [`try_content`] methods.
    ///
    /// [`err`]: method@Self::err
    /// [`try_content`]: method@Self::try_content
    type Error: Error;

    /// Recognizes the next lexical token in the JSON text.
    ///
    /// Returns the type of the token recognized. After this method returns, the text content of the
    /// recognized token can be obtained by calling the [`content`] method.
    ///
    /// If the end of the JSON text is reached, without encountering an error, the token type
    /// returned is `Token::Eof`; and this token type is also returned on all subsequent calls. For
    /// `Token::Eof`, the [`content`] method returns a value containing empty text.
    ///
    /// If an error is encountered while analyzing the JSON text, the token type returned is
    /// `Token::Err`; and this token type is also returned on all subsequent calls. For
    /// `Token::Err`, the [`content`] method panics.
    ///
    /// [`content`]: method@Self::content
    ///
    /// # Performance considerations
    ///
    /// Implementations of this method should not trigger an allocation unless an allocation is
    /// required to read in more input from an underlying source of JSON text, *e.g.* a file or
    /// stream. Outside this singular scenario, the process of recognizing the next JSON token
    /// should never allocate.
    fn next(&mut self) -> Token;

    /// Returns the text content of the non-error token most recently recognized by [`next`].
    ///
    /// If called before any call to `next`, returns empty content.
    ///
    /// If called repeatedly between calls to `next`, subsequent calls return a value equivalent to
    /// the value returned by the first call.
    ///
    /// # Panics
    ///
    /// Panics if the token most recently returned by `next` was [`Token::Err`].
    ///
    /// # Performance considerations
    ///
    /// A call to this method may allocate, although implementations should avoid allocation if
    /// possible. Therefore, it is best to cache the result of this method rather than calling it
    /// repeatedly to fetch the same value between calls to `next`. If the text content of the last
    /// token is not needed for some reason, the best course is not to call this method at all.
    ///
    /// [`next`]: method@Self::next
    #[inline]
    fn content(&self) -> Self::Content {
        self.try_content().unwrap()
    }

    /// Returns the error value associated with the error token most recently returned by [`next`].
    ///
    /// If called repeatedly after a call to `next` that returned [`Token::Err`], subsequent calls
    /// return a value equivalent to the value returned by the first call.
    ///
    /// # Panics
    ///
    /// If the token most recently returned by `next` was not [`Token::Err`].
    ///
    /// [`next`]: method@Self::next
    #[inline]
    fn err(&self) -> Self::Error {
        self.try_content().unwrap_err()
    }

    /// Returns the position of the lexical analyzer's cursor within the JSON text.
    ///
    /// Before the first call to [`next`], the return value is [`Pos::default`].
    ///
    /// After `next` is called, the return value is the position of the first character of the
    /// recognized token. In the case where `next` returns `Token::Err`, the return value is the
    /// position of the first character of the token that was being recognized at the time when the
    /// error was detected.
    ///
    /// [`next`]: method@Self::next
    fn pos(&self) -> &Pos;

    /// Returns the content or error value associated with the token most recently recognized by
    /// [`next`].
    ///
    /// If the most recent call to `next` returned [`Token::Err`], an `Err` result is returned.
    /// Otherwise, an `Ok` result containing the text content of the recognized lexical token is
    /// returned.
    ///
    /// If called before any call to `next`, this method returns an `Ok` result containing empty
    /// text.
    ///
    /// If called repeatedly between calls to `next`, subsequent calls return a value equivalent to
    /// the value returned by the first call.
    ///
    /// When the value of the most recent token is known, calling [`content`] or [`err`] directly,
    /// as the case may be, will produce cleaner and more compact code than calling this method and
    /// unwrapping the result.
    ///
    /// # Performance considerations
    ///
    /// A call to this method may allocate, although implementations should avoid allocation if
    /// possible. Therefore, it is best to cache the result of this method rather than calling it
    /// repeatedly to fetch the same value between calls to `next`. If the text content of the last
    /// token is not needed for some reason, the best course is not to call this method at all.
    ///
    /// [`next`]: method@Self::next
    /// [`content`]: method@Self::content
    /// [`err`]: method@Self::err
    fn try_content(&self) -> Result<Self::Content, Self::Error>;
}

#[inline(always)]
fn hex2u32(b: u8) -> u32 {
    if !b.is_ascii_hexdigit() {
        panic!("byte 0x{b:02x?} is not an ASCII hex digit")
    }

    ((b & 0xF) + (b >> 6) * 9) as u32
}

/// Expands escape sequences in the content of a valid JSON string.
///
/// The input to unescape must contain the literal content of a valid JSON string value, as it
/// appears in the JSON text (with or without the surrounding double quotation mark characters).
///
/// The unescaped text is appended to the given [`Sink`].
///
/// # Panics
///
/// Panics if the input contains an invalid or unterminated JSON escape sequence.
///
/// # Examples
///
/// Unescape a string with surrounding double quote characters...
///
/// ```
/// use bufjson::lexical::unescape;
///
/// let mut dst = Vec::new();
/// unescape(r#""foo\nbar""#, &mut dst);
/// assert_eq!(
///    &br#""foo
/// bar""#[..],
///     &dst);
/// ```
///
/// ...Or without them...
///
/// ```
/// use bufjson::lexical::unescape;
///
/// let mut dst = Vec::new();
/// unescape(r#"hello\u002c\u0020world"#, &mut dst);
/// assert_eq!(&b"hello, world"[..], &dst);
/// ```
pub fn unescape(literal: impl IntoBuf, dst: &mut impl Sink) {
    let mut literal = literal.into_buf();

    // Reserve bytes in the destination. If the incoming literal has at least one escape sequence,
    // the length should shrink by at least one byte. We therefore reserve this many bytes. If the
    // function is called on an input that contains no escape sequences, this may result in a
    // reallocation, but we're OK with this because we want to optimize for the case where the
    // caller already knows there's an escape sequence.
    if !literal.has_remaining() {
        return;
    }
    dst.reserve(literal.remaining() - 1);

    #[derive(Default)]
    struct Esc {
        len: u32, // Number of valid bytes, 1-5 (how many characters we have after first '\').
        hi: u32,  // High surrogate.
        lo: u32,  // Low surrogate.
    }
    let mut esc: Option<Esc> = None;

    loop {
        let chunk = literal.chunk();
        let (mut i, mut j) = (0usize, 0usize);

        loop {
            let b = chunk[j];
            match &mut esc {
                None if b != b'\\' => j += 1,

                None => {
                    dst.extend_from_slice(&chunk[i..j]);
                    esc = Some(Esc::default());
                    j += 1;
                    i = j;
                }

                Some(e) if e.len == 0 => {
                    let mut single = |b: u8, esc: &mut Option<Esc>| {
                        dst.push(b);
                        *esc = None;
                        j += 1;
                        i = j;
                    };

                    match b {
                        b'"' | b'\\' | b'/' => single(b, &mut esc),
                        b'b' => single(b'\x08', &mut esc),
                        b't' => single(b'\t', &mut esc),
                        b'f' => single(b'\x0c', &mut esc),
                        b'n' => single(b'\n', &mut esc),
                        b'r' => single(b'\r', &mut esc),
                        b'u' => {
                            e.len = 1;
                            j += 1;
                            i = j;
                        }
                        _ => panic!(r#"invalid escape sequence byte after '\': 0x{b:02x}"#),
                    }
                }

                Some(e) if (1..=4).contains(&e.len) => {
                    let shift = 4 * (4 - e.len);
                    e.hi |= hex2u32(b) << shift;
                    e.len += 1;
                    if e.len == 5 {
                        match e.hi {
                            0xd800..=0xdbff => (),

                            0xdc00..=0xdfff => panic!(
                                "Unicode escape low surrogate without preceding high surrogate: 0x{:02x}",
                                e.hi
                            ),

                            _ => {
                                append_code_point(e.hi, dst);
                                esc = None;
                            }
                        }
                    }
                    j += 1;
                    i = j;
                }

                Some(e) if e.len == 5 && b == b'\\' => {
                    e.len = 6;
                    j += 1;
                    i = j;
                }

                Some(e) if e.len == 5 => panic!(
                    r#"expected '\' to start low surrogate Unicode escape after high surrogate 0x{:04x}, found byte 0x{b:02x}"#,
                    e.hi
                ),

                Some(e) if e.len == 6 && b == b'u' => {
                    e.len = 7;
                    j += 1;
                    i = j;
                }

                Some(e) if e.len == 6 => panic!(
                    r#"expected '\u' to start low surrogate Unicode escape after high surrogate 0x{:04x}, found '\' followed by byte {b:02x}"#,
                    e.hi
                ),

                Some(e) if (7..=10).contains(&e.len) => {
                    let shift = 4 * (10 - e.len);
                    e.lo |= hex2u32(b) << shift;
                    e.len += 1;
                    if e.len == 11 {
                        match e.lo {
                            0xdc00..=0xdfff => {
                                let code_point =
                                    0x10000 + (((e.hi - 0xd800) << 10) | (e.lo - 0xdc00));
                                append_code_point(code_point, dst);
                                esc = None;
                            }

                            _ => {
                                panic!(
                                    "Unicode escape high surrogate not followed by low surrogate: 0x{:04x} and then 0x{:04x}",
                                    e.hi, e.lo
                                )
                            }
                        }
                    }
                    j += 1;
                    i = j;
                }

                _ => unreachable!(),
            }

            if j == chunk.len() {
                break;
            }
        }

        dst.extend_from_slice(&chunk[i..j]);
        literal.advance(chunk.len());
        if !literal.has_remaining() {
            break;
        }
    }

    if esc.is_some() {
        panic!("unexpected end of input within Unicode escape sequence");
    }
}

/// Compares valid JSON token content with a string, expanding escapes in the token content.
///
/// For non-string tokens, and string tokens that do not contain escape sequences, this method is
/// equivalent to comparing the literal bytes of the content directly with `other`.
///
/// For string tokens with one or more escape sequences, this method compares a normalized
/// version of the string value with escape sequences expanded to `other`. It gives the same
/// result as first rendering the token content as a string using the [`unescape`] function, and
/// then comparing it against `other`.
///
/// Note two important details. First, no unescaping is done on `other`, which is assumed to
/// contain a string with escape sequences fully expanded. Second, apart from expanding escape
/// sequences, `literal` is compared byte-for-byte against `other`, so if `literal` contains the
/// content of a string token, then `other` will need to begin and end with a quotation mark
/// character, `"` (U+0022) in order to compare equal.
///
/// # Performance considerations
///
/// Does not allocate.
///
/// # Panics
///
/// Panics if `literal` contains an invalid escape sequence.
///
/// # Example
///
/// ```
/// use bufjson::{IntoBuf, lexical::unescaped_cmp};
/// use std::cmp::Ordering;
///
/// assert_eq!(Ordering::Less, unescaped_cmp(r#""hello""#, r#""jello""#));
/// assert_eq!(
///     Ordering::Equal, unescaped_cmp(r#""hello\nworld! \ud83c\udf0d""#, r#""hello
/// world! 🌍""#));
/// assert_eq!(Ordering::Greater, unescaped_cmp(r#""foo""#, r#""bar""#));
/// ```
pub fn unescaped_cmp(literal: impl IntoBuf, other: &str) -> Ordering {
    let mut literal = literal.into_buf();
    let mut a = literal.chunk();
    let mut b = other.as_bytes();
    loop {
        match a.iter().position(|&b| b == b'\\') {
            // No escape sequences detected in the current chunk of `literal`.
            None => {
                let n = a.len();
                let o = a.cmp(&b[..n.min(b.len())]);
                if o != Ordering::Equal {
                    return o;
                }
                literal.advance(n);
                a = literal.chunk();
                b = &b[n..];
            }

            // Found an escape sequence at position `j` in the current chunk of `literal`.
            Some(j) => {
                // Compare the bytes up, and not including, the `\` that starts the escape sequence.
                let o = a[..j].cmp(&b[..j.min(b.len())]);
                if o != Ordering::Equal {
                    return o;
                } else if j >= b.len() {
                    return Ordering::Greater;
                }

                // Make sure we have the next byte following the `\` available in `a`.
                let i = if j + 1 < a.len() {
                    j + 1
                } else {
                    literal.advance(j + 1);
                    a = literal.chunk();
                    if a.is_empty() {
                        panic!("unterminated escape sequence");
                    }

                    0
                };

                // Evaluate the escape sequence.
                let y = b[j];
                match unescape_byte(a[i]) {
                    UnescapeByte::Byte(x) if x != y => return x.cmp(&y),
                    UnescapeByte::Byte(_) => {
                        literal.advance(i + 1);
                        a = literal.chunk();
                        b = &b[j + 1..];
                    }
                    UnescapeByte::Unicode => {
                        literal.advance(i + 1);
                        let (x, n) = unescape_unicode(&mut literal);
                        let n = n as usize;
                        let o = x[..n].cmp(&b[j..j + n.min(b.len() - j)]);
                        if o != Ordering::Equal {
                            return o;
                        }
                        a = literal.chunk();
                        b = &b[j + n..];
                    }
                }
            }
        }

        if a.is_empty() {
            return 0.cmp(&b.len());
        }
    }
}

fn append_code_point(code_point: u32, dst: &mut impl Sink) {
    match char::from_u32(code_point) {
        Some(c) => {
            let mut seq = [0u8; 4];
            let utf8_str = c.encode_utf8(&mut seq);
            dst.extend_from_slice(utf8_str.as_bytes());
        }

        None => unreachable!(),
    }
}

#[derive(Debug)]
pub(crate) enum UnescapeByte {
    Byte(u8),
    Unicode,
}

#[inline]
pub(crate) fn unescape_byte(b: u8) -> UnescapeByte {
    match b {
        b'"' | b'\\' | b'/' => UnescapeByte::Byte(b),
        b'b' => UnescapeByte::Byte(b'\x08'),
        b't' => UnescapeByte::Byte(b'\t'),
        b'f' => UnescapeByte::Byte(b'\x0c'),
        b'n' => UnescapeByte::Byte(b'\n'),
        b'r' => UnescapeByte::Byte(b'\r'),
        b'u' => UnescapeByte::Unicode,
        _ => panic!("invalid escape sequence: byte 0x{b:02x} cannot follow '\\'"),
    }
}

pub(crate) fn unescape_unicode(buf: &mut impl Buf) -> ([u8; 4], u32) {
    let mut digits = [0u8; 4];

    // Consume the four hex digits that are immediately part of the escape sequence.
    if buf.remaining() < 4 {
        panic!(
            "at least 4 hex digits are required to complete Unicode escape sequence, but only {} bytes remain",
            buf.remaining()
        );
    }
    buf.copy_to_slice(&mut digits);
    let mut code_point = digits.iter().fold(0u32, |acc, &b| acc << 4 | hex2u32(b));

    // If the code point is a high surrogate, then a low surrogate is expected afterward.
    if code_point & 0xfc00 == 0xd800 {
        if buf.remaining() < 6 {
            panic!(
                r#"at least 6 bytes are required for Unicode low surrogate escape sequence that follows "\u{}", but only {} bytes remain"#,
                str::from_utf8(&digits).unwrap(),
                buf.remaining(),
            )
        }
        let mut second = [0u8; 6];
        buf.copy_to_slice(&mut second);
        if second[0] != b'\\' || second[1] != b'u' {
            panic!(
                r#"low surrogate Unicode escape sequence must start with "\u", but {second:02x?} does not"#
            );
        }
        let lo = second
            .iter()
            .skip(2)
            .take(4)
            .fold(0u32, |acc, &b| acc << 4 | hex2u32(b));
        if !(0xdc00..=0xdfff).contains(&lo) {
            panic!(
                r#"high surrogate \u{} followed by invalid low surrogate \u{}"#,
                str::from_utf8(&digits).unwrap(),
                str::from_utf8(&second[2..]).unwrap()
            );
        }
        code_point = ((code_point - 0xd800) << 10 | (lo - 0xdc00)) + 0x10000;
    }

    // Convert the code point to a UTF-8 byte sequence.
    if let Some(c) = char::from_u32(code_point) {
        let mut buf = [0u8; 4];
        let s = c.encode_utf8(&mut buf);
        let n = s.len() as u32;

        (buf, n)
    } else {
        panic!("invalid Unicode escape sequence(s) produced invalid code point 0x{code_point:04x}");
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rstest::rstest;
    use std::collections::{BTreeMap, HashMap};

    #[rstest]
    #[case(Token::ArrBegin, false)]
    #[case(Token::ArrEnd, false)]
    #[case(Token::Eof, true)]
    #[case(Token::Err, false)]
    #[case(Token::LitFalse, false)]
    #[case(Token::LitNull, false)]
    #[case(Token::LitTrue, false)]
    #[case(Token::NameSep, false)]
    #[case(Token::Num, false)]
    #[case(Token::ObjBegin, false)]
    #[case(Token::ObjEnd, false)]
    #[case(Token::Str, false)]
    #[case(Token::ValueSep, false)]
    #[case(Token::White, false)]
    fn test_token_is_eof(#[case] token: Token, #[case] is_eof: bool) {
        assert_eq!(is_eof, token.is_eof());
    }

    #[rstest]
    #[case(Token::ArrBegin, false)]
    #[case(Token::ArrEnd, false)]
    #[case(Token::Eof, false)]
    #[case(Token::Err, true)]
    #[case(Token::LitFalse, false)]
    #[case(Token::LitNull, false)]
    #[case(Token::LitTrue, false)]
    #[case(Token::NameSep, false)]
    #[case(Token::Num, false)]
    #[case(Token::ObjBegin, false)]
    #[case(Token::ObjEnd, false)]
    #[case(Token::Str, false)]
    #[case(Token::ValueSep, false)]
    #[case(Token::White, false)]
    fn test_token_is_err(#[case] token: Token, #[case] is_err: bool) {
        assert_eq!(is_err, token.is_err());
    }

    #[rstest]
    #[case(Token::ArrBegin, false)]
    #[case(Token::ArrEnd, false)]
    #[case(Token::Eof, false)]
    #[case(Token::Err, false)]
    #[case(Token::LitFalse, true)]
    #[case(Token::LitNull, true)]
    #[case(Token::LitTrue, true)]
    #[case(Token::NameSep, false)]
    #[case(Token::Num, false)]
    #[case(Token::ObjBegin, false)]
    #[case(Token::ObjEnd, false)]
    #[case(Token::Str, false)]
    #[case(Token::ValueSep, false)]
    #[case(Token::White, false)]
    fn test_token_is_literal(#[case] token: Token, #[case] is_literal: bool) {
        assert_eq!(is_literal, token.is_literal());
    }

    #[rstest]
    #[case(Token::ArrBegin, false)]
    #[case(Token::ArrEnd, false)]
    #[case(Token::Eof, false)]
    #[case(Token::Err, false)]
    #[case(Token::LitFalse, true)]
    #[case(Token::LitNull, true)]
    #[case(Token::LitTrue, true)]
    #[case(Token::NameSep, false)]
    #[case(Token::Num, true)]
    #[case(Token::ObjBegin, false)]
    #[case(Token::ObjEnd, false)]
    #[case(Token::Str, true)]
    #[case(Token::ValueSep, false)]
    #[case(Token::White, false)]
    fn test_token_is_primitive(#[case] token: Token, #[case] is_primitive: bool) {
        assert_eq!(is_primitive, token.is_primitive());
    }

    #[rstest]
    #[case(Token::ArrBegin, false)]
    #[case(Token::ArrEnd, false)]
    #[case(Token::Eof, true)]
    #[case(Token::Err, true)]
    #[case(Token::LitFalse, false)]
    #[case(Token::LitNull, false)]
    #[case(Token::LitTrue, false)]
    #[case(Token::NameSep, false)]
    #[case(Token::Num, false)]
    #[case(Token::ObjBegin, false)]
    #[case(Token::ObjEnd, false)]
    #[case(Token::Str, false)]
    #[case(Token::ValueSep, false)]
    #[case(Token::White, true)]
    fn test_token_is_pseudo(#[case] token: Token, #[case] is_pseudo: bool) {
        assert_eq!(is_pseudo, token.is_pseudo());
    }

    #[rstest]
    #[case(Token::ArrBegin, false)]
    #[case(Token::ArrEnd, false)]
    #[case(Token::Eof, false)]
    #[case(Token::Err, false)]
    #[case(Token::LitFalse, false)]
    #[case(Token::LitNull, false)]
    #[case(Token::LitTrue, false)]
    #[case(Token::NameSep, true)]
    #[case(Token::Num, false)]
    #[case(Token::ObjBegin, false)]
    #[case(Token::ObjEnd, false)]
    #[case(Token::Str, false)]
    #[case(Token::ValueSep, true)]
    #[case(Token::White, false)]
    fn test_token_is_punct(#[case] token: Token, #[case] is_punct: bool) {
        assert_eq!(is_punct, token.is_punct());
    }

    #[rstest]
    #[case(Token::ArrBegin, true)]
    #[case(Token::ArrEnd, true)]
    #[case(Token::Eof, false)]
    #[case(Token::Err, false)]
    #[case(Token::LitFalse, false)]
    #[case(Token::LitNull, false)]
    #[case(Token::LitTrue, false)]
    #[case(Token::NameSep, false)]
    #[case(Token::Num, false)]
    #[case(Token::ObjBegin, true)]
    #[case(Token::ObjEnd, true)]
    #[case(Token::Str, false)]
    #[case(Token::ValueSep, false)]
    #[case(Token::White, false)]
    fn test_token_is_struct(#[case] token: Token, #[case] is_struct: bool) {
        assert_eq!(is_struct, token.is_struct());
    }

    #[rstest]
    #[case(Token::ArrBegin, false)]
    #[case(Token::ArrEnd, false)]
    #[case(Token::Eof, true)]
    #[case(Token::Err, true)]
    #[case(Token::LitFalse, false)]
    #[case(Token::LitNull, false)]
    #[case(Token::LitTrue, false)]
    #[case(Token::NameSep, false)]
    #[case(Token::Num, false)]
    #[case(Token::ObjBegin, false)]
    #[case(Token::ObjEnd, false)]
    #[case(Token::Str, false)]
    #[case(Token::ValueSep, false)]
    #[case(Token::White, false)]
    fn test_token_is_terminal(#[case] token: Token, #[case] is_terminal: bool) {
        assert_eq!(is_terminal, token.is_terminal());
    }

    #[rstest]
    #[case(Token::ArrBegin, Some("["))]
    #[case(Token::ArrEnd, Some("]"))]
    #[case(Token::Eof, None)]
    #[case(Token::Err, None)]
    #[case(Token::LitFalse, Some("false"))]
    #[case(Token::LitNull, Some("null"))]
    #[case(Token::LitTrue, Some("true"))]
    #[case(Token::NameSep, Some(":"))]
    #[case(Token::Num, None)]
    #[case(Token::ObjBegin, Some("{"))]
    #[case(Token::ObjEnd, Some("}"))]
    #[case(Token::Str, None)]
    #[case(Token::ValueSep, Some(","))]
    #[case(Token::White, None)]
    fn test_token_static_content(#[case] token: Token, #[case] static_content: Option<&str>) {
        assert_eq!(static_content, token.static_content());
    }

    #[rstest]
    #[case(Token::ArrBegin, "[")]
    #[case(Token::ArrEnd, "]")]
    #[case(Token::Eof, "EOF")]
    #[case(Token::Err, "error")]
    #[case(Token::LitFalse, "false")]
    #[case(Token::LitNull, "null")]
    #[case(Token::LitTrue, "true")]
    #[case(Token::NameSep, ":")]
    #[case(Token::Num, "number")]
    #[case(Token::ObjBegin, "{")]
    #[case(Token::ObjEnd, "}")]
    #[case(Token::Str, "string")]
    #[case(Token::ValueSep, ",")]
    #[case(Token::White, "whitespace")]
    fn test_token_display(#[case] token: Token, #[case] expect: &str) {
        assert_eq!(expect, format!("{token}"));
    }

    #[rstest]
    #[case(Unescaped::Literal("foo"), "foo")]
    #[case(Unescaped::Expanded("bar".to_string()), "bar")]
    fn test_unescaped_str_into_buf(#[case] u: Unescaped<&str>, #[case] expect: &str) {
        let mut b = u.into_buf();

        assert_eq!(expect.len(), b.remaining());
        assert_eq!(expect, str::from_utf8(b.chunk()).unwrap());

        if b.remaining() > 0 {
            b.advance(1);

            assert_eq!(expect.len() - 1, b.remaining());
            assert_eq!(&expect[1..], str::from_utf8(b.chunk()).unwrap());
        }

        let mut v = vec![0; expect.len() - 1];
        b.copy_to_slice(&mut v);

        assert_eq!(0, b.remaining());
        assert_eq!(b"", b.chunk())
    }

    #[test]
    fn test_unescaped_str() {
        let a1 = Unescaped::Literal("a");
        let b1 = Unescaped::Expanded("bb".to_string());
        let a2 = Unescaped::Expanded("a".to_string());
        let b2 = Unescaped::Literal("bb");

        assert_eq!("a", Into::<String>::into(a1.clone()));
        assert_eq!("bb", Into::<String>::into(b1.clone()));
        assert_eq!("a", Into::<String>::into(a2.clone()));
        assert_eq!("bb", Into::<String>::into(b2.clone()));

        assert!(matches!(a1.literal(), Some(&"a")));
        assert!(b1.literal().is_none());
        assert!(a2.literal().is_none());
        assert!(matches!(b2.literal(), Some(&"bb")));

        assert!(a1.expanded().is_none());
        assert!(matches!(b1.expanded(), Some("bb")));
        assert!(matches!(a2.expanded(), Some("a")));
        assert!(b2.expanded().is_none());

        assert!(a1.is_literal());
        assert!(!a1.is_expanded());
        assert!(!b1.is_literal());
        assert!(b1.is_expanded());

        assert_eq!(1, a1.len());
        assert_eq!(2, b1.len());
        assert_eq!(1, a2.len());
        assert_eq!(2, b2.len());

        let a3: &str = a1.as_ref();
        let b3: &str = b1.as_ref();
        let a4: &str = a2.as_ref();
        let b4: &str = b2.as_ref();

        assert_eq!("a", format!("{a1}"));
        assert_eq!("bb", format!("{b1}"));
        assert_eq!("a", format!("{a2}"));
        assert_eq!("bb", format!("{b2}"));

        assert_eq!("a", a3);
        assert_eq!("bb", b3);
        assert_eq!("a", a4);
        assert_eq!("bb", b4);

        let x1: &[u8] = a1.as_ref();
        let y1: &[u8] = b1.as_ref();
        let x2: &[u8] = a2.as_ref();
        let y2: &[u8] = b2.as_ref();

        assert_eq!(b"a", x1);
        assert_eq!(b"bb", y1);
        assert_eq!(b"a", x2);
        assert_eq!(b"bb", y2);

        assert_eq!(a1, a2);
        assert_eq!(a2, a1);
        assert_eq!(b1, b2);
        assert_eq!(b2, b1);

        assert_ne!(a1, b1);
        assert_ne!(b1, a1);
        assert_ne!(a1, b2);
        assert_ne!(b1, a2);

        assert!(a1 < b1);
        assert!(a1 < b2);
        assert!(a2 < b1);
        assert!(a2 < b2);
        assert!(b1 > a1);
        assert!(b1 > a2);
        assert!(b2 > a1);
        assert!(b2 > a2);

        assert_eq!("a", a1);
        assert_eq!(a1, "a");
        assert_eq!("bb", b1);
        assert_eq!(b1, "bb");
        assert_eq!("a", a2);
        assert_eq!(a2, "a");
        assert_eq!("bb", b2);
        assert_eq!(b2, "bb");

        assert_eq!("a".to_string(), a1);
        assert_eq!(a1, "a".to_string());
        assert_eq!("bb".to_string(), b1);
        assert_eq!(b1, "bb".to_string());
        assert_eq!("a".to_string(), a2);
        assert_eq!(a2, "a".to_string());
        assert_eq!("bb".to_string(), b2);
        assert_eq!(b2, "bb".to_string());

        assert!(a1 < "bb");
        assert!("bb" > a1);
        assert!(b1 > "a");
        assert!("a" < b1);

        let mut m1 = HashMap::new();
        m1.insert(a1.clone(), "a1");
        m1.insert(b1.clone(), "b1");

        assert_eq!(Some(&"a1"), m1.get("a"));
        assert_eq!(Some(&"a1"), m1.get(&a2));
        assert_eq!(Some(&"b1"), m1.get("bb"));
        assert_eq!(Some(&"b1"), m1.get(&b2));
        assert!(!m1.contains_key("aa"));

        let mut m2 = BTreeMap::new();
        m2.insert(a1.clone(), "a1");
        m2.insert(b1.clone(), "b1");

        assert_eq!(Some(&"a1"), m2.get("a"));
        assert_eq!(Some(&"a1"), m2.get(&a2));
        assert_eq!(Some(&"b1"), m2.get("bb"));
        assert_eq!(Some(&"b1"), m2.get(&b2));
        assert!(!m2.contains_key("aa"));
        assert_eq!(Some("a1"), m2.remove(&a2));
        assert_eq!(Some("b1"), m2.remove(&b2));

        m2.insert(b2.clone(), "b2");
        m2.insert(a2.clone(), "a2");

        assert_eq!(Some(&"a2"), m2.get("a"));
        assert_eq!(Some(&"a2"), m2.get(&a1));
        assert_eq!(Some(&"b2"), m2.get("bb"));
        assert_eq!(Some(&"b2"), m2.get(&b1));
        assert!(!m2.contains_key("aa"));
        assert_eq!(Some("a2"), m2.remove("a"));
        assert_eq!(Some("b2"), m2.remove("bb"));
    }

    #[rstest]
    #[case(ErrorKind::BadSurrogate {
        first: 0xD800,
        second: None,
    }, "bad Unicode escape sequence: high surrogate '\\uD800' not followed by low surrogate")]
    #[case(ErrorKind::BadUtf8ContByte {
        seq_len: 3,
        offset: 2,
        value: 0x20,
    }, "bad UTF-8 continuation byte 0x20 in 3-byte UTF-8 sequence (byte #2)")]
    #[case(ErrorKind::Read, "read error")]
    #[case(ErrorKind::UnexpectedByte {
        token: Some(Token::Num),
        expect: Expect::Digit,
        actual: 0x41,
    }, "expected digit character '0'..'9' but got character 'A' (ASCII 0x41) in number token")]
    #[case(ErrorKind::UnexpectedEof(Token::Str), "unexpected EOF in string token")]
    fn test_error_kind_display(#[case] kind: ErrorKind, #[case] expect: &str) {
        assert_eq!(expect, format!("{kind}"));

        struct Wrapper(ErrorKind);

        impl fmt::Display for Wrapper {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                let pos = Pos::default();

                self.0.fmt_at(f, Some(&pos))
            }
        }

        assert_eq!(
            format!("{expect} at line 1, column 1 (offset: 0)"),
            format!("{}", Wrapper(kind))
        );
    }

    #[rstest]
    #[case(r#""""#, r#""""#)]
    #[case(r#""f""#, r#""f""#)]
    #[case(r#""fo""#, r#""fo""#)]
    #[case(r#""foo""#, r#""foo""#)]
    #[case(r#""\\""#, r#""\""#)]
    #[case(r#""\/""#, r#""/""#)]
    #[case(r#""\"""#, r#"""""#)]
    #[case(r#""\b""#, "\"\x08\"")]
    #[case(r#""\t""#, "\"\t\"")]
    #[case(r#""\f""#, "\"\x0c\"")]
    #[case(r#""\n""#, "\"\n\"")]
    #[case(r#""\r""#, "\"\r\"")]
    #[case(r#""\u0000""#, "\"\0\"")]
    #[case(r#""\u0008""#, "\"\x08\"")]
    #[case(r#""\u0009""#, "\"\t\"")]
    #[case(r#""\u000c""#, "\"\x0c\"")]
    #[case(r#""\u000C""#, "\"\x0C\"")]
    #[case(r#""\u000a""#, "\"\n\"")]
    #[case(r#""\u000A""#, "\"\n\"")]
    #[case(r#""\u000d""#, "\"\r\"")]
    #[case(r#""\u000D""#, "\"\r\"")]
    #[case(r#""\u000D""#, "\"\r\"")]
    #[case(r#""\u0021""#, r#""!""#)]
    #[case(r#""\u0030""#, r#""0""#)]
    #[case(r#""\u0041""#, r#""A""#)]
    #[case(r#""\u0062""#, r#""b""#)]
    #[case(r#""\u007F""#, "\"\x7f\"")] // DEL (U+007F, highest 1-byte UTF-8)
    #[case(r#""\u00A9""#, r#""©""#)] // Copyright sign (U+00A9, 2-byte UTF-8)
    #[case(r#""\u03A9""#, r#""Ω""#)] // Greek capital Omega (U+03A9, 2-byte UTF-8)
    #[case(r#""\u0080""#, "\"\u{80}\"")] // First 2-byte UTF-8 code point
    #[case(r#""\u07FF""#, "\"\u{7ff}\"")] // Last 2-byte UTF-8 code point
    #[case(r#""\u20AC""#, r#""€""#)] // Euro sign (U+20AC, 3-byte UTF-8)
    #[case(r#""\u2603""#, r#""☃""#)] // Snowman (U+2603, 3-byte UTF-8)
    #[case(r#""\u0800""#, "\"\u{800}\"")] // First 3-byte UTF-8 code point
    #[case(r#""\uFFFF""#, "\"\u{ffff}\"")] // Last valid BMP code point (3-byte UTF-8)
    #[case(r#""\ud83D\uDe00""#, r#""😀""#)] // Grinning face emoji (U+1F600, 4-byte UTF-8)
    #[case(r#""\ud800\uDC00""#, "\"\u{10000}\"")] // First 4-byte UTF-8 code point
    #[case(r#""\uDBFF\udfff""#, "\"\u{10FFFF}\"")] // Highest valid Unicode scalar value
    fn test_unescape_ok(#[case] input: &str, #[case] expect: &str) {
        // Test with an empty buffer.
        {
            let mut buf = Vec::new();

            unescape(input, &mut buf);
            let actual = String::from_utf8(buf).unwrap();

            assert_eq!(actual, expect);
        }

        // Test with a non-empty buffer.
        {
            let mut buf = Vec::new();

            buf.extend_from_slice(b"foo");
            unescape(input, &mut buf);
            let actual = String::from_utf8(buf).unwrap();

            assert_eq!(actual, format!("foo{expect}"));
        }
    }

    #[rstest]
    #[case(r#""\a""#)]
    #[case(r#""\U""#)]
    #[case(r#""\:""#)]
    #[should_panic(expected = "invalid escape sequence byte after '\\'")]
    fn test_unescape_panic_invalid_esc_seq_byte(#[case] literal: &str) {
        let mut buf = Vec::new();

        unescape(literal, &mut buf);
    }

    #[rstest]
    #[case(r#"\ud800\u0000"#)]
    #[case(r#"\ud800\ud7ff"#)]
    #[case(r#"\ud800\ud800"#)]
    #[case(r#"\ud800\ue000"#)]
    #[case(r#"\ud800\uffff"#)]
    #[case(r#"\udbff\u0000"#)]
    #[case(r#"\udbff\ud7ff"#)]
    #[case(r#"\udbff\ud800"#)]
    #[case(r#"\udbff\ue000"#)]
    #[case(r#"\udbff\uffff"#)]
    #[should_panic(expected = "Unicode escape high surrogate not followed by low surrogate")]
    fn test_unescape_panic_low_surrogate_no_high(#[case] literal: &str) {
        let mut buf = Vec::new();

        unescape(literal, &mut buf);
    }

    #[rstest]
    #[case(r#""\ud800\u0000""#)]
    #[case(r#""\uDBFF\ud800""#)]
    #[should_panic(expected = "Unicode escape high surrogate not followed by low surrogate")]
    fn test_unescape_panic_high_surrogate_no_low(#[case] literal: &str) {
        let mut buf = Vec::new();

        unescape(literal, &mut buf);
    }

    #[rstest]
    #[case(r#"\ud800 "#)]
    #[case(r#"\udbff "#)]
    #[should_panic(
        expected = r#"expected '\' to start low surrogate Unicode escape after high surrogate"#
    )]
    fn test_unescape_panic_high_surrogate_no_backslash(#[case] literal: &str) {
        let mut buf = Vec::new();

        unescape(literal, &mut buf);
    }

    #[rstest]
    #[case(r#"\ud800\n"#)]
    #[case(r#"\udbff\a"#)]
    #[should_panic(
        expected = r#"expected '\u' to start low surrogate Unicode escape after high surrogate"#
    )]
    fn test_unescape_panic_high_surrogate_no_backslash_u(#[case] literal: &str) {
        let mut buf = Vec::new();

        unescape(literal, &mut buf);
    }

    #[rstest]
    #[case(r#"\"#)]
    #[case(r#"\u"#)]
    #[case(r#"\u0"#)]
    #[case(r#"\u00"#)]
    #[case(r#"\u000"#)]
    #[case(r#"\u0000\"#)]
    #[case(r#"\u0000\u"#)]
    #[case(r#"\u0000\u1"#)]
    #[case(r#"\u0000\u11"#)]
    #[case(r#"\u0000\u111"#)]
    #[case(r#"\ud800\u111"#)]
    #[case(r#"\udbff\u111"#)]
    #[should_panic(expected = "unexpected end of input within Unicode escape sequence")]
    fn test_unescape_panic_unexpected_eof(#[case] literal: &str) {
        let mut buf = Vec::new();

        unescape(literal, &mut buf);
    }

    #[rstest]
    #[case::arr_begin("[")]
    #[case::arr_end("]")]
    #[case::empty("")]
    #[case::lit_false("false")]
    #[case::lit_false("null")]
    #[case::lit_false("true")]
    #[case::name_sep(":")]
    #[case::num_0("1")]
    #[case::num_1("1")]
    #[case::num_pi("3.14159")]
    #[case::num_minus_1("-1")]
    #[case::num_big1("1234567890.1234567890")]
    #[case::num_big2("1e100")]
    #[case::num_big3("10.000e+99")]
    #[case::str_empty(r#""""#)]
    #[case::str_a(r#""a""#)]
    #[case::str_utf8_2_bytes(r#""è""#)]
    #[case::str_utf8_3_bytes(r#""€""#)]
    #[case::str_utf8_4_bytes(r#""🇮🇹""#)]
    #[case::str_utf8_all(r#""🇮🇹 Questo caffè costa 3€. ☕""#)]
    #[case::obj_begin("{")]
    #[case::obj_end("}")]
    #[case::value_sep(",")]
    #[case::white1(" ")]
    #[case::white2("\t")]
    #[case::white3("\r")]
    #[case::white4("\n")]
    #[case::white5("   \t\r\n \n\t\t\r \r\n\r")]
    fn test_unescaped_cmp_identity(#[case] input: &str) {
        assert_eq!(Ordering::Equal, unescaped_cmp(input, input));

        for chunked_str in ChunkedStr::chunkify(input) {
            assert_eq!(Ordering::Equal, unescaped_cmp(chunked_str, input));

            let content = chunked_str.into_content(false);
            assert_eq!(Ordering::Equal, content.unescaped_cmp(input));
        }
    }

    #[rstest]
    #[case::empty_str_empty("", r#""""#, Ordering::Less)]
    #[case::empty_num("", "1", Ordering::Less)]
    #[case::empty_white("", " ", Ordering::Less)]
    #[case::num_empty("0", "", Ordering::Greater)]
    #[case::str_a_str_b(r#""a""#, r#""b""#, Ordering::Less)]
    #[case::str_a_str_empty(r#""a""#, r#""""#, Ordering::Greater)]
    #[case::str_b_str_a(r#""b""#, r#""a""#, Ordering::Greater)]
    #[case::str_bar_str_bard(r#""bar""#, r#""bard""#, Ordering::Less)]
    #[case::str_bar_str_bark(r#""bar""#, r#""bark""#, Ordering::Less)]
    #[case::str_bard_str_bar(r#""bard""#, r#""bar""#, Ordering::Greater)]
    #[case::str_bard_str_bark(r#""bard""#, r#""bark""#, Ordering::Less)]
    #[case::str_bark_str_bar(r#""bark""#, r#""bar""#, Ordering::Greater)]
    #[case::str_bark_str_bard(r#""bark""#, r#""bard""#, Ordering::Greater)]
    #[case::str_empty_empty(r#""""#, "", Ordering::Greater)]
    #[case::str_empty_str_a(r#""""#, r#""a""#, Ordering::Less)]
    #[case::str_esc_quot_str_quot(r#""\"""#, "\"\"\"", Ordering::Equal)]
    #[case::str_esc_bsol_str_bsol(r#""\\""#, "\"\\\"", Ordering::Equal)]
    #[case::str_esc_sol_str_sol(r#""\/""#, "\"/\"", Ordering::Equal)]
    #[case::str_esc_bs_str_bs(r#""\b""#, "\"\x08\"", Ordering::Equal)]
    #[case::str_esc_ff_str_ff(r#""\f""#, "\"\x0c\"", Ordering::Equal)]
    #[case::str_esc_nl_str_nl(r#""\n""#, "\"\n\"", Ordering::Equal)]
    #[case::str_esc_nl_str_cr(r#""\n""#, "\"\r\"", Ordering::Less)]
    #[case::str_esc_cr_str_cr(r#""\r""#, "\"\r\"", Ordering::Equal)]
    #[case::str_esc_cr_str_nl(r#""\r""#, "\"\n\"", Ordering::Greater)]
    #[case::str_esc_tab_str_tab(r#""\t""#, "\"\t\"", Ordering::Equal)]
    #[case::str_esc_u_0008_str_bs(r#""\u0008""#, "\"\x08\"", Ordering::Equal)]
    #[case::str_esc_u_0009_str_tab(r#""\u0009""#, "\"\t\"", Ordering::Equal)]
    #[case::str_esc_u_000a_str_nl(r#""\u000a""#, "\"\n\"", Ordering::Equal)]
    #[case::str_esc_u_000c_str_ff(r#""\u000c""#, "\"\x0c\"", Ordering::Equal)]
    #[case::str_esc_u_000d_str_cr(r#""\u000d""#, "\"\r\"", Ordering::Equal)]
    #[case::str_esc_u_0022_str_quot(r#""\u0022""#, "\"\"\"", Ordering::Equal)]
    #[case::str_esc_u_002f_str_sol(r#""\u002f""#, "\"/\"", Ordering::Equal)]
    #[case::str_esc_u_005c_str_bsol(r#""\u005c""#, "\"\\\"", Ordering::Equal)]
    #[case::str_esc_u_0062_str_a(r#""\u0063""#, r#""a""#, Ordering::Greater)]
    #[case::str_esc_u_00e8_str_e_grave(r#""\u00e8""#, "\"\u{00e8}\"", Ordering::Equal)]
    #[case::str_esc_u_20ac_str_euro(r#""\u20ac""#, "\"\u{20ac}\"", Ordering::Equal)]
    #[case::str_esc_u_surrogate_str_globe(r#""\ud83c\udf0d""#, "\"\u{1f30d}\"", Ordering::Equal)]
    #[case::str_esc_at_end_of_longer_str(r#""abcde\n""#, r#""abc""#, Ordering::Greater)]
    #[case::str_esc_consecutive(r#""\n\t""#, "\"\n\t\"", Ordering::Equal)]
    #[case::str_where_other_terminates_early(r#""\n""#, "\"", Ordering::Greater)]
    #[case::white_empty(" ", "", Ordering::Greater)]
    #[case::smoke_1(
        r#""Path: C:\\Users\\alice\/docs""#,
        "\"Path: C:\\Users\\alice/docs\"",
        Ordering::Equal
    )]
    #[case::smoke_2(
        r#""At the caff\u00e8 counter, he paid the 10,50\u00a0\u20ac and\nquickly downed the cappucino. Then,\r\n\twith a quick\"ciao\u0022, he turned and walked out.""#,
        "\"At the caffè counter, he paid the 10,50\u{00a0}€ and\nquickly downed the cappucino. Then,\r\n\twith a quick\"ciao\", he turned and walked out.\"",
        Ordering::Equal,
    )]
    #[case::smoke_3(
        r#""At the caff\u00e8 counter, he paid the 10,50\u00a0\u20ac and\nquickly downed the cappucino. Then,\r\n\twith a quick\"ciao\u0022, he turned and ran out.""#,
        "\"At the caffè counter, he paid the 10,50\u{00a0}€ and\nquickly downed the cappucino. Then,\r\n\twith a quick\"ciao\", he turned and walked out.\"",
        Ordering::Less,
    )]
    fn test_unescaped_cmp_ok(#[case] a: &str, #[case] b: &str, #[case] expect: Ordering) {
        assert_eq!(expect, unescaped_cmp(a, b));

        for chunked_str in ChunkedStr::chunkify(a) {
            assert_eq!(expect, unescaped_cmp(chunked_str, b));

            let content = chunked_str.into_content(false);
            assert_eq!(expect, content.unescaped_cmp(b));
        }
    }

    #[rstest]
    #[case::unterminated_1(r#"\"#, r#"\"#, "unterminated escape sequence")]
    #[case::unterminated_2(r#""\"#, r#""\"#, "unterminated escape sequence")]
    #[case::unterminated_3(r#""hello\"#, r#""hello\"#, "unterminated escape sequence")]
    #[case::invalid_single_byte_1(
        r#"\0"#,
        r#"a"#,
        r#"invalid escape sequence: byte 0x30 cannot follow '\'"#
    )]
    #[case::invalid_single_byte_2(
        r#""\a""#,
        r#""a"#,
        r#"invalid escape sequence: byte 0x61 cannot follow '\'"#
    )]
    #[case::invalid_unicode_need_4(
        r#""\u"#,
        r#""a"#,
        "at least 4 hex digits are required to complete Unicode escape sequence, but only 0 bytes remain"
    )]
    #[case::invalid_unicode_need_3(
        r#""\u0"#,
        r#""a"#,
        "at least 4 hex digits are required to complete Unicode escape sequence, but only 1 bytes remain"
    )]
    #[case::invalid_unicode_need_2(
        r#""\u00"#,
        r#""a"#,
        "at least 4 hex digits are required to complete Unicode escape sequence, but only 2 bytes remain"
    )]
    #[case::invalid_unicode_need_1(
        r#""\u000"#,
        r#""a"#,
        "at least 4 hex digits are required to complete Unicode escape sequence, but only 3 bytes remain"
    )]
    #[case::invalid_unicode_not_hex_1(r#""\uG000"#, r#""a"#, "byte 0x47 is not an ASCII hex digit")]
    #[case::invalid_unicode_not_hex_2(r#""\u0H00"#, r#""a"#, "byte 0x48 is not an ASCII hex digit")]
    #[case::invalid_unicode_not_hex_3(r#""\u00I0"#, r#""a"#, "byte 0x49 is not an ASCII hex digit")]
    #[case::invalid_unicode_not_hex_4(r#""\u000J"#, r#""a"#, "byte 0x4a is not an ASCII hex digit")]
    #[case::lo_surrogate_incomplete_1(r#"\ud800"#, "a", r#"at least 6 bytes are required for Unicode low surrogate escape sequence that follows "\ud800", but only 0 bytes remain"#)]
    #[case::lo_surrogate_incomplete_2(r#"\uD801x"#, "a", r#"at least 6 bytes are required for Unicode low surrogate escape sequence that follows "\uD801", but only 1 bytes remain"#)]
    #[case::lo_surrogate_incomplete_3(r#"\uD802xx"#, "a", r#"at least 6 bytes are required for Unicode low surrogate escape sequence that follows "\uD802", but only 2 bytes remain"#)]
    #[case::lo_surrogate_incomplete_4(r#"\uD803xxx"#, "a", r#"at least 6 bytes are required for Unicode low surrogate escape sequence that follows "\uD803", but only 3 bytes remain"#)]
    #[case::lo_surrogate_incomplete_5(r#"\udbfdxxxx"#, "a", r#"at least 6 bytes are required for Unicode low surrogate escape sequence that follows "\udbfd", but only 4 bytes remain"#)]
    #[case::lo_surrogate_incomplete_6(r#"\uDBFExxxxx"#, "a", r#"at least 6 bytes are required for Unicode low surrogate escape sequence that follows "\uDBFE", but only 5 bytes remain"#)]
    #[case::lo_surrogate_no_leading_bsol_u_1(
        r#"\udbFFxxxxxx"#,
        "a",
        r#"low surrogate Unicode escape sequence must start with "\u""#
    )]
    #[case::lo_surrogate_no_leading_bsol_u_2(
        r#"\ud800\Uxxxx"#,
        "a",
        r#"low surrogate Unicode escape sequence must start with "\u""#
    )]
    #[case::lo_surrogate_no_leading_bsol_u_2(
        r#"\ud800\Uxxxx"#,
        "a",
        r#"low surrogate Unicode escape sequence must start with "\u""#
    )]
    #[case::lo_surrogate_not_hex_1(
        r#""\ud800\uK000""#,
        r#""a""#,
        "byte 0x4b is not an ASCII hex digit"
    )]
    #[case::lo_surrogate_not_hex_1(
        r#""\ud800\u0L00""#,
        r#""a""#,
        "byte 0x4c is not an ASCII hex digit"
    )]
    #[case::lo_surrogate_not_hex_1(
        r#""\ud800\u00M0""#,
        r#""a""#,
        "byte 0x4d is not an ASCII hex digit"
    )]
    #[case::lo_surrogate_not_hex_1(
        r#""\ud800\u000N""#,
        r#""a""#,
        "byte 0x4e is not an ASCII hex digit"
    )]
    #[case::lo_surrogate_alone(
        r#""\udc00""#,
        r#""a""#,
        "invalid Unicode escape sequence(s) produced invalid code point 0xdc00"
    )]
    #[case::invalid_surrogate_pair(
        r#""\ud800\u0041""#,
        r#""a""#,
        r#"high surrogate \ud800 followed by invalid low surrogate \u0041"#
    )]
    fn test_unescaped_cmp_panic(#[case] a: &str, #[case] b: &str, #[case] expect: &str) {
        assert_panic(|| unescaped_cmp(a, b), expect);
        for chunked_str in ChunkedStr::chunkify(a) {
            assert_panic(|| unescaped_cmp(chunked_str, b), expect);

            let content = chunked_str.into_content(false);
            assert_panic(|| content.unescaped_cmp(b), expect);
        }
    }

    #[cfg(feature = "num")]
    #[rstest]
    #[case::zero("0", 0)]
    #[case::minus_zero("-0", 0)]
    #[case::one("1", 1)]
    #[case::minus_one("-1", -1)]
    #[case::forty_two("42", 42)]
    #[case::minus_forty_two("-42", -42)]
    #[case::all_digits("9876543210", 9876543210)]
    #[case::max_minus_1(format!("{}", i64::MAX-1), i64::MAX-1)]
    #[case::max(format!("{}", i64::MAX), i64::MAX)]
    #[case::min_plus_1(format!("{}", i64::MIN+1), i64::MIN+1)]
    #[case::min(format!("{}", i64::MIN), i64::MIN)]
    fn test_parse_i64_ok(#[case] input: impl AsRef<str>, #[case] expect: i64) {
        let input = input.as_ref();

        assert_eq!(Ok(expect), parse_i64(input));
        for chunked_str in ChunkedStr::chunkify(input) {
            assert_eq!(Ok(expect), parse_i64(chunked_str));

            let content = chunked_str.into_content(false);
            assert_eq!(Ok(expect), content.parse_i64());
        }
    }

    #[cfg(feature = "num")]
    #[rstest]
    #[case::empty("", NumError::Format)]
    #[case::token_arr_begin("[", NumError::Format)]
    #[case::token_arr_end("]", NumError::Format)]
    #[case::token_lit_false("false", NumError::Format)]
    #[case::token_lit_null("null", NumError::Format)]
    #[case::token_lit_true("true", NumError::Format)]
    #[case::token_name_sep(":", NumError::Format)]
    #[case::token_obj_begin("{", NumError::Format)]
    #[case::token_obj_end("}", NumError::Format)]
    #[case::token_str_empty(r#""""#, NumError::Format)]
    #[case::token_str_one(r#""1""#, NumError::Format)]
    #[case::token_value_sep(",", NumError::Format)]
    #[case::token_white("   ", NumError::Format)]
    #[case::space_prefix(" 1", NumError::Format)]
    #[case::space_suffix("1 ", NumError::Format)]
    #[case::decimal_zero("10.0", NumError::Format)]
    #[case::decimal("3.14159", NumError::Format)]
    #[case::exponent_zero("0e0", NumError::Format)]
    #[case::exponent_positive("10E+1", NumError::Format)]
    #[case::exponent_negative("1E-98", NumError::Format)]
    #[case::range_i64_min_minus_1(format!("{}", i64::MIN as i128 - 1), NumError::Range)]
    #[case::range_i128_min(format!("{}", i128::MIN), NumError::Range)]
    #[case::range_i64_max_plus_1(format!("{}", i64::MAX as i128 + 1), NumError::Range)]
    #[case::range_i128_max(format!("{}", i128::MAX), NumError::Range)]
    fn test_parse_i64_err(#[case] input: impl AsRef<str>, #[case] expect: NumError) {
        let input = input.as_ref();

        assert_eq!(Err(expect), parse_i64(input));
        for chunked_str in ChunkedStr::chunkify(input) {
            assert_eq!(Err(expect), parse_i64(chunked_str));

            let content = chunked_str.into_content(false);
            assert_eq!(Err(expect), content.parse_i64());
        }
    }

    #[cfg(feature = "num")]
    #[rstest]
    #[case::zero("0", 0)]
    #[case::one("1", 1)]
    #[case::forty_two("42", 42)]
    #[case::all_digits("9876543210", 9876543210)]
    #[case::max_minus_1(format!("{}", u64::MAX-1), u64::MAX-1)]
    #[case::max(format!("{}", u64::MAX), u64::MAX)]
    fn test_parse_u64_ok(#[case] input: impl AsRef<str>, #[case] expect: u64) {
        let input = input.as_ref();

        assert_eq!(Ok(expect), parse_u64(input));
        for chunked_str in ChunkedStr::chunkify(input) {
            assert_eq!(Ok(expect), parse_u64(chunked_str));

            let content = chunked_str.into_content(false);
            assert_eq!(Ok(expect), content.parse_u64());
        }
    }

    #[cfg(feature = "num")]
    #[rstest]
    #[case::empty("", NumError::Format)]
    #[case::token_arr_begin("[", NumError::Format)]
    #[case::token_arr_end("]", NumError::Format)]
    #[case::token_lit_false("false", NumError::Format)]
    #[case::token_lit_null("null", NumError::Format)]
    #[case::token_lit_true("true", NumError::Format)]
    #[case::token_name_sep(":", NumError::Format)]
    #[case::token_obj_begin("{", NumError::Format)]
    #[case::token_obj_end("}", NumError::Format)]
    #[case::token_str_empty(r#""""#, NumError::Format)]
    #[case::token_str_one(r#""1""#, NumError::Format)]
    #[case::token_value_sep(",", NumError::Format)]
    #[case::token_white("   ", NumError::Format)]
    #[case::space_prefix(" 1", NumError::Format)]
    #[case::space_suffix("1 ", NumError::Format)]
    #[case::minus_zero("-0", NumError::Format)]
    #[case::minus_one("-1", NumError::Format)]
    #[case::minus_forty_two("-42", NumError::Format)]
    #[case::decimal_zero("10.0", NumError::Format)]
    #[case::decimal("3.14159", NumError::Format)]
    #[case::exponent_zero("0e0", NumError::Format)]
    #[case::exponent_positive("10E+1", NumError::Format)]
    #[case::exponent_negative("1E-98", NumError::Format)]
    #[case::range_u64_max_plus_1(format!("{}", u64::MAX as i128 + 1), NumError::Range)]
    #[case::range_i128_max(format!("{}", i128::MAX), NumError::Range)]
    fn test_parse_u64_err(#[case] input: impl AsRef<str>, #[case] expect: NumError) {
        let input = input.as_ref();

        assert_eq!(Err(expect), parse_u64(input));
        for chunked_str in ChunkedStr::chunkify(input) {
            assert_eq!(Err(expect), parse_u64(chunked_str));

            let content = chunked_str.into_content(false);
            assert_eq!(Err(expect), content.parse_u64());
        }
    }

    #[cfg(feature = "num_ext")]
    #[rstest]
    #[case::zero("0", 0)]
    #[case::minus_zero("-0", 0)]
    #[case::one("1", 1)]
    #[case::minus_one("-1", -1)]
    #[case::forty_two("42", 42)]
    #[case::minus_forty_two("-42", -42)]
    #[case::all_digits("9876543210", 9876543210)]
    #[case::i64_max(format!("{}", i64::MAX), i64::MAX as i128)]
    #[case::i64_min(format!("{}", i64::MIN), i64::MIN as i128)]
    #[case::beyond_i64_max(format!("{}", i64::MAX as i128 + 1), i64::MAX as i128 + 1)]
    #[case::beyond_i64_min(format!("{}", i64::MIN as i128 - 1), i64::MIN as i128 - 1)]
    #[case::pow10_38(format!("{}", 10_i128.pow(38)), 10_i128.pow(38))]
    #[case::neg_pow10_38(format!("{}", -10_i128.pow(38)), -10_i128.pow(38))]
    #[case::max_minus_1(format!("{}", i128::MAX - 1), i128::MAX - 1)]
    #[case::max(format!("{}", i128::MAX), i128::MAX)]
    #[case::min_plus_1(format!("{}", i128::MIN + 1), i128::MIN + 1)]
    #[case::min(format!("{}", i128::MIN), i128::MIN)]
    fn test_parse_i128_ok(#[case] input: impl AsRef<str>, #[case] expect: i128) {
        let input = input.as_ref();

        assert_eq!(Ok(expect), parse_i128(input));
        for chunked_str in ChunkedStr::chunkify(input) {
            assert_eq!(Ok(expect), parse_i128(chunked_str));

            let content = chunked_str.into_content(false);
            assert_eq!(Ok(expect), content.parse_i128());
        }
    }

    #[cfg(feature = "num_ext")]
    #[rstest]
    #[case::empty("", NumError::Format)]
    #[case::token_arr_begin("[", NumError::Format)]
    #[case::token_arr_end("]", NumError::Format)]
    #[case::token_lit_false("false", NumError::Format)]
    #[case::token_lit_null("null", NumError::Format)]
    #[case::token_lit_true("true", NumError::Format)]
    #[case::token_name_sep(":", NumError::Format)]
    #[case::token_obj_begin("{", NumError::Format)]
    #[case::token_obj_end("}", NumError::Format)]
    #[case::token_str_empty(r#""""#, NumError::Format)]
    #[case::token_str_one(r#""1""#, NumError::Format)]
    #[case::token_value_sep(",", NumError::Format)]
    #[case::token_white("   ", NumError::Format)]
    #[case::space_prefix(" 1", NumError::Format)]
    #[case::space_suffix("1 ", NumError::Format)]
    #[case::decimal_zero("10.0", NumError::Format)]
    #[case::decimal("3.14159", NumError::Format)]
    #[case::exponent_zero("0e0", NumError::Format)]
    #[case::exponent_positive("10E+1", NumError::Format)]
    #[case::exponent_negative("1E-98", NumError::Format)]
    #[case::range_i128_min_minus_1("−170141183460469231731687303715884105729", NumError::Format)]
    #[case::range_i128_max_plus_1(format!("{}", i128::MAX as u128 + 1), NumError::Range)]
    #[case::range_u128_max(format!("{}", u128::MAX), NumError::Range)]
    fn test_parse_i128_err(#[case] input: impl AsRef<str>, #[case] expect: NumError) {
        let input = input.as_ref();

        assert_eq!(Err(expect), parse_i128(input));
        for chunked_str in ChunkedStr::chunkify(input) {
            assert_eq!(Err(expect), parse_i128(chunked_str));

            let content = chunked_str.into_content(false);
            assert_eq!(Err(expect), content.parse_i128());
        }
    }

    #[cfg(feature = "num")]
    #[rstest]
    #[case::zero("0", 0.0)]
    #[case::minus_zero("-0", 0.0)]
    #[case::one("1", 1.0)]
    #[case::minus_one("-1", -1.0)]
    #[case::forty_two("42", 42.0)]
    #[case::pi("3.14159", 3.14159)]
    #[case::negative_decimal("-2.5", -2.5)]
    #[case::exponent_zero("0e0", 0.0)]
    #[case::exponent_positive("1e2", 100.0)]
    #[case::exponent_negative("1e-2", 0.01)]
    #[case::exponent_upper("1E2", 100.0)]
    #[case::exponent_plus_sign("1e+2", 100.0)]
    #[case::decimal_and_exponent("1.5e2", 150.0)]
    #[case::large_integer("9876543210", 9876543210.0)]
    #[case::f64_max(format!("{}", f64::MAX), f64::MAX)]
    #[case::f64_min_positive(format!("{}", f64::MIN_POSITIVE), f64::MIN_POSITIVE)]
    #[case::subnormal("5e-324", 5e-324)]
    fn test_parse_f64_ok(#[case] input: impl AsRef<str>, #[case] expect: f64) {
        let input = input.as_ref();

        let result = parse_f64(input);
        assert_eq!(Ok(expect), result);
        // Only test ChunkedStr for short inputs; in test mode the inline buffer is 7 bytes, so
        // multi-chunk inputs longer than that exercise the heap fallback which panics in
        // InlineSink::reserve. Short inputs cover the multi-chunk paths adequately.
        if input.len() <= 7 {
            for chunked_str in ChunkedStr::chunkify(input) {
                assert_eq!(Ok(expect), parse_f64(chunked_str));

                let content = chunked_str.into_content(false);
                assert_eq!(Ok(expect), content.parse_f64());
            }
        }
    }

    #[cfg(feature = "num")]
    #[rstest]
    #[case::empty("", NumError::Format)]
    #[case::token_lit_false("false", NumError::Format)]
    #[case::token_lit_null("null", NumError::Format)]
    #[case::token_lit_true("true", NumError::Format)]
    #[case::token_str_empty(r#""""#, NumError::Format)]
    #[case::token_str_one(r#""1""#, NumError::Format)]
    #[case::space_prefix(" 1", NumError::Format)]
    #[case::space_suffix("1 ", NumError::Format)]
    #[case::range_positive_overflow("1e309", NumError::Range)]
    #[case::range_negative_overflow("-1e309", NumError::Range)]
    fn test_parse_f64_err(#[case] input: impl AsRef<str>, #[case] expect: NumError) {
        let input = input.as_ref();

        assert_eq!(Err(expect), parse_f64(input));
        for chunked_str in ChunkedStr::chunkify(input) {
            assert_eq!(Err(expect), parse_f64(chunked_str));

            let content = chunked_str.into_content(false);
            assert_eq!(Err(expect), content.parse_f64());
        }
    }

    #[derive(Debug)]
    struct ChunkedContent<'a> {
        lit: ChunkedStr<'a>,
        esc: bool,
    }

    impl<'a> Content for ChunkedContent<'a> {
        type Literal<'b>
            = ChunkedStr<'a>
        where
            Self: 'b;

        fn literal<'b>(&'b self) -> Self::Literal<'b> {
            self.lit
        }

        fn literal_len(&self) -> usize {
            self.lit.buf.len()
        }

        fn is_escaped(&self) -> bool {
            self.esc
        }

        fn unescaped<'b>(&'b self) -> Unescaped<Self::Literal<'b>> {
            if !self.esc {
                Unescaped::Literal(self.lit)
            } else {
                let mut buf = Vec::new();
                unescape(self.lit, &mut buf);

                // SAFETY: `r` was valid UTF-8 before it was de-escaped, and the de-escaping process
                //         maintains UTF-8 safety.
                let s = unsafe { String::from_utf8_unchecked(buf) };

                Unescaped::Expanded(s)
            }
        }
    }

    #[derive(Clone, Copy, Debug)]
    struct ChunkedStr<'a> {
        buf: &'a [u8],
        pos: usize,
        n: usize,
    }

    impl<'a> ChunkedStr<'a> {
        fn new(s: &'a str, n: usize) -> Self {
            Self {
                buf: s.as_bytes(),
                pos: 0,
                n,
            }
        }

        fn chunkify(s: &'a str) -> Vec<Self> {
            let mut v = vec![Self::new(s, 1)];

            if s.len() >= 2 {
                v.push(Self::new(s, 2));
            }

            if s.len() >= 4 {
                v.push(Self::new(s, s.len() - 1));
                v.push(Self::new(s, s.len()));
            }

            v
        }

        fn into_content(&self, escaped: bool) -> ChunkedContent<'a> {
            ChunkedContent {
                lit: *self,
                esc: escaped,
            }
        }
    }

    impl<'a> IntoBuf for ChunkedStr<'a> {
        type Buf = Self;

        fn into_buf(self) -> Self::Buf {
            self
        }
    }

    impl<'a> Buf for ChunkedStr<'a> {
        fn advance(&mut self, n: usize) {
            let len = self.buf.len();
            let pos = self.pos;

            if len < pos + n {
                panic!(
                    "{}",
                    &crate::BufUnderflow {
                        requested: n,
                        remaining: len - pos,
                    }
                );
            } else {
                self.pos = pos + n;
            }
        }

        fn chunk(&self) -> &[u8] {
            let end = self.buf.len().min(self.pos + self.n);

            &self.buf[self.pos..end]
        }

        fn remaining(&self) -> usize {
            let len = self.buf.len();
            let pos = self.pos;

            len - pos
        }

        fn try_copy_to_slice(&mut self, dst: &mut [u8]) -> Result<(), crate::BufUnderflow> {
            let len = self.buf.len();
            let pos = self.pos;

            if len < pos + dst.len() {
                Err(crate::BufUnderflow {
                    requested: dst.len(),
                    remaining: len - pos,
                })
            } else {
                dst.copy_from_slice(&self.buf[pos..pos + dst.len()]);
                self.pos = pos + dst.len();

                Ok(())
            }
        }
    }

    fn assert_panic<F, T>(f: F, expect: &str)
    where
        F: FnOnce() -> T,
        T: fmt::Debug,
    {
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(f));
        let err = result.unwrap_err();
        let msg = err
            .downcast_ref::<String>()
            .map(|s| s.as_str())
            .or_else(|| err.downcast_ref::<&str>().copied())
            .unwrap();
        assert!(
            msg.contains(expect),
            "expected panic containing {expect:?}, got {msg:?}"
        );
    }
}