aboutsummaryrefslogtreecommitdiff
path: root/src/gen/rust/quote.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/gen/rust/quote.rs')
-rw-r--r--src/gen/rust/quote.rs58
1 files changed, 58 insertions, 0 deletions
diff --git a/src/gen/rust/quote.rs b/src/gen/rust/quote.rs
new file mode 100644
index 0000000..f01b4ee
--- /dev/null
+++ b/src/gen/rust/quote.rs
@@ -0,0 +1,58 @@
+fn hex_digit(value: u32) -> char {
+ if value < 10 {
+ (b'0' + value as u8) as char
+ } else if value < 0x10 {
+ (b'a' + value as u8 - 10) as char
+ } else {
+ unreachable!()
+ }
+}
+
+pub fn quote_escape_str(s: &str) -> String {
+ let mut buf = String::new();
+ buf.push('"');
+ buf.extend(s.chars().flat_map(|c| c.escape_default()));
+ buf.push('"');
+ buf
+}
+
+pub fn quote_escape_bytes(bytes: &[u8]) -> String {
+ let mut buf = String::new();
+ buf.push('b');
+ buf.push('"');
+ for &b in bytes {
+ match b {
+ b'\n' => buf.push_str(r"\n"),
+ b'\r' => buf.push_str(r"\r"),
+ b'\t' => buf.push_str(r"\t"),
+ b'"' => buf.push_str("\\\""),
+ b'\\' => buf.push_str(r"\\"),
+ b'\x20'..=b'\x7e' => buf.push(b as char),
+ _ => {
+ buf.push_str(r"\x");
+ buf.push(hex_digit((b as u32) >> 4));
+ buf.push(hex_digit((b as u32) & 0x0f));
+ }
+ }
+ }
+ buf.push('"');
+ buf
+}
+
+#[cfg(test)]
+mod test {
+
+ use super::*;
+
+ #[test]
+ fn test_quote_escape_bytes() {
+ assert_eq!("b\"\"", quote_escape_bytes(b""));
+ assert_eq!("b\"xyZW\"", quote_escape_bytes(b"xyZW"));
+ assert_eq!("b\"aa\\\"bb\"", quote_escape_bytes(b"aa\"bb"));
+ assert_eq!("b\"aa\\r\\n\\tbb\"", quote_escape_bytes(b"aa\r\n\tbb"));
+ assert_eq!(
+ "b\"\\x00\\x01\\x12\\xfe\\xff\"",
+ quote_escape_bytes(b"\x00\x01\x12\xfe\xff")
+ );
+ }
+}