simdjson_rust/
padded_string.rs

1use std::{io::Read, path::Path};
2
3use simdjson_sys as ffi;
4
5pub fn make_padded_string(s: &str) -> String {
6    let mut ps = String::with_capacity(s.len() + ffi::SIMDJSON_PADDING);
7    ps.push_str(s);
8    ps
9}
10
11pub fn load_padded_string<P: AsRef<Path>>(path: P) -> std::io::Result<String> {
12    let mut file = std::fs::File::open(path)?;
13    let len = file.metadata()?.len() as usize;
14    let mut buf = String::with_capacity(len + ffi::SIMDJSON_PADDING);
15    file.read_to_string(&mut buf)?;
16    Ok(buf)
17}
18
19pub trait ToPaddedString {
20    fn to_padded_string(&self) -> String;
21}
22
23pub trait IntoPaddedString {
24    fn into_padded_string(self) -> String;
25}
26
27impl ToPaddedString for &str {
28    fn to_padded_string(&self) -> String {
29        make_padded_string(self)
30    }
31}
32
33impl ToPaddedString for &mut str {
34    fn to_padded_string(&self) -> String {
35        make_padded_string(self)
36    }
37}
38
39impl IntoPaddedString for String {
40    fn into_padded_string(mut self) -> String {
41        if self.capacity() < self.len() + ffi::SIMDJSON_PADDING {
42            self.reserve(ffi::SIMDJSON_PADDING);
43        }
44        self
45    }
46}
47
48#[cfg(test)]
49mod tests {}