difftreelog
perf adopt string escape code from serde-json
in: master
1 file changed
crates/jrsonnet-evaluator/src/stdlib/manifest.rsdiffbeforeafterboth259 buf259 buf260}260}261262// Json string encoding was borrowed from https://github.com/serde-rs/json263264const BB: u8 = b'b'; // \x08265const TT: u8 = b't'; // \x09266const NN: u8 = b'n'; // \x0A267const FF: u8 = b'f'; // \x0C268const RR: u8 = b'r'; // \x0D269const QU: u8 = b'"'; // \x22270const BS: u8 = b'\\'; // \x5C271const UU: u8 = b'u'; // \x00...\x1F except the ones above272const __: u8 = 0;273274// Lookup table of escape sequences. A value of b'x' at index i means that byte275// i is escaped as "\x" in JSON. A value of 0 means that byte i is not escaped.276static ESCAPE: [u8; 256] = [277 // 1 2 3 4 5 6 7 8 9 A B C D E F278 UU, UU, UU, UU, UU, UU, UU, UU, BB, TT, NN, UU, FF, RR, UU, UU, // 0279 UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, // 1280 __, __, QU, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2281 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 3282 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 4283 __, __, __, __, __, __, __, __, __, __, __, __, BS, __, __, __, // 5284 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 6285 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7286 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8287 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9288 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A289 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B290 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C291 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D292 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E293 __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F294];261295262fn escape_string_json_buf(s: &str, buf: &mut String) {296fn escape_string_json_buf(value: &str, buf: &mut String) {297 // Safety: we only write correct utf-8 in this function263 buf.push('"');298 let mut buf: &mut Vec<u8> = unsafe { core::mem::transmute(buf) };264 for c in s.chars() {299 let bytes = value.as_bytes();265 match c {300266 '"' => buf.push_str("\\\""),301 // Perfect for ascii strings, removes any reallocations302 buf.reserve(value.len() + 2);303267 '\\' => buf.push_str("\\\\"),304 buf.push(b'"');305306 let mut start = 0;307268 '\u{0008}' => buf.push_str("\\b"),308 for (i, &byte) in bytes.iter().enumerate() {269 '\u{000c}' => buf.push_str("\\f"),309 let escape = ESCAPE[byte as usize];310 if escape == __ {311 continue;312 }313314 if start < i {270 '\n' => buf.push_str("\\n"),315 buf.extend_from_slice(&bytes[start..i]);271 '\r' => buf.push_str("\\r"),316 }317 start = i + 1;318319 match escape {272 '\t' => buf.push_str("\\t"),320 self::BB | self::TT | self::NN | self::FF | self::RR | self::QU | self::BS => {321 buf.extend_from_slice(&[b'\\', escape])322 }323 self::UU => {324 static HEX_DIGITS: [u8; 16] = *b"0123456789abcdef";325 let bytes = &[326 b'\\',327 b'u',328 b'0',329 b'0',273 c if c < 32 as char || (c >= 127 as char && c <= 159 as char) => {330 HEX_DIGITS[(byte >> 4) as usize],331 HEX_DIGITS[(byte & 0xF) as usize],332 ];274 write!(buf, "\\u{:04x}", c as u32).unwrap();333 buf.extend_from_slice(bytes)275 }334 }276 c => buf.push(c),335 _ => unreachable!(),277 }336 }278 }337 }338339 if start == bytes.len() {340 buf.push(b'"');341 return;342 }343344 buf.extend_from_slice(&bytes[start..]);279 buf.push('"');345 buf.push(b'"');280}346}281347282pub struct YamlFormat<'s> {348pub struct YamlFormat<'s> {