git.delta.rocks / jrsonnet / refs/commits / 4c21363ed30a

difftreelog

perf implement std.{startsWith, endsWith} in native

Yaroslav Bolyukin2022-08-07parent: #a586993.patch.diff
in: master

2 files changed

modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
9 error::{Error::*, Result},9 error::{Error::*, Result},
10 function::{builtin::Builtin, ArgLike, CallLocation, FuncVal, TlaArg},10 function::{builtin::Builtin, ArgLike, CallLocation, FuncVal, TlaArg},
11 gc::TraceBox,11 gc::TraceBox,
12 tb,12 tb, throw_runtime,
13 typed::{Any, Either, Either2, Either4, VecVal, M1},13 typed::{Any, Either, Either2, Either4, VecVal, M1},
14 val::ArrValue,14 val::{equals, ArrValue},
15 Context, ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,15 Context, ContextBuilder, IStr, ObjValue, ObjValueBuilder, State, Thunk, Val,
16};16};
17use jrsonnet_gcmodule::Cc;17use jrsonnet_gcmodule::Cc;
129 ("asciiUpper".into(), builtin_ascii_upper::INST),129 ("asciiUpper".into(), builtin_ascii_upper::INST),
130 ("asciiLower".into(), builtin_ascii_lower::INST),130 ("asciiLower".into(), builtin_ascii_lower::INST),
131 ("findSubstr".into(), builtin_find_substr::INST),131 ("findSubstr".into(), builtin_find_substr::INST),
132 ("startsWith".into(), builtin_starts_with::INST),
133 ("endsWith".into(), builtin_ends_with::INST),
132 ]134 ]
133 .iter()135 .iter()
134 .cloned()136 .cloned()
447 Ok(out.into())449 Ok(out.into())
448}450}
451
452#[builtin]
453fn builtin_starts_with(
454 s: State,
455 a: Either![IStr, ArrValue],
456 b: Either![IStr, ArrValue],
457) -> Result<bool> {
458 Ok(match (a, b) {
459 (Either2::A(a), Either2::A(b)) => a.starts_with(b.as_str()),
460 (Either2::B(a), Either2::B(b)) => {
461 if b.len() > a.len() {
462 return Ok(false);
463 } else if b.len() == a.len() {
464 return equals(s, &Val::Arr(a), &Val::Arr(b));
465 } else {
466 for (a, b) in a
467 .slice(None, Some(b.len()), None)
468 .iter(s.clone())
469 .zip(b.iter(s.clone()))
470 {
471 let a = a?;
472 let b = b?;
473 if !equals(s.clone(), &a, &b)? {
474 return Ok(false);
475 }
476 }
477 true
478 }
479 }
480 _ => throw_runtime!("both arguments should be of the same type"),
481 })
482}
483
484#[builtin]
485fn builtin_ends_with(
486 s: State,
487 a: Either![IStr, ArrValue],
488 b: Either![IStr, ArrValue],
489) -> Result<bool> {
490 Ok(match (a, b) {
491 (Either2::A(a), Either2::A(b)) => a.ends_with(b.as_str()),
492 (Either2::B(a), Either2::B(b)) => {
493 if b.len() > a.len() {
494 return Ok(false);
495 } else if b.len() == a.len() {
496 return equals(s, &Val::Arr(a), &Val::Arr(b));
497 } else {
498 let a_len = a.len();
499 for (a, b) in a
500 .slice(Some(a_len - b.len()), None, None)
501 .iter(s.clone())
502 .zip(b.iter(s.clone()))
503 {
504 let a = a?;
505 let b = b?;
506 if !equals(s.clone(), &a, &b)? {
507 return Ok(false);
508 }
509 }
510 true
511 }
512 }
513 _ => throw_runtime!("both arguments should be of the same type"),
514 })
515}
449516
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
44
5 thisFile:: error 'std.thisFile is deprecated, to enable its support in jrsonnet - recompile it with "legacy-this-file" support. This will slow down stdlib caching a bit, though',5 thisFile:: error 'std.thisFile is deprecated, to enable its support in jrsonnet - recompile it with "legacy-this-file" support. This will slow down stdlib caching a bit, though',
66
7 toString(a)::7 toString(a):: '' + a,
8 if std.type(a) == 'string' then a else '' + a,
98
10 startsWith(a, b)::
11 if std.length(a) < std.length(b) then
12 false
13 else
14 std.substr(a, 0, std.length(b)) == b,
15
16 endsWith(a, b)::
17 if std.length(a) < std.length(b) then
18 false
19 else
20 std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,
21
22 lstripChars(str, chars)::9 lstripChars(str, chars)::
23 if std.length(str) > 0 && std.member(chars, str[0]) then10 if std.length(str) > 0 && std.member(chars, str[0]) then
24 std.lstripChars(str[1:], chars)11 std.lstripChars(str[1:], chars)