Use Cow<str> for the values of filecheck variables.

Often, an implementation of VariableMap can return references to
internal strings, and Cow::Borrow() allows that without making any
copies.

We still want to allow VariableMap implementations to return owned
strings in case they have to manufacture variable values on demand. The
Cow::Owned() variant does exactly that.

Switch the internal VariableMap implementations over to Cows. It turns
out they can simply store references to substrings of the input test,
completely avoiding string allocation for script-defined variables.
This commit is contained in:
Jakob Stoklund Olesen
2016-09-16 09:36:34 -07:00
parent fb16762866
commit d5ed27cce6
3 changed files with 19 additions and 21 deletions

View File

@@ -330,19 +330,14 @@ impl Pattern {
Part::DefLit { ref regex, .. } => out.push_str(regex),
Part::DefVar { def, ref var } => {
// Wrap regex in a named capture group.
write!(out,
"(?P<{}>{})",
self.defs[def],
match vmap.lookup(var) {
None => {
return Err(Error::UndefVariable(format!("undefined variable \
${}",
var)))
}
Some(Value::Text(s)) => quote(&s),
Some(Value::Regex(rx)) => rx,
})
.unwrap()
write!(out, "(?P<{}>", self.defs[def]).unwrap();
match vmap.lookup(var) {
None => {
return Err(Error::UndefVariable(format!("undefined variable ${}", var)))
}
Some(Value::Text(s)) => write!(out, "{})", quote(&s[..])).unwrap(),
Some(Value::Regex(rx)) => write!(out, "{})", rx).unwrap(),
}
}
}