Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions examples/advanced.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@ fn main() {
println!("Encoding with pipe delimiter:\n");
let pipe_options = EncodeOptions::new().delimiter(Delimiter::Pipe);
let toon_pipe = encode(&data, Some(&pipe_options)).unwrap();
println!("{}", toon_pipe);
println!("{toon_pipe}");

// Encode with length marker
println!("\nEncoding with length marker:\n");
let marker_options = EncodeOptions::new().length_marker('#');
let toon_marker = encode(&data, Some(&marker_options)).unwrap();
println!("{}", toon_marker);
println!("{toon_marker}");

// Encode with custom indentation
println!("\nEncoding with 4-space indentation:\n");
let indent_options = EncodeOptions::new().indent(4);
let toon_indent = encode(&data, Some(&indent_options)).unwrap();
println!("{}", toon_indent);
println!("{toon_indent}");

// Decode with custom options
println!("\nDecoding with custom indentation:\n");
let decode_options = DecodeOptions::new().indent(4).strict(true);
let decoded = decode(&toon_indent, Some(&decode_options)).unwrap();
println!("Decoded: {}", decoded);
println!("Decoded: {decoded}");
}
4 changes: 2 additions & 2 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ fn main() {
// Encode to TOON
println!("Encoding to TOON format:\n");
let toon = encode(&data, None).unwrap();
println!("{}", toon);
println!("{toon}");

// Decode from TOON
println!("\nDecoding from TOON format:\n");
let decoded = decode(&toon, None).unwrap();
println!("Decoded: {}", decoded);
println!("Decoded: {decoded}");
}
4 changes: 2 additions & 2 deletions examples/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ fn main() {
// Serialize to TOON
println!("Serializing to TOON:\n");
let toon = to_string(&user).unwrap();
println!("{}", toon);
println!("{toon}");

// Deserialize from TOON
println!("\nDeserializing from TOON:\n");
let decoded: User = from_str(&toon).unwrap();
println!("Decoded: {:?}", decoded);
println!("Decoded: {decoded:?}");
assert_eq!(user, decoded);
}
36 changes: 13 additions & 23 deletions src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use crate::error::Error;
use crate::options::DecodeOptions;
use crate::simd;
use serde_json::{Map, Value};

/// Decode a TOON-formatted string to a JSON value
Expand Down Expand Up @@ -671,35 +672,24 @@ impl<'a> Parser<'a> {
fn detect_delimiter(&self) -> char {
// Look ahead to detect delimiter
let remaining = &self.input[self.pos..];
if remaining.contains('\t') {
'\t'
} else if remaining.contains('|') {
'|'

// Use SIMD for larger inputs, fallback for small ones
// Threshold: use SIMD if input is large enough to benefit (>= 32 bytes)
if remaining.len() >= 32 {
simd::detect_delimiter_simd(remaining)
} else {
','
simd::detect_delimiter_fallback(remaining)
}
}

fn split_row<'b>(&self, row: &'b str, delimiter: char) -> Vec<&'b str> {
let mut result = Vec::new();
let mut start = 0;
let mut in_quotes = false;
let chars: Vec<char> = row.chars().collect();

for (i, ch) in chars.iter().enumerate() {
match ch {
'"' if i == 0 || chars[i - 1] != '\\' => {
in_quotes = !in_quotes;
}
_ if *ch == delimiter && !in_quotes => {
result.push(&row[start..i]);
start = i + 1;
}
_ => {}
}
// Use SIMD for larger inputs, fallback for small ones
// Threshold: use SIMD if row is large enough to benefit (>= 32 bytes)
if row.len() >= 32 {
simd::split_row_simd(row, delimiter)
} else {
simd::split_row_fallback(row, delimiter)
}
result.push(&row[start..]);
result
}

fn count_indent(&mut self, indent_size: usize) -> usize {
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub mod decode;
pub mod encode;
pub mod error;
pub mod options;
mod simd;

pub use decode::decode;
pub use encode::encode;
Expand Down
Loading