From 3f5c5480ae7b2b56a34dcfd8b294061aeb7906e2 Mon Sep 17 00:00:00 2001 From: John Date: Sat, 27 Jul 2024 18:04:39 -0500 Subject: [PATCH] cl-interpret: [NOT FINAL] Add unicode-aware O(n) string indexing --- compiler/cl-interpret/src/lib.rs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/compiler/cl-interpret/src/lib.rs b/compiler/cl-interpret/src/lib.rs index aaa28a0..735eb42 100644 --- a/compiler/cl-interpret/src/lib.rs +++ b/compiler/cl-interpret/src/lib.rs @@ -90,12 +90,18 @@ pub mod convalue { let Self::Int(index) = index else { Err(Error::TypeError)? }; - let Self::Array(arr) = self else { - Err(Error::TypeError)? - }; - arr.get(*index as usize) - .cloned() - .ok_or(Error::OobIndex(*index as usize, arr.len())) + match self { + ConValue::String(string) => string + .chars() + .nth(*index as _) + .map(ConValue::Char) + .ok_or(Error::OobIndex(*index as usize, string.chars().count())), + ConValue::Array(arr) => arr + .get(*index as usize) + .cloned() + .ok_or(Error::OobIndex(*index as usize, arr.len())), + _ => Err(Error::TypeError), + } } cmp! { lt: false, <;