refactor(tvix/nix-compat/derivation/escape): move to Vec<u8>

We only need bstr::ByteSlice to be able to use replace, it doesn't need
to return a BString.

Change-Id: I811948436fb89652e880970c2c05356183f3e439
Reviewed-on: https://cl.tvl.fyi/c/depot/+/9084
Tested-by: BuildkiteCI
Autosubmit: flokli <flokli@flokli.de>
Reviewed-by: raitobezarius <tvl@lahfa.xyz>
This commit is contained in:
Florian Klink 2023-08-01 12:21:40 +02:00 committed by flokli
parent 72fc4fee53
commit 39efe50311
2 changed files with 7 additions and 7 deletions

View file

@ -1,7 +1,7 @@
use bstr::{BString, ByteSlice}; use bstr::ByteSlice;
/// Escapes a byte sequence. Does not add surrounding quotes. /// Escapes a byte sequence. Does not add surrounding quotes.
pub fn escape_bstr<P: AsRef<[u8]>>(s: P) -> BString { pub fn escape_bytes<P: AsRef<[u8]>>(s: P) -> Vec<u8> {
let mut s: Vec<u8> = s.as_ref().to_vec(); let mut s: Vec<u8> = s.as_ref().to_vec();
s = s.replace(b"\\", b"\\\\"); s = s.replace(b"\\", b"\\\\");
@ -10,18 +10,18 @@ pub fn escape_bstr<P: AsRef<[u8]>>(s: P) -> BString {
s = s.replace(b"\t", b"\\t"); s = s.replace(b"\t", b"\\t");
s = s.replace(b"\"", b"\\\""); s = s.replace(b"\"", b"\\\"");
s.into() s
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::escape_bstr; use super::escape_bytes;
use test_case::test_case; use test_case::test_case;
#[test_case(b"", b""; "empty")] #[test_case(b"", b""; "empty")]
#[test_case(b"\"", b"\\\""; "doublequote")] #[test_case(b"\"", b"\\\""; "doublequote")]
#[test_case(b":", b":"; "colon")] #[test_case(b":", b":"; "colon")]
fn escape(input: &[u8], expected: &[u8]) { fn escape(input: &[u8], expected: &[u8]) {
assert_eq!(expected, escape_bstr(input)) assert_eq!(expected, escape_bytes(input))
} }
} }

View file

@ -3,7 +3,7 @@
//! //!
//! [ATerm]: http://program-transformation.org/Tools/ATermFormat.html //! [ATerm]: http://program-transformation.org/Tools/ATermFormat.html
use crate::derivation::escape::escape_bstr; use crate::derivation::escape::escape_bytes;
use crate::derivation::output::Output; use crate::derivation::output::Output;
use bstr::BString; use bstr::BString;
use std::{ use std::{
@ -42,7 +42,7 @@ pub(crate) fn write_field<S: AsRef<[u8]>>(
if !escape { if !escape {
writer.write_all(s.as_ref())?; writer.write_all(s.as_ref())?;
} else { } else {
writer.write_all(&escape_bstr(s.as_ref()))?; writer.write_all(&escape_bytes(s.as_ref()))?;
} }
write_char(writer, QUOTE)?; write_char(writer, QUOTE)?;