Skip to content

Commit

Permalink
optimize formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
kaikalii committed Nov 25, 2024
1 parent 11a07fe commit 0023e2f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 18 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ This version is not yet released. If you are reading this on the website, then t
- This should not affect any language semantics
- Improve pattern matching error messages
- Optimize the "root" pattern `ⁿ%:1`
- Optimize format strings applied to strings or boxed strings
- Add an `-e`/`--experimental` flag to the `uiua eval` command to enable experimental features
### Website
- Add a new pad setting to show line values to the right of the code
Expand Down
35 changes: 31 additions & 4 deletions src/algorithm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ use std::{
option,
};

use ecow::EcoVec;
use ecow::{EcoString, EcoVec};
use tinyvec::TinyVec;

use crate::{
Array, ArrayCmp, ArrayValue, Boxed, CodeSpan, Complex, ExactDoubleIterator, Inputs, Ops,
PersistentMeta, Shape, SigNode, Signature, Span, Uiua, UiuaError, UiuaErrorKind, UiuaResult,
Value,
cowslice::ecovec_extend_cowslice, Array, ArrayCmp, ArrayValue, Boxed, CodeSpan, Complex,
ExactDoubleIterator, Inputs, Ops, PersistentMeta, Shape, SigNode, Signature, Span, Uiua,
UiuaError, UiuaErrorKind, UiuaResult, Value,
};

mod dyadic;
Expand Down Expand Up @@ -606,6 +606,33 @@ pub fn try_(ops: Ops, env: &mut Uiua) -> UiuaResult {
Ok(())
}

pub fn format(parts: &[EcoString], env: &mut Uiua) -> UiuaResult {
fn format_val(chars: &mut EcoVec<char>, val: Value) {
match val {
Value::Char(arr) if arr.rank() <= 1 => {
if chars.is_empty() {
*chars = arr.data.into();
} else {
ecovec_extend_cowslice(chars, arr.data);
}
}
Value::Box(arr) if arr.rank() == 0 => format_val(chars, arr.into_scalar().unwrap().0),
val => chars.extend(val.format().chars()),
}
}

let mut chars = EcoVec::new();
for (i, part) in parts.iter().enumerate() {
if i > 0 {
let value = env.pop(("format argument", i))?;
format_val(&mut chars, value);
}
chars.extend(part.chars());
}
env.push(chars);
Ok(())
}

#[repr(transparent)]
#[derive(Debug)]
struct ArrayCmpSlice<'a, T>(&'a [T]);
Expand Down
4 changes: 4 additions & 0 deletions src/cowslice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ pub(crate) fn extend_repeat_slice<T: Clone>(vec: &mut EcoVec<T>, slice: &[T], co
}
}

pub(crate) fn ecovec_extend_cowslice<T: Clone>(vec: &mut EcoVec<T>, cowslice: CowSlice<T>) {
unsafe { vec.extend_from_trusted(cowslice) }
}

/// Exact sized repeating iterator
pub(crate) struct Repeat<'a, T> {
elem: &'a T,
Expand Down
15 changes: 1 addition & 14 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,20 +565,7 @@ at {}",
} => self.with_span(span, |env| {
algorithm::switch(branches, sig, under_cond, env)
}),
Node::Format(parts, span) => {
let parts = parts.clone();
self.with_span(span, |env| {
let mut s = String::new();
for (i, part) in parts.into_iter().enumerate() {
if i > 0 {
s.push_str(&env.pop(("format argument", i))?.format());
}
s.push_str(&part);
}
env.push(s);
Ok(())
})
}
Node::Format(parts, span) => self.with_span(span, |env| algorithm::format(&parts, env)),
Node::MatchFormatPattern(parts, span) => {
self.with_span(span, |env| match_format_pattern(parts, env))
}
Expand Down

0 comments on commit 0023e2f

Please sign in to comment.