This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
362ebf9
commit 9496bd3
Showing
4 changed files
with
129 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/// Contains a partial copy of code from serde_json to serialize utf8. | ||
use serde_json::ser::CharEscape; | ||
|
||
fn write_escape(buf: &mut Vec<u8>, char_escape: serde_json::ser::CharEscape) { | ||
use serde_json::ser::CharEscape::*; | ||
|
||
let s = match char_escape { | ||
Quote => b"\\\"", | ||
ReverseSolidus => b"\\\\", | ||
Solidus => b"\\/", | ||
Backspace => b"\\b", | ||
FormFeed => b"\\f", | ||
LineFeed => b"\\n", | ||
CarriageReturn => b"\\r", | ||
Tab => b"\\t", | ||
AsciiControl(byte) => { | ||
static HEX_DIGITS: [u8; 16] = *b"0123456789abcdef"; | ||
let bytes = &[ | ||
b'\\', | ||
b'u', | ||
b'0', | ||
b'0', | ||
HEX_DIGITS[(byte >> 4) as usize], | ||
HEX_DIGITS[(byte & 0xF) as usize], | ||
]; | ||
return buf.extend_from_slice(bytes); | ||
} | ||
}; | ||
buf.extend_from_slice(s) | ||
} | ||
|
||
#[inline] | ||
fn from_escape_table(escape: u8, byte: u8) -> CharEscape { | ||
match escape { | ||
self::BB => CharEscape::Backspace, | ||
self::TT => CharEscape::Tab, | ||
self::NN => CharEscape::LineFeed, | ||
self::FF => CharEscape::FormFeed, | ||
self::RR => CharEscape::CarriageReturn, | ||
self::QU => CharEscape::Quote, | ||
self::BS => CharEscape::ReverseSolidus, | ||
self::UU => CharEscape::AsciiControl(byte), | ||
_ => unreachable!(), | ||
} | ||
} | ||
|
||
const BB: u8 = b'b'; // \x08 | ||
const TT: u8 = b't'; // \x09 | ||
const NN: u8 = b'n'; // \x0A | ||
const FF: u8 = b'f'; // \x0C | ||
const RR: u8 = b'r'; // \x0D | ||
const QU: u8 = b'"'; // \x22 | ||
const BS: u8 = b'\\'; // \x5C | ||
const UU: u8 = b'u'; // \x00...\x1F except the ones above | ||
const __: u8 = 0; | ||
|
||
// Lookup table of escape sequences. A value of b'x' at index i means that byte | ||
// i is escaped as "\x" in JSON. A value of 0 means that byte i is not escaped. | ||
static ESCAPE: [u8; 256] = [ | ||
// 1 2 3 4 5 6 7 8 9 A B C D E F | ||
UU, UU, UU, UU, UU, UU, UU, UU, BB, TT, NN, UU, FF, RR, UU, UU, // 0 | ||
UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, UU, // 1 | ||
__, __, QU, __, __, __, __, __, __, __, __, __, __, __, __, __, // 2 | ||
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 3 | ||
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 4 | ||
__, __, __, __, __, __, __, __, __, __, __, __, BS, __, __, __, // 5 | ||
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 6 | ||
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 7 | ||
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 8 | ||
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // 9 | ||
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // A | ||
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // B | ||
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // C | ||
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // D | ||
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // E | ||
__, __, __, __, __, __, __, __, __, __, __, __, __, __, __, __, // F | ||
]; | ||
|
||
#[inline] | ||
pub fn utf8_serialize(value: &str, buf: &mut Vec<u8>) { | ||
buf.reserve(value.len() + 2); | ||
buf.push(b'"'); | ||
let bytes = value.as_bytes(); | ||
|
||
let mut start = 0; | ||
|
||
for (i, &byte) in bytes.iter().enumerate() { | ||
let escape = ESCAPE[byte as usize]; | ||
if escape == 0 { | ||
continue; | ||
} | ||
|
||
if start < i { | ||
buf.extend_from_slice(&bytes[start..i]); | ||
} | ||
|
||
let char_escape = from_escape_table(escape, byte); | ||
write_escape(buf, char_escape); | ||
|
||
start = i + 1; | ||
} | ||
|
||
if start != bytes.len() { | ||
buf.extend_from_slice(&bytes[start..]); | ||
} | ||
buf.push(b'"'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters