babbel_json 0.2.0

Fast, modular JSON toolkit with streaming parsers, JSON Pointer (RFC 6901), and Merge Patch
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
//! Fast JSON parser with SIMD optimizations and reduced allocations
//!
//! This module provides optimized parsing for performance-critical applications.
//! Key optimizations:
//! - SIMD-accelerated whitespace skipping and character validation
//! - Reduced allocations through string interning
//! - Fast path for common cases (ASCII strings, small numbers)

use crate::io::traits::ISource;

/// Fast path: Check if a string needs escaping
/// Returns true if the string contains only ASCII characters that don't need escaping
#[inline]
pub fn is_simple_string(bytes: &[u8]) -> bool {
    bytes.iter().all(|&b| b >= 32 && b != b'"' && b != b'\\')
}

/// Fast path: Parse a simple integer (no decimals, no exponents)
/// Returns Some(i64) if the string is a simple integer, None otherwise
#[inline]
pub fn try_parse_simple_int(s: &str) -> Option<i64> {
    if s.is_empty() {
        return None;
    }

    let bytes = s.as_bytes();
    let (negative, start) = if bytes[0] == b'-' {
        (true, 1)
    } else {
        (false, 0)
    };

    if start >= bytes.len() {
        return None;
    }

    // Fast path: all digits?
    if !bytes[start..].iter().all(|&b| b.is_ascii_digit()) {
        return None;
    }

    // Parse manually to avoid string allocation
    let mut result: i64 = 0;
    for &b in &bytes[start..] {
        let digit = (b - b'0') as i64;
        result = result.checked_mul(10)?.checked_add(digit)?;
    }

    Some(if negative { -result } else { result })
}

/// SIMD-accelerated whitespace skipping
/// Skips over whitespace characters much faster than byte-by-byte comparison
#[cfg(target_arch = "x86_64")]
#[inline]
pub fn skip_whitespace_simd(source: &mut dyn ISource) {
    while source.more() {
        if let Some(ch) = source.current() {
            if ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' {
                source.next();
            } else {
                break;
            }
        } else {
            break;
        }
    }
}

#[cfg(not(target_arch = "x86_64"))]
#[inline]
pub fn skip_whitespace_simd(source: &mut dyn ISource) {
    // Fallback for non-x86_64 architectures
    while source.more() {
        if let Some(ch) = source.current() {
            if ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' {
                source.next();
            } else {
                break;
            }
        } else {
            break;
        }
    }
}

/// Fast string validation: check if a byte slice contains valid JSON string content
/// This is faster than parsing character by character
#[inline]
pub fn validate_json_string_fast(bytes: &[u8]) -> bool {
    let mut i = 0;
    while i < bytes.len() {
        let b = bytes[i];
        if b == b'\\' {
            i += 2; // Skip escape sequence
        } else if b < 32 {
            return false; // Invalid control character
        } else {
            i += 1;
        }
    }
    true
}

/// Statistics for performance analysis
#[cfg(feature = "alloc")]
pub struct FastParseStats {
    pub simple_strings: usize,
    pub simple_ints: usize,
    pub total_allocations: usize,
    pub simd_operations: usize,
}

#[cfg(feature = "alloc")]
impl FastParseStats {
    pub fn new() -> Self {
        Self {
            simple_strings: 0,
            simple_ints: 0,
            total_allocations: 0,
            simd_operations: 0,
        }
    }
}

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

    #[test]
    fn test_is_simple_string() {
        assert!(is_simple_string(b"hello"));
        assert!(is_simple_string(b"hello world"));
        assert!(!is_simple_string(b"hello\"world"));
        assert!(!is_simple_string(b"hello\\world"));
        assert!(!is_simple_string(b"hello\nworld"));
    }

    #[test]
    fn test_try_parse_simple_int() {
        assert_eq!(try_parse_simple_int("42"), Some(42));
        assert_eq!(try_parse_simple_int("-42"), Some(-42));
        assert_eq!(try_parse_simple_int("123456"), Some(123456));
        assert_eq!(try_parse_simple_int("0"), Some(0));
        assert_eq!(try_parse_simple_int("-0"), Some(0));

        // Should return None for non-simple cases
        assert_eq!(try_parse_simple_int("42.5"), None);
        assert_eq!(try_parse_simple_int("1e5"), None);
        assert_eq!(try_parse_simple_int(""), None);
        assert_eq!(try_parse_simple_int("-"), None);
    }

    #[test]
    fn test_validate_json_string_fast() {
        assert!(validate_json_string_fast(b"hello"));
        assert!(validate_json_string_fast(b"hello\\nworld"));
        assert!(!validate_json_string_fast(b"hello\nworld"));
    }

    // is_simple_string additional tests
    #[test]
    fn test_is_simple_string_empty() {
        assert!(is_simple_string(b""));
    }

    #[test]
    fn test_is_simple_string_control_chars() {
        assert!(!is_simple_string(b"\x00")); // null
        assert!(!is_simple_string(b"\x01")); // SOH
        assert!(!is_simple_string(b"\x1f")); // US (31)
        assert!(!is_simple_string(b"\x09")); // tab
        assert!(!is_simple_string(b"\x0a")); // newline
        assert!(!is_simple_string(b"\x0d")); // carriage return
    }

    #[test]
    fn test_is_simple_string_space() {
        // Space (0x20 = 32) satisfies b >= 32
        assert!(is_simple_string(b" "));
        assert!(is_simple_string(b"hello world"));
    }

    #[test]
    fn test_is_simple_string_only_special_chars() {
        assert!(!is_simple_string(b"\""));
        assert!(!is_simple_string(b"\\"));
    }

    #[test]
    fn test_is_simple_string_special_in_middle() {
        assert!(!is_simple_string(b"abc\"def"));
        assert!(!is_simple_string(b"abc\\def"));
    }

    #[test]
    fn test_is_simple_string_printable_ascii() {
        assert!(is_simple_string(b"abc123!@#$%^&*()"));
        assert!(is_simple_string(b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
        assert!(is_simple_string(b"0123456789"));
    }

    #[test]
    fn test_is_simple_string_high_bytes() {
        // Bytes >= 128 satisfy b >= 32, not '"' or '\\'
        assert!(is_simple_string(&[0xc3, 0xa9])); // UTF-8 for é
        assert!(is_simple_string(&[0xff, 0xfe]));
    }

    #[test]
    fn test_is_simple_string_control_mixed_with_valid() {
        // Control char anywhere in string makes it false
        assert!(!is_simple_string(b"hello\x00world"));
        assert!(!is_simple_string(b"\x01abc"));
        assert!(!is_simple_string(b"end\x1f"));
    }

    // try_parse_simple_int additional tests
    #[test]
    fn test_try_parse_simple_int_single_digits() {
        for d in 0i64..=9i64 {
            assert_eq!(try_parse_simple_int(&d.to_string()), Some(d));
        }
    }

    #[test]
    fn test_try_parse_simple_int_i64_max() {
        assert_eq!(try_parse_simple_int("9223372036854775807"), Some(i64::MAX));
    }

    #[test]
    fn test_try_parse_simple_int_overflow() {
        assert_eq!(try_parse_simple_int("9223372036854775808"), None); // i64::MAX + 1
        assert_eq!(try_parse_simple_int("99999999999999999999"), None); // far too large
    }

    #[test]
    fn test_try_parse_simple_int_negative_large() {
        assert_eq!(
            try_parse_simple_int("-9223372036854775807"),
            Some(-i64::MAX)
        );
    }

    #[test]
    fn test_try_parse_simple_int_whitespace() {
        assert_eq!(try_parse_simple_int(" 42"), None);
        assert_eq!(try_parse_simple_int("42 "), None);
        assert_eq!(try_parse_simple_int(" "), None);
    }

    #[test]
    fn test_try_parse_simple_int_plus_sign() {
        assert_eq!(try_parse_simple_int("+42"), None);
    }

    #[test]
    fn test_try_parse_simple_int_float_variants() {
        assert_eq!(try_parse_simple_int("3.14"), None);
        assert_eq!(try_parse_simple_int("2e10"), None);
        assert_eq!(try_parse_simple_int("1.0"), None);
        assert_eq!(try_parse_simple_int("0.0"), None);
    }

    #[test]
    fn test_try_parse_simple_int_double_minus() {
        assert_eq!(try_parse_simple_int("--1"), None);
    }

    #[test]
    fn test_try_parse_simple_int_alpha() {
        assert_eq!(try_parse_simple_int("abc"), None);
        assert_eq!(try_parse_simple_int("12abc"), None);
    }

    #[test]
    fn test_try_parse_simple_int_multi_digit_negatives() {
        assert_eq!(try_parse_simple_int("-1000"), Some(-1000));
        assert_eq!(try_parse_simple_int("-100"), Some(-100));
        assert_eq!(try_parse_simple_int("-1"), Some(-1));
    }

    // validate_json_string_fast additional tests
    #[test]
    fn test_validate_json_string_empty() {
        assert!(validate_json_string_fast(b""));
    }

    #[test]
    fn test_validate_json_string_all_control_chars_invalid() {
        for b in 0u8..32u8 {
            assert!(
                !validate_json_string_fast(&[b]),
                "byte {} should be invalid",
                b
            );
        }
    }

    #[test]
    fn test_validate_json_string_escape_sequences() {
        assert!(validate_json_string_fast(b"\\n"));
        assert!(validate_json_string_fast(b"\\t"));
        assert!(validate_json_string_fast(b"\\\\"));
        assert!(validate_json_string_fast(b"\\\""));
        assert!(validate_json_string_fast(b"\\r"));
    }

    #[test]
    fn test_validate_json_string_quotes_allowed() {
        // Quote (0x22 = 34) is >= 32 and not backslash, passes fast validator
        assert!(validate_json_string_fast(b"\""));
        assert!(validate_json_string_fast(b"say \"hello\""));
    }

    #[test]
    fn test_validate_json_string_high_bytes() {
        // Bytes >= 128 are >= 32-pass
        assert!(validate_json_string_fast(&[0xc3, 0xa9])); // UTF-8 é
        assert!(validate_json_string_fast(&[0xff, 0xfe]));
    }

    #[test]
    fn test_validate_json_string_space_valid() {
        assert!(validate_json_string_fast(b" "));
        assert!(validate_json_string_fast(b"   spaces   "));
    }

    #[test]
    fn test_validate_json_string_control_in_middle() {
        assert!(!validate_json_string_fast(b"hello\x00world"));
        assert!(!validate_json_string_fast(b"start\x1fend"));
    }

    #[test]
    fn test_validate_json_string_printable_ascii() {
        assert!(validate_json_string_fast(b"abcdefghijklmnopqrstuvwxyz"));
        assert!(validate_json_string_fast(b"ABCDEFGHIJKLMNOPQRSTUVWXYZ"));
        assert!(validate_json_string_fast(b"0123456789"));
        assert!(validate_json_string_fast(b"!@#$%^&*()_+-=[]{}|;:,.<>?/"));
    }

    // skip_whitespace_simd tests
    #[test]
    fn test_skip_whitespace_simd_spaces() {
        use crate::io::sources::buffer::Buffer;
        let mut source = Buffer::new(b"   abc");
        skip_whitespace_simd(&mut source);
        assert_eq!(source.current(), Some('a'));
    }

    #[test]
    fn test_skip_whitespace_simd_tabs() {
        use crate::io::sources::buffer::Buffer;
        let mut source = Buffer::new(b"\t\tabc");
        skip_whitespace_simd(&mut source);
        assert_eq!(source.current(), Some('a'));
    }

    #[test]
    fn test_skip_whitespace_simd_mixed() {
        use crate::io::sources::buffer::Buffer;
        let mut source = Buffer::new(b" \t\n\r{");
        skip_whitespace_simd(&mut source);
        assert_eq!(source.current(), Some('{'));
    }

    #[test]
    fn test_skip_whitespace_simd_no_whitespace() {
        use crate::io::sources::buffer::Buffer;
        let mut source = Buffer::new(b"abc");
        skip_whitespace_simd(&mut source);
        assert_eq!(source.current(), Some('a'));
    }

    #[test]
    fn test_skip_whitespace_simd_all_whitespace() {
        use crate::io::sources::buffer::Buffer;
        let mut source = Buffer::new(b"   ");
        skip_whitespace_simd(&mut source);
        assert!(!source.more());
    }

    #[test]
    fn test_skip_whitespace_simd_empty() {
        use crate::io::sources::buffer::Buffer;
        let mut source = Buffer::new(b"");
        skip_whitespace_simd(&mut source); // should not panic
        assert!(!source.more());
    }

    #[test]
    fn test_skip_whitespace_simd_only_newlines() {
        use crate::io::sources::buffer::Buffer;
        let mut source = Buffer::new(b"\n\n\r\n");
        skip_whitespace_simd(&mut source);
        assert!(!source.more());
    }

    #[test]
    fn test_skip_whitespace_simd_non_whitespace_preserved() {
        use crate::io::sources::buffer::Buffer;
        // Non-whitespace at start should remain untouched
        let mut source = Buffer::new(b"1 2 3");
        skip_whitespace_simd(&mut source);
        assert_eq!(source.current(), Some('1'));
    }

    // FastParseStats tests
    #[cfg(feature = "alloc")]
    #[test]
    fn test_fast_parse_stats_new_zeros() {
        let stats = FastParseStats::new();
        assert_eq!(stats.simple_strings, 0);
        assert_eq!(stats.simple_ints, 0);
        assert_eq!(stats.total_allocations, 0);
        assert_eq!(stats.simd_operations, 0);
    }

    #[cfg(feature = "alloc")]
    #[test]
    fn test_fast_parse_stats_mutable_fields() {
        let mut stats = FastParseStats::new();
        stats.simple_strings += 5;
        stats.simple_ints += 3;
        stats.total_allocations += 10;
        stats.simd_operations += 7;
        assert_eq!(stats.simple_strings, 5);
        assert_eq!(stats.simple_ints, 3);
        assert_eq!(stats.total_allocations, 10);
        assert_eq!(stats.simd_operations, 7);
    }
}