From beca01c826ee51c9ee6d5eadd7e5ef10f7fb9f58 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Fri, 18 Mar 2022 23:36:18 +0100 Subject: Methods --- src/eval/ops.rs | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) (limited to 'src/eval/ops.rs') diff --git a/src/eval/ops.rs b/src/eval/ops.rs index 6a8f5284..9b46e8f6 100644 --- a/src/eval/ops.rs +++ b/src/eval/ops.rs @@ -1,9 +1,8 @@ use std::cmp::Ordering; -use super::{Dynamic, Value}; +use super::{Dynamic, StrExt, Value}; use crate::diag::StrResult; use crate::geom::{Align, Spec, SpecAxis}; -use crate::util::EcoString; use Value::*; /// Bail with a type mismatch error. @@ -174,8 +173,8 @@ pub fn mul(lhs: Value, rhs: Value) -> StrResult { (Fractional(a), Float(b)) => Fractional(a * b), (Int(a), Fractional(b)) => Fractional(a as f64 * b), - (Str(a), Int(b)) => Str(repeat_str(a, b)?), - (Int(a), Str(b)) => Str(repeat_str(b, a)?), + (Str(a), Int(b)) => Str(StrExt::repeat(&a, b)?), + (Int(a), Str(b)) => Str(StrExt::repeat(&b, a)?), (Array(a), Int(b)) => Array(a.repeat(b)?), (Int(a), Array(b)) => Array(b.repeat(a)?), (Content(a), Int(b)) => Content(a.repeat(b)?), @@ -185,16 +184,6 @@ pub fn mul(lhs: Value, rhs: Value) -> StrResult { }) } -/// Repeat a string a number of times. -fn repeat_str(string: EcoString, n: i64) -> StrResult { - let n = usize::try_from(n) - .ok() - .and_then(|n| string.len().checked_mul(n).map(|_| n)) - .ok_or_else(|| format!("cannot repeat this string {} times", n))?; - - Ok(string.repeat(n)) -} - /// Compute the quotient of two values. pub fn div(lhs: Value, rhs: Value) -> StrResult { Ok(match (lhs, rhs) { @@ -358,7 +347,7 @@ pub fn not_in(lhs: Value, rhs: Value) -> StrResult { pub fn contains(lhs: &Value, rhs: &Value) -> Option { Some(match (lhs, rhs) { (Value::Str(a), Value::Str(b)) => b.contains(a.as_str()), - (Value::Str(a), Value::Dict(b)) => b.contains_key(a), + (Value::Str(a), Value::Dict(b)) => b.contains(a), (a, Value::Array(b)) => b.contains(a), _ => return Option::None, }) -- cgit v1.2.3