Function git_config::value::normalize
source · Expand description
Removes quotes, if any, from the provided inputs, and transforms
the 3 escape sequences \n
, \t
and \b
into newline and tab
respectively, while \b
will remove the previous character.
It assumes the input contains a even number of unescaped quotes, and will unescape escaped quotes and everything else (even though the latter would have been rejected in the parsing stage).
The return values should be safe for value interpretation.
This has optimizations for fully-quoted values, where the returned value will be a borrowed reference if the only mutation necessary is to unquote the value.
This is the function used to normalize raw values from higher level abstractions. Generally speaking these high level abstractions will handle normalization for you, and you do not need to call this yourself. However, if you’re directly handling events from the parser, you may want to use this to help with value interpretation.
Generally speaking, you’ll want to use one of the variants of this function,
such as normalize_bstr
or normalize_bstring
.
Examples
Values don’t need modification are returned borrowed, without allocation.
assert!(matches!(normalize_bstr("hello world"), Cow::Borrowed(_)));
Internally quoted values are turned into owned variant with quotes removed.
assert_eq!(normalize_bstr("hello \"world\""), Cow::<BStr>::Owned(BString::from("hello world")));
Escaped quotes are unescaped.
assert_eq!(normalize_bstr(r#"hello "world\"""#), Cow::<BStr>::Owned(BString::from(r#"hello world""#)));
Examples found in repository?
More examples
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
pub fn pop(&mut self) -> Option<(Key<'_>, Cow<'event, BStr>)> {
let mut values = Vec::new();
// events are popped in reverse order
let body = &mut self.section.body.0;
while let Some(e) = body.pop() {
match e {
Event::SectionKey(k) => {
// pop leading whitespace
if let Some(Event::Whitespace(_)) = body.last() {
body.pop();
}
if values.len() == 1 {
let value = values.pop().expect("vec is non-empty but popped to empty value");
return Some((k, normalize(value)));
}
return Some((
k,
normalize_bstring({
let mut s = BString::default();
for value in values.into_iter().rev() {
s.push_str(value.as_ref());
}
s
}),
));
}
Event::Value(v) | Event::ValueNotDone(v) | Event::ValueDone(v) => values.push(v),
_ => (),
}
}
None
}