diff options
| author | Laurenz <laurmaedje@gmail.com> | 2023-03-10 20:47:23 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2023-03-10 21:19:50 +0100 |
| commit | a9fdff244aef859449a76e5f762ee7c343a8ddcc (patch) | |
| tree | 172b543183296b4bc30b3008650f594688467914 /src/eval/array.rs | |
| parent | 62f35602a87574dcc607f1637aeae1be574981ff (diff) | |
Expose content representation more
Diffstat (limited to 'src/eval/array.rs')
| -rw-r--r-- | src/eval/array.rs | 49 |
1 files changed, 24 insertions, 25 deletions
diff --git a/src/eval/array.rs b/src/eval/array.rs index 53bae06f..8da9b3d2 100644 --- a/src/eval/array.rs +++ b/src/eval/array.rs @@ -1,11 +1,12 @@ use std::cmp::Ordering; -use std::fmt::{self, Debug, Formatter, Write}; +use std::fmt::{self, Debug, Formatter}; use std::ops::{Add, AddAssign}; use ecow::{eco_format, EcoString, EcoVec}; use super::{ops, Args, Func, Value, Vm}; use crate::diag::{bail, At, SourceResult, StrResult}; +use crate::util::pretty_array; /// Create a new [`Array`] from values. #[macro_export] @@ -147,7 +148,6 @@ impl Array { return Ok(Some(item.clone())); } } - Ok(None) } @@ -262,6 +262,14 @@ impl Array { self.0.iter().cloned().rev().collect() } + /// Split all values in the array. + pub fn split(&self, at: Value) -> Array { + self.as_slice() + .split(|value| *value == at) + .map(|subslice| Value::Array(subslice.iter().cloned().collect())) + .collect() + } + /// Join all values in the array, optionally with separator and last /// separator (between the final two items). pub fn join(&self, sep: Option<Value>, mut last: Option<Value>) -> StrResult<Value> { @@ -332,31 +340,10 @@ impl Array { } } -/// The out of bounds access error message. -#[cold] -fn out_of_bounds(index: i64, len: i64) -> EcoString { - eco_format!("array index out of bounds (index: {}, len: {})", index, len) -} - -/// The error message when the array is empty. -#[cold] -fn array_is_empty() -> EcoString { - "array is empty".into() -} - impl Debug for Array { fn fmt(&self, f: &mut Formatter) -> fmt::Result { - f.write_char('(')?; - for (i, value) in self.iter().enumerate() { - value.fmt(f)?; - if i + 1 < self.0.len() { - f.write_str(", ")?; - } - } - if self.len() == 1 { - f.write_char(',')?; - } - f.write_char(')') + let pieces: Vec<_> = self.iter().map(|value| eco_format!("{value:?}")).collect(); + f.write_str(&pretty_array(&pieces, self.len() == 1)) } } @@ -404,3 +391,15 @@ impl<'a> IntoIterator for &'a Array { self.iter() } } + +/// The error message when the array is empty. +#[cold] +fn array_is_empty() -> EcoString { + "array is empty".into() +} + +/// The out of bounds access error message. +#[cold] +fn out_of_bounds(index: i64, len: i64) -> EcoString { + eco_format!("array index out of bounds (index: {}, len: {})", index, len) +} |
