git.delta.rocks / jrsonnet / refs/commits / 747b2b782cb8

difftreelog

perf make std.splitLimit builtin

Yaroslav Bolyukin2021-07-12parent: #02b7991.patch.diff
in: master

2 files changed

modifiedcrates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth
--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -121,6 +121,7 @@
 			("reverse".into(), builtin_reverse),
 			("id".into(), builtin_id),
 			("strReplace".into(), builtin_str_replace),
+			("splitLimit".into(), builtin_splitlimit),
 			("parseJson".into(), builtin_parse_json),
 		].iter().cloned().collect()
 	};
@@ -751,6 +752,29 @@
 	})
 }
 
+fn builtin_splitlimit(
+	context: Context,
+	_loc: Option<&ExprLocation>,
+	args: &ArgsDesc,
+) -> Result<Val> {
+	parse_args!(context, "splitLimit", args, 3, [
+		0, str: ty!(string) => Val::Str;
+		1, c: ty!(char) => Val::Str;
+		2, maxsplits: ty!(number) => Val::Num;
+	], {
+		let maxsplits = maxsplits as isize;
+		let c = c.chars().next().unwrap();
+
+		let out: Vec<Val> = if maxsplits == -1 {
+			str.split(c).map(|s| Val::Str(s.into())).collect()
+		} else {
+			str.splitn(maxsplits as usize + 1, c).map(|s| Val::Str(s.into())).collect()
+		};
+
+		Ok(Val::Arr(out.into()))
+	})
+}
+
 pub fn call_builtin(
 	context: Context,
 	loc: Option<&ExprLocation>,
modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
before · crates/jrsonnet-stdlib/src/std.jsonnet
1{2  local std = self,3  local id = std.id,45  # Those functions aren't normally located in stdlib6  length:: $intrinsic(length),7  type:: $intrinsic(type),8  makeArray:: $intrinsic(makeArray),9  codepoint:: $intrinsic(codepoint),10  objectFieldsEx:: $intrinsic(objectFieldsEx),11  objectHasEx:: $intrinsic(objectHasEx),12  primitiveEquals:: $intrinsic(primitiveEquals),13  modulo:: $intrinsic(modulo),14  floor:: $intrinsic(floor),15  ceil:: $intrinsic(ceil),16  extVar:: $intrinsic(extVar),17  native:: $intrinsic(native),18  filter:: $intrinsic(filter),19  char:: $intrinsic(char),20  encodeUTF8:: $intrinsic(encodeUTF8),21  md5:: $intrinsic(md5),22  trace:: $intrinsic(trace),23  id:: $intrinsic(id),24  parseJson:: $intrinsic(parseJson),2526  log:: $intrinsic(log),27  pow:: $intrinsic(pow),28  sqrt:: $intrinsic(sqrt),2930  sin:: $intrinsic(sin),31  cos:: $intrinsic(cos),32  tan:: $intrinsic(tan),33  asin:: $intrinsic(asin),34  acos:: $intrinsic(acos),35  atan:: $intrinsic(atan),3637  exp:: $intrinsic(exp),38  mantissa:: $intrinsic(mantissa),39  exponent:: $intrinsic(exponent),4041  isString(v):: std.type(v) == 'string',42  isNumber(v):: std.type(v) == 'number',43  isBoolean(v):: std.type(v) == 'boolean',44  isObject(v):: std.type(v) == 'object',45  isArray(v):: std.type(v) == 'array',46  isFunction(v):: std.type(v) == 'function',4748  toString(a)::49    if std.type(a) == 'string' then a else '' + a,5051  substr:: $intrinsic(substr),5253  startsWith(a, b)::54    if std.length(a) < std.length(b) then55      false56    else57      std.substr(a, 0, std.length(b)) == b,5859  endsWith(a, b)::60    if std.length(a) < std.length(b) then61      false62    else63      std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,6465  lstripChars(str, chars)::66    if std.length(str) > 0 && std.member(chars, str[0]) then67      std.lstripChars(str[1:], chars)68    else69      str,7071  rstripChars(str, chars)::72    local len = std.length(str);73    if len > 0 && std.member(chars, str[len - 1]) then74      std.rstripChars(str[:len - 1], chars)75    else76      str,7778  stripChars(str, chars)::79    std.lstripChars(std.rstripChars(str, chars), chars),8081  stringChars(str)::82    std.makeArray(std.length(str), function(i) str[i]),8384  local parse_nat(str, base) =85    assert base > 0 && base <= 16 : 'integer base %d invalid' % base;86    // These codepoints are in ascending order:87    local zero_code = std.codepoint('0');88    local upper_a_code = std.codepoint('A');89    local lower_a_code = std.codepoint('a');90    local addDigit(aggregate, char) =91      local code = std.codepoint(char);92      local digit = if code >= lower_a_code then93        code - lower_a_code + 1094      else if code >= upper_a_code then95        code - upper_a_code + 1096      else97        code - zero_code;98      assert digit >= 0 && digit < base : '%s is not a base %d integer' % [str, base];99      base * aggregate + digit;100    std.foldl(addDigit, std.stringChars(str), 0),101102  parseInt(str)::103    assert std.isString(str) : 'Expected string, got ' + std.type(str);104    assert std.length(str) > 0 && str != '-' : 'Not an integer: "%s"' % [str];105    if str[0] == '-' then106      -parse_nat(str[1:], 10)107    else108      parse_nat(str, 10),109110  parseOctal(str)::111    assert std.isString(str) : 'Expected string, got ' + std.type(str);112    assert std.length(str) > 0 : 'Not an octal number: ""';113    parse_nat(str, 8),114115  parseHex(str)::116    assert std.isString(str) : 'Expected string, got ' + std.type(str);117    assert std.length(str) > 0 : 'Not hexadecimal: ""';118    parse_nat(str, 16),119120  split(str, c)::121    assert std.isString(str) : 'std.split first parameter should be a string, got ' + std.type(str);122    assert std.isString(c) : 'std.split second parameter should be a string, got ' + std.type(c);123    assert std.length(c) == 1 : 'std.split second parameter should have length 1, got ' + std.length(c);124    std.splitLimit(str, c, -1),125126  splitLimit(str, c, maxsplits)::127    assert std.isString(str) : 'std.splitLimit first parameter should be a string, got ' + std.type(str);128    assert std.isString(c) : 'std.splitLimit second parameter should be a string, got ' + std.type(c);129    assert std.length(c) == 1 : 'std.splitLimit second parameter should have length 1, got ' + std.length(c);130    assert std.isNumber(maxsplits) : 'std.splitLimit third parameter should be a number, got ' + std.type(maxsplits);131    local aux(str, delim, i, arr, v) =132      local c = str[i];133      local i2 = i + 1;134      if i >= std.length(str) then135        arr + [v]136      else if c == delim && (maxsplits == -1 || std.length(arr) < maxsplits) then137        aux(str, delim, i2, arr + [v], '') tailstrict138      else139        aux(str, delim, i2, arr, v + c) tailstrict;140    aux(str, c, 0, [], ''),141142  strReplace:: $intrinsic(strReplace),143144  asciiUpper(str)::145    local cp = std.codepoint;146    local up_letter(c) = if cp(c) >= 97 && cp(c) < 123 then147      std.char(cp(c) - 32)148    else149      c;150    std.join('', std.map(up_letter, std.stringChars(str))),151152  asciiLower(str)::153    local cp = std.codepoint;154    local down_letter(c) = if cp(c) >= 65 && cp(c) < 91 then155      std.char(cp(c) + 32)156    else157      c;158    std.join('', std.map(down_letter, std.stringChars(str))),159160  range:: $intrinsic(range),161162  repeat(what, count)::163    local joiner =164      if std.isString(what) then ''165      else if std.isArray(what) then []166      else error 'std.repeat first argument must be an array or a string';167    std.join(joiner, std.makeArray(count, function(i) what)),168169  slice:: $intrinsic(slice),170171  member(arr, x)::172    if std.isArray(arr) then173      std.count(arr, x) > 0174    else if std.isString(arr) then175      std.length(std.findSubstr(x, arr)) > 0176    else error 'std.member first argument must be an array or a string',177178  count(arr, x):: std.length(std.filter(function(v) v == x, arr)),179180  mod:: $intrinsic(mod),181182  map:: $intrinsic(map),183184  mapWithIndex(func, arr)::185    if !std.isFunction(func) then186      error ('std.mapWithIndex first param must be function, got ' + std.type(func))187    else if !std.isArray(arr) && !std.isString(arr) then188      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))189    else190      std.makeArray(std.length(arr), function(i) func(i, arr[i])),191192  mapWithKey(func, obj)::193    if !std.isFunction(func) then194      error ('std.mapWithKey first param must be function, got ' + std.type(func))195    else if !std.isObject(obj) then196      error ('std.mapWithKey second param must be object, got ' + std.type(obj))197    else198      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },199200  flatMap:: $intrinsic(flatMap),201202  join:: $intrinsic(join),203204  lines(arr)::205    std.join('\n', arr + ['']),206207  deepJoin(arr)::208    if std.isString(arr) then209      arr210    else if std.isArray(arr) then211      std.join('', [std.deepJoin(x) for x in arr])212    else213      error 'Expected string or array, got %s' % std.type(arr),214215216  format:: $intrinsic(format),217218  foldr:: $intrinsic(foldr),219220  foldl:: $intrinsic(foldl),221222  filterMap(filter_func, map_func, arr)::223    if !std.isFunction(filter_func) then224      error ('std.filterMap first param must be function, got ' + std.type(filter_func))225    else if !std.isFunction(map_func) then226      error ('std.filterMap second param must be function, got ' + std.type(map_func))227    else if !std.isArray(arr) then228      error ('std.filterMap third param must be array, got ' + std.type(arr))229    else230      std.map(map_func, std.filter(filter_func, arr)),231232  assertEqual(a, b)::233    if a == b then234      true235    else236      error 'Assertion failed. ' + a + ' != ' + b,237238  abs(n)::239    if !std.isNumber(n) then240      error 'std.abs expected number, got ' + std.type(n)241    else242      if n > 0 then n else -n,243244  sign(n)::245    if !std.isNumber(n) then246      error 'std.sign expected number, got ' + std.type(n)247    else248      if n > 0 then249        1250      else if n < 0 then251        -1252      else 0,253254  max(a, b)::255    if !std.isNumber(a) then256      error 'std.max first param expected number, got ' + std.type(a)257    else if !std.isNumber(b) then258      error 'std.max second param expected number, got ' + std.type(b)259    else260      if a > b then a else b,261262  min(a, b)::263    if !std.isNumber(a) then264      error 'std.min first param expected number, got ' + std.type(a)265    else if !std.isNumber(b) then266      error 'std.min second param expected number, got ' + std.type(b)267    else268      if a < b then a else b,269270  clamp(x, minVal, maxVal)::271    if x < minVal then minVal272    else if x > maxVal then maxVal273    else x,274275  flattenArrays(arrs)::276    std.foldl(function(a, b) a + b, arrs, []),277278  manifestIni(ini)::279    local body_lines(body) =280      std.join([], [281        local value_or_values = body[k];282        if std.isArray(value_or_values) then283          ['%s = %s' % [k, value] for value in value_or_values]284        else285          ['%s = %s' % [k, value_or_values]]286287        for k in std.objectFields(body)288      ]);289290    local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),291          main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],292          all_sections = [293      section_lines(k, ini.sections[k])294      for k in std.objectFields(ini.sections)295    ];296    std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),297298  manifestToml(value):: std.manifestTomlEx(value, '  '),299300  manifestTomlEx(value, indent)::301    local302      escapeStringToml = std.escapeStringJson,303      escapeKeyToml(key) =304        local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));305        if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),306      isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),307      isSection(v) = std.isObject(v) || isTableArray(v),308      renderValue(v, indexedPath, inline, cindent) =309        if v == true then310          'true'311        else if v == false then312          'false'313        else if v == null then314          error 'Tried to manifest "null" at ' + indexedPath315        else if std.isNumber(v) then316          '' + v317        else if std.isString(v) then318          escapeStringToml(v)319        else if std.isFunction(v) then320          error 'Tried to manifest function at ' + indexedPath321        else if std.isArray(v) then322          if std.length(v) == 0 then323            '[]'324          else325            local range = std.range(0, std.length(v) - 1);326            local new_indent = if inline then '' else cindent + indent;327            local separator = if inline then ' ' else '\n';328            local lines = ['[' + separator]329                          + std.join([',' + separator],330                                     [331                                       [new_indent + renderValue(v[i], indexedPath + [i], true, '')]332                                       for i in range333                                     ])334                          + [separator + (if inline then '' else cindent) + ']'];335            std.join('', lines)336        else if std.isObject(v) then337          local lines = ['{ ']338                        + std.join([', '],339                                   [340                                     [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]341                                     for k in std.objectFields(v)342                                   ])343                        + [' }'];344          std.join('', lines),345      renderTableInternal(v, path, indexedPath, cindent) =346        local kvp = std.flattenArrays([347          [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]348          for k in std.objectFields(v)349          if !isSection(v[k])350        ]);351        local sections = [std.join('\n', kvp)] + [352          (353            if std.isObject(v[k]) then354              renderTable(v[k], path + [k], indexedPath + [k], cindent)355            else356              renderTableArray(v[k], path + [k], indexedPath + [k], cindent)357          )358          for k in std.objectFields(v)359          if isSection(v[k])360        ];361        std.join('\n\n', sections),362      renderTable(v, path, indexedPath, cindent) =363        cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'364        + (if v == {} then '' else '\n')365        + renderTableInternal(v, path, indexedPath, cindent + indent),366      renderTableArray(v, path, indexedPath, cindent) =367        local range = std.range(0, std.length(v) - 1);368        local sections = [369          (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'370           + (if v[i] == {} then '' else '\n')371           + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))372          for i in range373        ];374        std.join('\n\n', sections);375    if std.isObject(value) then376      renderTableInternal(value, [], [], '')377    else378      error 'TOML body must be an object. Got ' + std.type(value),379380  escapeStringJson:: $intrinsic(escapeStringJson),381382  escapeStringPython(str)::383    std.escapeStringJson(str),384385  escapeStringBash(str_)::386    local str = std.toString(str_);387    local trans(ch) =388      if ch == "'" then389        "'\"'\"'"390      else391        ch;392    "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),393394  escapeStringDollars(str_)::395    local str = std.toString(str_);396    local trans(ch) =397      if ch == '$' then398        '$$'399      else400        ch;401    std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),402403  manifestJson(value):: std.manifestJsonEx(value, '    '),404405  manifestJsonEx:: $intrinsic(manifestJsonEx),406407  manifestYamlDoc(value, indent_array_in_object=false)::408    local aux(v, path, cindent) =409      if v == true then410        'true'411      else if v == false then412        'false'413      else if v == null then414        'null'415      else if std.isNumber(v) then416        '' + v417      else if std.isString(v) then418        local len = std.length(v);419        if len == 0 then420          '""'421        else if v[len - 1] == '\n' then422          local split = std.split(v, '\n');423          std.join('\n' + cindent + '  ', ['|'] + split[0:std.length(split) - 1])424        else425          std.escapeStringJson(v)426      else if std.isFunction(v) then427        error 'Tried to manifest function at ' + path428      else if std.isArray(v) then429        if std.length(v) == 0 then430          '[]'431        else432          local params(value) =433            if std.isArray(value) && std.length(value) > 0 then {434              // While we could avoid the new line, it yields YAML that is435              // hard to read, e.g.:436              // - - - 1437              //     - 2438              //   - - 3439              //     - 4440              new_indent: cindent + '  ',441              space: '\n' + self.new_indent,442            } else if std.isObject(value) && std.length(value) > 0 then {443              new_indent: cindent + '  ',444              // In this case we can start on the same line as the - because the indentation445              // matches up then.  The converse is not true, because fields are not always446              // 1 character long.447              space: ' ',448            } else {449              // In this case, new_indent is only used in the case of multi-line strings.450              new_indent: cindent,451              space: ' ',452            };453          local range = std.range(0, std.length(v) - 1);454          local parts = [455            '-' + param.space + aux(v[i], path + [i], param.new_indent)456            for i in range457            for param in [params(v[i])]458          ];459          std.join('\n' + cindent, parts)460      else if std.isObject(v) then461        if std.length(v) == 0 then462          '{}'463        else464          local params(value) =465            if std.isArray(value) && std.length(value) > 0 then {466              // Not indenting allows e.g.467              // ports:468              // - 80469              // instead of470              // ports:471              //   - 80472              new_indent: if indent_array_in_object then cindent + '  ' else cindent,473              space: '\n' + self.new_indent,474            } else if std.isObject(value) && std.length(value) > 0 then {475              new_indent: cindent + '  ',476              space: '\n' + self.new_indent,477            } else {478              // In this case, new_indent is only used in the case of multi-line strings.479              new_indent: cindent,480              space: ' ',481            };482          local lines = [483            std.escapeStringJson(k) + ':' + param.space + aux(v[k], path + [k], param.new_indent)484            for k in std.objectFields(v)485            for param in [params(v[k])]486          ];487          std.join('\n' + cindent, lines);488    aux(value, [], ''),489490  manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::491    if !std.isArray(value) then492      error 'manifestYamlStream only takes arrays, got ' + std.type(value)493    else494      '---\n' + std.join(495        '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]496      ) + if c_document_end then '\n...\n' else '\n',497498499  manifestPython(v)::500    if std.isObject(v) then501      local fields = [502        '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]503        for k in std.objectFields(v)504      ];505      '{%s}' % [std.join(', ', fields)]506    else if std.isArray(v) then507      '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]508    else if std.isString(v) then509      '%s' % [std.escapeStringPython(v)]510    else if std.isFunction(v) then511      error 'cannot manifest function'512    else if std.isNumber(v) then513      std.toString(v)514    else if v == true then515      'True'516    else if v == false then517      'False'518    else if v == null then519      'None',520521  manifestPythonVars(conf)::522    local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];523    std.join('\n', vars + ['']),524525  manifestXmlJsonml(value)::526    if !std.isArray(value) then527      error 'Expected a JSONML value (an array), got %s' % std.type(value)528    else529      local aux(v) =530        if std.isString(v) then531          v532        else533          local tag = v[0];534          local has_attrs = std.length(v) > 1 && std.isObject(v[1]);535          local attrs = if has_attrs then v[1] else {};536          local children = if has_attrs then v[2:] else v[1:];537          local attrs_str =538            std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);539          std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);540541      aux(value),542543  local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',544  local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },545546  base64:: $intrinsic(base64),547548  base64DecodeBytes(str)::549    if std.length(str) % 4 != 0 then550      error 'Not a base64 encoded string "%s"' % str551    else552      local aux(str, i, r) =553        if i >= std.length(str) then554          r555        else556          // all 6 bits of i, 2 MSB of i+1557          local n1 = [base64_inv[str[i]] << 2 | (base64_inv[str[i + 1]] >> 4)];558          // 4 LSB of i+1, 4MSB of i+2559          local n2 =560            if str[i + 2] == '=' then []561            else [(base64_inv[str[i + 1]] & 15) << 4 | (base64_inv[str[i + 2]] >> 2)];562          // 2 LSB of i+2, all 6 bits of i+3563          local n3 =564            if str[i + 3] == '=' then []565            else [(base64_inv[str[i + 2]] & 3) << 6 | base64_inv[str[i + 3]]];566          aux(str, i + 4, r + n1 + n2 + n3) tailstrict;567      aux(str, 0, []),568569  base64Decode(str)::570    local bytes = std.base64DecodeBytes(str);571    std.join('', std.map(function(b) std.char(b), bytes)),572573  reverse:: $intrinsic(reverse),574575  sortImpl:: $intrinsic(sortImpl),576577  sort(arr, keyF=id)::578    std.sortImpl(arr, keyF),579580  uniq(arr, keyF=id)::581    local f(a, b) =582      if std.length(a) == 0 then583        [b]584      else if keyF(a[std.length(a) - 1]) == keyF(b) then585        a586      else587        a + [b];588    std.foldl(f, arr, []),589590  set(arr, keyF=id)::591    std.uniq(std.sort(arr, keyF), keyF),592593  setMember(x, arr, keyF=id)::594    // TODO(dcunnin): Binary chop for O(log n) complexity595    std.length(std.setInter([x], arr, keyF)) > 0,596597  setUnion(a, b, keyF=id)::598    // NOTE: order matters, values in `a` win599    local aux(a, b, i, j, acc) =600      if i >= std.length(a) then601        acc + b[j:]602      else if j >= std.length(b) then603        acc + a[i:]604      else605        local ak = keyF(a[i]);606        local bk = keyF(b[j]);607        if ak == bk then608          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict609        else if ak < bk then610          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict611        else612          aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;613    aux(a, b, 0, 0, []),614615  setInter(a, b, keyF=id)::616    local aux(a, b, i, j, acc) =617      if i >= std.length(a) || j >= std.length(b) then618        acc619      else620        if keyF(a[i]) == keyF(b[j]) then621          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict622        else if keyF(a[i]) < keyF(b[j]) then623          aux(a, b, i + 1, j, acc) tailstrict624        else625          aux(a, b, i, j + 1, acc) tailstrict;626    aux(a, b, 0, 0, []) tailstrict,627628  setDiff(a, b, keyF=id)::629    local aux(a, b, i, j, acc) =630      if i >= std.length(a) then631        acc632      else if j >= std.length(b) then633        acc + a[i:]634      else635        if keyF(a[i]) == keyF(b[j]) then636          aux(a, b, i + 1, j + 1, acc) tailstrict637        else if keyF(a[i]) < keyF(b[j]) then638          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict639        else640          aux(a, b, i, j + 1, acc) tailstrict;641    aux(a, b, 0, 0, []) tailstrict,642643  mergePatch(target, patch)::644    if std.isObject(patch) then645      local target_object =646        if std.isObject(target) then target else {};647648      local target_fields =649        if std.isObject(target_object) then std.objectFields(target_object) else [];650651      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];652      local both_fields = std.setUnion(target_fields, std.objectFields(patch));653654      {655        [k]:656          if !std.objectHas(patch, k) then657            target_object[k]658          else if !std.objectHas(target_object, k) then659            std.mergePatch(null, patch[k]) tailstrict660          else661            std.mergePatch(target_object[k], patch[k]) tailstrict662        for k in std.setDiff(both_fields, null_fields)663      }664    else665      patch,666667  objectFields(o)::668    std.objectFieldsEx(o, false),669670  objectFieldsAll(o)::671    std.objectFieldsEx(o, true),672673  objectHas(o, f)::674    std.objectHasEx(o, f, false),675676  objectHasAll(o, f)::677    std.objectHasEx(o, f, true),678679  objectValues(o)::680    [o[k] for k in std.objectFields(o)],681682  objectValuesAll(o)::683    [o[k] for k in std.objectFieldsAll(o)],684685  equals:: $intrinsic(equals),686687  resolvePath(f, r)::688    local arr = std.split(f, '/');689    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),690691  prune(a)::692    local isContent(b) =693      if b == null then694        false695      else if std.isArray(b) then696        std.length(b) > 0697      else if std.isObject(b) then698        std.length(b) > 0699      else700        true;701    if std.isArray(a) then702      [std.prune(x) for x in a if isContent($.prune(x))]703    else if std.isObject(a) then {704      [x]: $.prune(a[x])705      for x in std.objectFields(a)706      if isContent(std.prune(a[x]))707    } else708      a,709710  findSubstr(pat, str)::711    if !std.isString(pat) then712      error 'findSubstr first parameter should be a string, got ' + std.type(pat)713    else if !std.isString(str) then714      error 'findSubstr second parameter should be a string, got ' + std.type(str)715    else716      local pat_len = std.length(pat);717      local str_len = std.length(str);718      if pat_len == 0 || str_len == 0 || pat_len > str_len then719        []720      else721        std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),722723  find(value, arr)::724    if !std.isArray(arr) then725      error 'find second parameter should be an array, got ' + std.type(arr)726    else727      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),728}
after · crates/jrsonnet-stdlib/src/std.jsonnet
1{2  local std = self,3  local id = std.id,45  # Those functions aren't normally located in stdlib6  length:: $intrinsic(length),7  type:: $intrinsic(type),8  makeArray:: $intrinsic(makeArray),9  codepoint:: $intrinsic(codepoint),10  objectFieldsEx:: $intrinsic(objectFieldsEx),11  objectHasEx:: $intrinsic(objectHasEx),12  primitiveEquals:: $intrinsic(primitiveEquals),13  modulo:: $intrinsic(modulo),14  floor:: $intrinsic(floor),15  ceil:: $intrinsic(ceil),16  extVar:: $intrinsic(extVar),17  native:: $intrinsic(native),18  filter:: $intrinsic(filter),19  char:: $intrinsic(char),20  encodeUTF8:: $intrinsic(encodeUTF8),21  md5:: $intrinsic(md5),22  trace:: $intrinsic(trace),23  id:: $intrinsic(id),24  parseJson:: $intrinsic(parseJson),2526  log:: $intrinsic(log),27  pow:: $intrinsic(pow),28  sqrt:: $intrinsic(sqrt),2930  sin:: $intrinsic(sin),31  cos:: $intrinsic(cos),32  tan:: $intrinsic(tan),33  asin:: $intrinsic(asin),34  acos:: $intrinsic(acos),35  atan:: $intrinsic(atan),3637  exp:: $intrinsic(exp),38  mantissa:: $intrinsic(mantissa),39  exponent:: $intrinsic(exponent),4041  isString(v):: std.type(v) == 'string',42  isNumber(v):: std.type(v) == 'number',43  isBoolean(v):: std.type(v) == 'boolean',44  isObject(v):: std.type(v) == 'object',45  isArray(v):: std.type(v) == 'array',46  isFunction(v):: std.type(v) == 'function',4748  toString(a)::49    if std.type(a) == 'string' then a else '' + a,5051  substr:: $intrinsic(substr),5253  startsWith(a, b)::54    if std.length(a) < std.length(b) then55      false56    else57      std.substr(a, 0, std.length(b)) == b,5859  endsWith(a, b)::60    if std.length(a) < std.length(b) then61      false62    else63      std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,6465  lstripChars(str, chars)::66    if std.length(str) > 0 && std.member(chars, str[0]) then67      std.lstripChars(str[1:], chars)68    else69      str,7071  rstripChars(str, chars)::72    local len = std.length(str);73    if len > 0 && std.member(chars, str[len - 1]) then74      std.rstripChars(str[:len - 1], chars)75    else76      str,7778  stripChars(str, chars)::79    std.lstripChars(std.rstripChars(str, chars), chars),8081  stringChars(str)::82    std.makeArray(std.length(str), function(i) str[i]),8384  local parse_nat(str, base) =85    assert base > 0 && base <= 16 : 'integer base %d invalid' % base;86    // These codepoints are in ascending order:87    local zero_code = std.codepoint('0');88    local upper_a_code = std.codepoint('A');89    local lower_a_code = std.codepoint('a');90    local addDigit(aggregate, char) =91      local code = std.codepoint(char);92      local digit = if code >= lower_a_code then93        code - lower_a_code + 1094      else if code >= upper_a_code then95        code - upper_a_code + 1096      else97        code - zero_code;98      assert digit >= 0 && digit < base : '%s is not a base %d integer' % [str, base];99      base * aggregate + digit;100    std.foldl(addDigit, std.stringChars(str), 0),101102  parseInt(str)::103    assert std.isString(str) : 'Expected string, got ' + std.type(str);104    assert std.length(str) > 0 && str != '-' : 'Not an integer: "%s"' % [str];105    if str[0] == '-' then106      -parse_nat(str[1:], 10)107    else108      parse_nat(str, 10),109110  parseOctal(str)::111    assert std.isString(str) : 'Expected string, got ' + std.type(str);112    assert std.length(str) > 0 : 'Not an octal number: ""';113    parse_nat(str, 8),114115  parseHex(str)::116    assert std.isString(str) : 'Expected string, got ' + std.type(str);117    assert std.length(str) > 0 : 'Not hexadecimal: ""';118    parse_nat(str, 16),119120  split(str, c)::121    assert std.isString(str) : 'std.split first parameter should be a string, got ' + std.type(str);122    assert std.isString(c) : 'std.split second parameter should be a string, got ' + std.type(c);123    assert std.length(c) == 1 : 'std.split second parameter should have length 1, got ' + std.length(c);124    std.splitLimit(str, c, -1),125126  splitLimit:: $intrinsic(splitLimit),127128  strReplace:: $intrinsic(strReplace),129130  asciiUpper(str)::131    local cp = std.codepoint;132    local up_letter(c) = if cp(c) >= 97 && cp(c) < 123 then133      std.char(cp(c) - 32)134    else135      c;136    std.join('', std.map(up_letter, std.stringChars(str))),137138  asciiLower(str)::139    local cp = std.codepoint;140    local down_letter(c) = if cp(c) >= 65 && cp(c) < 91 then141      std.char(cp(c) + 32)142    else143      c;144    std.join('', std.map(down_letter, std.stringChars(str))),145146  range:: $intrinsic(range),147148  repeat(what, count)::149    local joiner =150      if std.isString(what) then ''151      else if std.isArray(what) then []152      else error 'std.repeat first argument must be an array or a string';153    std.join(joiner, std.makeArray(count, function(i) what)),154155  slice:: $intrinsic(slice),156157  member(arr, x)::158    if std.isArray(arr) then159      std.count(arr, x) > 0160    else if std.isString(arr) then161      std.length(std.findSubstr(x, arr)) > 0162    else error 'std.member first argument must be an array or a string',163164  count(arr, x):: std.length(std.filter(function(v) v == x, arr)),165166  mod:: $intrinsic(mod),167168  map:: $intrinsic(map),169170  mapWithIndex(func, arr)::171    if !std.isFunction(func) then172      error ('std.mapWithIndex first param must be function, got ' + std.type(func))173    else if !std.isArray(arr) && !std.isString(arr) then174      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))175    else176      std.makeArray(std.length(arr), function(i) func(i, arr[i])),177178  mapWithKey(func, obj)::179    if !std.isFunction(func) then180      error ('std.mapWithKey first param must be function, got ' + std.type(func))181    else if !std.isObject(obj) then182      error ('std.mapWithKey second param must be object, got ' + std.type(obj))183    else184      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },185186  flatMap:: $intrinsic(flatMap),187188  join:: $intrinsic(join),189190  lines(arr)::191    std.join('\n', arr + ['']),192193  deepJoin(arr)::194    if std.isString(arr) then195      arr196    else if std.isArray(arr) then197      std.join('', [std.deepJoin(x) for x in arr])198    else199      error 'Expected string or array, got %s' % std.type(arr),200201202  format:: $intrinsic(format),203204  foldr:: $intrinsic(foldr),205206  foldl:: $intrinsic(foldl),207208  filterMap(filter_func, map_func, arr)::209    if !std.isFunction(filter_func) then210      error ('std.filterMap first param must be function, got ' + std.type(filter_func))211    else if !std.isFunction(map_func) then212      error ('std.filterMap second param must be function, got ' + std.type(map_func))213    else if !std.isArray(arr) then214      error ('std.filterMap third param must be array, got ' + std.type(arr))215    else216      std.map(map_func, std.filter(filter_func, arr)),217218  assertEqual(a, b)::219    if a == b then220      true221    else222      error 'Assertion failed. ' + a + ' != ' + b,223224  abs(n)::225    if !std.isNumber(n) then226      error 'std.abs expected number, got ' + std.type(n)227    else228      if n > 0 then n else -n,229230  sign(n)::231    if !std.isNumber(n) then232      error 'std.sign expected number, got ' + std.type(n)233    else234      if n > 0 then235        1236      else if n < 0 then237        -1238      else 0,239240  max(a, b)::241    if !std.isNumber(a) then242      error 'std.max first param expected number, got ' + std.type(a)243    else if !std.isNumber(b) then244      error 'std.max second param expected number, got ' + std.type(b)245    else246      if a > b then a else b,247248  min(a, b)::249    if !std.isNumber(a) then250      error 'std.min first param expected number, got ' + std.type(a)251    else if !std.isNumber(b) then252      error 'std.min second param expected number, got ' + std.type(b)253    else254      if a < b then a else b,255256  clamp(x, minVal, maxVal)::257    if x < minVal then minVal258    else if x > maxVal then maxVal259    else x,260261  flattenArrays(arrs)::262    std.foldl(function(a, b) a + b, arrs, []),263264  manifestIni(ini)::265    local body_lines(body) =266      std.join([], [267        local value_or_values = body[k];268        if std.isArray(value_or_values) then269          ['%s = %s' % [k, value] for value in value_or_values]270        else271          ['%s = %s' % [k, value_or_values]]272273        for k in std.objectFields(body)274      ]);275276    local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),277          main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],278          all_sections = [279      section_lines(k, ini.sections[k])280      for k in std.objectFields(ini.sections)281    ];282    std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),283284  manifestToml(value):: std.manifestTomlEx(value, '  '),285286  manifestTomlEx(value, indent)::287    local288      escapeStringToml = std.escapeStringJson,289      escapeKeyToml(key) =290        local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));291        if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),292      isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),293      isSection(v) = std.isObject(v) || isTableArray(v),294      renderValue(v, indexedPath, inline, cindent) =295        if v == true then296          'true'297        else if v == false then298          'false'299        else if v == null then300          error 'Tried to manifest "null" at ' + indexedPath301        else if std.isNumber(v) then302          '' + v303        else if std.isString(v) then304          escapeStringToml(v)305        else if std.isFunction(v) then306          error 'Tried to manifest function at ' + indexedPath307        else if std.isArray(v) then308          if std.length(v) == 0 then309            '[]'310          else311            local range = std.range(0, std.length(v) - 1);312            local new_indent = if inline then '' else cindent + indent;313            local separator = if inline then ' ' else '\n';314            local lines = ['[' + separator]315                          + std.join([',' + separator],316                                     [317                                       [new_indent + renderValue(v[i], indexedPath + [i], true, '')]318                                       for i in range319                                     ])320                          + [separator + (if inline then '' else cindent) + ']'];321            std.join('', lines)322        else if std.isObject(v) then323          local lines = ['{ ']324                        + std.join([', '],325                                   [326                                     [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]327                                     for k in std.objectFields(v)328                                   ])329                        + [' }'];330          std.join('', lines),331      renderTableInternal(v, path, indexedPath, cindent) =332        local kvp = std.flattenArrays([333          [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]334          for k in std.objectFields(v)335          if !isSection(v[k])336        ]);337        local sections = [std.join('\n', kvp)] + [338          (339            if std.isObject(v[k]) then340              renderTable(v[k], path + [k], indexedPath + [k], cindent)341            else342              renderTableArray(v[k], path + [k], indexedPath + [k], cindent)343          )344          for k in std.objectFields(v)345          if isSection(v[k])346        ];347        std.join('\n\n', sections),348      renderTable(v, path, indexedPath, cindent) =349        cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'350        + (if v == {} then '' else '\n')351        + renderTableInternal(v, path, indexedPath, cindent + indent),352      renderTableArray(v, path, indexedPath, cindent) =353        local range = std.range(0, std.length(v) - 1);354        local sections = [355          (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'356           + (if v[i] == {} then '' else '\n')357           + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))358          for i in range359        ];360        std.join('\n\n', sections);361    if std.isObject(value) then362      renderTableInternal(value, [], [], '')363    else364      error 'TOML body must be an object. Got ' + std.type(value),365366  escapeStringJson:: $intrinsic(escapeStringJson),367368  escapeStringPython(str)::369    std.escapeStringJson(str),370371  escapeStringBash(str_)::372    local str = std.toString(str_);373    local trans(ch) =374      if ch == "'" then375        "'\"'\"'"376      else377        ch;378    "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),379380  escapeStringDollars(str_)::381    local str = std.toString(str_);382    local trans(ch) =383      if ch == '$' then384        '$$'385      else386        ch;387    std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),388389  manifestJson(value):: std.manifestJsonEx(value, '    '),390391  manifestJsonEx:: $intrinsic(manifestJsonEx),392393  manifestYamlDoc(value, indent_array_in_object=false)::394    local aux(v, path, cindent) =395      if v == true then396        'true'397      else if v == false then398        'false'399      else if v == null then400        'null'401      else if std.isNumber(v) then402        '' + v403      else if std.isString(v) then404        local len = std.length(v);405        if len == 0 then406          '""'407        else if v[len - 1] == '\n' then408          local split = std.split(v, '\n');409          std.join('\n' + cindent + '  ', ['|'] + split[0:std.length(split) - 1])410        else411          std.escapeStringJson(v)412      else if std.isFunction(v) then413        error 'Tried to manifest function at ' + path414      else if std.isArray(v) then415        if std.length(v) == 0 then416          '[]'417        else418          local params(value) =419            if std.isArray(value) && std.length(value) > 0 then {420              // While we could avoid the new line, it yields YAML that is421              // hard to read, e.g.:422              // - - - 1423              //     - 2424              //   - - 3425              //     - 4426              new_indent: cindent + '  ',427              space: '\n' + self.new_indent,428            } else if std.isObject(value) && std.length(value) > 0 then {429              new_indent: cindent + '  ',430              // In this case we can start on the same line as the - because the indentation431              // matches up then.  The converse is not true, because fields are not always432              // 1 character long.433              space: ' ',434            } else {435              // In this case, new_indent is only used in the case of multi-line strings.436              new_indent: cindent,437              space: ' ',438            };439          local range = std.range(0, std.length(v) - 1);440          local parts = [441            '-' + param.space + aux(v[i], path + [i], param.new_indent)442            for i in range443            for param in [params(v[i])]444          ];445          std.join('\n' + cindent, parts)446      else if std.isObject(v) then447        if std.length(v) == 0 then448          '{}'449        else450          local params(value) =451            if std.isArray(value) && std.length(value) > 0 then {452              // Not indenting allows e.g.453              // ports:454              // - 80455              // instead of456              // ports:457              //   - 80458              new_indent: if indent_array_in_object then cindent + '  ' else cindent,459              space: '\n' + self.new_indent,460            } else if std.isObject(value) && std.length(value) > 0 then {461              new_indent: cindent + '  ',462              space: '\n' + self.new_indent,463            } else {464              // In this case, new_indent is only used in the case of multi-line strings.465              new_indent: cindent,466              space: ' ',467            };468          local lines = [469            std.escapeStringJson(k) + ':' + param.space + aux(v[k], path + [k], param.new_indent)470            for k in std.objectFields(v)471            for param in [params(v[k])]472          ];473          std.join('\n' + cindent, lines);474    aux(value, [], ''),475476  manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::477    if !std.isArray(value) then478      error 'manifestYamlStream only takes arrays, got ' + std.type(value)479    else480      '---\n' + std.join(481        '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]482      ) + if c_document_end then '\n...\n' else '\n',483484485  manifestPython(v)::486    if std.isObject(v) then487      local fields = [488        '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]489        for k in std.objectFields(v)490      ];491      '{%s}' % [std.join(', ', fields)]492    else if std.isArray(v) then493      '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]494    else if std.isString(v) then495      '%s' % [std.escapeStringPython(v)]496    else if std.isFunction(v) then497      error 'cannot manifest function'498    else if std.isNumber(v) then499      std.toString(v)500    else if v == true then501      'True'502    else if v == false then503      'False'504    else if v == null then505      'None',506507  manifestPythonVars(conf)::508    local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];509    std.join('\n', vars + ['']),510511  manifestXmlJsonml(value)::512    if !std.isArray(value) then513      error 'Expected a JSONML value (an array), got %s' % std.type(value)514    else515      local aux(v) =516        if std.isString(v) then517          v518        else519          local tag = v[0];520          local has_attrs = std.length(v) > 1 && std.isObject(v[1]);521          local attrs = if has_attrs then v[1] else {};522          local children = if has_attrs then v[2:] else v[1:];523          local attrs_str =524            std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);525          std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);526527      aux(value),528529  local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',530  local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },531532  base64:: $intrinsic(base64),533534  base64DecodeBytes(str)::535    if std.length(str) % 4 != 0 then536      error 'Not a base64 encoded string "%s"' % str537    else538      local aux(str, i, r) =539        if i >= std.length(str) then540          r541        else542          // all 6 bits of i, 2 MSB of i+1543          local n1 = [base64_inv[str[i]] << 2 | (base64_inv[str[i + 1]] >> 4)];544          // 4 LSB of i+1, 4MSB of i+2545          local n2 =546            if str[i + 2] == '=' then []547            else [(base64_inv[str[i + 1]] & 15) << 4 | (base64_inv[str[i + 2]] >> 2)];548          // 2 LSB of i+2, all 6 bits of i+3549          local n3 =550            if str[i + 3] == '=' then []551            else [(base64_inv[str[i + 2]] & 3) << 6 | base64_inv[str[i + 3]]];552          aux(str, i + 4, r + n1 + n2 + n3) tailstrict;553      aux(str, 0, []),554555  base64Decode(str)::556    local bytes = std.base64DecodeBytes(str);557    std.join('', std.map(function(b) std.char(b), bytes)),558559  reverse:: $intrinsic(reverse),560561  sortImpl:: $intrinsic(sortImpl),562563  sort(arr, keyF=id)::564    std.sortImpl(arr, keyF),565566  uniq(arr, keyF=id)::567    local f(a, b) =568      if std.length(a) == 0 then569        [b]570      else if keyF(a[std.length(a) - 1]) == keyF(b) then571        a572      else573        a + [b];574    std.foldl(f, arr, []),575576  set(arr, keyF=id)::577    std.uniq(std.sort(arr, keyF), keyF),578579  setMember(x, arr, keyF=id)::580    // TODO(dcunnin): Binary chop for O(log n) complexity581    std.length(std.setInter([x], arr, keyF)) > 0,582583  setUnion(a, b, keyF=id)::584    // NOTE: order matters, values in `a` win585    local aux(a, b, i, j, acc) =586      if i >= std.length(a) then587        acc + b[j:]588      else if j >= std.length(b) then589        acc + a[i:]590      else591        local ak = keyF(a[i]);592        local bk = keyF(b[j]);593        if ak == bk then594          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict595        else if ak < bk then596          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict597        else598          aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;599    aux(a, b, 0, 0, []),600601  setInter(a, b, keyF=id)::602    local aux(a, b, i, j, acc) =603      if i >= std.length(a) || j >= std.length(b) then604        acc605      else606        if keyF(a[i]) == keyF(b[j]) then607          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict608        else if keyF(a[i]) < keyF(b[j]) then609          aux(a, b, i + 1, j, acc) tailstrict610        else611          aux(a, b, i, j + 1, acc) tailstrict;612    aux(a, b, 0, 0, []) tailstrict,613614  setDiff(a, b, keyF=id)::615    local aux(a, b, i, j, acc) =616      if i >= std.length(a) then617        acc618      else if j >= std.length(b) then619        acc + a[i:]620      else621        if keyF(a[i]) == keyF(b[j]) then622          aux(a, b, i + 1, j + 1, acc) tailstrict623        else if keyF(a[i]) < keyF(b[j]) then624          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict625        else626          aux(a, b, i, j + 1, acc) tailstrict;627    aux(a, b, 0, 0, []) tailstrict,628629  mergePatch(target, patch)::630    if std.isObject(patch) then631      local target_object =632        if std.isObject(target) then target else {};633634      local target_fields =635        if std.isObject(target_object) then std.objectFields(target_object) else [];636637      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];638      local both_fields = std.setUnion(target_fields, std.objectFields(patch));639640      {641        [k]:642          if !std.objectHas(patch, k) then643            target_object[k]644          else if !std.objectHas(target_object, k) then645            std.mergePatch(null, patch[k]) tailstrict646          else647            std.mergePatch(target_object[k], patch[k]) tailstrict648        for k in std.setDiff(both_fields, null_fields)649      }650    else651      patch,652653  objectFields(o)::654    std.objectFieldsEx(o, false),655656  objectFieldsAll(o)::657    std.objectFieldsEx(o, true),658659  objectHas(o, f)::660    std.objectHasEx(o, f, false),661662  objectHasAll(o, f)::663    std.objectHasEx(o, f, true),664665  objectValues(o)::666    [o[k] for k in std.objectFields(o)],667668  objectValuesAll(o)::669    [o[k] for k in std.objectFieldsAll(o)],670671  equals:: $intrinsic(equals),672673  resolvePath(f, r)::674    local arr = std.split(f, '/');675    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),676677  prune(a)::678    local isContent(b) =679      if b == null then680        false681      else if std.isArray(b) then682        std.length(b) > 0683      else if std.isObject(b) then684        std.length(b) > 0685      else686        true;687    if std.isArray(a) then688      [std.prune(x) for x in a if isContent($.prune(x))]689    else if std.isObject(a) then {690      [x]: $.prune(a[x])691      for x in std.objectFields(a)692      if isContent(std.prune(a[x]))693    } else694      a,695696  findSubstr(pat, str)::697    if !std.isString(pat) then698      error 'findSubstr first parameter should be a string, got ' + std.type(pat)699    else if !std.isString(str) then700      error 'findSubstr second parameter should be a string, got ' + std.type(str)701    else702      local pat_len = std.length(pat);703      local str_len = std.length(str);704      if pat_len == 0 || str_len == 0 || pat_len > str_len then705        []706      else707        std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),708709  find(value, arr)::710    if !std.isArray(arr) then711      error 'find second parameter should be an array, got ' + std.type(arr)712    else713      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),714}