git.delta.rocks / jrsonnet / refs/commits / 970fbe703e95

difftreelog

perf faster strReplace

Yaroslav Bolyukin2021-01-12parent: #d9acc2c.patch.diff
in: master

2 files changed

modifiedcrates/jrsonnet-evaluator/build.rsdiffbeforeafterboth
39 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(),
modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
73 ("manifestJsonEx".into(), builtin_manifest_json_ex),73 ("manifestJsonEx".into(), builtin_manifest_json_ex),
74 ("reverse".into(), builtin_reverse),74 ("reverse".into(), builtin_reverse),
75 ("id".into(), builtin_id),75 ("id".into(), builtin_id),
76 ("strReplace".into(), builtin_str_replace),
76 ].iter().cloned().collect()77 ].iter().cloned().collect()
77 };78 };
78}79}
131 parse_args!(context, "codepoint", args, 1, [132 parse_args!(context, "codepoint", args, 1, [
132 0, str: ty!(char) => Val::Str;133 0, str: ty!(char) => Val::Str;
133 ], {134 ], {
134 Ok(Val::Num(str.chars().take(1).next().unwrap() as u32 as f64))135 Ok(Val::Num(str.chars().next().unwrap() as u32 as f64))
135 })136 })
136}137}
137138
557 })558 })
558}559}
560
561// faster
562fn builtin_str_replace(context: Context, _loc: &Option<ExprLocation>, args: &ArgsDesc) -> Result<Val> {
563 parse_args!(context, "strReplace", args, 3, [
564 0, str: ty!(string) => Val::Str;
565 1, from: ty!(string) => Val::Str;
566 2, to: ty!(string) => Val::Str;
567 ], {
568 let mut out = String::new();
569 let mut last_idx = 0;
570 while let Some(idx) = (&str[last_idx..]).find(&from as &str) {
571 out.push_str(&str[last_idx..last_idx+idx]);
572 out.push_str(&to);
573 last_idx += idx + from.len();
574 }
575 if last_idx == 0 {
576 return Ok(Val::Str(str))
577 }
578 out.push_str(&str[last_idx..]);
579 Ok(Val::Str(out.into()))
580 })
581}
559582
560#[allow(clippy::cognitive_complexity)]583#[allow(clippy::cognitive_complexity)]
561pub fn call_builtin(584pub fn call_builtin(