eat_calc_args doesn't allocate its own string

This commit is contained in:
ConnorSkees 2020-05-06 12:15:42 -04:00
parent edb9dde259
commit e5226e5cfe
2 changed files with 14 additions and 12 deletions

View File

@ -13,42 +13,44 @@ pub(crate) fn eat_calc_args<I: Iterator<Item = Token>>(
toks: &mut PeekMoreIterator<I>, toks: &mut PeekMoreIterator<I>,
scope: &Scope, scope: &Scope,
super_selector: &Selector, super_selector: &Selector,
) -> SassResult<String> { buf: &mut String,
let mut string = String::from("("); ) -> SassResult<()> {
buf.reserve(2);
buf.push('(');
let mut nesting = 0; let mut nesting = 0;
while let Some(tok) = toks.next() { while let Some(tok) = toks.next() {
match tok.kind { match tok.kind {
' ' | '\t' | '\n' => { ' ' | '\t' | '\n' => {
devour_whitespace(toks); devour_whitespace(toks);
string.push(' '); buf.push(' ');
} }
'#' => { '#' => {
if toks.peek().is_some() && toks.peek().unwrap().kind == '{' { if toks.peek().is_some() && toks.peek().unwrap().kind == '{' {
let span = toks.next().unwrap().pos(); let span = toks.next().unwrap().pos();
string.push_str( buf.push_str(
&parse_interpolation(toks, scope, super_selector)?.to_css_string(span)?, &parse_interpolation(toks, scope, super_selector)?.to_css_string(span)?,
); );
} else { } else {
string.push('#'); buf.push('#');
} }
} }
'(' => { '(' => {
nesting += 1; nesting += 1;
string.push('('); buf.push('(');
} }
')' => { ')' => {
if nesting == 0 { if nesting == 0 {
break; break;
} else { } else {
nesting -= 1; nesting -= 1;
string.push(')'); buf.push(')');
} }
} }
c => string.push(c), c => buf.push(c),
} }
} }
string.push(')'); buf.push(')');
Ok(string) Ok(())
} }
pub(crate) fn is_special_function(s: &str) -> bool { pub(crate) fn is_special_function(s: &str) -> bool {
@ -73,7 +75,7 @@ pub(crate) fn eat_progid<I: Iterator<Item = Token>>(
string.push(tok.kind); string.push(tok.kind);
} }
'(' => { '(' => {
string.push_str(&eat_calc_args(toks, scope, super_selector)?); eat_calc_args(toks, scope, super_selector, &mut string)?;
break; break;
} }
_ => return Err(("expected \"(\".", span).into()), _ => return Err(("expected \"(\".", span).into()),

View File

@ -555,7 +555,7 @@ impl Value {
None => { None => {
match lower.as_str() { match lower.as_str() {
"calc" | "element" | "expression" => { "calc" | "element" | "expression" => {
s.push_str(&eat_calc_args(toks, scope, super_selector)?) eat_calc_args(toks, scope, super_selector, &mut s)?;
} }
// "min" => {} // "min" => {}
// "max" => {} // "max" => {}