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:
@@ -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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user