succinctly 0.7.0

High-performance succinct data structures for Rust
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
//! SIMD-accelerated string scanning for YAML parsing.
//!
//! This module provides vectorized implementations for scanning inside
//! quoted strings, which is one of the main hot paths in YAML parsing.
//!
//! The key optimization is skipping to the next "interesting" character
//! (quote or escape) rather than checking each byte individually.
//!
//! ## Algorithm
//!
//! For double-quoted strings, find the next `"` or `\` character.
//! For single-quoted strings, find the next `'` character.
//!
//! Uses SIMD comparisons to check 16/32 bytes at a time, then
//! extracts a bitmask and uses `trailing_zeros()` to find the position.
//!
//! ## Broadword Fallback
//!
//! The `broadword` module provides a portable implementation using pure u64
//! arithmetic (SWAR = SIMD Within A Register). This works on any platform
//! without CPU-specific intrinsics and can be enabled with `--features broadword-yaml`
//! to use instead of NEON on ARM64 for comparison testing.
//!
//! ## Scalar Fallback
//!
//! Use `--features scalar-yaml` to force pure scalar (byte-by-byte) implementations.
//! This is useful for benchmarking baseline performance without any SIMD or broadword.

// NEON module only when not using broadword or scalar fallback
#[cfg(all(
    target_arch = "aarch64",
    not(feature = "broadword-yaml"),
    not(feature = "scalar-yaml")
))]
mod neon;

// x86 module only when not using scalar fallback
#[cfg(all(target_arch = "x86_64", not(feature = "scalar-yaml")))]
mod x86;

// Portable broadword module - compiled when:
// 1. broadword-yaml feature is enabled on ARM64 (explicit opt-in), OR
// 2. On non-SIMD platforms (automatic fallback)
// NOT compiled on x86_64 (uses native SIMD) or ARM64 without broadword-yaml (uses NEON)
#[cfg(all(
    not(feature = "scalar-yaml"),
    any(
        all(target_arch = "aarch64", feature = "broadword-yaml"),
        not(any(target_arch = "aarch64", target_arch = "x86_64"))
    )
))]
mod broadword;

// Re-export platform-specific types for P0 optimizations
#[cfg(all(target_arch = "x86_64", not(feature = "scalar-yaml")))]
pub use x86::YamlCharClass;

// Re-export broadword types when broadword module is compiled
#[cfg(all(
    not(feature = "scalar-yaml"),
    any(
        all(target_arch = "aarch64", feature = "broadword-yaml"),
        not(any(target_arch = "aarch64", target_arch = "x86_64"))
    )
))]
pub use broadword::{YamlCharClass16, YamlCharClassBroadword};

// Re-export NEON types on ARM64 when using NEON (not broadword)
// Only YamlCharClass16 is used; YamlCharClassBroadword is broadword-specific
#[cfg(all(
    target_arch = "aarch64",
    not(feature = "broadword-yaml"),
    not(feature = "scalar-yaml")
))]
pub use neon::YamlCharClass16;

// ============================================================================
// Public API - platform-dispatched functions
// ============================================================================

/// Find the next double-quote (`"`) or backslash (`\`) in the input.
///
/// Returns the offset from `start` to the found character, or `None` if
/// neither is found before `end`.
///
/// This is used for fast-path scanning inside double-quoted strings.
#[inline]
pub fn find_quote_or_escape(input: &[u8], start: usize, end: usize) -> Option<usize> {
    if start >= end || start >= input.len() {
        return None;
    }
    let end = end.min(input.len());

    // Scalar fallback: when scalar-yaml feature is enabled
    #[cfg(feature = "scalar-yaml")]
    {
        find_quote_or_escape_scalar(input, start, end)
    }

    // ARM64 with NEON (default, when not scalar or broadword)
    #[cfg(all(
        target_arch = "aarch64",
        not(feature = "broadword-yaml"),
        not(feature = "scalar-yaml")
    ))]
    {
        neon::find_quote_or_escape_neon(input, start, end)
    }

    // x86_64 uses native SIMD (when not scalar)
    #[cfg(all(target_arch = "x86_64", not(feature = "scalar-yaml")))]
    {
        x86::find_quote_or_escape_x86(input, start, end)
    }

    // Broadword fallback: when feature enabled on ARM64, or on non-SIMD platforms (when not scalar)
    #[cfg(all(
        not(feature = "scalar-yaml"),
        any(
            all(target_arch = "aarch64", feature = "broadword-yaml"),
            not(any(target_arch = "aarch64", target_arch = "x86_64"))
        )
    ))]
    {
        broadword::find_quote_or_escape_broadword(input, start, end)
    }
}

/// Find the next single-quote (`'`) in the input.
///
/// Returns the offset from `start` to the found character, or `None` if
/// not found before `end`.
///
/// This is used for fast-path scanning inside single-quoted strings.
#[inline]
pub fn find_single_quote(input: &[u8], start: usize, end: usize) -> Option<usize> {
    if start >= end || start >= input.len() {
        return None;
    }
    let end = end.min(input.len());

    // Scalar fallback: when scalar-yaml feature is enabled
    #[cfg(feature = "scalar-yaml")]
    {
        find_single_quote_scalar(input, start, end)
    }

    // ARM64 with NEON (default, when not scalar or broadword)
    #[cfg(all(
        target_arch = "aarch64",
        not(feature = "broadword-yaml"),
        not(feature = "scalar-yaml")
    ))]
    {
        neon::find_single_quote_neon(input, start, end)
    }

    // x86_64 uses native SIMD (when not scalar)
    #[cfg(all(target_arch = "x86_64", not(feature = "scalar-yaml")))]
    {
        x86::find_single_quote_x86(input, start, end)
    }

    // Broadword fallback: when feature enabled on ARM64, or on non-SIMD platforms (when not scalar)
    #[cfg(all(
        not(feature = "scalar-yaml"),
        any(
            all(target_arch = "aarch64", feature = "broadword-yaml"),
            not(any(target_arch = "aarch64", target_arch = "x86_64"))
        )
    ))]
    {
        broadword::find_single_quote_broadword(input, start, end)
    }
}

/// Count leading spaces (indentation) from the given position.
///
/// Returns the number of consecutive space characters starting at `start`.
/// Stops at the first non-space character or end of input.
///
/// This is used for fast indentation counting in YAML block-style parsing.
#[inline]
pub fn count_leading_spaces(input: &[u8], start: usize) -> usize {
    if start >= input.len() {
        return 0;
    }

    // Scalar fallback: when scalar-yaml feature is enabled
    #[cfg(feature = "scalar-yaml")]
    {
        count_leading_spaces_scalar(input, start)
    }

    // ARM64 with NEON (default, when not scalar or broadword)
    #[cfg(all(
        target_arch = "aarch64",
        not(feature = "broadword-yaml"),
        not(feature = "scalar-yaml")
    ))]
    {
        neon::count_leading_spaces_neon(input, start)
    }

    // x86_64 uses native SIMD (when not scalar)
    #[cfg(all(target_arch = "x86_64", not(feature = "scalar-yaml")))]
    {
        x86::count_leading_spaces_x86(input, start)
    }

    // Broadword fallback: when feature enabled on ARM64, or on non-SIMD platforms (when not scalar)
    #[cfg(all(
        not(feature = "scalar-yaml"),
        any(
            all(target_arch = "aarch64", feature = "broadword-yaml"),
            not(any(target_arch = "aarch64", target_arch = "x86_64"))
        )
    ))]
    {
        broadword::count_leading_spaces_broadword(input, start)
    }
}

// ============================================================================
// P0 Optimizations - Multi-Character Classification
// ============================================================================

/// Classify YAML structural characters in a chunk (32 bytes on AVX2, 16 on SSE2).
///
/// Returns classification bitmasks for multiple character types at once.
/// This is the main P0 optimization for bulk character detection.
///
/// Currently only available on x86_64. Returns None on other platforms.
#[inline]
#[cfg(all(target_arch = "x86_64", not(feature = "scalar-yaml")))]
pub fn classify_yaml_chars(input: &[u8], offset: usize) -> Option<YamlCharClass> {
    x86::classify_yaml_chars(input, offset)
}

/// Classify 16 bytes of YAML structural characters.
///
/// On ARM64: Uses NEON or broadword depending on feature flags.
/// On other platforms: Uses broadword (SWAR).
/// Returns classification bitmasks for 8 character types at once.
///
/// Available on ARM64 and non-SIMD platforms.
#[inline]
#[cfg(all(
    not(feature = "scalar-yaml"),
    any(target_arch = "aarch64", not(target_arch = "x86_64"))
))]
#[allow(dead_code)]
pub fn classify_yaml_chars_16(input: &[u8], offset: usize) -> Option<YamlCharClass16> {
    // ARM64 with NEON (default)
    #[cfg(all(target_arch = "aarch64", not(feature = "broadword-yaml")))]
    {
        neon::classify_yaml_chars_16(input, offset)
    }

    // Broadword: when feature enabled on ARM64, or on non-SIMD platforms
    #[cfg(any(
        all(target_arch = "aarch64", feature = "broadword-yaml"),
        not(any(target_arch = "aarch64", target_arch = "x86_64"))
    ))]
    {
        broadword::classify_yaml_chars_16(input, offset)
    }
}

/// Classify 8 bytes of YAML structural characters using broadword operations.
///
/// Uses pure u64 arithmetic instead of SIMD intrinsics.
///
/// Available on ARM64 (with broadword-yaml feature) and non-SIMD platforms.
#[inline]
#[cfg(all(
    not(feature = "scalar-yaml"),
    any(
        all(target_arch = "aarch64", feature = "broadword-yaml"),
        not(any(target_arch = "aarch64", target_arch = "x86_64"))
    )
))]
#[allow(dead_code)]
pub fn classify_yaml_chars_8(input: &[u8], offset: usize) -> Option<YamlCharClassBroadword> {
    broadword::classify_yaml_chars_broadword(input, offset)
}

/// Find the next newline (`\n`) from the given position.
///
/// Returns offset from `start` to the newline, or `None` if not found.
/// Uses SIMD/broadword for fast scanning on supported platforms.
#[inline]
#[allow(dead_code)]
pub fn find_newline(input: &[u8], start: usize) -> Option<usize> {
    if start >= input.len() {
        return None;
    }

    // Scalar fallback: when scalar-yaml feature is enabled
    #[cfg(feature = "scalar-yaml")]
    {
        find_newline_scalar(input, start)
    }

    // x86_64 uses native SIMD (when not scalar)
    #[cfg(all(target_arch = "x86_64", not(feature = "scalar-yaml")))]
    {
        x86::find_newline_x86(input, start)
    }

    // ARM64 with NEON (default, when not scalar or broadword)
    #[cfg(all(
        target_arch = "aarch64",
        not(feature = "broadword-yaml"),
        not(feature = "scalar-yaml")
    ))]
    {
        neon::find_newline_broadword(input, start)
    }

    // Broadword: when feature enabled on ARM64, or on non-SIMD platforms (when not scalar)
    #[cfg(all(
        not(feature = "scalar-yaml"),
        any(
            all(target_arch = "aarch64", feature = "broadword-yaml"),
            not(any(target_arch = "aarch64", target_arch = "x86_64"))
        )
    ))]
    {
        broadword::find_newline_broadword(input, start)
    }
}

/// Find the end of a block scalar by scanning for a line with insufficient indentation.
///
/// Uses SIMD to find newlines and check indentation efficiently.
/// Returns the position where the block ends (start of line with insufficient indent),
/// or input.len() if EOF is reached.
///
/// This is used to quickly skip to the end of `|` (literal) or `>` (folded) block scalars.
#[inline]
pub fn find_block_scalar_end(input: &[u8], start: usize, min_indent: usize) -> Option<usize> {
    #[cfg(all(target_arch = "x86_64", not(feature = "scalar-yaml")))]
    {
        x86::find_block_scalar_end(input, start, min_indent)
    }

    #[cfg(all(
        target_arch = "aarch64",
        not(feature = "broadword-yaml"),
        not(feature = "scalar-yaml")
    ))]
    {
        Some(neon::find_block_scalar_end_neon(input, start, min_indent))
    }

    #[cfg(not(any(
        all(target_arch = "x86_64", not(feature = "scalar-yaml")),
        all(
            target_arch = "aarch64",
            not(feature = "broadword-yaml"),
            not(feature = "scalar-yaml")
        )
    )))]
    {
        find_block_scalar_end_scalar(input, start, min_indent)
    }
}

// ============================================================================
// P4 Optimizations - Anchor/Alias Name Parsing
// ============================================================================

/// Parse anchor/alias name using SIMD to find terminator characters.
///
/// Searches for YAML anchor name terminators:
/// - Whitespace: space, tab, newline, CR
/// - Flow indicators: [ ] { } ,
/// - Colons followed by whitespace
///
/// Returns the position of the first terminator, or end of input.
///
/// This is the main P4 optimization for fast anchor/alias name scanning.
#[inline]
pub fn parse_anchor_name(input: &[u8], start: usize) -> usize {
    #[cfg(all(target_arch = "x86_64", not(feature = "scalar-yaml")))]
    {
        x86::parse_anchor_name(input, start)
    }

    #[cfg(all(
        target_arch = "aarch64",
        not(feature = "broadword-yaml"),
        not(feature = "scalar-yaml")
    ))]
    {
        neon::parse_anchor_name_neon(input, start)
    }

    #[cfg(not(any(
        all(target_arch = "x86_64", not(feature = "scalar-yaml")),
        all(
            target_arch = "aarch64",
            not(feature = "broadword-yaml"),
            not(feature = "scalar-yaml")
        )
    )))]
    {
        parse_anchor_name_scalar(input, start)
    }
}

// ============================================================================
// Issue #87: JSON Escape Scanning for Streaming Output
// ============================================================================

/// Find the next JSON escapable character in the input.
///
/// Searches for characters that need escaping in JSON strings:
/// - Double quote (`"`)
/// - Backslash (`\`)
/// - Control characters (bytes < 0x20)
///
/// Returns the index of the first escapable character, or `bytes.len()`
/// if no escapable character is found.
///
/// This is the main optimization for `write_json_string` in the YAML→JSON
/// streaming path. Uses SIMD to process 16-32 bytes at a time.
#[inline(always)]
pub fn find_json_escape(bytes: &[u8], start: usize) -> usize {
    if start >= bytes.len() {
        return bytes.len();
    }

    // ARM64 with NEON (default)
    #[cfg(all(
        target_arch = "aarch64",
        not(feature = "broadword-yaml"),
        not(feature = "scalar-yaml")
    ))]
    {
        neon::find_json_escape_neon(bytes, start)
    }

    // x86_64 - use AVX2/SSE2 SIMD
    #[cfg(all(target_arch = "x86_64", not(feature = "scalar-yaml")))]
    {
        x86::find_json_escape_x86(bytes, start).map_or(bytes.len(), |offset| start + offset)
    }

    // Scalar fallback for other platforms
    #[cfg(any(
        feature = "scalar-yaml",
        not(any(target_arch = "aarch64", target_arch = "x86_64"))
    ))]
    {
        find_json_escape_scalar(bytes, start)
    }
}

/// Scalar implementation of find_json_escape.
#[inline(always)]
#[allow(dead_code)]
fn find_json_escape_scalar(bytes: &[u8], start: usize) -> usize {
    for (i, &b) in bytes[start..].iter().enumerate() {
        if b == b'"' || b == b'\\' || b < 0x20 {
            return start + i;
        }
    }
    bytes.len()
}

/// Scalar fallback for parse_anchor_name
#[allow(dead_code)]
fn parse_anchor_name_scalar(input: &[u8], start: usize) -> usize {
    let mut pos = start;
    while pos < input.len() {
        let b = input[pos];
        match b {
            // Stop at flow indicators, whitespace, and newlines
            b' ' | b'\t' | b'\n' | b'\r' | b'[' | b']' | b'{' | b'}' | b',' => break,
            // Colon is allowed in anchor names if not followed by whitespace
            b':' => {
                if pos + 1 < input.len() {
                    let next = input[pos + 1];
                    if next == b' ' || next == b'\t' || next == b'\n' || next == b'\r' {
                        break;
                    }
                }
                pos += 1;
            }
            _ => pos += 1,
        }
    }
    pos
}

/// Scalar fallback for find_block_scalar_end
#[allow(dead_code)]
fn find_block_scalar_end_scalar(input: &[u8], start: usize, min_indent: usize) -> Option<usize> {
    let mut pos = start;

    while pos < input.len() {
        if input[pos] == b'\n' {
            let line_start = pos + 1;

            if line_start >= input.len() {
                return Some(input.len());
            }

            // Count leading spaces
            let mut indent = 0;
            while line_start + indent < input.len() && input[line_start + indent] == b' ' {
                indent += 1;
            }

            // Check if this line has content at insufficient indent
            if line_start + indent < input.len() {
                let next_char = input[line_start + indent];
                if next_char != b'\n' && next_char != b'\r' && indent < min_indent {
                    return Some(line_start);
                }
            }
        }
        pos += 1;
    }

    Some(input.len())
}

/// Scalar implementation of find_newline.
#[allow(dead_code)]
fn find_newline_scalar(input: &[u8], start: usize) -> Option<usize> {
    for (i, &b) in input[start..].iter().enumerate() {
        if b == b'\n' {
            return Some(i);
        }
    }
    None
}

// ============================================================================
// Scalar fallbacks (for non-SIMD platforms or testing)
// ============================================================================

/// Scalar implementation of find_quote_or_escape.
#[allow(dead_code)]
fn find_quote_or_escape_scalar(input: &[u8], start: usize, end: usize) -> Option<usize> {
    for (i, &b) in input[start..end].iter().enumerate() {
        if b == b'"' || b == b'\\' {
            return Some(i);
        }
    }
    None
}

/// Scalar implementation of find_single_quote.
#[allow(dead_code)]
fn find_single_quote_scalar(input: &[u8], start: usize, end: usize) -> Option<usize> {
    for (i, &b) in input[start..end].iter().enumerate() {
        if b == b'\'' {
            return Some(i);
        }
    }
    None
}

/// Scalar implementation of count_leading_spaces.
#[allow(dead_code)]
fn count_leading_spaces_scalar(input: &[u8], start: usize) -> usize {
    input[start..].iter().take_while(|&&b| b == b' ').count()
}

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

    #[test]
    fn test_find_quote_or_escape_basic() {
        let input = b"hello\"world";
        assert_eq!(find_quote_or_escape(input, 0, input.len()), Some(5));
    }

    #[test]
    fn test_find_quote_or_escape_backslash() {
        let input = b"hello\\world";
        assert_eq!(find_quote_or_escape(input, 0, input.len()), Some(5));
    }

    #[test]
    fn test_find_quote_or_escape_none() {
        let input = b"hello world";
        assert_eq!(find_quote_or_escape(input, 0, input.len()), None);
    }

    #[test]
    fn test_find_quote_or_escape_start_offset() {
        let input = b"ab\"cd\"ef";
        assert_eq!(find_quote_or_escape(input, 3, input.len()), Some(2)); // 'c' at 3, '"' at 5
    }

    #[test]
    fn test_find_quote_or_escape_long_string() {
        // Test with > 16 bytes to exercise SIMD path
        let mut input = vec![b'a'; 100];
        input[50] = b'"';
        assert_eq!(find_quote_or_escape(&input, 0, input.len()), Some(50));
    }

    #[test]
    fn test_find_single_quote_basic() {
        let input = b"hello'world";
        assert_eq!(find_single_quote(input, 0, input.len()), Some(5));
    }

    #[test]
    fn test_find_single_quote_none() {
        let input = b"hello world";
        assert_eq!(find_single_quote(input, 0, input.len()), None);
    }

    #[test]
    fn test_find_single_quote_long_string() {
        // Test with > 16 bytes to exercise SIMD path
        let mut input = vec![b'a'; 100];
        input[75] = b'\'';
        assert_eq!(find_single_quote(&input, 0, input.len()), Some(75));
    }

    #[test]
    fn test_empty_range() {
        let input = b"hello";
        assert_eq!(find_quote_or_escape(input, 5, 5), None);
        assert_eq!(find_quote_or_escape(input, 10, 5), None);
    }

    #[test]
    fn test_find_at_boundary() {
        // Quote exactly at 16-byte boundary
        let mut input = vec![b'a'; 32];
        input[16] = b'"';
        assert_eq!(find_quote_or_escape(&input, 0, input.len()), Some(16));
    }

    // ========================================================================
    // JSON escape scanning tests (Issue #87)
    // ========================================================================

    #[test]
    fn test_find_json_escape_basic() {
        let input = b"hello\"world";
        assert_eq!(find_json_escape(input, 0), 5);
    }

    #[test]
    fn test_find_json_escape_backslash() {
        let input = b"hello\\world";
        assert_eq!(find_json_escape(input, 0), 5);
    }

    #[test]
    fn test_find_json_escape_control() {
        let input = b"hello\nworld";
        assert_eq!(find_json_escape(input, 0), 5);
    }

    #[test]
    fn test_find_json_escape_none() {
        let input = b"hello world";
        // Returns input.len() when no escape character found
        assert_eq!(find_json_escape(input, 0), input.len());
    }

    #[test]
    fn test_find_json_escape_long() {
        let mut input = vec![b'a'; 100];
        input[50] = b'"';
        assert_eq!(find_json_escape(&input, 0), 50);
    }

    #[test]
    fn test_find_json_escape_simd_matches_scalar() {
        let test_cases: &[&[u8]] = &[
            b"",
            b"\"",
            b"\\",
            b"\n",
            b"\t",
            b"no special chars",
            b"quote\"here",
            b"newline\nhere",
            &[b'x'; 100],
        ];

        for &input in test_cases {
            let scalar = find_json_escape_scalar(input, 0);
            let simd = find_json_escape(input, 0);
            assert_eq!(
                scalar,
                simd,
                "JSON escape mismatch for {:?}",
                String::from_utf8_lossy(input)
            );
        }
    }

    // Compare SIMD vs scalar for correctness
    #[test]
    fn test_simd_matches_scalar() {
        let test_cases: &[&[u8]] = &[
            b"",
            b"\"",
            b"\\",
            b"'",
            b"no special chars here",
            b"quote at end\"",
            b"\"quote at start",
            b"has\\backslash",
            b"has both \" and \\ chars",
            // Long strings
            &[b'x'; 100],
        ];

        for &input in test_cases {
            let scalar_dq = find_quote_or_escape_scalar(input, 0, input.len());
            let simd_dq = find_quote_or_escape(input, 0, input.len());
            assert_eq!(
                scalar_dq,
                simd_dq,
                "double-quote mismatch for {:?}",
                String::from_utf8_lossy(input)
            );

            let scalar_sq = find_single_quote_scalar(input, 0, input.len());
            let simd_sq = find_single_quote(input, 0, input.len());
            assert_eq!(
                scalar_sq,
                simd_sq,
                "single-quote mismatch for {:?}",
                String::from_utf8_lossy(input)
            );
        }
    }

    #[test]
    fn test_count_leading_spaces_basic() {
        assert_eq!(count_leading_spaces(b"  hello", 0), 2);
        assert_eq!(count_leading_spaces(b"    world", 0), 4);
        assert_eq!(count_leading_spaces(b"no spaces", 0), 0);
        assert_eq!(count_leading_spaces(b"", 0), 0);
    }

    #[test]
    fn test_count_leading_spaces_offset() {
        assert_eq!(count_leading_spaces(b"xx  hello", 2), 2);
        assert_eq!(count_leading_spaces(b"key:   value", 4), 3);
    }

    #[test]
    fn test_count_leading_spaces_all_spaces() {
        let spaces = vec![b' '; 100];
        assert_eq!(count_leading_spaces(&spaces, 0), 100);
    }

    #[test]
    fn test_count_leading_spaces_long() {
        // Test with > 16 bytes to exercise SIMD path
        let mut input = vec![b' '; 50];
        input.extend_from_slice(b"content");
        assert_eq!(count_leading_spaces(&input, 0), 50);
    }

    #[test]
    fn test_count_leading_spaces_at_boundary() {
        // Spaces ending exactly at 16-byte boundary
        let mut input = vec![b' '; 16];
        input.push(b'x');
        assert_eq!(count_leading_spaces(&input, 0), 16);

        // Spaces ending at 32-byte boundary
        let mut input32 = vec![b' '; 32];
        input32.push(b'x');
        assert_eq!(count_leading_spaces(&input32, 0), 32);
    }

    #[test]
    fn test_count_leading_spaces_simd_matches_scalar() {
        let test_cases: &[&[u8]] = &[
            b"",
            b" ",
            b"  ",
            b"   ",
            b"    ",
            b"        ",                         // 8 spaces
            b"                ",                 // 16 spaces
            b"                                ", // 32 spaces
            b"no spaces",
            b" one",
            b"  two",
            b"                x",  // 16 spaces + char
            b"                 x", // 17 spaces + char
        ];

        for &input in test_cases {
            let scalar = count_leading_spaces_scalar(input, 0);
            let simd = count_leading_spaces(input, 0);
            assert_eq!(
                scalar,
                simd,
                "leading spaces mismatch for {:?}",
                String::from_utf8_lossy(input)
            );
        }
    }
}