Skip to content

Commit

Permalink
removed unnecessary string allocations for escaped json value
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrocarlo committed Feb 4, 2025
1 parent d9b6b47 commit 1db3392
Showing 1 changed file with 17 additions and 23 deletions.
40 changes: 17 additions & 23 deletions core/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -545,34 +545,28 @@ pub fn json_quote(value: &OwnedValue) -> crate::Result<OwnedValue> {
return Ok(value.to_owned());
}

let escaped_value: String = t
.value
.chars()
.flat_map(|c| match c {
'"' => vec!['\\', c],
'\n' => vec!['\\', 'n'],
'\r' => vec!['\\', 'r'],
'\t' => vec!['\\', 't'],
'\\' => vec!['\\', '\\'],
'\u{0008}' => vec!['\\', 'b'],
'\u{000c}' => vec!['\\', 'f'],
c => vec![c],
})
.collect();

let quoted_value = format!("\"{}\"", escaped_value);

Ok(OwnedValue::Text(LimboText::new(Rc::new(quoted_value))))
let mut escaped_value = String::with_capacity(t.value.len());
escaped_value.push('"');
for c in t.value.chars() {
match c {
'"' | '\\' | '\n' | '\r' | '\t' | '\u{0008}' | '\u{000c}' => {
escaped_value.push('\\');
escaped_value.push(c);
}
c => escaped_value.push(c),
}
}
escaped_value.push('"');

Ok(OwnedValue::Text(LimboText::new(Rc::new(escaped_value))))
}
// Numbers are unquoted in json
OwnedValue::Integer(ref int) => Ok(OwnedValue::Integer(int.to_owned())),
OwnedValue::Float(ref float) => Ok(OwnedValue::Float(float.to_owned())),
OwnedValue::Blob(_) => crate::bail_constraint_error!("JSON cannot hold BLOB values"),
OwnedValue::Null => {
let null_value = "null".to_string();

Ok(OwnedValue::Text(LimboText::new(Rc::new(null_value))))
}
OwnedValue::Null => Ok(OwnedValue::Text(LimboText::new(Rc::new(
"null".to_string(),
)))),
_ => {
// TODO not too sure what message should be here
crate::bail_parse_error!("Syntax error");
Expand Down

0 comments on commit 1db3392

Please # to comment.