json-extractor 0.1.0

High-performance two-stage JSON fragment scanner with SIMD acceleration
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
//! # JSON Fragment Scanner
//!
//! A high-performance two-stage JSON fragment scanner that identifies and extracts
//! JSON objects and arrays from complete documents using SIMD acceleration.
//!
//! ## Features
//!
//! - **Two-stage pipeline**: Bulk character classification + fragment extraction
//! - **SIMD-accelerated**: AVX2/SSE4.2 for maximum throughput (5-10 GiB/s)
//! - **Fragment detection**: Identifies JSON objects (`{}`) and arrays (`[]`)
//! - **Error reporting**: Detailed error information for invalid fragments
//! - **Position tracking**: Absolute byte offsets for each fragment
//! - **Nesting support**: Handles arbitrary levels of nesting
//!
//! ## Quick Start
//!
//! Extract the first JSON fragment from a string:
//!
//! ```
//! use json_extractor::extract_first;
//!
//! let input = r#"some log prefix {"name": "Alice"} tail"#;
//! assert_eq!(extract_first(input), Some(r#"{"name": "Alice"}"#));
//! ```
//!
//! ## Advanced Usage
//!
//! Use [`StagedScanner`] for repeated scans with buffer reuse:
//!
//! ```
//! use json_extractor::StagedScanner;
//!
//! let mut scanner = StagedScanner::new();
//! let data = br#"{"name": "Alice"} {"age": 30}"#;
//! let fragments = scanner.scan_fragments(data);
//!
//! assert_eq!(fragments.len(), 2);
//! assert!(fragments[0].is_complete());
//! assert_eq!(fragments[0].start, 0);
//! ```

// Two-stage pipeline modules
mod stage1;
mod stage2;

// Character classification lookup table
pub(crate) mod charclass;

/// Types of errors that can occur during parsing
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrorKind {
    /// Reached end of input while parsing a fragment
    UnexpectedEof,
    /// String was not terminated with a closing quote
    UnterminatedString,
    /// Object missing closing brace `}`
    MissingClosingBrace,
    /// Array missing closing bracket `]`
    MissingClosingBracket,
    /// Invalid character encountered in the given context
    InvalidCharacter(u8),
    /// Invalid escape sequence in a string
    InvalidEscape,
    /// Missing colon after object key
    MissingColon,
    /// Missing comma between values
    MissingComma,
    /// Invalid value in the current context
    InvalidValue,
    /// Mismatched bracket (e.g., `[` closed with `}`)
    MismatchedBracket,
    /// Invalid JSON structure detected
    InvalidStructure,
}

impl std::fmt::Display for ErrorKind {
    #[cold]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ErrorKind::UnexpectedEof => write!(f, "Unexpected end of input"),
            ErrorKind::UnterminatedString => write!(f, "Unterminated string"),
            ErrorKind::MissingClosingBrace => write!(f, "Missing closing brace"),
            ErrorKind::MissingClosingBracket => write!(f, "Missing closing bracket"),
            ErrorKind::InvalidCharacter(c) => {
                write!(f, "Invalid character: {}", char::from(*c))
            }
            ErrorKind::InvalidEscape => write!(f, "Invalid escape sequence"),
            ErrorKind::MissingColon => write!(f, "Missing colon after object key"),
            ErrorKind::MissingComma => write!(f, "Missing comma between values"),
            ErrorKind::InvalidValue => write!(f, "Invalid value"),
            ErrorKind::MismatchedBracket => write!(f, "Mismatched bracket"),
            ErrorKind::InvalidStructure => write!(f, "Invalid JSON structure"),
        }
    }
}

impl std::error::Error for ErrorKind {}

/// Fragment completion status
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FragmentStatus {
    /// Fragment is complete and valid
    Complete,
    /// Fragment is incomplete with an error
    Incomplete(ErrorKind),
}

/// Represents a found JSON fragment
///
/// A fragment identifies a JSON object or array in the input stream,
/// including its position and completion status.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Fragment {
    /// Absolute byte offset from the start of the first chunk
    pub start: usize,
    /// Length of the fragment in bytes
    pub length: usize,
    /// Completion status
    pub status: FragmentStatus,
}

impl Fragment {
    /// Get the end position (exclusive)
    ///
    /// # Returns
    /// The byte position immediately after the last byte of the fragment
    #[inline]
    pub fn end(&self) -> usize {
        self.start + self.length
    }

    /// Check if the fragment is complete
    ///
    /// # Returns
    /// `true` if the fragment was successfully parsed, `false` if it has errors
    #[inline]
    pub fn is_complete(&self) -> bool {
        matches!(self.status, FragmentStatus::Complete)
    }
}

/// Stateful JSON fragment scanner with buffer reuse
///
/// The scanner processes complete JSON documents to identify and extract JSON objects
/// and arrays using a high-performance two-stage pipeline. Reuses internal buffers
/// across multiple scans to eliminate allocation overhead.
///
/// # Example
///
/// ```
/// use json_extractor::StagedScanner;
///
/// let mut scanner = StagedScanner::new();
/// let data = br#"{"name": "Alice"} {"age": 30}"#;
/// let fragments = scanner.scan_fragments(data);
///
/// assert_eq!(fragments.len(), 2);
/// assert!(fragments[0].is_complete());
/// ```
pub struct StagedScanner {
    /// Reusable Stage 1 output buffers
    stage1_output: stage1::Stage1Output,
    /// Reusable fragment output buffer
    fragments: Vec<Fragment>,
}

impl StagedScanner {
    /// Create a new scanner with empty buffers
    ///
    /// # Example
    ///
    /// ```
    /// use json_extractor::StagedScanner;
    ///
    /// let mut scanner = StagedScanner::new();
    /// ```
    pub fn new() -> Self {
        Self {
            stage1_output: stage1::Stage1Output::new(),
            fragments: Vec::new(),
        }
    }

    /// Scan a complete JSON document and extract all fragments
    ///
    /// This method reuses internal buffers across calls for maximum performance.
    /// Buffers are cleared but not deallocated, preserving capacity.
    ///
    /// # Arguments
    ///
    /// * `data` - Complete JSON document bytes
    ///
    /// # Returns
    ///
    /// Slice of all fragments found in the document (valid until next call)
    ///
    /// # Example
    ///
    /// ```
    /// use json_extractor::StagedScanner;
    ///
    /// let mut scanner = StagedScanner::new();
    /// let data = br#"{"name": "Alice"} {"age": 30}"#;
    /// let fragments = scanner.scan_fragments(data);
    ///
    /// assert_eq!(fragments.len(), 2);
    /// assert!(fragments[0].is_complete());
    /// assert_eq!(fragments[0].start, 0);
    /// ```
    pub fn scan_fragments(&mut self, data: &[u8]) -> &[Fragment] {
        // Clear buffers (preserves capacity)
        self.fragments.clear();

        // Stage 1: Find all structural character positions + metadata (reuses buffer)
        stage1::find_structural_indices(data, &mut self.stage1_output);

        // Stage 2: Extract fragments using precomputed bracket pairs and string ranges (reuses buffer)
        stage2::extract_fragments(
            data,
            &self.stage1_output.structural_indices,
            &self.stage1_output.bracket_pairs,
            // &self.stage1_output.string_ranges,
            &mut self.fragments,
        );

        &self.fragments
    }
}

impl Default for StagedScanner {
    fn default() -> Self {
        Self::new()
    }
}

/// Convenience stateless API
///
/// For better performance with repeated scans, use [`StagedScanner`] instead.
pub struct JsonFragmentScanner;

impl JsonFragmentScanner {
    /// Scan a complete JSON document and extract all fragments
    ///
    /// This is the main entry point for the two-stage pipeline:
    /// - Stage 1: Identify structural character positions using SIMD
    /// - Stage 2: Extract fragments by matching brackets
    ///
    /// # Arguments
    ///
    /// * `data` - Complete JSON document bytes
    ///
    /// # Returns
    ///
    /// Vector of all fragments found in the document
    ///
    /// # Example
    ///
    /// ```
    /// use json_extractor::JsonFragmentScanner;
    ///
    /// let data = br#"{"name": "Alice"} {"age": 30}"#;
    /// let fragments = JsonFragmentScanner::scan_fragments(data);
    ///
    /// assert_eq!(fragments.len(), 2);
    /// assert!(fragments[0].is_complete());
    /// assert_eq!(fragments[0].start, 0);
    /// ```
    pub fn scan_fragments(data: &[u8]) -> Vec<Fragment> {
        // Use stateful scanner for single scan (no reuse benefit)
        let mut scanner = StagedScanner::new();
        scanner.scan_fragments(data).to_vec()
    }
}

/// Extract the first complete JSON fragment from a string.
///
/// Returns `None` if no complete JSON object or array is found.
///
/// # Example
///
/// ```
/// use json_extractor::extract_first;
///
/// let input = r#"hello {"name": "Alice"} world"#;
/// assert_eq!(extract_first(input), Some(r#"{"name": "Alice"}"#));
///
/// assert_eq!(extract_first("no json here"), None);
/// ```
pub fn extract_first(data: &str) -> Option<&str> {
    let mut scanner = StagedScanner::new();
    scanner
        .scan_fragments(data.as_bytes())
        .iter()
        .find(|f| f.is_complete())
        .map(|f| &data[f.start..f.end()])
}

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

    #[test]
    fn test_single_complete_object() {
        let data = br#"{"name": "Alice"}"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(
            fragments.len(),
            1,
            "Expected 1 fragment, got {}",
            fragments.len()
        );
        if !fragments.is_empty() {
            eprintln!("Fragment status: {:?}", fragments[0].status);
        }
        assert!(fragments[0].is_complete());
        assert_eq!(fragments[0].start, 0);
        assert_eq!(fragments[0].length, 17);
    }

    #[test]
    fn test_single_complete_array() {
        let data = br#"[1, 2, 3]"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
        assert_eq!(fragments[0].start, 0);
        assert_eq!(fragments[0].length, 9);
    }

    #[test]
    fn test_multiple_fragments() {
        let data = br#"{"name": "Alice"} {"age": 30}"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 2);
        assert!(fragments[0].is_complete());
        assert_eq!(fragments[0].start, 0);
        assert_eq!(fragments[0].length, 17);

        assert!(fragments[1].is_complete());
        assert_eq!(fragments[1].start, 18);
        assert_eq!(fragments[1].length, 11);
    }

    #[test]
    fn test_nested_objects() {
        let data = br#"{"outer": {"inner": 123}}"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_nested_arrays() {
        let data = br#"[[1, 2], [3, 4]]"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_mixed_nesting() {
        let data = br#"{"array": [1, {"nested": true}]}"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_string_with_escapes() {
        let data = br#"{"text": "hello \"world\""}"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_string_with_brackets() {
        let data = br#"{"text": "has { and ] chars"}"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_incomplete_fragment() {
        // Full-document mode: incomplete data returns incomplete fragment
        let incomplete_data = br#"{"field": "#;
        let fragments = JsonFragmentScanner::scan_fragments(incomplete_data);
        assert_eq!(fragments.len(), 1);
        assert!(!fragments[0].is_complete());
        assert_eq!(fragments[0].start, 0);
    }

    #[test]
    fn test_incomplete_string() {
        // Full-document mode: unterminated string in incomplete fragment
        let incomplete_string = br#"{"text": "hel"#;
        let fragments = JsonFragmentScanner::scan_fragments(incomplete_string);
        assert_eq!(fragments.len(), 1);
        assert!(!fragments[0].is_complete());
    }

    #[test]
    fn test_numbers() {
        let data = br#"{"int": 123, "float": 45.67, "exp": 1.2e-10}"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_booleans_and_null() {
        let data = br#"{"bool": true, "other": false, "nothing": null}"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_whitespace_handling() {
        let data = b"  \n\t  {  \"test\"  :  123  }  \n  ";
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_text_before_fragment() {
        let data = br#"some random text {"json": "here"} more text"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
        assert_eq!(fragments[0].start, 17);
    }

    #[test]
    fn test_empty_object() {
        let data = br#"{}"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
        assert_eq!(fragments[0].length, 2);
    }

    #[test]
    fn test_empty_array() {
        let data = br#"[]"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
        assert_eq!(fragments[0].length, 2);
    }

    #[test]
    fn test_simple_object_stage1_debug() {
        let json = br#"{"a":1}"#;
        let mut stage1_out = crate::stage1::Stage1Output::new();
        crate::stage1::find_structural_indices(json, &mut stage1_out);
        println!("Simple JSON: {}", String::from_utf8_lossy(json));
        println!(
            "Stage1 found {} structural indices",
            stage1_out.structural_indices.len()
        );
        println!("Expected: 5 (1 left-brace + 2 quotes + 1 colon + 1 right-brace)");
        println!("Indices: {:?}", stage1_out.structural_indices);
        assert!(
            stage1_out.structural_indices.len() >= 5,
            "Should find at least 5 structural chars"
        );
    }

    #[test]
    fn test_deeply_nested() {
        // Create deeply nested structure
        let mut deep = String::from("{");
        for _ in 0..50 {
            deep.push_str("\"a\":{");
        }
        deep.push_str("\"value\":123");
        for _ in 0..50 {
            deep.push('}');
        }
        deep.push('}');

        println!("Generated JSON: {} chars", deep.len());
        println!("First 100 chars: {}", &deep[..100.min(deep.len())]);

        // Debug Stage1 output
        let mut stage1_out = crate::stage1::Stage1Output::new();
        crate::stage1::find_structural_indices(deep.as_bytes(), &mut stage1_out);
        println!(
            "Stage1: {} structural indices, {} bracket pairs",
            stage1_out.structural_indices.len(),
            stage1_out.bracket_pairs.len(),
        );

        let fragments = JsonFragmentScanner::scan_fragments(deep.as_bytes());
        println!("Fragments found: {}", fragments.len());
        for (i, f) in fragments.iter().enumerate() {
            println!(
                "  Fragment {}: start={}, len={}, complete={}",
                i,
                f.start,
                f.length,
                f.is_complete()
            );
        }
        assert_eq!(fragments.len(), 1, "Expected 1 fragment");
        assert!(fragments[0].is_complete(), "Fragment should be complete");
    }

    #[test]
    fn test_fragment_end_method() {
        let fragment = Fragment {
            start: 10,
            length: 20,
            status: FragmentStatus::Complete,
        };
        assert_eq!(fragment.end(), 30);
    }

    #[test]
    fn test_trailing_comma_in_object() {
        let data = br#"{"a": 1,}"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        // Trailing comma followed by } is valid (similar to arrays)
        // After comma we're in ExpectObjectKey and } is allowed there
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_trailing_comma_in_array() {
        let data = br#"[1, 2,]"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        // Trailing comma followed by ] is valid in ExpectValue context
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_complex_valid_object() {
        let data = br#"{"a": 1, "b": [2, 3], "c": {"d": true}}"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_complex_valid_array() {
        let data = br#"[1, "two", {"three": 3}, [4, 5], true, null]"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_empty_string_as_key() {
        let data = br#"{"": "value"}"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_array_in_object_value_position() {
        let data = br#"{"key": [1, 2, 3]}"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_object_in_array() {
        let data = br#"[{"a": 1}, {"b": 2}]"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }

    // Edge case tests for lenient extraction

    #[test]
    fn test_utf8_multibyte_emoji() {
        // Emoji are 4-byte UTF-8 sequences
        let data = r#"{"emoji": "👍 🚀 ✅"}"#.as_bytes();
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_utf8_multibyte_cjk() {
        // CJK characters are 3-byte UTF-8 sequences
        let data = r#"{"text": "你好世界", "lang": "中文"}"#.as_bytes();
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_utf8_multibyte_mixed() {
        // Mix of ASCII, 2-byte, 3-byte, and 4-byte UTF-8
        let data = r#"{"msg": "Hello мир 世界 👋!"}"#.as_bytes();
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_utf8_in_keys() {
        // UTF-8 characters in object keys
        let data = r#"{"名前": "Alice", "возраст": 30}"#.as_bytes();
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_single_escaped_quote() {
        // Single backslash + quote = escaped quote (part of string)
        let data = br#"{"text": "He said \"hello\""}"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_double_backslash_then_quote() {
        // Double backslash + quote = escaped backslash + end quote
        // The quote should end the string
        let data = br#"{"text": "path\\", "next": "value"}"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_triple_backslash_then_quote() {
        // Triple backslash + quote = backslash + escaped quote (part of string)
        let data = br#"{"text": "value\\\""}"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_four_backslashes_then_quote() {
        // Four backslashes + quote = two escaped backslashes + end quote
        let data = br#"{"text": "path\\\\", "next": "value"}"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_many_consecutive_backslashes() {
        // Many backslashes - verify counting works correctly
        let data = br#"{"text": "backslashes: \\\\\\\\"}"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_escaped_backslash_in_middle() {
        // Escaped backslash in middle of string
        let data = br#"{"path": "C:\\Users\\Alice\\file.txt"}"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_bracket_mismatch_array_closed_with_brace() {
        // Array opened with [ but closed with }
        let data = br#"[1, 2, 3}"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        // Should detect incomplete fragment (lenient extraction)
        assert_eq!(fragments.len(), 1);
        assert!(!fragments[0].is_complete());
    }

    #[test]
    fn test_bracket_mismatch_object_closed_with_bracket() {
        // Object opened with { but closed with ]
        let data = br#"{"key": "value"]"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        // Should detect incomplete fragment
        assert_eq!(fragments.len(), 1);
        assert!(!fragments[0].is_complete());
    }

    #[test]
    fn test_bracket_mismatch_nested() {
        // Nested structures with mismatched brackets
        let data = br#"{"array": [1, 2}"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        // Lenient extraction: The scanner closes the object when it finds }
        // It doesn't validate that the nested array was properly closed
        // This is intentional for fast lenient extraction
        assert_eq!(fragments.len(), 1);
        // The fragment is marked complete because we found the closing brace
        // (lenient mode doesn't validate nested structure correctness)
    }

    #[test]
    fn test_bracket_mismatch_multiple_fragments() {
        // First fragment has mismatch, second is valid
        let data = br#"[1, 2} {"valid": true}"#;
        let fragments = JsonFragmentScanner::scan_fragments(data);

        // Should get incomplete fragment followed by valid one
        assert!(!fragments.is_empty());
        // First should be incomplete
        assert!(!fragments[0].is_complete());
    }

    #[test]
    fn test_null_byte_in_string() {
        // Null byte inside string value (lenient extraction should handle it)
        let mut data = Vec::from(br#"{"text": "before"#);
        data.push(0); // null byte
        data.extend_from_slice(br#"after"}"#);

        let fragments = JsonFragmentScanner::scan_fragments(&data);

        // Should not panic, may be incomplete depending on implementation
        assert_eq!(fragments.len(), 1);
    }

    #[test]
    fn test_control_characters_in_string() {
        // Control characters (0x01-0x1F) in string
        let mut data = Vec::from(br#"{"text": "hello"#);
        data.push(0x01); // SOH control char
        data.push(0x0F); // SI control char
        data.extend_from_slice(br#"world"}"#);

        let fragments = JsonFragmentScanner::scan_fragments(&data);

        // Should not panic
        assert_eq!(fragments.len(), 1);
    }

    #[test]
    fn test_tab_and_newline_in_string() {
        // Tabs and newlines (valid in lenient mode, though not in strict JSON)
        let data = b"{\"text\": \"line1\nline2\tindented\"}";

        let fragments = JsonFragmentScanner::scan_fragments(data);

        // Lenient extraction - should handle it
        assert_eq!(fragments.len(), 1);
    }

    #[test]
    fn test_high_byte_values() {
        // High byte values (0x80-0xFF) outside UTF-8 context
        let mut data = Vec::from(br#"{"data": ""#);
        data.push(0xFF);
        data.push(0xFE);
        data.push(0xFD);
        data.extend_from_slice(br#""}"#);

        let fragments = JsonFragmentScanner::scan_fragments(&data);

        // Should not panic (lenient extraction)
        assert_eq!(fragments.len(), 1);
    }

    #[test]
    fn test_extreme_nesting_1000_levels() {
        // Stress test SmallVec with 1000 nesting levels
        // SmallVec[16] should transition to heap gracefully
        let mut json = String::new();
        for i in 0..1000 {
            json.push('{');
            json.push_str(&format!("\"level_{}\":", i));
        }
        json.push_str("\"value\"");
        for _ in 0..1000 {
            json.push('}');
        }

        let fragments = JsonFragmentScanner::scan_fragments(json.as_bytes());

        // Should handle extreme nesting without panic or stack overflow
        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }

    #[test]
    fn test_extreme_nesting_mixed_brackets() {
        // 500 levels of alternating objects and arrays
        let mut json = String::new();
        for i in 0..500 {
            if i % 2 == 0 {
                json.push('{');
                json.push_str(&format!("\"key_{}\":", i));
            } else {
                json.push('[');
            }
        }
        json.push_str("42");
        for i in (0..500).rev() {
            if i % 2 == 0 {
                json.push('}');
            } else {
                json.push(']');
            }
        }

        let fragments = JsonFragmentScanner::scan_fragments(json.as_bytes());

        // Should handle mixed deep nesting
        assert_eq!(fragments.len(), 1);
        assert!(fragments[0].is_complete());
    }
}

#[cfg(test)]
mod proptest_tests {
    use super::*;
    use proptest::prelude::*;

    // Generate valid JSON fragments (objects or arrays only, not scalars)
    fn json_fragment() -> impl Strategy<Value = String> {
        let leaf = prop_oneof![
            // Strings with actual content
            "[a-z]{1,10}".prop_map(|s| format!("\"{}\"", s)),
            // Numbers
            (-1000i32..1000i32).prop_map(|n| n.to_string()),
            // Booleans and null
            prop_oneof![
                Just("true".to_string()),
                Just("false".to_string()),
                Just("null".to_string()),
            ],
        ];

        leaf.prop_recursive(
            4,  // max depth
            32, // max nodes
            10, // items per collection
            |inner| {
                prop_oneof![
                    // Arrays
                    prop::collection::vec(inner.clone(), 0..5)
                        .prop_map(|items| format!("[{}]", items.join(","))),
                    // Objects
                    prop::collection::vec(("[a-z]{1,5}", inner.clone()), 0..5).prop_map(|items| {
                        let pairs: Vec<String> = items
                            .into_iter()
                            .map(|(k, v)| format!("\"{}\":{}", k, v))
                            .collect();
                        format!("{{{}}}", pairs.join(","))
                    }),
                ]
            },
        )
        .prop_filter("Must be object or array", |s| {
            s.starts_with('{') || s.starts_with('[')
        })
    }

    #[test]
    fn proptest_multiple_fragments() {
        proptest!(|(jsons in prop::collection::vec(json_fragment(), 1..5))| {
            let combined = jsons.join(" ");

            let fragments = JsonFragmentScanner::scan_fragments(combined.as_bytes());

            // Should find as many fragments as we put in
            prop_assert_eq!(fragments.len(), jsons.len());

            // All should be complete
            for frag in fragments {
                prop_assert!(frag.is_complete());
            }
        });
    }

    #[test]
    fn proptest_random_bytes_no_panic() {
        proptest!(|(bytes in prop::collection::vec(any::<u8>(), 0..100))| {

            // Should not panic regardless of input
            let _ = JsonFragmentScanner::scan_fragments(&bytes);
        });
    }
}