Coverage Report

Created: 2026-10-28 07:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/escape-simd-0.1.0/src/json.rs
Line
Count
Source
1
use crate::format_json_string;
2
3
/// Escapes `value` as a JSON string, including the surrounding `"` quotes.
4
0
pub fn escape(value: &str) -> String {
5
0
    let capacity = value.len() * 6 + 32 + 3;
6
0
    let mut buf = Vec::with_capacity(capacity);
7
    #[allow(clippy::uninit_vec)]
8
    unsafe {
9
0
        buf.set_len(capacity)
10
    };
11
0
    let cnt = format_json_string(value, &mut buf);
12
0
    unsafe { buf.set_len(cnt) };
13
0
    unsafe { String::from_utf8_unchecked(buf) }
14
0
}
15
16
/// Escapes `value` (including the surrounding `"`) and appends the result to
17
/// `dst`, growing `dst` as needed.
18
0
pub fn escape_into<S: AsRef<str>>(value: S, dst: &mut Vec<u8>) {
19
0
    let value = value.as_ref();
20
21
    // The SIMD kernels perform full-register speculative stores and copy 8 bytes
22
    // per escape, so they need up to `len * 6 + 32 + 3` scratch bytes past the
23
    // current end regardless of the final output length. Reserve that up front
24
    // so the unchecked writes below can never exceed the allocation. `reserve`
25
    // is effectively free when the caller already sized `dst` large enough.
26
0
    dst.reserve(value.len() * 6 + 32 + 3);
27
0
    let old_len = dst.len();
28
29
    // SAFETY: the `reserve` above guarantees `dst.capacity() - old_len` is at
30
    // least `value.len() * 6 + 32 + 3`, which upper-bounds every store
31
    // `format_json_string` performs. It writes valid UTF-8 and returns the
32
    // number of bytes written, which we then commit as the new length.
33
0
    unsafe {
34
0
        let spare =
35
0
            std::slice::from_raw_parts_mut(dst.as_mut_ptr().add(old_len), dst.capacity() - old_len);
36
0
        let cnt = format_json_string(value, spare);
37
0
        dst.set_len(old_len + cnt);
38
0
    }
39
0
}
Unexecuted instantiation: escape_simd::json::escape_into::<&str>
Unexecuted instantiation: escape_simd::json::escape_into::<_>