difftreelog
perf faster strReplace
in: master
2 files changed
crates/jrsonnet-evaluator/build.rsdiffbeforeafterboth39 name == "escapeStringJson" || name == "equals" ||39 name == "escapeStringJson" || name == "equals" ||40 name == "base64" || name == "foldl" || name == "foldr" ||40 name == "base64" || name == "foldl" || name == "foldr" ||41 name == "sortImpl" || name == "format" || name == "range" ||41 name == "sortImpl" || name == "format" || name == "range" ||42 name == "reverse" || name == "slice" || name == "mod"42 name == "reverse" || name == "slice" || name == "mod" ||43 name == "strReplace"43 )44 )44 })45 })45 .collect(),46 .collect(),crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -73,6 +73,7 @@
("manifestJsonEx".into(), builtin_manifest_json_ex),
("reverse".into(), builtin_reverse),
("id".into(), builtin_id),
+ ("strReplace".into(), builtin_str_replace),
].iter().cloned().collect()
};
}
@@ -131,7 +132,7 @@
parse_args!(context, "codepoint", args, 1, [
0, str: ty!(char) => Val::Str;
], {
- Ok(Val::Num(str.chars().take(1).next().unwrap() as u32 as f64))
+ Ok(Val::Num(str.chars().next().unwrap() as u32 as f64))
})
}
@@ -557,6 +558,28 @@
})
}
+// faster
+fn builtin_str_replace(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {
+ parse_args!(context, "strReplace", args, 3, [
+ 0, str: ty!(string) => Val::Str;
+ 1, from: ty!(string) => Val::Str;
+ 2, to: ty!(string) => Val::Str;
+ ], {
+ let mut out = String::new();
+ let mut last_idx = 0;
+ while let Some(idx) = (&str[last_idx..]).find(&from as &str) {
+ out.push_str(&str[last_idx..last_idx+idx]);
+ out.push_str(&to);
+ last_idx += idx + from.len();
+ }
+ if last_idx == 0 {
+ return Ok(Val::Str(str))
+ }
+ out.push_str(&str[last_idx..]);
+ Ok(Val::Str(out.into()))
+ })
+}
+
#[allow(clippy::cognitive_complexity)]
pub fn call_builtin(
context: Context,