git.delta.rocks / jrsonnet / refs/commits / f51b87e6aff4

difftreelog

perf implement std.findSubstr in native

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

4 files changed

modifiedCargo.tomldiffbeforeafterboth
8opt-level = 38opt-level = 3
9lto = "fat"9lto = "fat"
10codegen-units = 110codegen-units = 1
11# debug = 011debug = 0
12panic = "abort"12panic = "abort"
13# strip = true13strip = true
1414
modifiedcrates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth
206 pub fn new_eager() -> Self {206 pub fn new_eager() -> Self {
207 Self::Eager(Cc::new(Vec::new()))207 Self::Eager(Cc::new(Vec::new()))
208 }208 }
209 pub fn empty() -> Self {
210 Self::new_range(0, 0)
211 }
209212
210 /// # Panics213 /// # Panics
211 /// If a > b214 /// If a > b
215 #[inline]
212 pub fn new_range(a: i32, b: i32) -> Self {216 pub fn new_range(a: i32, b: i32) -> Self {
213 assert!(a <= b);217 assert!(a <= b);
214 Self::Range(a, b)218 Self::Range(a, b)
modifiedcrates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth
128 ("splitLimit".into(), builtin_splitlimit::INST),128 ("splitLimit".into(), builtin_splitlimit::INST),
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 ]132 ]
132 .iter()133 .iter()
133 .cloned()134 .cloned()
421 Ok(str.to_ascii_lowercase())422 Ok(str.to_ascii_lowercase())
422}423}
424
425#[builtin]
426fn builtin_find_substr(pat: IStr, str: IStr) -> Result<ArrValue> {
427 if pat.is_empty() || str.is_empty() || pat.len() > str.len() {
428 return Ok(ArrValue::empty());
429 }
430
431 let str = str.as_str();
432 let pat = pat.as_bytes();
433 let strb = str.as_bytes();
434
435 let max_pos = str.len() - pat.len();
436
437 let mut out: Vec<Val> = Vec::new();
438 for (ch_idx, (i, _)) in str
439 .char_indices()
440 .take_while(|(i, _)| i <= &max_pos)
441 .enumerate()
442 {
443 if &strb[i..i + pat.len()] == pat {
444 out.push(Val::Num(ch_idx as f64))
445 }
446 }
447 Ok(out.into())
448}
423449
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
477 } else477 } else
478 a,478 a,
479479
480 findSubstr(pat, str)::
481 if !std.isString(pat) then
482 error 'findSubstr first parameter should be a string, got ' + std.type(pat)
483 else if !std.isString(str) then
484 error 'findSubstr second parameter should be a string, got ' + std.type(str)
485 else
486 local pat_len = std.length(pat);
487 local str_len = std.length(str);
488 if pat_len == 0 || str_len == 0 || pat_len > str_len then
489 []
490 else
491 std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),
492
493 find(value, arr)::480 find(value, arr)::
494 if !std.isArray(arr) then481 if !std.isArray(arr) then
495 error 'find second parameter should be an array, got ' + std.type(arr)482 error 'find second parameter should be an array, got ' + std.type(arr)