difftreelog
refactor extended strings
in: master
16 files changed
crates/jrsonnet-evaluator/src/evaluate/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/mod.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/mod.rs
@@ -17,7 +17,7 @@
function::{CallLocation, FuncDesc, FuncVal},
tb, throw,
typed::Typed,
- val::{CachedUnbound, IndexableVal, Thunk, ThunkValue},
+ val::{CachedUnbound, IndexableVal, StrValue, Thunk, ThunkValue},
Context, GcHashMap, ObjValue, ObjValueBuilder, ObjectAssertion, Pending, Result, State,
Unbound, Val,
};
@@ -36,7 +36,7 @@
}
}
Some(match &*expr.0 {
- Expr::Str(s) => Val::Str(s.clone()),
+ Expr::Str(s) => Val::Str(StrValue::Flat(s.clone())),
Expr::Num(n) => Val::Num(*n),
Expr::Literal(LiteralType::False) => Val::Bool(false),
Expr::Literal(LiteralType::True) => Val::Bool(true),
@@ -135,7 +135,7 @@
let fctx = Pending::new();
let mut new_bindings = GcHashMap::with_capacity(var.capacity_hint());
let value = Thunk::evaluated(Val::Arr(ArrValue::lazy(Cc::new(vec![
- Thunk::evaluated(Val::Str(field.clone())),
+ Thunk::evaluated(Val::Str(StrValue::Flat(field.clone()))),
Thunk::new(tb!(ObjectFieldThunk {
field: field.clone(),
obj: obj.clone(),
@@ -436,7 +436,7 @@
Literal(LiteralType::False) => Val::Bool(false),
Literal(LiteralType::Null) => Val::Null,
Parened(e) => evaluate(ctx, e)?,
- Str(v) => Val::Str(v.clone()),
+ Str(v) => Val::Str(StrValue::Flat(v.clone())),
Num(v) => Val::new_checked_num(*v)?,
BinaryOp(v1, o, v2) => evaluate_binary_op_special(ctx, v1, *o, v2)?,
UnaryOp(o, v) => evaluate_unary_op(*o, &evaluate(ctx, v)?)?,
@@ -457,14 +457,14 @@
ctx.super_obj()
.clone()
.expect("no super found")
- .get_for(name, ctx.this().clone().expect("no this found"))?
+ .get_for(name.into_flat(), ctx.this().clone().expect("no this found"))?
.expect("value not found")
}
Index(value, index) => match (evaluate(ctx.clone(), value)?, evaluate(ctx, index)?) {
(Val::Obj(v), Val::Str(key)) => State::push(
CallLocation::new(loc),
|| format!("field <{key}> access"),
- || match v.get(key.clone()) {
+ || match v.get(key.clone().into_flat()) {
Ok(Some(v)) => Ok(v),
#[cfg(not(feature = "friendly-errors"))]
Ok(None) => throw!(NoSuchField(key.clone(), vec![])),
@@ -476,7 +476,10 @@
#[cfg(feature = "exp-preserve-order")]
false,
) {
- let conf = strsim::jaro_winkler(&field as &str, &key as &str);
+ let conf = strsim::jaro_winkler(
+ &field as &str,
+ &key.clone().into_flat() as &str,
+ );
if conf < 0.8 {
continue;
}
@@ -485,7 +488,7 @@
heap.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(Ordering::Equal));
throw!(NoSuchField(
- key.clone(),
+ key.clone().into_flat(),
heap.into_iter().map(|(_, v)| v).collect()
))
}
@@ -505,7 +508,7 @@
v.get(n as usize)?
.ok_or_else(|| ArrayBoundsError(n as usize, v.len()))?
}
- (Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n)),
+ (Val::Arr(_), Val::Str(n)) => throw!(AttemptedIndexAnArrayWithString(n.into_flat())),
(Val::Arr(_), n) => throw!(ValueIndexMustBeTypeGot(
ValType::Arr,
ValType::Num,
@@ -514,16 +517,18 @@
(Val::Str(s), Val::Num(n)) => Val::Str({
let v: IStr = s
+ .clone()
+ .into_flat()
.chars()
.skip(n as usize)
.take(1)
.collect::<String>()
.into();
if v.is_empty() {
- let size = s.chars().count();
+ let size = s.into_flat().chars().count();
throw!(StringBoundsError(n as usize, size))
}
- v
+ StrValue::Flat(v)
}),
(Val::Str(_), n) => throw!(ValueIndexMustBeTypeGot(
ValType::Str,
@@ -654,7 +659,7 @@
|| format!("import {:?}", path.clone()),
|| s.import_resolved(resolved_path),
)?,
- ImportStr(_) => Val::Str(s.import_resolved_str(resolved_path)?),
+ ImportStr(_) => Val::Str(StrValue::Flat(s.import_resolved_str(resolved_path)?)),
ImportBin(_) => Val::Arr(ArrValue::bytes(s.import_resolved_bin(resolved_path)?)),
_ => unreachable!(),
}
crates/jrsonnet-evaluator/src/evaluate/operator.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/evaluate/operator.rs
+++ b/crates/jrsonnet-evaluator/src/evaluate/operator.rs
@@ -3,8 +3,14 @@
use jrsonnet_parser::{BinaryOpType, LocExpr, UnaryOpType};
use crate::{
- arr::ArrValue, error::ErrorKind::*, evaluate, stdlib::std_format, throw, typed::Typed,
- val::equals, Context, Result, Val,
+ arr::ArrValue,
+ error::ErrorKind::*,
+ evaluate,
+ stdlib::std_format,
+ throw,
+ typed::Typed,
+ val::{equals, StrValue},
+ Context, Result, Val,
};
pub fn evaluate_unary_op(op: UnaryOpType, b: &Val) -> Result<Val> {
@@ -25,15 +31,21 @@
Ok(match (a, b) {
(Str(a), Str(b)) if a.is_empty() => Val::Str(b.clone()),
(Str(a), Str(b)) if b.is_empty() => Val::Str(a.clone()),
- (Str(v1), Str(v2)) => Str(((**v1).to_owned() + v2).into()),
+ (Str(v1), Str(v2)) => Str(StrValue::concat(v1.clone(), v2.clone())),
// Can't use generic json serialization way, because it depends on number to string concatenation (std.jsonnet:890)
- (Num(a), Str(b)) => Str(format!("{a}{b}").into()),
- (Str(a), Num(b)) => Str(format!("{a}{b}").into()),
+ (Num(a), Str(b)) => Str(StrValue::Flat(format!("{a}{b}").into())),
+ (Str(a), Num(b)) => Str(StrValue::Flat(format!("{a}{b}").into())),
- (Str(a), o) | (o, Str(a)) if a.is_empty() => Val::Str(o.clone().to_string()?),
- (Str(a), o) => Str(format!("{a}{}", o.clone().to_string()?).into()),
- (o, Str(a)) => Str(format!("{}{a}", o.clone().to_string()?).into()),
+ (Str(a), o) | (o, Str(a)) if a.is_empty() => {
+ Val::Str(StrValue::Flat(o.clone().to_string()?))
+ }
+ (Str(a), o) => Str(StrValue::Flat(
+ format!("{a}{}", o.clone().to_string()?).into(),
+ )),
+ (o, Str(a)) => Str(StrValue::Flat(
+ format!("{}{a}", o.clone().to_string()?).into(),
+ )),
(Obj(v1), Obj(v2)) => Obj(v2.extend_from(v1.clone())),
(Arr(a), Arr(b)) => Val::Arr(ArrValue::extended(a.clone(), b.clone())),
@@ -56,7 +68,9 @@
}
Ok(Num(a % b))
}
- (Str(str), vals) => String::into_untyped(std_format(str.clone(), vals.clone())?),
+ (Str(str), vals) => {
+ String::into_untyped(std_format(&str.clone().into_flat(), vals.clone())?)
+ }
(a, b) => throw!(BinaryOperatorDoesNotOperateOnValues(
BinaryOpType::Mod,
a.value_type(),
@@ -120,10 +134,10 @@
(a, Lte, b) => Bool(evaluate_compare_op(a, b, Lte)?.is_le()),
(a, Gte, b) => Bool(evaluate_compare_op(a, b, Gte)?.is_ge()),
- (Str(a), In, Obj(obj)) => Bool(obj.has_field_ex(a.clone(), true)),
+ (Str(a), In, Obj(obj)) => Bool(obj.has_field_ex(a.clone().into_flat(), true)),
(a, Mod, b) => evaluate_mod_op(a, b)?,
- (Str(v1), Mul, Num(v2)) => Str(v1.repeat(*v2 as usize).into()),
+ (Str(v1), Mul, Num(v2)) => Str(StrValue::Flat(v1.to_string().repeat(*v2 as usize).into())),
// Bool X Bool
(Bool(a), And, Bool(b)) => Bool(*a && *b),
crates/jrsonnet-evaluator/src/function/arglike.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/function/arglike.rs
+++ b/crates/jrsonnet-evaluator/src/function/arglike.rs
@@ -4,7 +4,13 @@
use jrsonnet_parser::{ArgsDesc, LocExpr};
use crate::{
- error::Result, evaluate, gc::GcHashMap, tb, typed::Typed, val::ThunkValue, Context, Thunk, Val,
+ error::Result,
+ evaluate,
+ gc::GcHashMap,
+ tb,
+ typed::Typed,
+ val::{StrValue, ThunkValue},
+ Context, Thunk, Val,
};
/// Marker for arguments, which can be evaluated with context set to None
@@ -59,7 +65,7 @@
impl ArgLike for TlaArg {
fn evaluate_arg(&self, ctx: Context, tailstrict: bool) -> Result<Thunk<Val>> {
match self {
- TlaArg::String(s) => Ok(Thunk::evaluated(Val::Str(s.clone()))),
+ TlaArg::String(s) => Ok(Thunk::evaluated(Val::Str(StrValue::Flat(s.clone())))),
TlaArg::Code(code) => Ok(if tailstrict {
Thunk::evaluated(evaluate(ctx, code)?)
} else {
crates/jrsonnet-evaluator/src/integrations/serde.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/integrations/serde.rs
+++ b/crates/jrsonnet-evaluator/src/integrations/serde.rs
@@ -7,7 +7,7 @@
Deserialize, Serialize,
};
-use crate::{arr::ArrValue, error::Result, ObjValueBuilder, State, Val};
+use crate::{arr::ArrValue, error::Result, val::StrValue, ObjValueBuilder, State, Val};
impl<'de> Deserialize<'de> for Val {
fn deserialize<D>(deserializer: D) -> Result<Val, D::Error>
@@ -49,7 +49,7 @@
where
E: serde::de::Error,
{
- Ok(Val::Str(v.into()))
+ Ok(Val::Str(StrValue::Flat(v.into())))
}
// visit_num! {
@@ -152,7 +152,7 @@
match self {
Val::Bool(v) => serializer.serialize_bool(*v),
Val::Null => serializer.serialize_none(),
- Val::Str(s) => serializer.serialize_str(s),
+ Val::Str(s) => serializer.serialize_str(&s.clone().into_flat()),
Val::Num(n) => serializer.serialize_f64(*n),
Val::Arr(arr) => {
let mut seq = serializer.serialize_seq(Some(arr.len()))?;
crates/jrsonnet-evaluator/src/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/manifest.rs
@@ -147,7 +147,7 @@
}
}
Val::Null => buf.push_str("null"),
- Val::Str(s) => escape_string_json_buf(s, buf),
+ Val::Str(s) => escape_string_json_buf(&s.clone().into_flat(), buf),
Val::Num(n) => write!(buf, "{n}").unwrap(),
Val::Arr(items) => {
buf.push('[');
@@ -256,7 +256,7 @@
let Val::Str(s) = val else {
throw!("output should be string for string manifest format, got {}", val.value_type())
};
- out.write_str(&s).unwrap();
+ write!(out, "{s}").unwrap();
Ok(())
}
}
crates/jrsonnet-evaluator/src/stdlib/format.rsdiffbeforeafterboth1//! faster std.format impl2#![allow(clippy::too_many_arguments)]34use jrsonnet_gcmodule::Trace;5use jrsonnet_interner::IStr;6use jrsonnet_types::ValType;7use thiserror::Error;89use crate::{error::ErrorKind::*, throw, typed::Typed, Error, ObjValue, Result, Val};1011#[derive(Debug, Clone, Error, Trace)]12pub enum FormatError {13 #[error("truncated format code")]14 TruncatedFormatCode,15 #[error("unrecognized conversion type: {0}")]16 UnrecognizedConversionType(char),1718 #[error("not enough values")]19 NotEnoughValues,2021 #[error("cannot use * width with object")]22 CannotUseStarWidthWithObject,23 #[error("mapping keys required")]24 MappingKeysRequired,25 #[error("no such format field: {0}")]26 NoSuchFormatField(IStr),27}2829impl From<FormatError> for Error {30 fn from(e: FormatError) -> Self {31 Self::new(Format(e))32 }33}3435use FormatError::*;3637type ParseResult<'t, T> = std::result::Result<(T, &'t str), FormatError>;3839pub fn try_parse_mapping_key(str: &str) -> ParseResult<'_, &str> {40 if str.is_empty() {41 return Err(TruncatedFormatCode);42 }43 let bytes = str.as_bytes();44 if bytes[0] == b'(' {45 let mut i = 1;46 while i < bytes.len() {47 if bytes[i] == b')' {48 return Ok((&str[1..i], &str[i + 1..]));49 }50 i += 1;51 }52 Err(TruncatedFormatCode)53 } else {54 Ok(("", str))55 }56}5758#[cfg(test)]59pub mod tests_key {60 use super::*;6162 #[test]63 fn parse_key() {64 assert_eq!(65 try_parse_mapping_key("(hello ) world").unwrap(),66 ("hello ", " world")67 );68 assert_eq!(try_parse_mapping_key("() world").unwrap(), ("", " world"));69 assert_eq!(try_parse_mapping_key(" world").unwrap(), ("", " world"));70 assert_eq!(71 try_parse_mapping_key(" () world").unwrap(),72 ("", " () world")73 );74 }7576 #[test]77 #[should_panic]78 fn parse_key_missing_start() {79 try_parse_mapping_key("").unwrap();80 }8182 #[test]83 #[should_panic]84 fn parse_key_missing_end() {85 try_parse_mapping_key("( ").unwrap();86 }87}8889#[allow(clippy::struct_excessive_bools)]90#[derive(Default, Debug)]91pub struct CFlags {92 pub alt: bool,93 pub zero: bool,94 pub left: bool,95 pub blank: bool,96 pub sign: bool,97}9899pub fn try_parse_cflags(str: &str) -> ParseResult<'_, CFlags> {100 if str.is_empty() {101 return Err(TruncatedFormatCode);102 }103 let bytes = str.as_bytes();104 let mut i = 0;105 let mut out = CFlags::default();106 loop {107 if bytes.len() == i {108 return Err(TruncatedFormatCode);109 }110 match bytes[i] {111 b'#' => out.alt = true,112 b'0' => out.zero = true,113 b'-' => out.left = true,114 b' ' => out.blank = true,115 b'+' => out.sign = true,116 _ => break,117 }118 i += 1;119 }120 Ok((out, &str[i..]))121}122123#[derive(Debug, PartialEq, Eq)]124pub enum Width {125 Star,126 Fixed(usize),127}128pub fn try_parse_field_width(str: &str) -> ParseResult<'_, Width> {129 if str.is_empty() {130 return Err(TruncatedFormatCode);131 }132 let bytes = str.as_bytes();133 if bytes[0] == b'*' {134 return Ok((Width::Star, &str[1..]));135 }136 let mut out: usize = 0;137 let mut digits = 0;138 while let Some(digit) = (bytes[digits] as char).to_digit(10) {139 out *= 10;140 out += digit as usize;141 digits += 1;142 if digits == bytes.len() {143 return Err(TruncatedFormatCode);144 }145 }146 Ok((Width::Fixed(out), &str[digits..]))147}148149pub fn try_parse_precision(str: &str) -> ParseResult<'_, Option<Width>> {150 if str.is_empty() {151 return Err(TruncatedFormatCode);152 }153 let bytes = str.as_bytes();154 if bytes[0] == b'.' {155 try_parse_field_width(&str[1..]).map(|(r, s)| (Some(r), s))156 } else {157 Ok((None, str))158 }159}160161// Only skips162pub fn try_parse_length_modifier(str: &str) -> ParseResult<'_, ()> {163 if str.is_empty() {164 return Err(TruncatedFormatCode);165 }166 let bytes = str.as_bytes();167 let mut idx = 0;168 while bytes[idx] == b'h' || bytes[idx] == b'l' || bytes[idx] == b'L' {169 idx += 1;170 if bytes.len() == idx {171 return Err(TruncatedFormatCode);172 }173 }174 Ok(((), &str[idx..]))175}176177#[derive(Debug, PartialEq, Eq)]178pub enum ConvTypeV {179 Decimal,180 Octal,181 Hexadecimal,182 Scientific,183 Float,184 Shorter,185 Char,186 String,187 Percent,188}189pub struct ConvType {190 v: ConvTypeV,191 caps: bool,192}193194pub fn parse_conversion_type(str: &str) -> ParseResult<'_, ConvType> {195 if str.is_empty() {196 return Err(TruncatedFormatCode);197 }198199 let code = str.as_bytes()[0];200 let v: (ConvTypeV, bool) = match code {201 b'd' | b'i' | b'u' => (ConvTypeV::Decimal, false),202 b'o' => (ConvTypeV::Octal, false),203 b'x' => (ConvTypeV::Hexadecimal, false),204 b'X' => (ConvTypeV::Hexadecimal, true),205 b'e' => (ConvTypeV::Scientific, false),206 b'E' => (ConvTypeV::Scientific, true),207 b'f' => (ConvTypeV::Float, false),208 b'F' => (ConvTypeV::Float, true),209 b'g' => (ConvTypeV::Shorter, false),210 b'G' => (ConvTypeV::Shorter, true),211 b'c' => (ConvTypeV::Char, false),212 b's' => (ConvTypeV::String, false),213 b'%' => (ConvTypeV::Percent, false),214 c => return Err(UnrecognizedConversionType(c as char)),215 };216217 Ok((ConvType { v: v.0, caps: v.1 }, &str[1..]))218}219220#[derive(Debug)]221pub struct Code<'s> {222 mkey: &'s str,223 cflags: CFlags,224 width: Width,225 precision: Option<Width>,226 convtype: ConvTypeV,227 caps: bool,228}229pub fn parse_code(str: &str) -> ParseResult<'_, Code<'_>> {230 if str.is_empty() {231 return Err(TruncatedFormatCode);232 }233 let (mkey, str) = try_parse_mapping_key(str)?;234 let (cflags, str) = try_parse_cflags(str)?;235 let (width, str) = try_parse_field_width(str)?;236 let (precision, str) = try_parse_precision(str)?;237 let (_, str) = try_parse_length_modifier(str)?;238 let (convtype, str) = parse_conversion_type(str)?;239240 Ok((241 Code {242 mkey,243 cflags,244 width,245 precision,246 convtype: convtype.v,247 caps: convtype.caps,248 },249 str,250 ))251}252253#[derive(Debug)]254pub enum Element<'s> {255 String(&'s str),256 Code(Code<'s>),257}258pub fn parse_codes(mut str: &str) -> Result<Vec<Element<'_>>> {259 let mut bytes = str.as_bytes();260 let mut out = vec![];261 let mut offset = 0;262263 loop {264 while offset != bytes.len() && bytes[offset] != b'%' {265 offset += 1;266 }267 if offset != 0 {268 out.push(Element::String(&str[0..offset]));269 }270 if offset == bytes.len() {271 return Ok(out);272 }273 str = &str[offset + 1..];274 let code;275 (code, str) = parse_code(str)?;276 bytes = str.as_bytes();277 offset = 0;278279 out.push(Element::Code(code));280 }281}282283const NUMBERS: &[u8] = b"0123456789abcdefghijklmnopqrstuvwxyz";284285#[inline]286pub fn render_integer(287 out: &mut String,288 iv: f64,289 padding: usize,290 precision: usize,291 blank: bool,292 sign: bool,293 radix: i64,294 prefix: &str,295 caps: bool,296) {297 let radix = radix as f64;298 let iv = iv.floor();299 // Digit char indexes in reverse order, i.e300 // for radix = 16 and n = 12f: [15, 2, 1]301 let digits = if iv == 0.0 {302 vec![0u8]303 } else {304 let mut v = iv.abs();305 let mut nums = Vec::with_capacity(1);306 while v != 0.0 {307 nums.push((v % radix) as u8);308 v = (v / radix).floor();309 }310 nums311 };312 let neg = iv < 0.0;313 #[allow(clippy::bool_to_int_with_if)]314 let zp = padding.saturating_sub(if neg || blank || sign { 1 } else { 0 });315 let zp2 = zp316 .max(precision)317 .saturating_sub(prefix.len() + digits.len());318319 if neg {320 out.push('-');321 } else if sign {322 out.push('+');323 } else if blank {324 out.push(' ');325 }326327 out.reserve(zp2);328 for _ in 0..zp2 {329 out.push('0');330 }331 out.push_str(prefix);332333 for digit in digits.into_iter().rev() {334 let ch = NUMBERS[digit as usize] as char;335 out.push(if caps { ch.to_ascii_uppercase() } else { ch });336 }337}338339pub fn render_decimal(340 out: &mut String,341 iv: f64,342 padding: usize,343 precision: usize,344 blank: bool,345 sign: bool,346) {347 render_integer(out, iv, padding, precision, blank, sign, 10, "", false);348}349pub fn render_octal(350 out: &mut String,351 iv: f64,352 padding: usize,353 precision: usize,354 alt: bool,355 blank: bool,356 sign: bool,357) {358 render_integer(359 out,360 iv,361 padding,362 precision,363 blank,364 sign,365 8,366 if alt && iv != 0.0 { "0" } else { "" },367 false,368 );369}370371#[allow(clippy::fn_params_excessive_bools)]372pub fn render_hexadecimal(373 out: &mut String,374 iv: f64,375 padding: usize,376 precision: usize,377 alt: bool,378 blank: bool,379 sign: bool,380 caps: bool,381) {382 render_integer(383 out,384 iv,385 padding,386 precision,387 blank,388 sign,389 16,390 match (alt, caps) {391 (true, true) => "0X",392 (true, false) => "0x",393 (false, _) => "",394 },395 caps,396 );397}398399#[allow(clippy::fn_params_excessive_bools)]400pub fn render_float(401 out: &mut String,402 n: f64,403 mut padding: usize,404 precision: usize,405 blank: bool,406 sign: bool,407 ensure_pt: bool,408 trailing: bool,409) {410 #[allow(clippy::bool_to_int_with_if)]411 let dot_size = if precision == 0 && !ensure_pt { 0 } else { 1 };412 padding = padding.saturating_sub(dot_size + precision);413 render_decimal(out, n.floor(), padding, 0, blank, sign);414 if precision == 0 {415 if ensure_pt {416 out.push('.');417 }418 return;419 }420 let frac = n421 .fract()422 .mul_add(10.0_f64.powf(precision as f64), 0.5)423 .floor();424 if trailing || frac > 0.0 {425 out.push('.');426 let mut frac_str = String::new();427 render_decimal(&mut frac_str, frac, precision, 0, false, false);428 let mut trim = frac_str.len();429 if !trailing {430 for b in frac_str.as_bytes().iter().rev() {431 if *b == b'0' {432 trim -= 1;433 }434 }435 }436 out.push_str(&frac_str[..trim]);437 } else if ensure_pt {438 out.push('.');439 }440}441442#[allow(clippy::fn_params_excessive_bools)]443pub fn render_float_sci(444 out: &mut String,445 n: f64,446 mut padding: usize,447 precision: usize,448 blank: bool,449 sign: bool,450 ensure_pt: bool,451 trailing: bool,452 caps: bool,453) {454 let exponent = n.log10().floor();455 let mantissa = if exponent as i16 == -324 {456 n * 10.0 / 10.0_f64.powf(exponent + 1.0)457 } else {458 n / 10.0_f64.powf(exponent)459 };460 let mut exponent_str = String::new();461 render_decimal(&mut exponent_str, exponent, 3, 0, false, true);462463 // +1 for e464 padding = padding.saturating_sub(exponent_str.len() + 1);465466 render_float(467 out, mantissa, padding, precision, blank, sign, ensure_pt, trailing,468 );469 out.push(if caps { 'E' } else { 'e' });470 out.push_str(&exponent_str);471}472473#[allow(clippy::too_many_lines)]474pub fn format_code(475 out: &mut String,476 value: &Val,477 code: &Code<'_>,478 width: usize,479 precision: Option<usize>,480) -> Result<()> {481 let clfags = &code.cflags;482 let (fpprec, iprec) = precision.map_or((6, 0), |v| (v, v));483 let padding = if clfags.zero && !clfags.left {484 width485 } else {486 0487 };488489 // TODO: If left padded, can optimize by writing directly to out490 let mut tmp_out = String::new();491492 match code.convtype {493 ConvTypeV::String => tmp_out.push_str(&value.clone().to_string()?),494 ConvTypeV::Decimal => {495 let value = f64::from_untyped(value.clone())?;496 render_decimal(497 &mut tmp_out,498 value,499 padding,500 iprec,501 clfags.blank,502 clfags.sign,503 );504 }505 ConvTypeV::Octal => {506 let value = f64::from_untyped(value.clone())?;507 render_octal(508 &mut tmp_out,509 value,510 padding,511 iprec,512 clfags.alt,513 clfags.blank,514 clfags.sign,515 );516 }517 ConvTypeV::Hexadecimal => {518 let value = f64::from_untyped(value.clone())?;519 render_hexadecimal(520 &mut tmp_out,521 value,522 padding,523 iprec,524 clfags.alt,525 clfags.blank,526 clfags.sign,527 code.caps,528 );529 }530 ConvTypeV::Scientific => {531 let value = f64::from_untyped(value.clone())?;532 render_float_sci(533 &mut tmp_out,534 value,535 padding,536 fpprec,537 clfags.blank,538 clfags.sign,539 clfags.alt,540 true,541 code.caps,542 );543 }544 ConvTypeV::Float => {545 let value = f64::from_untyped(value.clone())?;546 render_float(547 &mut tmp_out,548 value,549 padding,550 fpprec,551 clfags.blank,552 clfags.sign,553 clfags.alt,554 true,555 );556 }557 ConvTypeV::Shorter => {558 let value = f64::from_untyped(value.clone())?;559 let exponent = value.log10().floor();560 if exponent < -4.0 || exponent >= fpprec as f64 {561 render_float_sci(562 &mut tmp_out,563 value,564 padding,565 fpprec - 1,566 clfags.blank,567 clfags.sign,568 clfags.alt,569 clfags.alt,570 code.caps,571 );572 } else {573 let digits_before_pt = 1.max(exponent as usize + 1);574 render_float(575 &mut tmp_out,576 value,577 padding,578 fpprec - digits_before_pt,579 clfags.blank,580 clfags.sign,581 clfags.alt,582 clfags.alt,583 );584 }585 }586 ConvTypeV::Char => match value.clone() {587 Val::Num(n) => tmp_out.push(588 std::char::from_u32(n as u32)589 .ok_or_else(|| InvalidUnicodeCodepointGot(n as u32))?,590 ),591 Val::Str(s) => {592 if s.chars().count() != 1 {593 throw!("%c expected 1 char string, got {}", s.chars().count(),);594 }595 tmp_out.push_str(&s);596 }597 _ => {598 throw!(TypeMismatch(599 "%c requires number/string",600 vec![ValType::Num, ValType::Str],601 value.value_type(),602 ));603 }604 },605 ConvTypeV::Percent => tmp_out.push('%'),606 };607608 let padding = width.saturating_sub(tmp_out.len());609610 if !clfags.left {611 for _ in 0..padding {612 out.push(' ');613 }614 }615 out.push_str(&tmp_out);616 if clfags.left {617 for _ in 0..padding {618 out.push(' ');619 }620 }621622 Ok(())623}624625pub fn format_arr(str: &str, mut values: &[Val]) -> Result<String> {626 let codes = parse_codes(str)?;627 let mut out = String::new();628 let value_count = values.len();629630 for code in codes {631 match code {632 Element::String(s) => {633 out.push_str(s);634 }635 Element::Code(c) => {636 let width = match c.width {637 Width::Star => {638 if values.is_empty() {639 throw!(NotEnoughValues);640 }641 let value = &values[0];642 values = &values[1..];643 usize::from_untyped(value.clone())?644 }645 Width::Fixed(n) => n,646 };647 let precision = match c.precision {648 Some(Width::Star) => {649 if values.is_empty() {650 throw!(NotEnoughValues);651 }652 let value = &values[0];653 values = &values[1..];654 Some(usize::from_untyped(value.clone())?)655 }656 Some(Width::Fixed(n)) => Some(n),657 None => None,658 };659660 // %% should not consume a value661 let value = if c.convtype == ConvTypeV::Percent {662 &Val::Null663 } else {664 if values.is_empty() {665 throw!(NotEnoughValues);666 }667 let value = &values[0];668 values = &values[1..];669 value670 };671672 format_code(&mut out, value, &c, width, precision)?;673 }674 }675 }676677 if !values.is_empty() {678 throw!(679 "too many values to format, expected {value_count}, got {}",680 value_count + values.len()681 )682 }683684 Ok(out)685}686687pub fn format_obj(str: &str, values: &ObjValue) -> Result<String> {688 let codes = parse_codes(str)?;689 let mut out = String::new();690691 for code in codes {692 match code {693 Element::String(s) => {694 out.push_str(s);695 }696 Element::Code(c) => {697 // TODO: Operate on ref698 let f: IStr = c.mkey.into();699 let width = match c.width {700 Width::Star => {701 throw!(CannotUseStarWidthWithObject);702 }703 Width::Fixed(n) => n,704 };705 let precision = match c.precision {706 Some(Width::Star) => {707 throw!(CannotUseStarWidthWithObject);708 }709 Some(Width::Fixed(n)) => Some(n),710 None => None,711 };712713 let value = if c.convtype == ConvTypeV::Percent {714 Val::Null715 } else {716 if f.is_empty() {717 throw!(MappingKeysRequired);718 }719 if let Some(v) = values.get(f.clone())? {720 v721 } else {722 throw!(NoSuchFormatField(f));723 }724 };725726 format_code(&mut out, &value, &c, width, precision)?;727 }728 }729 }730731 Ok(out)732}733734#[cfg(test)]735pub mod test_format {736 use super::*;737738 #[test]739 fn parse() {740 assert_eq!(741 parse_codes(742 "How much error budget is left looking at our %.3f%% availability gurantees?"743 )744 .unwrap()745 .len(),746 4747 );748 }749750 #[test]751 fn octals() {752 assert_eq!(format_arr("%#o", &[Val::Num(8.0)]).unwrap(), "010");753 assert_eq!(format_arr("%#4o", &[Val::Num(8.0)]).unwrap(), " 010");754 assert_eq!(format_arr("%4o", &[Val::Num(8.0)]).unwrap(), " 10");755 assert_eq!(format_arr("%04o", &[Val::Num(8.0)]).unwrap(), "0010");756 assert_eq!(format_arr("%+4o", &[Val::Num(8.0)]).unwrap(), " +10");757 assert_eq!(format_arr("%+04o", &[Val::Num(8.0)]).unwrap(), "+010");758 assert_eq!(format_arr("%-4o", &[Val::Num(8.0)]).unwrap(), "10 ");759 assert_eq!(format_arr("%+-4o", &[Val::Num(8.0)]).unwrap(), "+10 ");760 assert_eq!(format_arr("%+-04o", &[Val::Num(8.0)]).unwrap(), "+10 ");761 }762763 #[test]764 fn percent_doesnt_consumes_values() {765 assert_eq!(766 format_arr(767 "How much error budget is left looking at our %.3f%% availability gurantees?",768 &[Val::Num(4.0)]769 )770 .unwrap(),771 "How much error budget is left looking at our 4.000% availability gurantees?"772 );773 }774}crates/jrsonnet-evaluator/src/stdlib/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/stdlib/mod.rs
+++ b/crates/jrsonnet-evaluator/src/stdlib/mod.rs
@@ -2,13 +2,12 @@
#![allow(clippy::unnecessary_wraps)]
use format::{format_arr, format_obj};
-use jrsonnet_interner::IStr;
use crate::{error::Result, function::CallLocation, State, Val};
pub mod format;
-pub fn std_format(str: IStr, vals: Val) -> Result<String> {
+pub fn std_format(str: &str, vals: Val) -> Result<String> {
State::push(
CallLocation::native(),
|| format!("std.format of {str}"),
crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -11,7 +11,7 @@
function::{native::NativeDesc, FuncDesc, FuncVal},
throw,
typed::CheckType,
- val::IndexableVal,
+ val::{IndexableVal, StrValue},
ObjValue, ObjValueBuilder, Val,
};
@@ -187,13 +187,13 @@
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);
fn into_untyped(value: Self) -> Result<Val> {
- Ok(Val::Str(value))
+ Ok(Val::Str(StrValue::Flat(value)))
}
fn from_untyped(value: Val) -> Result<Self> {
<Self as Typed>::TYPE.check(&value)?;
match value {
- Val::Str(s) => Ok(s),
+ Val::Str(s) => Ok(s.into_flat()),
_ => unreachable!(),
}
}
@@ -203,7 +203,7 @@
const TYPE: &'static ComplexValType = &ComplexValType::Simple(ValType::Str);
fn into_untyped(value: Self) -> Result<Val> {
- Ok(Val::Str(value.into()))
+ Ok(Val::Str(StrValue::Flat(value.into())))
}
fn from_untyped(value: Val) -> Result<Self> {
@@ -219,13 +219,13 @@
const TYPE: &'static ComplexValType = &ComplexValType::Char;
fn into_untyped(value: Self) -> Result<Val> {
- Ok(Val::Str(value.to_string().into()))
+ Ok(Val::Str(StrValue::Flat(value.to_string().into())))
}
fn from_untyped(value: Val) -> Result<Self> {
<Self as Typed>::TYPE.check(&value)?;
match value {
- Val::Str(s) => Ok(s.chars().next().unwrap()),
+ Val::Str(s) => Ok(s.into_flat().chars().next().unwrap()),
_ => unreachable!(),
}
}
@@ -480,7 +480,7 @@
fn into_untyped(value: Self) -> Result<Val> {
match value {
- IndexableVal::Str(s) => Ok(Val::Str(s)),
+ IndexableVal::Str(s) => Ok(Val::Str(StrValue::Flat(s))),
IndexableVal::Arr(a) => Ok(Val::Arr(a)),
}
}
crates/jrsonnet-evaluator/src/typed/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/mod.rs
+++ b/crates/jrsonnet-evaluator/src/typed/mod.rs
@@ -150,7 +150,7 @@
Self::Any => Ok(()),
Self::Simple(t) => t.check(value),
Self::Char => match value {
- Val::Str(s) if s.len() == 1 || s.chars().count() == 1 => Ok(()),
+ Val::Str(s) if s.len() == 1 || s.clone().into_flat().chars().count() == 1 => Ok(()),
v => Err(TypeError::ExpectedGot(self.clone(), v.value_type()).into()),
},
Self::BoundedNumber(from, to) => {
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -1,4 +1,9 @@
-use std::{cell::RefCell, fmt::Debug, mem::replace};
+use std::{
+ cell::RefCell,
+ fmt::{self, Debug, Display},
+ mem::replace,
+ rc::Rc,
+};
use jrsonnet_gcmodule::{Cc, Trace};
use jrsonnet_interner::IStr;
@@ -117,7 +122,7 @@
}
impl<T: Debug + Trace> Debug for Thunk<T> {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Lazy")
}
}
@@ -187,6 +192,87 @@
}
}
+#[derive(Debug, Clone, Trace)]
+pub enum StrValue {
+ Flat(IStr),
+ Tree(Rc<(StrValue, StrValue, usize)>),
+}
+impl StrValue {
+ pub fn concat(a: StrValue, b: StrValue) -> Self {
+ if a.is_empty() {
+ b
+ } else if b.is_empty() {
+ a
+ } else {
+ let len = a.len() + b.len();
+ Self::Tree(Rc::new((a, b, len)))
+ }
+ }
+ pub fn into_flat(self) -> IStr {
+ match self {
+ StrValue::Flat(f) => f,
+ StrValue::Tree(_) => {
+ let mut buf = String::new();
+ self.into_flat_buf(&mut buf);
+ buf.into()
+ }
+ }
+ }
+ fn into_flat_buf(&self, out: &mut String) {
+ match self {
+ StrValue::Flat(f) => out.push_str(f),
+ StrValue::Tree(t) => {
+ t.0.into_flat_buf(out);
+ t.1.into_flat_buf(out);
+ }
+ }
+ }
+ pub fn len(&self) -> usize {
+ match self {
+ StrValue::Flat(v) => v.len(),
+ StrValue::Tree(t) => t.2,
+ }
+ }
+ pub fn is_empty(&self) -> bool {
+ match self {
+ Self::Flat(v) => v.is_empty(),
+ _ => false,
+ }
+ }
+}
+impl Display for StrValue {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ StrValue::Flat(v) => write!(f, "{v}"),
+ StrValue::Tree(t) => {
+ write!(f, "{}", t.0)?;
+ write!(f, "{}", t.1)
+ }
+ }
+ }
+}
+impl PartialEq for StrValue {
+ fn eq(&self, other: &Self) -> bool {
+ let a = self.clone().into_flat();
+ let b = other.clone().into_flat();
+ a == b
+ }
+}
+impl Eq for StrValue {}
+impl PartialOrd for StrValue {
+ fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
+ let a = self.clone().into_flat();
+ let b = other.clone().into_flat();
+ Some(a.cmp(&b))
+ }
+}
+impl Ord for StrValue {
+ fn cmp(&self, other: &Self) -> std::cmp::Ordering {
+ self.partial_cmp(other)
+ .expect("partial_cmp always returns Some")
+ }
+}
+
/// Represents any valid Jsonnet value.
#[derive(Debug, Clone, Trace)]
pub enum Val {
@@ -195,7 +281,7 @@
/// Represents a Jsonnet null value.
Null,
/// Represents a Jsonnet string.
- Str(IStr),
+ Str(StrValue),
/// Represents a Jsonnet number.
/// Should be finite, and not NaN
/// This restriction isn't enforced by enum, as enum field can't be marked as private
@@ -208,10 +294,12 @@
Func(FuncVal),
}
+static_assertions::assert_eq_size!(Val, [u8; 24]);
+
impl From<IndexableVal> for Val {
fn from(v: IndexableVal) -> Self {
match v {
- IndexableVal::Str(s) => Self::Str(s),
+ IndexableVal::Str(s) => Self::Str(StrValue::Flat(s)),
IndexableVal::Arr(a) => Self::Arr(a),
}
}
@@ -232,7 +320,7 @@
}
pub fn as_str(&self) -> Option<IStr> {
match self {
- Self::Str(s) => Some(s.clone()),
+ Self::Str(s) => Some(s.clone().into_flat()),
_ => None,
}
}
@@ -295,14 +383,14 @@
Self::Bool(true) => "true".into(),
Self::Bool(false) => "false".into(),
Self::Null => "null".into(),
- Self::Str(s) => s.clone(),
+ Self::Str(s) => s.clone().into_flat(),
_ => self.manifest(ToStringFormat).map(IStr::from)?,
})
}
pub fn into_indexable(self) -> Result<IndexableVal> {
Ok(match self {
- Val::Str(s) => IndexableVal::Str(s),
+ Val::Str(s) => IndexableVal::Str(s.into_flat()),
Val::Arr(arr) => IndexableVal::Arr(arr),
_ => throw!(ValueIsNotIndexable(self.value_type())),
})
crates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -37,12 +37,13 @@
func: NativeFn<((Either![String, Any],), Any)>,
arr: IndexableVal,
) -> Result<IndexableVal> {
+ use std::fmt::Write;
match arr {
IndexableVal::Str(str) => {
let mut out = String::new();
for c in str.chars() {
match func(Either2::A(c.to_string()))?.0 {
- Val::Str(o) => out.push_str(&o),
+ Val::Str(o) => write!(out, "{o}").unwrap(),
Val::Null => continue,
_ => throw!("in std.join all items should be strings"),
};
@@ -101,6 +102,7 @@
#[builtin]
pub fn builtin_join(sep: IndexableVal, arr: ArrValue) -> Result<IndexableVal> {
+ use std::fmt::Write;
Ok(match sep {
IndexableVal::Arr(joiner_items) => {
let mut out = Vec::new();
@@ -141,7 +143,7 @@
out += &sep;
}
first = false;
- out += &item;
+ write!(out, "{item}").unwrap()
} else if matches!(item, Val::Null) {
continue;
} else {
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -320,15 +320,19 @@
}
#[cfg(feature = "legacy-this-file")]
fn initialize(&self, s: State, source: Source) -> Context {
+ use jrsonnet_evaluator::val::StrValue;
+
let mut builder = ObjValueBuilder::new();
builder.with_super(self.stdlib_obj.clone());
builder
.member("thisFile".into())
.hide()
- .value(Val::Str(match source.source_path().path() {
- Some(p) => self.settings().path_resolver.resolve(p).into(),
- None => source.source_path().to_string().into(),
- }))
+ .value(Val::Str(StrValue::Flat(
+ match source.source_path().path() {
+ Some(p) => self.settings().path_resolver.resolve(p).into(),
+ None => source.source_path().to_string().into(),
+ },
+ )))
.expect("this object builder is empty");
let stdlib_with_this_file = builder.build();
crates/jrsonnet-stdlib/src/manifest/yaml.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/manifest/yaml.rs
+++ b/crates/jrsonnet-stdlib/src/manifest/yaml.rs
@@ -118,6 +118,7 @@
}
Val::Null => buf.push_str("null"),
Val::Str(s) => {
+ let s = s.clone().into_flat();
if s.is_empty() {
buf.push_str("\"\"");
} else if let Some(s) = s.strip_suffix('\n') {
@@ -128,10 +129,10 @@
buf.push_str(&options.padding);
buf.push_str(line);
}
- } else if !options.quote_keys && !yaml_needs_quotes(s) {
- buf.push_str(s);
+ } else if !options.quote_keys && !yaml_needs_quotes(&s) {
+ buf.push_str(&s);
} else {
- escape_string_json_buf(s, buf);
+ escape_string_json_buf(&s, buf);
}
}
Val::Num(n) => write!(buf, "{}", *n).unwrap(),
crates/jrsonnet-stdlib/src/objects.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/objects.rs
+++ b/crates/jrsonnet-stdlib/src/objects.rs
@@ -1,5 +1,9 @@
use jrsonnet_evaluator::{
- error::Result, function::builtin, typed::VecVal, val::Val, IStr, ObjValue,
+ error::Result,
+ function::builtin,
+ typed::VecVal,
+ val::{StrValue, Val},
+ IStr, ObjValue,
};
use jrsonnet_gcmodule::Cc;
@@ -17,7 +21,10 @@
preserve_order,
);
Ok(VecVal(Cc::new(
- out.into_iter().map(Val::Str).collect::<Vec<_>>(),
+ out.into_iter()
+ .map(StrValue::Flat)
+ .map(Val::Str)
+ .collect::<Vec<_>>(),
)))
}
crates/jrsonnet-stdlib/src/operator.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/operator.rs
+++ b/crates/jrsonnet-stdlib/src/operator.rs
@@ -7,7 +7,7 @@
operator::evaluate_mod_op,
stdlib::std_format,
typed::{Any, Either, Either2},
- val::{equals, primitive_equals},
+ val::{equals, primitive_equals, StrValue},
IStr, Val,
};
@@ -17,7 +17,7 @@
Ok(Any(evaluate_mod_op(
&match a {
A(v) => Val::Num(v),
- B(s) => Val::Str(s),
+ B(s) => Val::Str(StrValue::Flat(s)),
},
&b.0,
)?))
@@ -35,5 +35,5 @@
#[builtin]
pub fn builtin_format(str: IStr, vals: Any) -> Result<String> {
- std_format(str, vals.0)
+ std_format(&str, vals.0)
}
crates/jrsonnet-stdlib/src/strings.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/strings.rs
+++ b/crates/jrsonnet-stdlib/src/strings.rs
@@ -3,7 +3,7 @@
function::builtin,
throw,
typed::{Either2, VecVal, M1},
- val::ArrValue,
+ val::{ArrValue, StrValue},
Either, IStr, Val,
};
use jrsonnet_gcmodule::Cc;
@@ -34,9 +34,12 @@
Ok(VecVal(Cc::new(match maxsplits {
A(n) => str
.splitn(n + 1, &c as &str)
- .map(|s| Val::Str(s.into()))
+ .map(|s| Val::Str(StrValue::Flat(s.into())))
+ .collect(),
+ B(_) => str
+ .split(&c as &str)
+ .map(|s| Val::Str(StrValue::Flat(s.into())))
.collect(),
- B(_) => str.split(&c as &str).map(|s| Val::Str(s.into())).collect(),
})))
}