difftreelog
perf implement std.{startsWith, endsWith} in native
in: master
2 files changed
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth9 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}451452#[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 a467 .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 true478 }479 }480 _ => throw_runtime!("both arguments should be of the same type"),481 })482}483484#[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 a500 .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 true511 }512 }513 _ => throw_runtime!("both arguments should be of the same type"),514 })515}449516crates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth445 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',667 toString(a)::7 toString(a):: '' + a,8 if std.type(a) == 'string' then a else '' + a,9810 startsWith(a, b)::11 if std.length(a) < std.length(b) then12 false13 else14 std.substr(a, 0, std.length(b)) == b,1516 endsWith(a, b)::17 if std.length(a) < std.length(b) then18 false19 else20 std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,2122 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]) then24 std.lstripChars(str[1:], chars)11 std.lstripChars(str[1:], chars)