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

difftreelog

feat std.{mantissa,exponent} builtins

Yaroslav Bolyukin2021-07-12parent: #f62134d.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
@@ -97,6 +97,8 @@
 			("acos".into(), builtin_acos),
 			("atan".into(), builtin_atan),
 			("exp".into(), builtin_exp),
+			("mantissa".into(), builtin_mantissa),
+			("exponent".into(), builtin_exponent),
 			("extVar".into(), builtin_ext_var),
 			("native".into(), builtin_native),
 			("filter".into(), builtin_filter),
@@ -376,6 +378,33 @@
 	})
 }
 
+fn frexp(s: f64) -> (f64, i16) {
+	if 0.0 == s {
+		return (s, 0);
+	} else {
+		let lg = s.abs().log2();
+		let x = (lg - lg.floor() - 1.0).exp2();
+		let exp = lg.floor() + 1.0;
+		(s.signum() * x, exp as i16)
+	}
+}
+
+fn builtin_mantissa(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
+	parse_args!(context, "mantissa", args, 1, [
+		0, x: ty!(number) => Val::Num;
+	], {
+		Ok(Val::Num(frexp(x).0))
+	})
+}
+
+fn builtin_exponent(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
+	parse_args!(context, "exponent", args, 1, [
+		0, x: ty!(number) => Val::Num;
+	], {
+		Ok(Val::Num(frexp(x).1.into()))
+	})
+}
+
 fn builtin_ext_var(context: Context, _loc: Option<&ExprLocation>, args: &ArgsDesc) -> Result<Val> {
 	parse_args!(context, "extVar", args, 1, [
 		0, x: ty!(string) => Val::Str;
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),3839  isString(v):: std.type(v) == 'string',40  isNumber(v):: std.type(v) == 'number',41  isBoolean(v):: std.type(v) == 'boolean',42  isObject(v):: std.type(v) == 'object',43  isArray(v):: std.type(v) == 'array',44  isFunction(v):: std.type(v) == 'function',4546  toString(a)::47    if std.type(a) == 'string' then a else '' + a,4849  substr(str, from, len)::50    assert std.isString(str) : 'substr first parameter should be a string, got ' + std.type(str);51    assert std.isNumber(from) : 'substr second parameter should be a string, got ' + std.type(from);52    assert std.isNumber(len) : 'substr third parameter should be a string, got ' + std.type(len);53    assert len >= 0 : 'substr third parameter should be greater than zero, got ' + len;54    std.join('', std.makeArray(std.max(0, std.min(len, std.length(str) - from)), function(i) str[i + from])),5556  startsWith(a, b)::57    if std.length(a) < std.length(b) then58      false59    else60      std.substr(a, 0, std.length(b)) == b,6162  endsWith(a, b)::63    if std.length(a) < std.length(b) then64      false65    else66      std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,6768  lstripChars(str, chars)::69    if std.length(str) > 0 && std.member(chars, str[0]) then70      std.lstripChars(str[1:], chars)71    else72      str,7374  rstripChars(str, chars)::75    local len = std.length(str);76    if len > 0 && std.member(chars, str[len - 1]) then77      std.rstripChars(str[:len - 1], chars)78    else79      str,8081  stripChars(str, chars)::82    std.lstripChars(std.rstripChars(str, chars), chars),8384  stringChars(str)::85    std.makeArray(std.length(str), function(i) str[i]),8687  local parse_nat(str, base) =88    assert base > 0 && base <= 16 : 'integer base %d invalid' % base;89    // These codepoints are in ascending order:90    local zero_code = std.codepoint('0');91    local upper_a_code = std.codepoint('A');92    local lower_a_code = std.codepoint('a');93    local addDigit(aggregate, char) =94      local code = std.codepoint(char);95      local digit = if code >= lower_a_code then96        code - lower_a_code + 1097      else if code >= upper_a_code then98        code - upper_a_code + 1099      else100        code - zero_code;101      assert digit >= 0 && digit < base : '%s is not a base %d integer' % [str, base];102      base * aggregate + digit;103    std.foldl(addDigit, std.stringChars(str), 0),104105  parseInt(str)::106    assert std.isString(str) : 'Expected string, got ' + std.type(str);107    assert std.length(str) > 0 && str != '-' : 'Not an integer: "%s"' % [str];108    if str[0] == '-' then109      -parse_nat(str[1:], 10)110    else111      parse_nat(str, 10),112113  parseOctal(str)::114    assert std.isString(str) : 'Expected string, got ' + std.type(str);115    assert std.length(str) > 0 : 'Not an octal number: ""';116    parse_nat(str, 8),117118  parseHex(str)::119    assert std.isString(str) : 'Expected string, got ' + std.type(str);120    assert std.length(str) > 0 : 'Not hexadecimal: ""';121    parse_nat(str, 16),122123  split(str, c)::124    assert std.isString(str) : 'std.split first parameter should be a string, got ' + std.type(str);125    assert std.isString(c) : 'std.split second parameter should be a string, got ' + std.type(c);126    assert std.length(c) == 1 : 'std.split second parameter should have length 1, got ' + std.length(c);127    std.splitLimit(str, c, -1),128129  splitLimit(str, c, maxsplits)::130    assert std.isString(str) : 'std.splitLimit first parameter should be a string, got ' + std.type(str);131    assert std.isString(c) : 'std.splitLimit second parameter should be a string, got ' + std.type(c);132    assert std.length(c) == 1 : 'std.splitLimit second parameter should have length 1, got ' + std.length(c);133    assert std.isNumber(maxsplits) : 'std.splitLimit third parameter should be a number, got ' + std.type(maxsplits);134    local aux(str, delim, i, arr, v) =135      local c = str[i];136      local i2 = i + 1;137      if i >= std.length(str) then138        arr + [v]139      else if c == delim && (maxsplits == -1 || std.length(arr) < maxsplits) then140        aux(str, delim, i2, arr + [v], '') tailstrict141      else142        aux(str, delim, i2, arr, v + c) tailstrict;143    aux(str, c, 0, [], ''),144145  strReplace:: $intrinsic(strReplace),146147  asciiUpper(str)::148    local cp = std.codepoint;149    local up_letter(c) = if cp(c) >= 97 && cp(c) < 123 then150      std.char(cp(c) - 32)151    else152      c;153    std.join('', std.map(up_letter, std.stringChars(str))),154155  asciiLower(str)::156    local cp = std.codepoint;157    local down_letter(c) = if cp(c) >= 65 && cp(c) < 91 then158      std.char(cp(c) + 32)159    else160      c;161    std.join('', std.map(down_letter, std.stringChars(str))),162163  range:: $intrinsic(range),164165  repeat(what, count)::166    local joiner =167      if std.isString(what) then ''168      else if std.isArray(what) then []169      else error 'std.repeat first argument must be an array or a string';170    std.join(joiner, std.makeArray(count, function(i) what)),171172  slice:: $intrinsic(slice),173174  member(arr, x)::175    if std.isArray(arr) then176      std.count(arr, x) > 0177    else if std.isString(arr) then178      std.length(std.findSubstr(x, arr)) > 0179    else error 'std.member first argument must be an array or a string',180181  count(arr, x):: std.length(std.filter(function(v) v == x, arr)),182183  mod:: $intrinsic(mod),184185  map:: $intrinsic(map),186187  mapWithIndex(func, arr)::188    if !std.isFunction(func) then189      error ('std.mapWithIndex first param must be function, got ' + std.type(func))190    else if !std.isArray(arr) && !std.isString(arr) then191      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))192    else193      std.makeArray(std.length(arr), function(i) func(i, arr[i])),194195  mapWithKey(func, obj)::196    if !std.isFunction(func) then197      error ('std.mapWithKey first param must be function, got ' + std.type(func))198    else if !std.isObject(obj) then199      error ('std.mapWithKey second param must be object, got ' + std.type(obj))200    else201      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },202203  flatMap:: $intrinsic(flatMap),204205  join:: $intrinsic(join),206207  lines(arr)::208    std.join('\n', arr + ['']),209210  deepJoin(arr)::211    if std.isString(arr) then212      arr213    else if std.isArray(arr) then214      std.join('', [std.deepJoin(x) for x in arr])215    else216      error 'Expected string or array, got %s' % std.type(arr),217218219  format:: $intrinsic(format),220221  foldr:: $intrinsic(foldr),222223  foldl:: $intrinsic(foldl),224225  filterMap(filter_func, map_func, arr)::226    if !std.isFunction(filter_func) then227      error ('std.filterMap first param must be function, got ' + std.type(filter_func))228    else if !std.isFunction(map_func) then229      error ('std.filterMap second param must be function, got ' + std.type(map_func))230    else if !std.isArray(arr) then231      error ('std.filterMap third param must be array, got ' + std.type(arr))232    else233      std.map(map_func, std.filter(filter_func, arr)),234235  assertEqual(a, b)::236    if a == b then237      true238    else239      error 'Assertion failed. ' + a + ' != ' + b,240241  abs(n)::242    if !std.isNumber(n) then243      error 'std.abs expected number, got ' + std.type(n)244    else245      if n > 0 then n else -n,246247  sign(n)::248    if !std.isNumber(n) then249      error 'std.sign expected number, got ' + std.type(n)250    else251      if n > 0 then252        1253      else if n < 0 then254        -1255      else 0,256257  max(a, b)::258    if !std.isNumber(a) then259      error 'std.max first param expected number, got ' + std.type(a)260    else if !std.isNumber(b) then261      error 'std.max second param expected number, got ' + std.type(b)262    else263      if a > b then a else b,264265  min(a, b)::266    if !std.isNumber(a) then267      error 'std.min first param expected number, got ' + std.type(a)268    else if !std.isNumber(b) then269      error 'std.min second param expected number, got ' + std.type(b)270    else271      if a < b then a else b,272273  clamp(x, minVal, maxVal)::274    if x < minVal then minVal275    else if x > maxVal then maxVal276    else x,277278  flattenArrays(arrs)::279    std.foldl(function(a, b) a + b, arrs, []),280281  manifestIni(ini)::282    local body_lines(body) =283      std.join([], [284        local value_or_values = body[k];285        if std.isArray(value_or_values) then286          ['%s = %s' % [k, value] for value in value_or_values]287        else288          ['%s = %s' % [k, value_or_values]]289290        for k in std.objectFields(body)291      ]);292293    local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),294          main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],295          all_sections = [296      section_lines(k, ini.sections[k])297      for k in std.objectFields(ini.sections)298    ];299    std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),300301  manifestToml(value):: std.manifestTomlEx(value, '  '),302303  manifestTomlEx(value, indent)::304    local305      escapeStringToml = std.escapeStringJson,306      escapeKeyToml(key) =307        local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));308        if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),309      isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),310      isSection(v) = std.isObject(v) || isTableArray(v),311      renderValue(v, indexedPath, inline, cindent) =312        if v == true then313          'true'314        else if v == false then315          'false'316        else if v == null then317          error 'Tried to manifest "null" at ' + indexedPath318        else if std.isNumber(v) then319          '' + v320        else if std.isString(v) then321          escapeStringToml(v)322        else if std.isFunction(v) then323          error 'Tried to manifest function at ' + indexedPath324        else if std.isArray(v) then325          if std.length(v) == 0 then326            '[]'327          else328            local range = std.range(0, std.length(v) - 1);329            local new_indent = if inline then '' else cindent + indent;330            local separator = if inline then ' ' else '\n';331            local lines = ['[' + separator]332                          + std.join([',' + separator],333                                     [334                                       [new_indent + renderValue(v[i], indexedPath + [i], true, '')]335                                       for i in range336                                     ])337                          + [separator + (if inline then '' else cindent) + ']'];338            std.join('', lines)339        else if std.isObject(v) then340          local lines = ['{ ']341                        + std.join([', '],342                                   [343                                     [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]344                                     for k in std.objectFields(v)345                                   ])346                        + [' }'];347          std.join('', lines),348      renderTableInternal(v, path, indexedPath, cindent) =349        local kvp = std.flattenArrays([350          [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]351          for k in std.objectFields(v)352          if !isSection(v[k])353        ]);354        local sections = [std.join('\n', kvp)] + [355          (356            if std.isObject(v[k]) then357              renderTable(v[k], path + [k], indexedPath + [k], cindent)358            else359              renderTableArray(v[k], path + [k], indexedPath + [k], cindent)360          )361          for k in std.objectFields(v)362          if isSection(v[k])363        ];364        std.join('\n\n', sections),365      renderTable(v, path, indexedPath, cindent) =366        cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'367        + (if v == {} then '' else '\n')368        + renderTableInternal(v, path, indexedPath, cindent + indent),369      renderTableArray(v, path, indexedPath, cindent) =370        local range = std.range(0, std.length(v) - 1);371        local sections = [372          (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'373           + (if v[i] == {} then '' else '\n')374           + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))375          for i in range376        ];377        std.join('\n\n', sections);378    if std.isObject(value) then379      renderTableInternal(value, [], [], '')380    else381      error 'TOML body must be an object. Got ' + std.type(value),382383  escapeStringJson:: $intrinsic(escapeStringJson),384385  escapeStringPython(str)::386    std.escapeStringJson(str),387388  escapeStringBash(str_)::389    local str = std.toString(str_);390    local trans(ch) =391      if ch == "'" then392        "'\"'\"'"393      else394        ch;395    "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),396397  escapeStringDollars(str_)::398    local str = std.toString(str_);399    local trans(ch) =400      if ch == '$' then401        '$$'402      else403        ch;404    std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),405406  manifestJson(value):: std.manifestJsonEx(value, '    '),407408  manifestJsonEx:: $intrinsic(manifestJsonEx),409410  manifestYamlDoc(value, indent_array_in_object=false)::411    local aux(v, path, cindent) =412      if v == true then413        'true'414      else if v == false then415        'false'416      else if v == null then417        'null'418      else if std.isNumber(v) then419        '' + v420      else if std.isString(v) then421        local len = std.length(v);422        if len == 0 then423          '""'424        else if v[len - 1] == '\n' then425          local split = std.split(v, '\n');426          std.join('\n' + cindent + '  ', ['|'] + split[0:std.length(split) - 1])427        else428          std.escapeStringJson(v)429      else if std.isFunction(v) then430        error 'Tried to manifest function at ' + path431      else if std.isArray(v) then432        if std.length(v) == 0 then433          '[]'434        else435          local params(value) =436            if std.isArray(value) && std.length(value) > 0 then {437              // While we could avoid the new line, it yields YAML that is438              // hard to read, e.g.:439              // - - - 1440              //     - 2441              //   - - 3442              //     - 4443              new_indent: cindent + '  ',444              space: '\n' + self.new_indent,445            } else if std.isObject(value) && std.length(value) > 0 then {446              new_indent: cindent + '  ',447              // In this case we can start on the same line as the - because the indentation448              // matches up then.  The converse is not true, because fields are not always449              // 1 character long.450              space: ' ',451            } else {452              // In this case, new_indent is only used in the case of multi-line strings.453              new_indent: cindent,454              space: ' ',455            };456          local range = std.range(0, std.length(v) - 1);457          local parts = [458            '-' + param.space + aux(v[i], path + [i], param.new_indent)459            for i in range460            for param in [params(v[i])]461          ];462          std.join('\n' + cindent, parts)463      else if std.isObject(v) then464        if std.length(v) == 0 then465          '{}'466        else467          local params(value) =468            if std.isArray(value) && std.length(value) > 0 then {469              // Not indenting allows e.g.470              // ports:471              // - 80472              // instead of473              // ports:474              //   - 80475              new_indent: if indent_array_in_object then cindent + '  ' else cindent,476              space: '\n' + self.new_indent,477            } else if std.isObject(value) && std.length(value) > 0 then {478              new_indent: cindent + '  ',479              space: '\n' + self.new_indent,480            } else {481              // In this case, new_indent is only used in the case of multi-line strings.482              new_indent: cindent,483              space: ' ',484            };485          local lines = [486            std.escapeStringJson(k) + ':' + param.space + aux(v[k], path + [k], param.new_indent)487            for k in std.objectFields(v)488            for param in [params(v[k])]489          ];490          std.join('\n' + cindent, lines);491    aux(value, [], ''),492493  manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::494    if !std.isArray(value) then495      error 'manifestYamlStream only takes arrays, got ' + std.type(value)496    else497      '---\n' + std.join(498        '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]499      ) + if c_document_end then '\n...\n' else '\n',500501502  manifestPython(v)::503    if std.isObject(v) then504      local fields = [505        '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]506        for k in std.objectFields(v)507      ];508      '{%s}' % [std.join(', ', fields)]509    else if std.isArray(v) then510      '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]511    else if std.isString(v) then512      '%s' % [std.escapeStringPython(v)]513    else if std.isFunction(v) then514      error 'cannot manifest function'515    else if std.isNumber(v) then516      std.toString(v)517    else if v == true then518      'True'519    else if v == false then520      'False'521    else if v == null then522      'None',523524  manifestPythonVars(conf)::525    local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];526    std.join('\n', vars + ['']),527528  manifestXmlJsonml(value)::529    if !std.isArray(value) then530      error 'Expected a JSONML value (an array), got %s' % std.type(value)531    else532      local aux(v) =533        if std.isString(v) then534          v535        else536          local tag = v[0];537          local has_attrs = std.length(v) > 1 && std.isObject(v[1]);538          local attrs = if has_attrs then v[1] else {};539          local children = if has_attrs then v[2:] else v[1:];540          local attrs_str =541            std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);542          std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);543544      aux(value),545546  local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',547  local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },548549  base64:: $intrinsic(base64),550551  base64DecodeBytes(str)::552    if std.length(str) % 4 != 0 then553      error 'Not a base64 encoded string "%s"' % str554    else555      local aux(str, i, r) =556        if i >= std.length(str) then557          r558        else559          // all 6 bits of i, 2 MSB of i+1560          local n1 = [base64_inv[str[i]] << 2 | (base64_inv[str[i + 1]] >> 4)];561          // 4 LSB of i+1, 4MSB of i+2562          local n2 =563            if str[i + 2] == '=' then []564            else [(base64_inv[str[i + 1]] & 15) << 4 | (base64_inv[str[i + 2]] >> 2)];565          // 2 LSB of i+2, all 6 bits of i+3566          local n3 =567            if str[i + 3] == '=' then []568            else [(base64_inv[str[i + 2]] & 3) << 6 | base64_inv[str[i + 3]]];569          aux(str, i + 4, r + n1 + n2 + n3) tailstrict;570      aux(str, 0, []),571572  base64Decode(str)::573    local bytes = std.base64DecodeBytes(str);574    std.join('', std.map(function(b) std.char(b), bytes)),575576  reverse:: $intrinsic(reverse),577578  sortImpl:: $intrinsic(sortImpl),579580  sort(arr, keyF=id)::581    std.sortImpl(arr, keyF),582583  uniq(arr, keyF=id)::584    local f(a, b) =585      if std.length(a) == 0 then586        [b]587      else if keyF(a[std.length(a) - 1]) == keyF(b) then588        a589      else590        a + [b];591    std.foldl(f, arr, []),592593  set(arr, keyF=id)::594    std.uniq(std.sort(arr, keyF), keyF),595596  setMember(x, arr, keyF=id)::597    // TODO(dcunnin): Binary chop for O(log n) complexity598    std.length(std.setInter([x], arr, keyF)) > 0,599600  setUnion(a, b, keyF=id)::601    // NOTE: order matters, values in `a` win602    local aux(a, b, i, j, acc) =603      if i >= std.length(a) then604        acc + b[j:]605      else if j >= std.length(b) then606        acc + a[i:]607      else608        local ak = keyF(a[i]);609        local bk = keyF(b[j]);610        if ak == bk then611          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict612        else if ak < bk then613          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict614        else615          aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;616    aux(a, b, 0, 0, []),617618  setInter(a, b, keyF=id)::619    local aux(a, b, i, j, acc) =620      if i >= std.length(a) || j >= std.length(b) then621        acc622      else623        if keyF(a[i]) == keyF(b[j]) then624          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict625        else if keyF(a[i]) < keyF(b[j]) then626          aux(a, b, i + 1, j, acc) tailstrict627        else628          aux(a, b, i, j + 1, acc) tailstrict;629    aux(a, b, 0, 0, []) tailstrict,630631  setDiff(a, b, keyF=id)::632    local aux(a, b, i, j, acc) =633      if i >= std.length(a) then634        acc635      else if j >= std.length(b) then636        acc + a[i:]637      else638        if keyF(a[i]) == keyF(b[j]) then639          aux(a, b, i + 1, j + 1, acc) tailstrict640        else if keyF(a[i]) < keyF(b[j]) then641          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict642        else643          aux(a, b, i, j + 1, acc) tailstrict;644    aux(a, b, 0, 0, []) tailstrict,645646  mergePatch(target, patch)::647    if std.isObject(patch) then648      local target_object =649        if std.isObject(target) then target else {};650651      local target_fields =652        if std.isObject(target_object) then std.objectFields(target_object) else [];653654      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];655      local both_fields = std.setUnion(target_fields, std.objectFields(patch));656657      {658        [k]:659          if !std.objectHas(patch, k) then660            target_object[k]661          else if !std.objectHas(target_object, k) then662            std.mergePatch(null, patch[k]) tailstrict663          else664            std.mergePatch(target_object[k], patch[k]) tailstrict665        for k in std.setDiff(both_fields, null_fields)666      }667    else668      patch,669670  objectFields(o)::671    std.objectFieldsEx(o, false),672673  objectFieldsAll(o)::674    std.objectFieldsEx(o, true),675676  objectHas(o, f)::677    std.objectHasEx(o, f, false),678679  objectHasAll(o, f)::680    std.objectHasEx(o, f, true),681682  objectValues(o)::683    [o[k] for k in std.objectFields(o)],684685  objectValuesAll(o)::686    [o[k] for k in std.objectFieldsAll(o)],687688  equals:: $intrinsic(equals),689690  resolvePath(f, r)::691    local arr = std.split(f, '/');692    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),693694  prune(a)::695    local isContent(b) =696      if b == null then697        false698      else if std.isArray(b) then699        std.length(b) > 0700      else if std.isObject(b) then701        std.length(b) > 0702      else703        true;704    if std.isArray(a) then705      [std.prune(x) for x in a if isContent($.prune(x))]706    else if std.isObject(a) then {707      [x]: $.prune(a[x])708      for x in std.objectFields(a)709      if isContent(std.prune(a[x]))710    } else711      a,712713  findSubstr(pat, str)::714    if !std.isString(pat) then715      error 'findSubstr first parameter should be a string, got ' + std.type(pat)716    else if !std.isString(str) then717      error 'findSubstr second parameter should be a string, got ' + std.type(str)718    else719      local pat_len = std.length(pat);720      local str_len = std.length(str);721      if pat_len == 0 || str_len == 0 || pat_len > str_len then722        []723      else724        std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),725726  find(value, arr)::727    if !std.isArray(arr) then728      error 'find second parameter should be an array, got ' + std.type(arr)729    else730      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),731}
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(str, from, len)::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])),5758  startsWith(a, b)::59    if std.length(a) < std.length(b) then60      false61    else62      std.substr(a, 0, std.length(b)) == b,6364  endsWith(a, b)::65    if std.length(a) < std.length(b) then66      false67    else68      std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,6970  lstripChars(str, chars)::71    if std.length(str) > 0 && std.member(chars, str[0]) then72      std.lstripChars(str[1:], chars)73    else74      str,7576  rstripChars(str, chars)::77    local len = std.length(str);78    if len > 0 && std.member(chars, str[len - 1]) then79      std.rstripChars(str[:len - 1], chars)80    else81      str,8283  stripChars(str, chars)::84    std.lstripChars(std.rstripChars(str, chars), chars),8586  stringChars(str)::87    std.makeArray(std.length(str), function(i) str[i]),8889  local parse_nat(str, base) =90    assert base > 0 && base <= 16 : 'integer base %d invalid' % base;91    // These codepoints are in ascending order:92    local zero_code = std.codepoint('0');93    local upper_a_code = std.codepoint('A');94    local lower_a_code = std.codepoint('a');95    local addDigit(aggregate, char) =96      local code = std.codepoint(char);97      local digit = if code >= lower_a_code then98        code - lower_a_code + 1099      else if code >= upper_a_code then100        code - upper_a_code + 10101      else102        code - zero_code;103      assert digit >= 0 && digit < base : '%s is not a base %d integer' % [str, base];104      base * aggregate + digit;105    std.foldl(addDigit, std.stringChars(str), 0),106107  parseInt(str)::108    assert std.isString(str) : 'Expected string, got ' + std.type(str);109    assert std.length(str) > 0 && str != '-' : 'Not an integer: "%s"' % [str];110    if str[0] == '-' then111      -parse_nat(str[1:], 10)112    else113      parse_nat(str, 10),114115  parseOctal(str)::116    assert std.isString(str) : 'Expected string, got ' + std.type(str);117    assert std.length(str) > 0 : 'Not an octal number: ""';118    parse_nat(str, 8),119120  parseHex(str)::121    assert std.isString(str) : 'Expected string, got ' + std.type(str);122    assert std.length(str) > 0 : 'Not hexadecimal: ""';123    parse_nat(str, 16),124125  split(str, c)::126    assert std.isString(str) : 'std.split first parameter should be a string, got ' + std.type(str);127    assert std.isString(c) : 'std.split second parameter should be a string, got ' + std.type(c);128    assert std.length(c) == 1 : 'std.split second parameter should have length 1, got ' + std.length(c);129    std.splitLimit(str, c, -1),130131  splitLimit(str, c, maxsplits)::132    assert std.isString(str) : 'std.splitLimit first parameter should be a string, got ' + std.type(str);133    assert std.isString(c) : 'std.splitLimit second parameter should be a string, got ' + std.type(c);134    assert std.length(c) == 1 : 'std.splitLimit second parameter should have length 1, got ' + std.length(c);135    assert std.isNumber(maxsplits) : 'std.splitLimit third parameter should be a number, got ' + std.type(maxsplits);136    local aux(str, delim, i, arr, v) =137      local c = str[i];138      local i2 = i + 1;139      if i >= std.length(str) then140        arr + [v]141      else if c == delim && (maxsplits == -1 || std.length(arr) < maxsplits) then142        aux(str, delim, i2, arr + [v], '') tailstrict143      else144        aux(str, delim, i2, arr, v + c) tailstrict;145    aux(str, c, 0, [], ''),146147  strReplace:: $intrinsic(strReplace),148149  asciiUpper(str)::150    local cp = std.codepoint;151    local up_letter(c) = if cp(c) >= 97 && cp(c) < 123 then152      std.char(cp(c) - 32)153    else154      c;155    std.join('', std.map(up_letter, std.stringChars(str))),156157  asciiLower(str)::158    local cp = std.codepoint;159    local down_letter(c) = if cp(c) >= 65 && cp(c) < 91 then160      std.char(cp(c) + 32)161    else162      c;163    std.join('', std.map(down_letter, std.stringChars(str))),164165  range:: $intrinsic(range),166167  repeat(what, count)::168    local joiner =169      if std.isString(what) then ''170      else if std.isArray(what) then []171      else error 'std.repeat first argument must be an array or a string';172    std.join(joiner, std.makeArray(count, function(i) what)),173174  slice:: $intrinsic(slice),175176  member(arr, x)::177    if std.isArray(arr) then178      std.count(arr, x) > 0179    else if std.isString(arr) then180      std.length(std.findSubstr(x, arr)) > 0181    else error 'std.member first argument must be an array or a string',182183  count(arr, x):: std.length(std.filter(function(v) v == x, arr)),184185  mod:: $intrinsic(mod),186187  map:: $intrinsic(map),188189  mapWithIndex(func, arr)::190    if !std.isFunction(func) then191      error ('std.mapWithIndex first param must be function, got ' + std.type(func))192    else if !std.isArray(arr) && !std.isString(arr) then193      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))194    else195      std.makeArray(std.length(arr), function(i) func(i, arr[i])),196197  mapWithKey(func, obj)::198    if !std.isFunction(func) then199      error ('std.mapWithKey first param must be function, got ' + std.type(func))200    else if !std.isObject(obj) then201      error ('std.mapWithKey second param must be object, got ' + std.type(obj))202    else203      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },204205  flatMap:: $intrinsic(flatMap),206207  join:: $intrinsic(join),208209  lines(arr)::210    std.join('\n', arr + ['']),211212  deepJoin(arr)::213    if std.isString(arr) then214      arr215    else if std.isArray(arr) then216      std.join('', [std.deepJoin(x) for x in arr])217    else218      error 'Expected string or array, got %s' % std.type(arr),219220221  format:: $intrinsic(format),222223  foldr:: $intrinsic(foldr),224225  foldl:: $intrinsic(foldl),226227  filterMap(filter_func, map_func, arr)::228    if !std.isFunction(filter_func) then229      error ('std.filterMap first param must be function, got ' + std.type(filter_func))230    else if !std.isFunction(map_func) then231      error ('std.filterMap second param must be function, got ' + std.type(map_func))232    else if !std.isArray(arr) then233      error ('std.filterMap third param must be array, got ' + std.type(arr))234    else235      std.map(map_func, std.filter(filter_func, arr)),236237  assertEqual(a, b)::238    if a == b then239      true240    else241      error 'Assertion failed. ' + a + ' != ' + b,242243  abs(n)::244    if !std.isNumber(n) then245      error 'std.abs expected number, got ' + std.type(n)246    else247      if n > 0 then n else -n,248249  sign(n)::250    if !std.isNumber(n) then251      error 'std.sign expected number, got ' + std.type(n)252    else253      if n > 0 then254        1255      else if n < 0 then256        -1257      else 0,258259  max(a, b)::260    if !std.isNumber(a) then261      error 'std.max first param expected number, got ' + std.type(a)262    else if !std.isNumber(b) then263      error 'std.max second param expected number, got ' + std.type(b)264    else265      if a > b then a else b,266267  min(a, b)::268    if !std.isNumber(a) then269      error 'std.min first param expected number, got ' + std.type(a)270    else if !std.isNumber(b) then271      error 'std.min second param expected number, got ' + std.type(b)272    else273      if a < b then a else b,274275  clamp(x, minVal, maxVal)::276    if x < minVal then minVal277    else if x > maxVal then maxVal278    else x,279280  flattenArrays(arrs)::281    std.foldl(function(a, b) a + b, arrs, []),282283  manifestIni(ini)::284    local body_lines(body) =285      std.join([], [286        local value_or_values = body[k];287        if std.isArray(value_or_values) then288          ['%s = %s' % [k, value] for value in value_or_values]289        else290          ['%s = %s' % [k, value_or_values]]291292        for k in std.objectFields(body)293      ]);294295    local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),296          main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],297          all_sections = [298      section_lines(k, ini.sections[k])299      for k in std.objectFields(ini.sections)300    ];301    std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),302303  manifestToml(value):: std.manifestTomlEx(value, '  '),304305  manifestTomlEx(value, indent)::306    local307      escapeStringToml = std.escapeStringJson,308      escapeKeyToml(key) =309        local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));310        if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),311      isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),312      isSection(v) = std.isObject(v) || isTableArray(v),313      renderValue(v, indexedPath, inline, cindent) =314        if v == true then315          'true'316        else if v == false then317          'false'318        else if v == null then319          error 'Tried to manifest "null" at ' + indexedPath320        else if std.isNumber(v) then321          '' + v322        else if std.isString(v) then323          escapeStringToml(v)324        else if std.isFunction(v) then325          error 'Tried to manifest function at ' + indexedPath326        else if std.isArray(v) then327          if std.length(v) == 0 then328            '[]'329          else330            local range = std.range(0, std.length(v) - 1);331            local new_indent = if inline then '' else cindent + indent;332            local separator = if inline then ' ' else '\n';333            local lines = ['[' + separator]334                          + std.join([',' + separator],335                                     [336                                       [new_indent + renderValue(v[i], indexedPath + [i], true, '')]337                                       for i in range338                                     ])339                          + [separator + (if inline then '' else cindent) + ']'];340            std.join('', lines)341        else if std.isObject(v) then342          local lines = ['{ ']343                        + std.join([', '],344                                   [345                                     [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]346                                     for k in std.objectFields(v)347                                   ])348                        + [' }'];349          std.join('', lines),350      renderTableInternal(v, path, indexedPath, cindent) =351        local kvp = std.flattenArrays([352          [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]353          for k in std.objectFields(v)354          if !isSection(v[k])355        ]);356        local sections = [std.join('\n', kvp)] + [357          (358            if std.isObject(v[k]) then359              renderTable(v[k], path + [k], indexedPath + [k], cindent)360            else361              renderTableArray(v[k], path + [k], indexedPath + [k], cindent)362          )363          for k in std.objectFields(v)364          if isSection(v[k])365        ];366        std.join('\n\n', sections),367      renderTable(v, path, indexedPath, cindent) =368        cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'369        + (if v == {} then '' else '\n')370        + renderTableInternal(v, path, indexedPath, cindent + indent),371      renderTableArray(v, path, indexedPath, cindent) =372        local range = std.range(0, std.length(v) - 1);373        local sections = [374          (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'375           + (if v[i] == {} then '' else '\n')376           + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))377          for i in range378        ];379        std.join('\n\n', sections);380    if std.isObject(value) then381      renderTableInternal(value, [], [], '')382    else383      error 'TOML body must be an object. Got ' + std.type(value),384385  escapeStringJson:: $intrinsic(escapeStringJson),386387  escapeStringPython(str)::388    std.escapeStringJson(str),389390  escapeStringBash(str_)::391    local str = std.toString(str_);392    local trans(ch) =393      if ch == "'" then394        "'\"'\"'"395      else396        ch;397    "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),398399  escapeStringDollars(str_)::400    local str = std.toString(str_);401    local trans(ch) =402      if ch == '$' then403        '$$'404      else405        ch;406    std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),407408  manifestJson(value):: std.manifestJsonEx(value, '    '),409410  manifestJsonEx:: $intrinsic(manifestJsonEx),411412  manifestYamlDoc(value, indent_array_in_object=false)::413    local aux(v, path, cindent) =414      if v == true then415        'true'416      else if v == false then417        'false'418      else if v == null then419        'null'420      else if std.isNumber(v) then421        '' + v422      else if std.isString(v) then423        local len = std.length(v);424        if len == 0 then425          '""'426        else if v[len - 1] == '\n' then427          local split = std.split(v, '\n');428          std.join('\n' + cindent + '  ', ['|'] + split[0:std.length(split) - 1])429        else430          std.escapeStringJson(v)431      else if std.isFunction(v) then432        error 'Tried to manifest function at ' + path433      else if std.isArray(v) then434        if std.length(v) == 0 then435          '[]'436        else437          local params(value) =438            if std.isArray(value) && std.length(value) > 0 then {439              // While we could avoid the new line, it yields YAML that is440              // hard to read, e.g.:441              // - - - 1442              //     - 2443              //   - - 3444              //     - 4445              new_indent: cindent + '  ',446              space: '\n' + self.new_indent,447            } else if std.isObject(value) && std.length(value) > 0 then {448              new_indent: cindent + '  ',449              // In this case we can start on the same line as the - because the indentation450              // matches up then.  The converse is not true, because fields are not always451              // 1 character long.452              space: ' ',453            } else {454              // In this case, new_indent is only used in the case of multi-line strings.455              new_indent: cindent,456              space: ' ',457            };458          local range = std.range(0, std.length(v) - 1);459          local parts = [460            '-' + param.space + aux(v[i], path + [i], param.new_indent)461            for i in range462            for param in [params(v[i])]463          ];464          std.join('\n' + cindent, parts)465      else if std.isObject(v) then466        if std.length(v) == 0 then467          '{}'468        else469          local params(value) =470            if std.isArray(value) && std.length(value) > 0 then {471              // Not indenting allows e.g.472              // ports:473              // - 80474              // instead of475              // ports:476              //   - 80477              new_indent: if indent_array_in_object then cindent + '  ' else cindent,478              space: '\n' + self.new_indent,479            } else if std.isObject(value) && std.length(value) > 0 then {480              new_indent: cindent + '  ',481              space: '\n' + self.new_indent,482            } else {483              // In this case, new_indent is only used in the case of multi-line strings.484              new_indent: cindent,485              space: ' ',486            };487          local lines = [488            std.escapeStringJson(k) + ':' + param.space + aux(v[k], path + [k], param.new_indent)489            for k in std.objectFields(v)490            for param in [params(v[k])]491          ];492          std.join('\n' + cindent, lines);493    aux(value, [], ''),494495  manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::496    if !std.isArray(value) then497      error 'manifestYamlStream only takes arrays, got ' + std.type(value)498    else499      '---\n' + std.join(500        '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]501      ) + if c_document_end then '\n...\n' else '\n',502503504  manifestPython(v)::505    if std.isObject(v) then506      local fields = [507        '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]508        for k in std.objectFields(v)509      ];510      '{%s}' % [std.join(', ', fields)]511    else if std.isArray(v) then512      '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]513    else if std.isString(v) then514      '%s' % [std.escapeStringPython(v)]515    else if std.isFunction(v) then516      error 'cannot manifest function'517    else if std.isNumber(v) then518      std.toString(v)519    else if v == true then520      'True'521    else if v == false then522      'False'523    else if v == null then524      'None',525526  manifestPythonVars(conf)::527    local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];528    std.join('\n', vars + ['']),529530  manifestXmlJsonml(value)::531    if !std.isArray(value) then532      error 'Expected a JSONML value (an array), got %s' % std.type(value)533    else534      local aux(v) =535        if std.isString(v) then536          v537        else538          local tag = v[0];539          local has_attrs = std.length(v) > 1 && std.isObject(v[1]);540          local attrs = if has_attrs then v[1] else {};541          local children = if has_attrs then v[2:] else v[1:];542          local attrs_str =543            std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);544          std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);545546      aux(value),547548  local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',549  local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },550551  base64:: $intrinsic(base64),552553  base64DecodeBytes(str)::554    if std.length(str) % 4 != 0 then555      error 'Not a base64 encoded string "%s"' % str556    else557      local aux(str, i, r) =558        if i >= std.length(str) then559          r560        else561          // all 6 bits of i, 2 MSB of i+1562          local n1 = [base64_inv[str[i]] << 2 | (base64_inv[str[i + 1]] >> 4)];563          // 4 LSB of i+1, 4MSB of i+2564          local n2 =565            if str[i + 2] == '=' then []566            else [(base64_inv[str[i + 1]] & 15) << 4 | (base64_inv[str[i + 2]] >> 2)];567          // 2 LSB of i+2, all 6 bits of i+3568          local n3 =569            if str[i + 3] == '=' then []570            else [(base64_inv[str[i + 2]] & 3) << 6 | base64_inv[str[i + 3]]];571          aux(str, i + 4, r + n1 + n2 + n3) tailstrict;572      aux(str, 0, []),573574  base64Decode(str)::575    local bytes = std.base64DecodeBytes(str);576    std.join('', std.map(function(b) std.char(b), bytes)),577578  reverse:: $intrinsic(reverse),579580  sortImpl:: $intrinsic(sortImpl),581582  sort(arr, keyF=id)::583    std.sortImpl(arr, keyF),584585  uniq(arr, keyF=id)::586    local f(a, b) =587      if std.length(a) == 0 then588        [b]589      else if keyF(a[std.length(a) - 1]) == keyF(b) then590        a591      else592        a + [b];593    std.foldl(f, arr, []),594595  set(arr, keyF=id)::596    std.uniq(std.sort(arr, keyF), keyF),597598  setMember(x, arr, keyF=id)::599    // TODO(dcunnin): Binary chop for O(log n) complexity600    std.length(std.setInter([x], arr, keyF)) > 0,601602  setUnion(a, b, keyF=id)::603    // NOTE: order matters, values in `a` win604    local aux(a, b, i, j, acc) =605      if i >= std.length(a) then606        acc + b[j:]607      else if j >= std.length(b) then608        acc + a[i:]609      else610        local ak = keyF(a[i]);611        local bk = keyF(b[j]);612        if ak == bk then613          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict614        else if ak < bk then615          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict616        else617          aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;618    aux(a, b, 0, 0, []),619620  setInter(a, b, keyF=id)::621    local aux(a, b, i, j, acc) =622      if i >= std.length(a) || j >= std.length(b) then623        acc624      else625        if keyF(a[i]) == keyF(b[j]) then626          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict627        else if keyF(a[i]) < keyF(b[j]) then628          aux(a, b, i + 1, j, acc) tailstrict629        else630          aux(a, b, i, j + 1, acc) tailstrict;631    aux(a, b, 0, 0, []) tailstrict,632633  setDiff(a, b, keyF=id)::634    local aux(a, b, i, j, acc) =635      if i >= std.length(a) then636        acc637      else if j >= std.length(b) then638        acc + a[i:]639      else640        if keyF(a[i]) == keyF(b[j]) then641          aux(a, b, i + 1, j + 1, acc) tailstrict642        else if keyF(a[i]) < keyF(b[j]) then643          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict644        else645          aux(a, b, i, j + 1, acc) tailstrict;646    aux(a, b, 0, 0, []) tailstrict,647648  mergePatch(target, patch)::649    if std.isObject(patch) then650      local target_object =651        if std.isObject(target) then target else {};652653      local target_fields =654        if std.isObject(target_object) then std.objectFields(target_object) else [];655656      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];657      local both_fields = std.setUnion(target_fields, std.objectFields(patch));658659      {660        [k]:661          if !std.objectHas(patch, k) then662            target_object[k]663          else if !std.objectHas(target_object, k) then664            std.mergePatch(null, patch[k]) tailstrict665          else666            std.mergePatch(target_object[k], patch[k]) tailstrict667        for k in std.setDiff(both_fields, null_fields)668      }669    else670      patch,671672  objectFields(o)::673    std.objectFieldsEx(o, false),674675  objectFieldsAll(o)::676    std.objectFieldsEx(o, true),677678  objectHas(o, f)::679    std.objectHasEx(o, f, false),680681  objectHasAll(o, f)::682    std.objectHasEx(o, f, true),683684  objectValues(o)::685    [o[k] for k in std.objectFields(o)],686687  objectValuesAll(o)::688    [o[k] for k in std.objectFieldsAll(o)],689690  equals:: $intrinsic(equals),691692  resolvePath(f, r)::693    local arr = std.split(f, '/');694    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),695696  prune(a)::697    local isContent(b) =698      if b == null then699        false700      else if std.isArray(b) then701        std.length(b) > 0702      else if std.isObject(b) then703        std.length(b) > 0704      else705        true;706    if std.isArray(a) then707      [std.prune(x) for x in a if isContent($.prune(x))]708    else if std.isObject(a) then {709      [x]: $.prune(a[x])710      for x in std.objectFields(a)711      if isContent(std.prune(a[x]))712    } else713      a,714715  findSubstr(pat, str)::716    if !std.isString(pat) then717      error 'findSubstr first parameter should be a string, got ' + std.type(pat)718    else if !std.isString(str) then719      error 'findSubstr second parameter should be a string, got ' + std.type(str)720    else721      local pat_len = std.length(pat);722      local str_len = std.length(str);723      if pat_len == 0 || str_len == 0 || pat_len > str_len then724        []725      else726        std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),727728  find(value, arr)::729    if !std.isArray(arr) then730      error 'find second parameter should be an array, got ' + std.type(arr)731    else732      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),733}