json-escape 0.3.1

A no_std, zero-copy, allocation-free library for streaming JSON string escaping and unescaping. Ergonomic, fast, RFC 8259 compliant, with layered APIs for iterators, I/O streaming, and low-level tokens.
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
//! Provides low-level, granular, token-based JSON string processing.
//!
//! This module offers the most fundamental building blocks for both escaping and
//! unescaping. It provides iterators, [`UnescapeTokens`] and [`EscapeTokens`],
//! that walk a byte/string slice and yield tokens. This approach is highly flexible
//! and composable, allowing consumers to handle the data in a zero-copy,
//! streaming fashion for literal (non-processed) parts.
//!
//! ## Unescaping
//!
//! The [`UnescapeTokens`] iterator yields [`UnescapedToken`]s, separating literal
//! byte slices from single unescaped `char`s. This ensures that in the case of an
//! error (e.g., an invalid escape sequence), all preceding valid literal parts have
//! already been successfully yielded.
//!
//! ## Escaping
//!
//! The [`EscapeTokens`] iterator yields [`EscapedToken`]s, separating literal
//! string slices from the `&'static str` representation of an escaped character.
//! This allows for efficient, allocation-free iteration over a string to produce
//! its JSON-escaped form.

#[cfg(feature = "alloc")]
use crate::DecodeUtf8Error;
use crate::{
    InvalidEscapeError, InvalidHexError, LoneSurrogateError, UnescapeError, UnescapeErrorKind,
};
use core::{
    fmt::{self, Write as _},
    iter::FusedIterator,
};
use memchr::memchr;

#[cfg(feature = "alloc")]
use alloc::{borrow::Cow, string::String, vec::Vec};

//==============================================================================
// Escaping
//==============================================================================

/// Creates an iterator that yields tokens of an escaped JSON string.
///
/// See the [module-level documentation](self) for more details.
#[inline]
pub fn escape_str(s: &str) -> EscapeTokens<'_> {
    EscapeTokens {
        bytes: s.as_bytes(),
    }
}

/// A token representing a piece of an escaped JSON string.
///
/// This enum is the item yielded by the [`EscapeTokens`] iterator.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum EscapedToken<'a> {
    /// A slice of the original input that did not require escaping.
    Literal(&'a str),
    /// The `&'static str` representation of an escaped character (e.g., `r#"\n"#`).
    Escaped(&'static str),
}

impl<'a> EscapedToken<'a> {
    #[inline(always)]
    pub(crate) fn as_str(&self) -> &'a str {
        match self {
            EscapedToken::Literal(s) => s,
            EscapedToken::Escaped(s) => s,
        }
    }
}

impl fmt::Display for EscapedToken<'_> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

/// An iterator over a string that yields [`EscapedToken`]s.
///
/// This is a low-level API for producing a JSON-escaped representation of a
/// string slice without allocation. It yields borrowed string slices for literal
/// parts and static string slices for the escape sequences themselves.
#[derive(Clone, Debug)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct EscapeTokens<'a> {
    pub(crate) bytes: &'a [u8],
}

impl<'a> EscapeTokens<'a> {
    /// Creates a new tokenizing escaper for the given string slice.
    #[inline]
    pub const fn new(s: &'a str) -> Self {
        Self {
            bytes: s.as_bytes(),
        }
    }

    #[inline(always)]
    pub(crate) fn escape(byte: u8) -> Option<&'static str> {
        ESCAPE_TABLE[byte as usize]
    }

    /// Splits the slice at the first byte that needs to be escaped.
    ///
    /// The first element of the returned tuple is the literal part, which is
    /// guaranteed to be valid UTF-8. The second is the rest of the slice,
    /// which starts with an escapable byte, or is empty.
    ///
    /// # SAFETY
    ///
    /// The input byte slice `bytes` must be valid UTF-8. This is because the
    /// function uses `from_utf8_unchecked`, relying on the fact that all
    /// escapable characters are single-byte ASCII and thus cannot occur in
    /// the middle of a multi-byte UTF-8 sequence.
    #[inline(always)]
    pub(crate) unsafe fn split_at_escape(bytes: &[u8]) -> (&str, &[u8]) {
        // Find the first byte that needs escaping.
        let pos = match Self::find_escape_char(bytes) {
            // Found a backslash, the literal is the part before it.
            Some(p) => p,
            // No more backslashes, the rest of the slice is a literal.
            None => bytes.len(),
        };

        let (literal_bytes, rest) = bytes.split_at(pos);
        // SAFETY: `find_escape_char` guarantees `pos` is on a UTF-8 boundary
        // because escapable characters are single-byte ASCII.
        (
            unsafe { std::str::from_utf8_unchecked(literal_bytes) },
            rest,
        )
    }

    // Not public API. Exposed for test
    #[doc(hidden)]
    // This is the SIMD version, compiled only when the "simd" feature is enabled on nightly build.
    #[cfg(all(feature = "simd", nightly))]
    #[inline]
    pub fn find_escape_char(bytes: &[u8]) -> Option<usize> {
        use std::simd::{Simd, prelude::SimdPartialEq, prelude::SimdPartialOrd};

        const LANES: usize = 16; // Process 16 bytes at a time (fits in SSE2/AVX)
        let mut i = 0;

        // SIMD main loop
        while i + LANES <= bytes.len() {
            // Load 16 bytes from the slice into a SIMD vector.
            let chunk = Simd::<u8, LANES>::from_slice(&bytes[i..]);

            // Create comparison vectors. These are effectively 16 copies of the byte.
            let space_v = Simd::splat(b' ' - 1); // For the < ' ' check (i.e., <= 0x1F)
            let quote_v = Simd::splat(b'"');
            let slash_v = Simd::splat(b'\\');

            // Perform all 16 comparisons at once. The result is a mask.
            let lt_space_mask = chunk.simd_le(space_v);
            let eq_quote_mask = chunk.simd_eq(quote_v);
            let eq_slash_mask = chunk.simd_eq(slash_v);

            // Combine the masks. A byte needs escaping if ANY of the conditions are true.
            let combined_mask = lt_space_mask | eq_quote_mask | eq_slash_mask;

            // Check if any lane in the combined mask is true.
            if combined_mask.any() {
                // If yes, find the index of the *first* true lane.
                // trailing_zeros() on the bitmask gives us this index directly.
                let first_match_index = combined_mask.to_bitmask().trailing_zeros() as usize;
                return Some(i + first_match_index);
            }

            i += LANES;
        }

        // Handle the remaining bytes (if any) with the simple iterator method.
        if i < bytes.len() {
            if let Some(pos) = bytes[i..]
                .iter()
                .position(|&b| ESCAPE_DECISION_TABLE[b as usize] != 0)
            {
                return Some(i + pos);
            }
        }

        None
    }

    // Not public API. Exposed for test
    #[doc(hidden)]
    #[cfg(all(feature = "simd", not(nightly), target_arch = "x86_64"))]
    #[inline]
    pub fn find_escape_char(bytes: &[u8]) -> Option<usize> {
        // This is the stable Rust path using explicit CPU intrinsics.
        // It's guarded by cfg flags to only compile on x86_64 with the simd feature.
        use std::arch::x86_64::*;

        let mut i = 0;
        const LANES: usize = 16; // SSE2 works on 128-bit registers, which is 16 bytes.

        // On x86_64, we can tell the compiler to use SSE2 features in this specific function.
        // This is safe because we've already checked the target architecture.
        // SAFETY: calling this unsafe function is only safe if the caller ensures:
        //  - the CPU supports SSE2, and
        //  - i + LANES <= bytes.len()
        #[target_feature(enable = "sse2")]
        unsafe fn find_in_chunk(bytes: &[u8], i: usize) -> Option<usize> {
            // Safety check: ensure the 16 bytes we will load are inside the slice.
            // This is a debug-time assertion to help catch incorrect call.
            debug_assert!(
                i + LANES <= bytes.len(),
                "find_in_chunk: attempted to load past end of slice"
            );

            // Load 16 bytes of data from the slice.
            // SAFETY: caller must guarantee `i + LANES <= bytes.len()`. We assert that above.
            let chunk = unsafe { _mm_loadu_si128(bytes.as_ptr().add(i) as *const _) };

            // Create comparison vectors for quote and slash.
            let quote_v = _mm_set1_epi8(b'"' as i8);
            let slash_v = _mm_set1_epi8(b'\\' as i8);

            // Emulate unsigned comparison for control characters
            // Create a vector with the value 0x80 in each lane.
            let bias = _mm_set1_epi8(0x80u8 as i8);
            // Create the comparison vector for bytes < 0x20 (' ').
            let space_v = _mm_set1_epi8(b' ' as i8);

            // Bias both the input chunk and the comparison vector by XORing with 0x80.
            let biased_chunk = _mm_xor_si128(chunk, bias);
            let biased_space_v = _mm_xor_si128(space_v, bias);

            // Now, a signed less-than comparison on the biased values gives the
            // same result as an unsigned less-than on the original values.
            let lt_space_mask = _mm_cmplt_epi8(biased_chunk, biased_space_v);

            // Perform the equality comparisons (these are unaffected by signedness).
            let eq_quote_mask = _mm_cmpeq_epi8(chunk, quote_v);
            let eq_slash_mask = _mm_cmpeq_epi8(chunk, slash_v);

            // Combine the results.
            let combined_mask =
                _mm_or_si128(lt_space_mask, _mm_or_si128(eq_quote_mask, eq_slash_mask));

            // Create a bitmask to find the first match.
            let mask = _mm_movemask_epi8(combined_mask);

            if mask != 0 {
                Some(i + mask.trailing_zeros() as usize)
            } else {
                None
            }
        }

        if cfg!(target_feature = "sse2") {
            // Main loop (vectorized)
            while i + LANES <= bytes.len() {
                // Safety: calling `find_in_chunk` is safe here because:
                //  - we've checked CPU supports SSE2 via is_x86_feature_detected!
                //  - loop condition ensures `i + LANES <= bytes.len()`, matching the debug_assert in the function.
                if let Some(result) = unsafe { find_in_chunk(bytes, i) } {
                    return Some(result);
                }
                i += LANES;
            }
        } else {
            // CPU doesn't support SSE2: fall through to scalar path below.
            // (We intentionally do not attempt to call the sse2 function.)
        }

        // Handle the remainder with the fast scalar lookup.
        if i < bytes.len() {
            if let Some(pos) = bytes[i..]
                .iter()
                .position(|&b| ESCAPE_DECISION_TABLE[b as usize] != 0)
            {
                return Some(i + pos);
            }
        }

        None
    }

    // Not public API. Exposed for test
    // A fallback for when SIMD feature is off.
    #[doc(hidden)]
    #[cfg(not(feature = "simd"))]
    #[inline]
    pub fn find_escape_char(bytes: &[u8]) -> Option<usize> {
        use core::mem::size_of;

        const WORD_SIZE: usize = size_of::<usize>();
        const THRESH: u8 = 0x20; // control threshold

        // helper: repeat a byte across a usize (works for any usize width)
        const fn repeat(b: u8) -> usize {
            let mut m: usize = 0;
            let mut i = 0;
            while i < WORD_SIZE {
                m = (m << 8) | (b as usize);
                i += 1;
            }
            m
        }

        // Precompute masks as constants
        const ONE_MASK: usize = repeat(0x01);
        const MSB_MASK: usize = repeat(0x80);
        const QUOTE_MASK: usize = repeat(b'"');
        const SLASH_MASK: usize = repeat(b'\\');
        const THR_MASK: usize = repeat(THRESH);

        let mut i = 0usize;
        while i + WORD_SIZE <= bytes.len() {
            // SAFETY: we checked bounds; read_unaligned is allowed for any alignment.
            let word = unsafe { (bytes.as_ptr().add(i) as *const usize).read_unaligned() };

            // equality tests (SWAR zero-byte detection on XOR)
            let xq = word ^ QUOTE_MASK;
            let quote_bits = (xq.wrapping_sub(ONE_MASK) & !xq) & MSB_MASK;

            let xs = word ^ SLASH_MASK;
            let slash_bits = (xs.wrapping_sub(ONE_MASK) & !xs) & MSB_MASK;

            // control: detect bytes < 0x20 using subtract+~word+msb trick
            // If any byte b satisfies b < 0x20 then the corresponding MSB bit in control_bits is set.
            let control_bits = (word.wrapping_sub(THR_MASK) & !word) & MSB_MASK;

            // combined mask: MSB-bit set per candidate byte
            let combined = quote_bits | slash_bits | control_bits;

            if combined != 0 {
                // Find earliest matching byte inside this word in a portable way:
                // - on little-endian the least-significant set bit corresponds to the earliest byte
                // - on big-endian the most-significant set bit corresponds to the earliest byte
                let byte_index = if cfg!(target_endian = "little") {
                    (combined.trailing_zeros() as usize) / 8
                } else {
                    (combined.leading_zeros() as usize) / 8
                };
                return Some(i + byte_index);
            }

            i += WORD_SIZE;
        }

        // tail bytes
        if i < bytes.len() {
            if let Some(pos) = bytes[i..]
                .iter()
                .position(|&b| ESCAPE_DECISION_TABLE[b as usize] != 0)
            {
                return Some(i + pos);
            }
        }

        None
    }

    #[cfg(all(feature = "simd", not(nightly), not(target_arch = "x86_64")))]
    compile_error! { "simd requires nightly or target_arch = \"x86_64\"" }
}

impl<'a> Iterator for EscapeTokens<'a> {
    type Item = EscapedToken<'a>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if self.bytes.is_empty() {
            return None;
        }

        if let Some(escaped) = Self::escape(self.bytes[0]) {
            // --- Handle Escape ---
            // An escapable byte is at the beginning of the slice.
            self.bytes = &self.bytes[1..];
            Some(EscapedToken::Escaped(escaped))
        } else {
            // --- Handle Literal ---
            // SAFETY: Input is string
            let (literal, rest) = unsafe { Self::split_at_escape(self.bytes) };
            self.bytes = rest;
            Some(EscapedToken::Literal(literal))
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        if self.bytes.is_empty() {
            (0, Some(0))
        } else {
            // We'll yield at least 1 slice, and at most `len` slices if every byte is escaped.
            (1, Some(self.bytes.len()))
        }
    }
}

impl<'a> FusedIterator for EscapeTokens<'a> {}

impl fmt::Display for EscapeTokens<'_> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for token in self.clone() {
            f.write_str(token.as_str())?;
        }
        Ok(())
    }
}

#[cfg(feature = "alloc")]
impl<'a> From<EscapeTokens<'a>> for Cow<'a, str> {
    /// Efficiently collects the escaped parts into a `Cow<'a, str>`.
    ///
    /// This implementation is optimized to avoid allocation if possible:
    /// - If the input string requires **no escaping**, it returns `Cow::Borrowed`
    ///   with a slice of the original string.
    /// - If escaping is needed, it allocates a `String` and returns `Cow::Owned`.
    fn from(mut iter: EscapeTokens<'a>) -> Self {
        match iter.next() {
            None => Cow::Borrowed(""),
            Some(EscapedToken::Literal(s)) if iter.bytes.is_empty() => {
                // No escape in the first (and only) chunk, so no escaping was needed.
                Cow::Borrowed(s)
            }
            Some(first) => {
                // Escaping occurred. We must allocate.
                let mut s = String::with_capacity(first.as_str().len() + iter.bytes.len());
                s.push_str(first.as_str());
                s.extend(iter);
                Cow::Owned(s)
            }
        }
    }
}

//==============================================================================
// Unescaping
//==============================================================================

/// Creates an iterator that yields tokens of an unescaped JSON string.
///
/// See the [module-level documentation](self) for more details.
#[inline]
pub fn unescape<I: AsRef<[u8]> + ?Sized>(input: &I) -> UnescapeTokens<'_> {
    UnescapeTokens {
        bytes: input.as_ref(),
    }
}

/// A token representing a piece of an unescaped JSON string.
///
/// This enum is the item yielded by the [`UnescapeTokens`] iterator.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnescapedToken<'a> {
    /// A slice of the original input that did not require unescaping.
    Literal(&'a [u8]),
    /// A single character that was unescaped from an escape sequence.
    Unescaped(char),
}

impl UnescapedToken<'_> {
    /// Returns a wrapper that implements [`fmt::Display`].
    ///
    /// If the token is a `Literal` containing invalid UTF-8, a `fmt::Error`
    /// is returned, which will cause `format!` and friends to panic.
    pub fn display_utf8(&self) -> DisplayUnescapedToken<'_> {
        DisplayUnescapedToken {
            token: self,
            lossy: true,
        }
    }

    /// Returns a wrapper that implements [`fmt::Display`] for lossy UTF-8 decoding.
    ///
    /// If the token is a `Literal` containing invalid UTF-8, it will be replaced
    /// with the replacement character.
    pub fn display_utf8_lossy(&self) -> DisplayUnescapedToken<'_> {
        DisplayUnescapedToken {
            token: self,
            lossy: true,
        }
    }

    #[inline(always)]
    const fn len(&self) -> usize {
        match self {
            UnescapedToken::Literal(literal) => literal.len(),
            UnescapedToken::Unescaped(ch) => ch.len_utf8(),
        }
    }
}

/// Helper struct for safely displaying an [`DisplayUnescapedToken`].
pub struct DisplayUnescapedToken<'a> {
    token: &'a UnescapedToken<'a>,
    lossy: bool,
}

impl fmt::Display for DisplayUnescapedToken<'_> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self.token {
            UnescapedToken::Literal(bytes) => crate::display_bytes_utf8(bytes, f, self.lossy),
            UnescapedToken::Unescaped(c) => f.write_char(*c),
        }
    }
}

/// An iterator over a byte slice that yields [`UnescapedToken`]s.
///
/// This is the foundational, low-level unescaping API. It processes the byte
/// slice, yielding runs of literal bytes as borrowed slices and successfully
/// parsed escape sequences as `char`s.
///
/// If an error is encountered while parsing an escape, the iterator will yield
/// an `Err` and all subsequent calls to `next()` will return `None`.
///
/// Created by [`UnescapeTokens::new`].
#[derive(Clone, Debug)]
#[must_use = "iterators are lazy and do nothing unless consumed"]
pub struct UnescapeTokens<'a> {
    bytes: &'a [u8],
}

impl<'a> UnescapeTokens<'a> {
    /// Creates a new tokenizing unescaper for the given byte slice.
    #[inline]
    pub const fn new(bytes: &'a [u8]) -> Self {
        Self { bytes }
    }

    /// Returns the remaining unprocessed slice of bytes.
    ///
    /// If the iterator encounters an `UnexpectedEof` error, this method can be
    /// used to retrieve the incomplete segment that needs to be stitched with
    /// the next chunk in a streaming context.
    #[inline]
    pub const fn remnant(&self) -> &'a [u8] {
        self.bytes
    }

    /// Decodes the unescaped byte stream into a UTF-8 string.
    ///
    /// This method consumes the iterator and collects all resulting byte chunks
    /// into a `Cow<[u8]>`, which is then validated as UTF-8. If an unescaping
    /// error occurs, it's returned immediately. If the final sequence of bytes
    /// is not valid UTF-8, a UTF-8 error is returned.
    ///
    /// This is optimized to return a `Cow::Borrowed` if no escapes were present
    /// in the input, avoiding allocation.
    ///
    /// **Requires the `alloc` feature.**
    #[cfg(feature = "alloc")]
    pub fn decode_utf8(self) -> Result<Cow<'a, str>, DecodeUtf8Error> {
        match self.try_into().map_err(DecodeUtf8Error::Unescape)? {
            Cow::Borrowed(bytes) => str::from_utf8(bytes)
                .map(Cow::Borrowed)
                .map_err(DecodeUtf8Error::Utf8),
            Cow::Owned(bytes) => String::from_utf8(bytes)
                .map(Cow::Owned)
                .map_err(|e| DecodeUtf8Error::Utf8(e.utf8_error())),
        }
    }

    /// Decodes the unescaped byte stream lossily into a UTF-8 string.
    ///
    /// This is similar to [`UnescapeTokens::decode_utf8`] but replaces any invalid UTF-8 sequences
    /// with the replacement character (`U+FFFD`) instead of returning an error.
    ///
    /// An `UnescapeError` can still be returned if the JSON escaping itself is invalid.
    ///
    /// **Requires the `alloc` feature.**
    #[cfg(feature = "alloc")]
    pub fn decode_utf8_lossy(self) -> Result<Cow<'a, str>, UnescapeError> {
        use crate::decode_utf8_lossy;

        Ok(decode_utf8_lossy(self.try_into()?))
    }

    /// Returns a wrapper that implements [`fmt::Display`].
    ///
    /// If an unescaping error or invalid UTF-8 sequence is encountered,
    /// a `fmt::Error` is returned, which will cause `format!` and friends to panic.
    pub fn display_utf8(self) -> DisplayUnescapeTokens<'a> {
        DisplayUnescapeTokens {
            inner: self,
            lossy: false,
        }
    }

    /// Returns a wrapper that implements [`fmt::Display` for lossy UTF-8 decoding.
    ///
    /// Invalid UTF-8 sequences will be replaced with the replacement character.
    /// An unescaping error will still result in a `fmt::Error`.
    pub fn display_utf8_lossy(self) -> DisplayUnescapeTokens<'a> {
        DisplayUnescapeTokens {
            inner: self,
            lossy: true,
        }
    }

    /// Splits the slice at the first backslash `\`.
    ///
    /// The first element of the returned tuple is the literal part before the
    /// backslash. The second is the rest of the slice, which starts with the
    /// backslash, or is empty if no backslash was found.
    #[inline(always)]
    pub(crate) fn split_at_escape(bytes: &'a [u8]) -> (&'a [u8], &'a [u8]) {
        let pos = match memchr(b'\\', bytes) {
            // Found a backslash, the literal is the part before it.
            Some(p) => p,
            // No more backslashes, the rest of the slice is a literal.
            None => bytes.len(),
        };

        let (literal, rest) = bytes.split_at(pos);
        (literal, rest)
    }

    /// Parses any escape sequence (`\uXXXX`, '\n', e.t.c).
    /// The input slice `bytes` must be positioned *after* the `\`.
    ///
    /// On error, returns an `Err` and the input slice may be modified.
    #[inline(always)]
    pub(crate) fn handle_escape(bytes: &mut &'a [u8]) -> Result<char, UnescapeError> {
        match bytes.first() {
            Some(b'u') => {
                // Advance past 'u' and parse unicode
                *bytes = &bytes[1..];
                Self::handle_unicode_escape(bytes)
            }
            Some(&byte) => {
                // Simple 1-char escape like \n, \t, etc.
                match UNESCAPE_TABLE[byte as usize] {
                    Some(c) => {
                        *bytes = &bytes[1..];
                        Ok(c)
                    }
                    None => {
                        Err(UnescapeError {
                            kind: UnescapeErrorKind::InvalidEscape(InvalidEscapeError {
                                found: byte,
                            }),
                            // The invalid character is 1 byte after '\'.
                            offset: 1,
                        })
                    }
                }
            }
            None => {
                // Dangling backslash at the end of input
                Err(UnescapeError {
                    kind: UnescapeErrorKind::UnexpectedEof,
                    // EOF occurred 1 byte after '\'.
                    offset: 1,
                })
            }
        }
    }

    /// Parses a unicode escape sequence `\uXXXX` which may be a surrogate pair.
    /// The input slice `bytes` must be positioned *after* the `\u`.
    ///
    /// On success, returns the parsed `char` and advances the slice.
    /// On error, returns an `Err` and the input slice may be modified.
    #[inline(always)]
    fn handle_unicode_escape(bytes: &mut &'a [u8]) -> Result<char, UnescapeError> {
        // Parse first 4 hex digits (\uXXXX)
        //
        // The slice starts *after* '\u'. The first hex digit is at offset 2 from '\'.
        let first = Self::parse_hex4(bytes, 2)?;
        *bytes = &bytes[4..];

        // High surrogate → must be followed by another \uXXXX low surrogate
        if (0xD800..=0xDBFF).contains(&first) {
            // A high surrogate must be followed by a `\u` sequence.
            // We check for at least 2 bytes and that they are `\` and `u`.
            #[allow(clippy::get_first)]
            match (bytes.get(0), bytes.get(1)) {
                (Some(b'\\'), Some(b'u')) => {
                    // We have `\u`, so we now expect 4 more hex digits for the low surrogate.
                    // The slice for `parse_hex4` starts after `\u`, and the overall offset
                    // from the beginning of the original escape is 8 (`\uXXXX\u`).
                    match Self::parse_hex4(&bytes[2..], 8) {
                        Ok(low) if (0xDC00..=0xDFFF).contains(&low) => {
                            // Valid low surrogate found. Combine them.
                            let high_t = first as u32;
                            let low_t = low as u32;
                            let code = 0x10000 + (((high_t - 0xD800) << 10) | (low_t - 0xDC00));
                            let result_char = char::from_u32(code).expect(
                                "valid surrogate pair math should always produce a valid char",
                            );

                            // Advance the slice past the entire low surrogate sequence.
                            *bytes = &bytes[6..];
                            return Ok(result_char);
                        }
                        Ok(_) => {
                            // We parsed `\uXXXX`, but the value was not a valid low surrogate.
                            // This makes the initial high surrogate a "lone surrogate".
                            return Err(UnescapeError {
                                kind: UnescapeErrorKind::LoneSurrogate(LoneSurrogateError {
                                    surrogate: first,
                                }),
                                offset: 6,
                            });
                        }
                        Err(err) => {
                            // `parse_hex4` failed (e.g., incomplete hex, invalid char).
                            // Propagate the error.
                            return Err(err);
                        }
                    }
                }
                (Some(b'\\'), None) => {
                    return Err(UnescapeError {
                        kind: UnescapeErrorKind::UnexpectedEof,
                        offset: 7,
                    });
                }
                (None, None) => {
                    // The input ended immediately after the high surrogate.
                    return Err(UnescapeError {
                        kind: UnescapeErrorKind::UnexpectedEof,
                        offset: 6,
                    });
                }
                // Something else after high surrogate → LoneSurrogate
                _ => {
                    // There are other characters, but they don't form a `\u` sequence.
                    return Err(UnescapeError {
                        kind: UnescapeErrorKind::LoneSurrogate(LoneSurrogateError {
                            surrogate: first,
                        }),
                        offset: 6,
                    });
                }
            }
        }

        // Not a surrogate → normal path
        match char::from_u32(first as u32) {
            Some(c) => Ok(c),
            None => {
                // The parsed value is not a valid char (e.g., it's a lone low surrogate).
                Err(UnescapeError {
                    kind: UnescapeErrorKind::LoneSurrogate(LoneSurrogateError { surrogate: first }),
                    // The error is detected after consuming `\uXXXX` (6 bytes total from '\').
                    offset: 6,
                })
            }
        }
    }

    /// Parses 4 hex digits, optimized for the success path.
    #[inline(always)]
    fn parse_hex4(slice: &[u8], base_offset: u8) -> Result<u16, UnescapeError> {
        // --- HOT PATH ---
        // This is the path we expect to take most of the time.
        if let Some(chunk) = slice.get(..4) {
            // By slicing to 4, we've performed a single bounds check.
            // The compiler now knows any access from chunk[0] to chunk[3] is safe,
            // so it will not generate additional bounds checks.

            // We can now safely access the bytes.
            let b0 = chunk[0];
            let b1 = chunk[1];
            let b2 = chunk[2];
            let b3 = chunk[3];

            // Use the LUT to get the values.
            if let (Some(v0), Some(v1), Some(v2), Some(v3)) = (
                HEX[b0 as usize],
                HEX[b1 as usize],
                HEX[b2 as usize],
                HEX[b3 as usize],
            ) {
                // All characters are valid hex, combine and return.
                let result = (v0 as u16) << 12 | (v1 as u16) << 8 | (v2 as u16) << 4 | (v3 as u16);
                return Ok(result);
            }

            // If we're here, it means the slice was long enough, but one
            // of the characters was not a valid hex digit. Fall through to the cold path
            // to correctly identify which character was invalid.
        }

        // --- COLD PATH ---
        // This path handles all errors. It's marked as `#[cold]` to hint to the
        // compiler that it's less frequently executed.
        #[cold]
        fn handle_error(slice: &[u8], base_offset: u8) -> UnescapeError {
            // Loop through the bytes we *do* have.
            for (i, &b) in slice.iter().enumerate() {
                if HEX[b as usize].is_none() {
                    // We found an invalid hex character before running out of bytes.
                    return UnescapeError {
                        kind: UnescapeErrorKind::InvalidHex(InvalidHexError { found: b }),
                        offset: base_offset + i as u8,
                    };
                }
            }

            // If the loop completes, all available characters were valid,
            // but there weren't enough of them.
            UnescapeError {
                kind: UnescapeErrorKind::UnexpectedEof,
                // The error is at the position of the first *missing* character.
                offset: base_offset + slice.len() as u8,
            }
        }

        Err(handle_error(slice, base_offset))
    }
}

impl<'a> Iterator for UnescapeTokens<'a> {
    type Item = Result<UnescapedToken<'a>, UnescapeError>;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        if self.bytes.is_empty() {
            return None;
        }

        // Check if the next part is an escape sequence or a literal.
        if self.bytes[0] == b'\\' {
            // --- Handle Escape Sequence ---
            Some({
                // TODO: Try abstract... repeated in explicit
                // rest starts with '\\'
                let mut remainder = &self.bytes[1..];
                match UnescapeTokens::handle_escape(&mut remainder) {
                    Ok(unescaped_char) => {
                        self.bytes = remainder;
                        Ok(UnescapedToken::Unescaped(unescaped_char))
                    }
                    Err(err) => Err(err),
                }
            })
        } else {
            // --- Handle Literal ---
            let (literal, rest) = Self::split_at_escape(self.bytes);
            self.bytes = rest;
            Some(Ok(UnescapedToken::Literal(literal)))
        }
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        if self.bytes.is_empty() {
            (0, Some(0))
        } else {
            // Worst-case is \uXXXX -> 1 byte, so 6 -> 1.
            (
                self.bytes.len().saturating_add(1) / 6,
                Some(self.bytes.len()),
            )
        }
    }
}

impl<'a> FusedIterator for UnescapeTokens<'a> {}

#[cfg(feature = "alloc")]
impl<'a> TryFrom<UnescapeTokens<'a>> for Cow<'a, [u8]> {
    type Error = UnescapeError;

    /// Efficiently collects the unescaped bytes into a `Cow<'a, [u8]>`.
    ///
    /// Returns `Cow::Borrowed` if no escape sequences were present, avoiding
    /// allocation. Otherwise, returns `Cow::Owned`. If an error occurs, it's
    /// returned immediately.
    fn try_from(mut value: UnescapeTokens<'a>) -> Result<Self, Self::Error> {
        match value.next() {
            None => Ok(Cow::Borrowed(b"")),
            Some(Ok(UnescapedToken::Literal(literal))) if value.bytes.is_empty() => {
                // The first and only token is a literal. No allocation needed.
                Ok(Cow::Borrowed(literal))
            }
            Some(Ok(first_token)) => {
                // An escape was processed or there are more tokens. Must allocate.
                let mut buf = Vec::with_capacity(first_token.len() + value.bytes.len());

                let process_token = |buf: &mut Vec<u8>, token: UnescapedToken| match token {
                    UnescapedToken::Literal(bytes) => buf.extend_from_slice(bytes),
                    UnescapedToken::Unescaped(c) => {
                        append_char(buf, c);
                    }
                };

                process_token(&mut buf, first_token);
                for item in value {
                    process_token(&mut buf, item?);
                }

                Ok(Cow::Owned(buf))
            }
            Some(Err(e)) => Err(e),
        }
    }
}

/// A wrapper struct for implementing `fmt::Display` on an [`UnescapeTokens`] iterator.
pub struct DisplayUnescapeTokens<'a> {
    inner: UnescapeTokens<'a>,
    lossy: bool,
}

impl<'a> fmt::Display for DisplayUnescapeTokens<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for chunk_result in self.inner.clone() {
            match chunk_result {
                Ok(token) => {
                    let display_chunk = DisplayUnescapedToken {
                        token: &token,
                        lossy: self.lossy,
                    };
                    write!(f, "{}", display_chunk)?;
                }
                Err(_) => return Err(fmt::Error), // Signal error to formatter
            }
        }
        Ok(())
    }
}

// =============================================================================
// Utilities
// =============================================================================

// A const lookup table for JSON escape sequences.
// Maps a byte to its escaped `&'static str` representation.
// `None` indicates the byte does not need to be escaped.
const ESCAPE_TABLE: [Option<&'static str>; 256] = {
    let mut table: [Option<&'static str>; 256] = [None; 256];

    // Special characters
    table[b'"' as usize] = Some(r#"\""#);
    table[b'\\' as usize] = Some(r#"\\"#);

    // Common control characters with short escapes
    table[0x08] = Some(r#"\b"#); // Backspace
    table[0x09] = Some(r#"\t"#); // Tab
    table[0x0A] = Some(r#"\n"#); // Line Feed
    table[0x0C] = Some(r#"\f"#); // Form Feed
    table[0x0D] = Some(r#"\r"#); // Carriage Return

    // The rest of the control characters must be `\uXXXX` encoded.
    // We can pre-calculate and store all of them as static strings.
    table[0x00] = Some(r#"\u0000"#);
    table[0x01] = Some(r#"\u0001"#);
    table[0x02] = Some(r#"\u0002"#);
    table[0x03] = Some(r#"\u0003"#);
    table[0x04] = Some(r#"\u0004"#);
    table[0x05] = Some(r#"\u0005"#);
    table[0x06] = Some(r#"\u0006"#);
    table[0x07] = Some(r#"\u0007"#);
    // 0x08 to 0x0D are already handled above
    table[0x0B] = Some(r#"\u000b"#);
    table[0x0E] = Some(r#"\u000e"#);
    table[0x0F] = Some(r#"\u000f"#);
    table[0x10] = Some(r#"\u0010"#);
    table[0x11] = Some(r#"\u0011"#);
    table[0x12] = Some(r#"\u0012"#);
    table[0x13] = Some(r#"\u0013"#);
    table[0x14] = Some(r#"\u0014"#);
    table[0x15] = Some(r#"\u0015"#);
    table[0x16] = Some(r#"\u0016"#);
    table[0x17] = Some(r#"\u0017"#);
    table[0x18] = Some(r#"\u0018"#);
    table[0x19] = Some(r#"\u0019"#);
    table[0x1A] = Some(r#"\u001a"#);
    table[0x1B] = Some(r#"\u001b"#);
    table[0x1C] = Some(r#"\u001c"#);
    table[0x1D] = Some(r#"\u001d"#);
    table[0x1E] = Some(r#"\u001e"#);
    table[0x1F] = Some(r#"\u001f"#);

    table
};

// Not public API. Exposed for test
#[doc(hidden)]
// A simple boolean-like lookup table for SIMD.
// 0 = no escape needed, 1 = escape needed.
// This is very compact (256 bytes) and fits easily in the L1 cache.
#[allow(unused)]
pub const ESCAPE_DECISION_TABLE: [u8; 256] = {
    let mut table = [0u8; 256];
    let mut i = 0;
    while i < 256 {
        if ESCAPE_TABLE[i].is_some() {
            table[i] = 1;
        }
        i += 1;
    }
    table
};

// Escape table: maps the byte after '\' to its escaped representation.
const UNESCAPE_TABLE: [Option<char>; 256] = {
    let mut tbl: [Option<char>; 256] = [None; 256];
    tbl[b'"' as usize] = Some('\"');
    tbl[b'\\' as usize] = Some('\\');
    tbl[b'/' as usize] = Some('/');
    tbl[b'b' as usize] = Some('\x08');
    tbl[b'f' as usize] = Some('\x0C');
    tbl[b'n' as usize] = Some('\n');
    tbl[b'r' as usize] = Some('\r');
    tbl[b't' as usize] = Some('\t');
    tbl
};

// --- Look-Up Table for Hex Decoding ---
const HEX: [Option<u8>; 256] = {
    let mut table = [None; 256];
    let mut i = 0;
    while i < 256 {
        table[i] = match i as u8 {
            b'0'..=b'9' => Some(i as u8 - b'0'),
            b'a'..=b'f' => Some(i as u8 - b'a' + 10),
            b'A'..=b'F' => Some(i as u8 - b'A' + 10),
            _ => None,
        };
        i += 1;
    }
    table
};

// Helper to append a char directly to the Vec<u8> buffer.
// This should be more efficient than using an intermediate stack buffer.
#[inline]
pub(crate) fn append_char(buf: &mut Vec<u8>, c: char) {
    // Reserve space for the character's bytes and write directly into the buffer.
    let char_len = c.len_utf8();
    let old_len = buf.len();
    buf.resize(old_len + char_len, 0);
    c.encode_utf8(&mut buf[old_len..]);
}

//==============================================================================
// Iterator Trait Implementations
//==============================================================================

#[cfg(feature = "alloc")]
mod iter_traits {
    use super::{EscapedToken, UnescapedToken, append_char};
    use alloc::string::String;
    use alloc::vec::Vec;

    /// Collects an iterator of escaped chunks into a single `String`.
    impl<'a> FromIterator<EscapedToken<'a>> for String {
        #[inline]
        fn from_iter<I: IntoIterator<Item = EscapedToken<'a>>>(iter: I) -> String {
            let mut s = String::new();
            s.extend(iter);
            s
        }
    }

    /// Extends a `String` with an iterator of escaped tokens.
    impl<'a> Extend<EscapedToken<'a>> for String {
        #[inline]
        fn extend<I: IntoIterator<Item = EscapedToken<'a>>>(&mut self, iter: I) {
            iter.into_iter().for_each(move |token| {
                self.push_str(token.as_str());
            });
        }
    }

    /// Collects an iterator of unescaped chunks into a byte vector.
    impl<'a> FromIterator<UnescapedToken<'a>> for Vec<u8> {
        #[inline]
        fn from_iter<I: IntoIterator<Item = UnescapedToken<'a>>>(iter: I) -> Vec<u8> {
            let mut buf = Vec::new();
            buf.extend(iter);
            buf
        }
    }

    /// Extends a byte vector with an iterator of unescaped chunks.
    impl<'a> Extend<UnescapedToken<'a>> for Vec<u8> {
        #[inline]
        fn extend<I: IntoIterator<Item = UnescapedToken<'a>>>(&mut self, iter: I) {
            iter.into_iter().for_each(move |token| match token {
                UnescapedToken::Literal(literal) => self.extend_from_slice(literal),
                UnescapedToken::Unescaped(ch) => append_char(self, ch),
            })
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_empty_string() {
        let mut iter = UnescapeTokens::new(b"");
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn test_pure_literal() {
        let mut iter = UnescapeTokens::new(b"hello world");
        assert_eq!(
            iter.next(),
            Some(Ok(UnescapedToken::Literal(b"hello world")))
        );
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn test_simple_escapes() {
        let mut iter = UnescapeTokens::new(b"a\\nb\\tc");
        assert_eq!(iter.next(), Some(Ok(UnescapedToken::Literal(b"a"))));
        assert_eq!(iter.next(), Some(Ok(UnescapedToken::Unescaped('\n'))));
        assert_eq!(iter.next(), Some(Ok(UnescapedToken::Literal(b"b"))));
        assert_eq!(iter.next(), Some(Ok(UnescapedToken::Unescaped('\t'))));
        assert_eq!(iter.next(), Some(Ok(UnescapedToken::Literal(b"c"))));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn test_starts_with_escape() {
        let mut iter = UnescapeTokens::new(b"\\nhello");
        assert_eq!(iter.next(), Some(Ok(UnescapedToken::Unescaped('\n'))));
        assert_eq!(iter.next(), Some(Ok(UnescapedToken::Literal(b"hello"))));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn test_ends_with_escape() {
        let mut iter = UnescapeTokens::new(b"hello\\n");
        assert_eq!(iter.next(), Some(Ok(UnescapedToken::Literal(b"hello"))));
        assert_eq!(iter.next(), Some(Ok(UnescapedToken::Unescaped('\n'))));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn test_unicode_and_surrogate() {
        let mut iter = UnescapeTokens::new(b"A is \\u0041, smiley is \\uD83D\\uDE00!");
        assert_eq!(iter.next(), Some(Ok(UnescapedToken::Literal(b"A is "))));
        assert_eq!(iter.next(), Some(Ok(UnescapedToken::Unescaped('A'))));
        assert_eq!(
            iter.next(),
            Some(Ok(UnescapedToken::Literal(b", smiley is ")))
        );
        assert_eq!(iter.next(), Some(Ok(UnescapedToken::Unescaped('😀'))));
        assert_eq!(iter.next(), Some(Ok(UnescapedToken::Literal(b"!"))));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn test_invalid_escape_yields_literal_first() {
        let mut iter = UnescapeTokens::new(b"ValidPart\\zInvalid");
        // First, we get the valid literal part. THIS is the key fix.
        assert_eq!(iter.next(), Some(Ok(UnescapedToken::Literal(b"ValidPart"))));
        // Then, we get the error.
        let err = iter.next().unwrap().unwrap_err();
        assert_eq!(
            err,
            UnescapeError {
                kind: UnescapeErrorKind::InvalidEscape(InvalidEscapeError { found: b'z' }),
                offset: 1,
            }
        );
        // The iterator should keep erroring
        assert_eq!(iter.remnant(), b"\\zInvalid");
        assert_eq!(iter.next(), Some(Err(err)));
    }

    #[test]
    fn test_sticky_error_behavior() {
        let mut iter = UnescapeTokens::new(b"a\\zb");
        assert_eq!(iter.next(), Some(Ok(UnescapedToken::Literal(b"a"))));

        // First error
        let err1 = iter.next().unwrap().unwrap_err();
        assert_eq!(
            err1.kind,
            UnescapeErrorKind::InvalidEscape(InvalidEscapeError { found: b'z' })
        );
        assert_eq!(iter.remnant(), b"\\zb");

        // Second call should yield the same error
        let err2 = iter.next().unwrap().unwrap_err();
        assert_eq!(err1, err2);
        assert_eq!(iter.remnant(), b"\\zb"); // Remnant is unchanged
    }

    #[test]
    fn test_incomplete_escape_at_end() {
        let mut iter = UnescapeTokens::new(b"ValidPart\\u12");
        assert_eq!(iter.next(), Some(Ok(UnescapedToken::Literal(b"ValidPart"))));

        // Before consuming the error, check the remnant.
        assert_eq!(iter.remnant(), b"\\u12");

        let err = iter.next().unwrap().unwrap_err();
        assert_eq!(
            err,
            UnescapeError {
                kind: UnescapeErrorKind::UnexpectedEof,
                offset: 4,
            }
        );

        assert_eq!(iter.remnant(), b"\\u12");
        assert_eq!(iter.next(), Some(Err(err)));
    }

    #[test]
    fn test_dangling_backslash() {
        let mut iter = UnescapeTokens::new(b"end with \\");
        assert_eq!(iter.next(), Some(Ok(UnescapedToken::Literal(b"end with "))));
        let err = iter.next().unwrap().unwrap_err();
        assert_eq!(
            err,
            UnescapeError {
                kind: UnescapeErrorKind::UnexpectedEof,
                offset: 1,
            }
        );
        assert_eq!(iter.next(), Some(Err(err)));
    }

    #[test]
    fn test_display_unescape_tokens() {
        let iter = UnescapeTokens::new(b"hello \\u0041\\nworld");
        let display = iter.display_utf8();
        assert_eq!(alloc::format!("{}", display), "hello A\nworld");
    }

    #[test]
    fn test_display_unescape_error() {
        let iter = UnescapeTokens::new(b"hello\\z");
        let mut out = String::new();
        write!(out, "{}", iter.display_utf8_lossy()).unwrap_err();
        // Formatting fails, but doesn't panic. The result is just truncated.
        // The exact output may vary, but we test that it doesn't contain the bad part.
        assert!(out.starts_with("hello"));
    }

    // --- Escape Tests ---
    #[test]
    fn test_escape_no_escapes() {
        let mut iter = EscapeTokens::new("hello world");
        assert_eq!(iter.next(), Some(EscapedToken::Literal("hello world")));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn test_escape_simple() {
        let mut iter = EscapeTokens::new("hello\nworld");
        assert_eq!(iter.next(), Some(EscapedToken::Literal("hello")));
        assert_eq!(iter.next(), Some(EscapedToken::Escaped(r#"\n"#)));
        assert_eq!(iter.next(), Some(EscapedToken::Literal("world")));
        assert_eq!(iter.next(), None);
    }

    #[test]
    fn test_display_escape_tokens() {
        let iter = EscapeTokens::new("a\"b\tc");
        assert_eq!(alloc::format!("{}", iter), r#"a\"b\tc"#);
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_escape_to_cow_borrowed() {
        let iter = EscapeTokens::new("no escapes here");
        let cow: Cow<'_, str> = iter.into();
        assert!(matches!(cow, Cow::Borrowed(_)));
        assert_eq!(cow, "no escapes here");
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_escape_to_cow_owned() {
        let iter = EscapeTokens::new("has\n an escape");
        let cow: Cow<'_, str> = iter.into();
        assert!(matches!(cow, Cow::Owned(_)));
        assert_eq!(cow, r#"has\n an escape"#);
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_unescape_to_cow_borrowed() {
        let iter = UnescapeTokens::new(b"no escapes here");
        let cow: Cow<'_, [u8]> = iter.try_into().unwrap();
        assert!(matches!(cow, Cow::Borrowed(_)));
        assert_eq!(*cow, *b"no escapes here");
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_unescape_to_cow_owned() {
        let iter = UnescapeTokens::new(b"has\\n an escape");
        let cow: Cow<'_, [u8]> = iter.try_into().unwrap();
        assert!(matches!(cow, Cow::Owned(_)));
        assert_eq!(*cow, *b"has\n an escape");
    }
}

#[cfg(test)]
mod find_escape_char_tests {
    use std::format;

    use super::{ESCAPE_DECISION_TABLE, EscapeTokens};

    /// Helper function to run a single test case and provide a clear error message on failure.
    fn run_test(input: &str, expected: Option<usize>, case_name: &str) {
        let result = EscapeTokens::find_escape_char(input.as_bytes());
        assert_eq!(result, expected, "Failed test case: '{}'", case_name);
    }

    #[test]
    fn test_no_escapes() {
        run_test("", None, "Empty string");
        run_test("Hello, world!", None, "Simple ASCII");
        run_test("This string is exactly 16 bytes", None, "16-byte ASCII");
        run_test(
            "This string is over 16 bytes long now",
            None,
            "Over 16-byte ASCII",
        );

        // The original source of the bug: non-ASCII UTF-8 characters.
        // This ensures the signedness bug is truly fixed.
        run_test("Hello, éàçüö!", None, "Non-ASCII UTF-8");
        run_test("Testing with emojis 😀❤️✅", None, "Emojis");
    }

    #[test]
    fn test_single_escapes() {
        run_test("\"", Some(0), "Quote at start");
        run_test("Hello \" world", Some(6), "Quote in middle");
        run_test("Hello\\", Some(5), "Backslash at end");
        run_test("\n", Some(0), "Control char (newline) at start");
        run_test("Hello\tworld", Some(5), "Control char (tab) in middle");
        run_test(
            "Control char at end\u{08}",
            Some(19),
            "Control char (backspace) at end",
        );
    }

    #[test]
    fn test_finds_first_of_multiple() {
        // This confirms it always finds the *first* match, not a later one.
        run_test("a\"b\\c\nd", Some(1), "Finds first quote");
        run_test("ab\\c\"d\ne", Some(2), "Finds first backslash");
        run_test("abc\nd\"e\\f", Some(3), "Finds first control char");
        run_test("\"\n\\", Some(0), "Multiple escapes at start");
    }

    #[test]
    fn test_simd_chunk_boundaries() {
        // These tests are critical for verifying the SIMD logic. A chunk is 16 bytes.
        let s15 = "a".repeat(15);
        let s16 = "a".repeat(16);
        let s17 = "a".repeat(17);

        // Escape at the exact end of the first 16-byte chunk
        run_test(&format!("{}\"", s15), Some(15), "Escape at index 15");

        // Escape at the exact start of the second 16-byte chunk
        run_test(&format!("{}\n", s16), Some(16), "Escape at index 16");

        // Escape within the second chunk
        run_test(&format!("{}\t", s17), Some(17), "Escape at index 17");

        // A long string with an escape several chunks in
        let long = "a".repeat(40);
        run_test(
            &format!("{}\\\\", long),
            Some(40),
            "Escape deep in a long string",
        );
    }

    #[test]
    fn test_remainder_logic() {
        // These tests ensure the scalar fallback logic works correctly for inputs
        // that are not a multiple of 16 bytes long.

        // String shorter than 16 bytes
        run_test("short\nstring", Some(5), "Short string with escape");
        run_test("no escapes", None, "Short string no escape");

        // String with 17 bytes (16 for SIMD, 1 for remainder)
        let s16 = "a".repeat(16);
        run_test(
            &format!("{}\"", s16),
            Some(16),
            "Escape in 1-byte remainder",
        );

        // String with 31 bytes (16 for SIMD, 15 for remainder)
        let s15 = "b".repeat(15);
        run_test(
            &format!("{}{}\t", s15, s15),
            Some(30),
            "Escape at end of 15-byte remainder",
        );
    }

    #[test]
    fn test_all_escapable_bytes_individually() {
        // This is the ultimate test. It iterates through all 256 possible byte values
        // and confirms that our function's decision matches the ESCAPE_DECISION_TABLE.
        let prefix = "0123456789abcdef"; // A 16-byte safe prefix to engage the SIMD loop.

        for byte_val in 0..=255u8 {
            // We can't create a &str from invalid UTF-8, so we work with byte slices.
            let mut test_bytes = prefix.as_bytes().to_vec();
            test_bytes.push(byte_val);

            let result = EscapeTokens::find_escape_char(&test_bytes);
            let expected_to_escape = ESCAPE_DECISION_TABLE[byte_val as usize] == 1;

            if expected_to_escape {
                // If this byte SHOULD be escaped, we expect to find it at index 16.
                assert_eq!(
                    result,
                    Some(16),
                    "Failed to find required escape for byte 0x{:02X}",
                    byte_val
                );
            } else {
                // If this byte should NOT be escaped, we expect to find nothing.
                assert_eq!(
                    result, None,
                    "Incorrectly found an escape for byte 0x{:02X}",
                    byte_val
                );
            }
        }
    }
}