summaryrefslogtreecommitdiff
path: root/src/eval/ops.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2022-03-18 23:36:18 +0100
committerLaurenz <laurmaedje@gmail.com>2022-03-18 23:43:58 +0100
commitbeca01c826ee51c9ee6d5eadd7e5ef10f7fb9f58 (patch)
treee0ebb40b8775bba3b4be7bc47dceda3d349e2ac0 /src/eval/ops.rs
parent77d153d315a2a5909840ebcd47491e4cef14428b (diff)
Methods
Diffstat (limited to 'src/eval/ops.rs')
-rw-r--r--src/eval/ops.rs19
1 files changed, 4 insertions, 15 deletions
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<Value> {
(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<Value> {
})
}
-/// Repeat a string a number of times.
-fn repeat_str(string: EcoString, n: i64) -> StrResult<EcoString> {
- 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<Value> {
Ok(match (lhs, rhs) {
@@ -358,7 +347,7 @@ pub fn not_in(lhs: Value, rhs: Value) -> StrResult<Value> {
pub fn contains(lhs: &Value, rhs: &Value) -> Option<bool> {
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,
})