git.delta.rocks / jrsonnet / refs/commits / f58bb8e97c94

difftreelog

perf move deepJoin to native

Yaroslav Bolyukin2024-06-18parent: #0e22242.patch.diff
in: master

3 files changed

modifiedcrates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -204,10 +204,31 @@
 pub fn builtin_lines(arr: ArrValue) -> Result<IndexableVal> {
 	builtin_join(
 		IndexableVal::Str("\n".into()),
-		ArrValue::extended(arr, ArrValue::eager(vec![Val::string("")])).into(),
+		ArrValue::extended(arr, ArrValue::eager(vec![Val::string("")])),
 	)
 }
 
+pub fn deep_join_inner(out: &mut String, arr: IndexableVal) -> Result<()> {
+	use std::fmt::Write;
+	match arr {
+		IndexableVal::Str(s) => write!(out, "{s}").expect("no error"),
+		IndexableVal::Arr(arr) => {
+			for ele in arr.iter() {
+				let indexable = IndexableVal::from_untyped(ele?)?;
+				deep_join_inner(out, indexable)?;
+			}
+		}
+	}
+	Ok(())
+}
+
+#[builtin]
+pub fn builtin_deep_join(arr: IndexableVal) -> Result<String> {
+	let mut out = String::new();
+	deep_join_inner(&mut out, arr)?;
+	Ok(out)
+}
+
 #[builtin]
 pub fn builtin_reverse(arr: ArrValue) -> ArrValue {
 	arr.reversed()
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -86,6 +86,7 @@
 		("range", builtin_range::INST),
 		("join", builtin_join::INST),
 		("lines", builtin_lines::INST),
+		("deepJoin", builtin_deep_join::INST),
 		("reverse", builtin_reverse::INST),
 		("any", builtin_any::INST),
 		("all", builtin_all::INST),
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
11 else11 else
12 { [k]: func(k, obj[k]) for k in std.objectFields(obj) },12 { [k]: func(k, obj[k]) for k in std.objectFields(obj) },
1313
14 deepJoin(arr)::
15 if std.isString(arr) then
16 arr
17 else if std.isArray(arr) then
18 std.join('', [std.deepJoin(x) for x in arr])
19 else
20 error 'Expected string or array, got %s' % std.type(arr),
21
22 assertEqual(a, b)::14 assertEqual(a, b)::
23 if a == b then15 if a == b then
24 true16 true