git_quote/single.rs
1use bstr::{BStr, BString, ByteSlice, ByteVec};
2
3/// Transforms the given `value` to be suitable for use as an argument for Bourne shells by wrapping it into single quotes.
4///
5/// Every single-quote `'` is escaped with `\'`, every exclamation mark `!` is escaped with `\!`, and the entire string is enclosed
6/// in single quotes.
7pub fn single(mut value: &BStr) -> BString {
8 let mut quoted = BString::new(b"'".to_vec());
9
10 while let Some(pos) = value.find_byteset(b"'!") {
11 quoted.extend_from_slice(&value[..pos]);
12 quoted.push_str(b"'\\");
13 quoted.push(value[pos]);
14 quoted.push(b'\'');
15
16 value = &value[pos + 1..];
17 }
18
19 quoted.extend_from_slice(value);
20 quoted.push(b'\'');
21 quoted
22}