difftreelog
perf make std.substr builtin
in: master
2 files changed
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -81,6 +81,7 @@
("objectFieldsEx".into(), builtin_object_fields_ex),
("objectHasEx".into(), builtin_object_has_ex),
("slice".into(), builtin_slice),
+ ("substr".into(), builtin_substr),
("primitiveEquals".into(), builtin_primitive_equals),
("equals".into(), builtin_equals),
("modulo".into(), builtin_modulo),
@@ -241,6 +242,17 @@
})
}
+fn builtin_substr(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
+ parse_args!(context, "substr", args, 3, [
+ 0, str: ty!(string) => Val::Str;
+ 1, from: ty!(BoundedNumber<(Some(0.0)), (None)>) => Val::Num;
+ 2, len: ty!(BoundedNumber<(Some(0.0)), (None)>) => Val::Num;
+ ], {
+ let out: String = str.chars().skip(from as usize).take(len as usize).collect();
+ Ok(Val::Str(out.into()))
+ })
+}
+
fn builtin_primitive_equals(
context: Context,
_loc: Option<&ExprLocation>,
crates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth48 toString(a)::48 toString(a)::49 if std.type(a) == 'string' then a else '' + a,49 if std.type(a) == 'string' then a else '' + a,505051 substr(str, from, len)::51 substr:: $intrinsic(substr),52 assert std.isString(str) : 'substr first parameter should be a string, got ' + std.type(str);53 assert std.isNumber(from) : 'substr second parameter should be a string, got ' + std.type(from);54 assert std.isNumber(len) : 'substr third parameter should be a string, got ' + std.type(len);55 assert len >= 0 : 'substr third parameter should be greater than zero, got ' + len;56 std.join('', std.makeArray(std.max(0, std.min(len, std.length(str) - from)), function(i) str[i + from])),575258 startsWith(a, b)::53 startsWith(a, b)::59 if std.length(a) < std.length(b) then54 if std.length(a) < std.length(b) then