aboutsummaryrefslogtreecommitdiff
path: root/2.27.1/src/text_format/print.rs
diff options
context:
space:
mode:
Diffstat (limited to '2.27.1/src/text_format/print.rs')
-rw-r--r--2.27.1/src/text_format/print.rs38
1 files changed, 38 insertions, 0 deletions
diff --git a/2.27.1/src/text_format/print.rs b/2.27.1/src/text_format/print.rs
new file mode 100644
index 0000000..397e8cd
--- /dev/null
+++ b/2.27.1/src/text_format/print.rs
@@ -0,0 +1,38 @@
+#[doc(hidden)]
+pub fn quote_bytes_to(bytes: &[u8], buf: &mut String) {
+ for &c in bytes {
+ match c {
+ 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("\\\""),
+ b'\\' => buf.push_str(r"\\"),
+ b'\x20'..=b'\x7e' => buf.push(c as char),
+ _ => {
+ buf.push('\\');
+ buf.push((b'0' + (c >> 6)) as char);
+ buf.push((b'0' + ((c >> 3) & 7)) as char);
+ buf.push((b'0' + (c & 7)) as char);
+ }
+ }
+ }
+}
+
+pub(crate) fn quote_escape_bytes_to(bytes: &[u8], buf: &mut String) {
+ buf.push('"');
+ quote_bytes_to(bytes, buf);
+ buf.push('"');
+}
+
+#[doc(hidden)]
+pub fn quote_escape_bytes(bytes: &[u8]) -> String {
+ let mut r = String::new();
+ quote_escape_bytes_to(bytes, &mut r);
+ r
+}
+
+pub(crate) fn print_str_to(s: &str, buf: &mut String) {
+ // TODO: keep printable Unicode
+ quote_escape_bytes_to(s.as_bytes(), buf);
+}