about summary refs log tree commit diff
path: root/tvix/nix-compat/src/derivation/escape.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tvix/nix-compat/src/derivation/escape.rs')
-rw-r--r--tvix/nix-compat/src/derivation/escape.rs18
1 files changed, 7 insertions, 11 deletions
diff --git a/tvix/nix-compat/src/derivation/escape.rs b/tvix/nix-compat/src/derivation/escape.rs
index 03106c44209e..8c2c6fce0753 100644
--- a/tvix/nix-compat/src/derivation/escape.rs
+++ b/tvix/nix-compat/src/derivation/escape.rs
@@ -1,7 +1,8 @@
 use bstr::{BString, ByteSlice};
 
-pub fn escape_bstr(s: &[u8]) -> BString {
-    let mut s: Vec<u8> = s.to_owned();
+/// Escapes a byte sequence. Does not add surrounding quotes.
+pub fn escape_bstr<P: AsRef<[u8]>>(s: P) -> BString {
+    let mut s: Vec<u8> = s.as_ref().to_vec();
 
     s = s.replace(b"\\", b"\\\\");
     s = s.replace(b"\n", b"\\n");
@@ -9,12 +10,7 @@ pub fn escape_bstr(s: &[u8]) -> BString {
     s = s.replace(b"\t", b"\\t");
     s = s.replace(b"\"", b"\\\"");
 
-    let mut out: Vec<u8> = Vec::new();
-    out.push(b'\"');
-    out.append(&mut s);
-    out.push(b'\"');
-
-    out.into()
+    s.into()
 }
 
 #[cfg(test)]
@@ -22,9 +18,9 @@ mod tests {
     use super::escape_bstr;
     use test_case::test_case;
 
-    #[test_case(b"", b"\"\""; "empty")]
-    #[test_case(b"\"", b"\"\\\"\""; "doublequote")]
-    #[test_case(b":", b"\":\""; "colon")]
+    #[test_case(b"", b""; "empty")]
+    #[test_case(b"\"", b"\\\""; "doublequote")]
+    #[test_case(b":", b":"; "colon")]
     fn escape(input: &[u8], expected: &[u8]) {
         assert_eq!(expected, escape_bstr(input))
     }