avoid unnecessary allocations when converting String to Identifier

This commit is contained in:
Connor Skees 2020-07-08 10:38:25 -04:00
parent 558b49af6d
commit 3a5526ab26
3 changed files with 9 additions and 5 deletions

View File

@ -127,7 +127,7 @@ fn mixin_exists(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Value
args.max_args(2)?;
match parser.arg(&mut args, 0, "name")? {
Value::String(s, _) => Ok(Value::bool(
parser.scopes.last().mixin_exists(&s, parser.global_scope),
parser.scopes.last().mixin_exists(s, parser.global_scope),
)),
v => Err((
format!("$name: {} is not a string.", v.inspect(args.span())?),
@ -141,7 +141,7 @@ fn function_exists(mut args: CallArgs, parser: &mut Parser<'_>) -> SassResult<Va
args.max_args(2)?;
match parser.arg(&mut args, 0, "name")? {
Value::String(s, _) => Ok(Value::bool(
parser.scopes.last().fn_exists(&s, parser.global_scope),
parser.scopes.last().fn_exists(s, parser.global_scope),
)),
v => Err((
format!("$name: {} is not a string.", v.inspect(args.span())?),

View File

@ -115,7 +115,11 @@ pub(crate) struct Identifier(String);
impl From<String> for Identifier {
fn from(s: String) -> Identifier {
if s.contains('_') {
Identifier(s.replace('_', "-"))
} else {
Identifier(s)
}
}
}

View File

@ -176,7 +176,7 @@ impl<'a> Parser<'a> {
if name.is_empty() {
CallArg::Positional(args.len())
} else {
CallArg::Named(name.into())
CallArg::Named(mem::take(&mut name).into())
},
self.parse_value_from_vec(val),
);
@ -261,7 +261,7 @@ impl<'a> Parser<'a> {
if name.is_empty() {
CallArg::Positional(args.len())
} else {
CallArg::Named(name.as_str().into())
CallArg::Named(mem::take(&mut name).into())
},
self.parse_value_from_vec(mem::take(&mut val)),
);