git.delta.rocks / jrsonnet / refs/commits / 46b4dd0e38ba

difftreelog

source

crates/jrsonnet-stdlib/src/std.jsonnet23.9 KiBsourcehistory
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  log:: $intrinsic(log),16  pow:: $intrinsic(pow),17  extVar:: $intrinsic(extVar),18  native:: $intrinsic(native),19  filter:: $intrinsic(filter),20  char:: $intrinsic(char),21  encodeUTF8:: $intrinsic(encodeUTF8),22  md5:: $intrinsic(md5),23  trace:: $intrinsic(trace),24  id:: $intrinsic(id),25  parseJson:: $intrinsic(parseJson),2627  isString(v):: std.type(v) == 'string',28  isNumber(v):: std.type(v) == 'number',29  isBoolean(v):: std.type(v) == 'boolean',30  isObject(v):: std.type(v) == 'object',31  isArray(v):: std.type(v) == 'array',32  isFunction(v):: std.type(v) == 'function',3334  toString(a)::35    if std.type(a) == 'string' then a else '' + a,3637  substr(str, from, len)::38    assert std.isString(str) : 'substr first parameter should be a string, got ' + std.type(str);39    assert std.isNumber(from) : 'substr second parameter should be a string, got ' + std.type(from);40    assert std.isNumber(len) : 'substr third parameter should be a string, got ' + std.type(len);41    assert len >= 0 : 'substr third parameter should be greater than zero, got ' + len;42    std.join('', std.makeArray(std.max(0, std.min(len, std.length(str) - from)), function(i) str[i + from])),4344  startsWith(a, b)::45    if std.length(a) < std.length(b) then46      false47    else48      std.substr(a, 0, std.length(b)) == b,4950  endsWith(a, b)::51    if std.length(a) < std.length(b) then52      false53    else54      std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,5556  lstripChars(str, chars)::57    if std.length(str) > 0 && std.member(chars, str[0]) then58      std.lstripChars(str[1:], chars)59    else60      str,6162  rstripChars(str, chars)::63    local len = std.length(str);64    if len > 0 && std.member(chars, str[len - 1]) then65      std.rstripChars(str[:len - 1], chars)66    else67      str,6869  stripChars(str, chars)::70    std.lstripChars(std.rstripChars(str, chars), chars),7172  stringChars(str)::73    std.makeArray(std.length(str), function(i) str[i]),7475  local parse_nat(str, base) =76    assert base > 0 && base <= 16 : 'integer base %d invalid' % base;77    // These codepoints are in ascending order:78    local zero_code = std.codepoint('0');79    local upper_a_code = std.codepoint('A');80    local lower_a_code = std.codepoint('a');81    local addDigit(aggregate, char) =82      local code = std.codepoint(char);83      local digit = if code >= lower_a_code then84        code - lower_a_code + 1085      else if code >= upper_a_code then86        code - upper_a_code + 1087      else88        code - zero_code;89      assert digit >= 0 && digit < base : '%s is not a base %d integer' % [str, base];90      base * aggregate + digit;91    std.foldl(addDigit, std.stringChars(str), 0),9293  parseInt(str)::94    assert std.isString(str) : 'Expected string, got ' + std.type(str);95    assert std.length(str) > 0 && str != '-' : 'Not an integer: "%s"' % [str];96    if str[0] == '-' then97      -parse_nat(str[1:], 10)98    else99      parse_nat(str, 10),100101  parseOctal(str)::102    assert std.isString(str) : 'Expected string, got ' + std.type(str);103    assert std.length(str) > 0 : 'Not an octal number: ""';104    parse_nat(str, 8),105106  parseHex(str)::107    assert std.isString(str) : 'Expected string, got ' + std.type(str);108    assert std.length(str) > 0 : 'Not hexadecimal: ""';109    parse_nat(str, 16),110111  split(str, c)::112    assert std.isString(str) : 'std.split first parameter should be a string, got ' + std.type(str);113    assert std.isString(c) : 'std.split second parameter should be a string, got ' + std.type(c);114    assert std.length(c) == 1 : 'std.split second parameter should have length 1, got ' + std.length(c);115    std.splitLimit(str, c, -1),116117  splitLimit(str, c, maxsplits)::118    assert std.isString(str) : 'std.splitLimit first parameter should be a string, got ' + std.type(str);119    assert std.isString(c) : 'std.splitLimit second parameter should be a string, got ' + std.type(c);120    assert std.length(c) == 1 : 'std.splitLimit second parameter should have length 1, got ' + std.length(c);121    assert std.isNumber(maxsplits) : 'std.splitLimit third parameter should be a number, got ' + std.type(maxsplits);122    local aux(str, delim, i, arr, v) =123      local c = str[i];124      local i2 = i + 1;125      if i >= std.length(str) then126        arr + [v]127      else if c == delim && (maxsplits == -1 || std.length(arr) < maxsplits) then128        aux(str, delim, i2, arr + [v], '') tailstrict129      else130        aux(str, delim, i2, arr, v + c) tailstrict;131    aux(str, c, 0, [], ''),132133  strReplace:: $intrinsic(strReplace),134135  asciiUpper(str)::136    local cp = std.codepoint;137    local up_letter(c) = if cp(c) >= 97 && cp(c) < 123 then138      std.char(cp(c) - 32)139    else140      c;141    std.join('', std.map(up_letter, std.stringChars(str))),142143  asciiLower(str)::144    local cp = std.codepoint;145    local down_letter(c) = if cp(c) >= 65 && cp(c) < 91 then146      std.char(cp(c) + 32)147    else148      c;149    std.join('', std.map(down_letter, std.stringChars(str))),150151  range:: $intrinsic(range),152153  repeat(what, count)::154    local joiner =155      if std.isString(what) then ''156      else if std.isArray(what) then []157      else error 'std.repeat first argument must be an array or a string';158    std.join(joiner, std.makeArray(count, function(i) what)),159160  slice:: $intrinsic(slice),161162  member(arr, x)::163    if std.isArray(arr) then164      std.count(arr, x) > 0165    else if std.isString(arr) then166      std.length(std.findSubstr(x, arr)) > 0167    else error 'std.member first argument must be an array or a string',168169  count(arr, x):: std.length(std.filter(function(v) v == x, arr)),170171  mod:: $intrinsic(mod),172173  map:: $intrinsic(map),174175  mapWithIndex(func, arr)::176    if !std.isFunction(func) then177      error ('std.mapWithIndex first param must be function, got ' + std.type(func))178    else if !std.isArray(arr) && !std.isString(arr) then179      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))180    else181      std.makeArray(std.length(arr), function(i) func(i, arr[i])),182183  mapWithKey(func, obj)::184    if !std.isFunction(func) then185      error ('std.mapWithKey first param must be function, got ' + std.type(func))186    else if !std.isObject(obj) then187      error ('std.mapWithKey second param must be object, got ' + std.type(obj))188    else189      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },190191  flatMap:: $intrinsic(flatMap),192193  join:: $intrinsic(join),194195  lines(arr)::196    std.join('\n', arr + ['']),197198  deepJoin(arr)::199    if std.isString(arr) then200      arr201    else if std.isArray(arr) then202      std.join('', [std.deepJoin(x) for x in arr])203    else204      error 'Expected string or array, got %s' % std.type(arr),205206207  format:: $intrinsic(format),208209  foldr:: $intrinsic(foldr),210211  foldl:: $intrinsic(foldl),212213  filterMap(filter_func, map_func, arr)::214    if !std.isFunction(filter_func) then215      error ('std.filterMap first param must be function, got ' + std.type(filter_func))216    else if !std.isFunction(map_func) then217      error ('std.filterMap second param must be function, got ' + std.type(map_func))218    else if !std.isArray(arr) then219      error ('std.filterMap third param must be array, got ' + std.type(arr))220    else221      std.map(map_func, std.filter(filter_func, arr)),222223  assertEqual(a, b)::224    if a == b then225      true226    else227      error 'Assertion failed. ' + a + ' != ' + b,228229  abs(n)::230    if !std.isNumber(n) then231      error 'std.abs expected number, got ' + std.type(n)232    else233      if n > 0 then n else -n,234235  sign(n)::236    if !std.isNumber(n) then237      error 'std.sign expected number, got ' + std.type(n)238    else239      if n > 0 then240        1241      else if n < 0 then242        -1243      else 0,244245  max(a, b)::246    if !std.isNumber(a) then247      error 'std.max first param expected number, got ' + std.type(a)248    else if !std.isNumber(b) then249      error 'std.max second param expected number, got ' + std.type(b)250    else251      if a > b then a else b,252253  min(a, b)::254    if !std.isNumber(a) then255      error 'std.min first param expected number, got ' + std.type(a)256    else if !std.isNumber(b) then257      error 'std.min second param expected number, got ' + std.type(b)258    else259      if a < b then a else b,260261  clamp(x, minVal, maxVal)::262    if x < minVal then minVal263    else if x > maxVal then maxVal264    else x,265266  flattenArrays(arrs)::267    std.foldl(function(a, b) a + b, arrs, []),268269  manifestIni(ini)::270    local body_lines(body) =271      std.join([], [272        local value_or_values = body[k];273        if std.isArray(value_or_values) then274          ['%s = %s' % [k, value] for value in value_or_values]275        else276          ['%s = %s' % [k, value_or_values]]277278        for k in std.objectFields(body)279      ]);280281    local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),282          main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],283          all_sections = [284      section_lines(k, ini.sections[k])285      for k in std.objectFields(ini.sections)286    ];287    std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),288289  manifestToml(value):: std.manifestTomlEx(value, '  '),290291  manifestTomlEx(value, indent)::292    local293      escapeStringToml = std.escapeStringJson,294      escapeKeyToml(key) =295        local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));296        if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),297      isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),298      isSection(v) = std.isObject(v) || isTableArray(v),299      renderValue(v, indexedPath, inline, cindent) =300        if v == true then301          'true'302        else if v == false then303          'false'304        else if v == null then305          error 'Tried to manifest "null" at ' + indexedPath306        else if std.isNumber(v) then307          '' + v308        else if std.isString(v) then309          escapeStringToml(v)310        else if std.isFunction(v) then311          error 'Tried to manifest function at ' + indexedPath312        else if std.isArray(v) then313          if std.length(v) == 0 then314            '[]'315          else316            local range = std.range(0, std.length(v) - 1);317            local new_indent = if inline then '' else cindent + indent;318            local separator = if inline then ' ' else '\n';319            local lines = ['[' + separator]320                          + std.join([',' + separator],321                                     [322                                       [new_indent + renderValue(v[i], indexedPath + [i], true, '')]323                                       for i in range324                                     ])325                          + [separator + (if inline then '' else cindent) + ']'];326            std.join('', lines)327        else if std.isObject(v) then328          local lines = ['{ ']329                        + std.join([', '],330                                   [331                                     [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]332                                     for k in std.objectFields(v)333                                   ])334                        + [' }'];335          std.join('', lines),336      renderTableInternal(v, path, indexedPath, cindent) =337        local kvp = std.flattenArrays([338          [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]339          for k in std.objectFields(v)340          if !isSection(v[k])341        ]);342        local sections = [std.join('\n', kvp)] + [343          (344            if std.isObject(v[k]) then345              renderTable(v[k], path + [k], indexedPath + [k], cindent)346            else347              renderTableArray(v[k], path + [k], indexedPath + [k], cindent)348          )349          for k in std.objectFields(v)350          if isSection(v[k])351        ];352        std.join('\n\n', sections),353      renderTable(v, path, indexedPath, cindent) =354        cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'355        + (if v == {} then '' else '\n')356        + renderTableInternal(v, path, indexedPath, cindent + indent),357      renderTableArray(v, path, indexedPath, cindent) =358        local range = std.range(0, std.length(v) - 1);359        local sections = [360          (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'361           + (if v[i] == {} then '' else '\n')362           + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))363          for i in range364        ];365        std.join('\n\n', sections);366    if std.isObject(value) then367      renderTableInternal(value, [], [], '')368    else369      error 'TOML body must be an object. Got ' + std.type(value),370371  escapeStringJson:: $intrinsic(escapeStringJson),372373  escapeStringPython(str)::374    std.escapeStringJson(str),375376  escapeStringBash(str_)::377    local str = std.toString(str_);378    local trans(ch) =379      if ch == "'" then380        "'\"'\"'"381      else382        ch;383    "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),384385  escapeStringDollars(str_)::386    local str = std.toString(str_);387    local trans(ch) =388      if ch == '$' then389        '$$'390      else391        ch;392    std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),393394  manifestJson(value):: std.manifestJsonEx(value, '    '),395396  manifestJsonEx:: $intrinsic(manifestJsonEx),397398  manifestYamlDoc(value, indent_array_in_object=false)::399    local aux(v, path, cindent) =400      if v == true then401        'true'402      else if v == false then403        'false'404      else if v == null then405        'null'406      else if std.isNumber(v) then407        '' + v408      else if std.isString(v) then409        local len = std.length(v);410        if len == 0 then411          '""'412        else if v[len - 1] == '\n' then413          local split = std.split(v, '\n');414          std.join('\n' + cindent + '  ', ['|'] + split[0:std.length(split) - 1])415        else416          std.escapeStringJson(v)417      else if std.isFunction(v) then418        error 'Tried to manifest function at ' + path419      else if std.isArray(v) then420        if std.length(v) == 0 then421          '[]'422        else423          local params(value) =424            if std.isArray(value) && std.length(value) > 0 then {425              // While we could avoid the new line, it yields YAML that is426              // hard to read, e.g.:427              // - - - 1428              //     - 2429              //   - - 3430              //     - 4431              new_indent: cindent + '  ',432              space: '\n' + self.new_indent,433            } else if std.isObject(value) && std.length(value) > 0 then {434              new_indent: cindent + '  ',435              // In this case we can start on the same line as the - because the indentation436              // matches up then.  The converse is not true, because fields are not always437              // 1 character long.438              space: ' ',439            } else {440              // In this case, new_indent is only used in the case of multi-line strings.441              new_indent: cindent,442              space: ' ',443            };444          local range = std.range(0, std.length(v) - 1);445          local parts = [446            '-' + param.space + aux(v[i], path + [i], param.new_indent)447            for i in range448            for param in [params(v[i])]449          ];450          std.join('\n' + cindent, parts)451      else if std.isObject(v) then452        if std.length(v) == 0 then453          '{}'454        else455          local params(value) =456            if std.isArray(value) && std.length(value) > 0 then {457              // Not indenting allows e.g.458              // ports:459              // - 80460              // instead of461              // ports:462              //   - 80463              new_indent: if indent_array_in_object then cindent + '  ' else cindent,464              space: '\n' + self.new_indent,465            } else if std.isObject(value) && std.length(value) > 0 then {466              new_indent: cindent + '  ',467              space: '\n' + self.new_indent,468            } else {469              // In this case, new_indent is only used in the case of multi-line strings.470              new_indent: cindent,471              space: ' ',472            };473          local lines = [474            std.escapeStringJson(k) + ':' + param.space + aux(v[k], path + [k], param.new_indent)475            for k in std.objectFields(v)476            for param in [params(v[k])]477          ];478          std.join('\n' + cindent, lines);479    aux(value, [], ''),480481  manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::482    if !std.isArray(value) then483      error 'manifestYamlStream only takes arrays, got ' + std.type(value)484    else485      '---\n' + std.join(486        '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]487      ) + if c_document_end then '\n...\n' else '\n',488489490  manifestPython(v)::491    if std.isObject(v) then492      local fields = [493        '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]494        for k in std.objectFields(v)495      ];496      '{%s}' % [std.join(', ', fields)]497    else if std.isArray(v) then498      '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]499    else if std.isString(v) then500      '%s' % [std.escapeStringPython(v)]501    else if std.isFunction(v) then502      error 'cannot manifest function'503    else if std.isNumber(v) then504      std.toString(v)505    else if v == true then506      'True'507    else if v == false then508      'False'509    else if v == null then510      'None',511512  manifestPythonVars(conf)::513    local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];514    std.join('\n', vars + ['']),515516  manifestXmlJsonml(value)::517    if !std.isArray(value) then518      error 'Expected a JSONML value (an array), got %s' % std.type(value)519    else520      local aux(v) =521        if std.isString(v) then522          v523        else524          local tag = v[0];525          local has_attrs = std.length(v) > 1 && std.isObject(v[1]);526          local attrs = if has_attrs then v[1] else {};527          local children = if has_attrs then v[2:] else v[1:];528          local attrs_str =529            std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);530          std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);531532      aux(value),533534  local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',535  local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },536537  base64:: $intrinsic(base64),538539  base64DecodeBytes(str)::540    if std.length(str) % 4 != 0 then541      error 'Not a base64 encoded string "%s"' % str542    else543      local aux(str, i, r) =544        if i >= std.length(str) then545          r546        else547          // all 6 bits of i, 2 MSB of i+1548          local n1 = [base64_inv[str[i]] << 2 | (base64_inv[str[i + 1]] >> 4)];549          // 4 LSB of i+1, 4MSB of i+2550          local n2 =551            if str[i + 2] == '=' then []552            else [(base64_inv[str[i + 1]] & 15) << 4 | (base64_inv[str[i + 2]] >> 2)];553          // 2 LSB of i+2, all 6 bits of i+3554          local n3 =555            if str[i + 3] == '=' then []556            else [(base64_inv[str[i + 2]] & 3) << 6 | base64_inv[str[i + 3]]];557          aux(str, i + 4, r + n1 + n2 + n3) tailstrict;558      aux(str, 0, []),559560  base64Decode(str)::561    local bytes = std.base64DecodeBytes(str);562    std.join('', std.map(function(b) std.char(b), bytes)),563564  reverse:: $intrinsic(reverse),565566  sortImpl:: $intrinsic(sortImpl),567568  sort(arr, keyF=id)::569    std.sortImpl(arr, keyF),570571  uniq(arr, keyF=id)::572    local f(a, b) =573      if std.length(a) == 0 then574        [b]575      else if keyF(a[std.length(a) - 1]) == keyF(b) then576        a577      else578        a + [b];579    std.foldl(f, arr, []),580581  set(arr, keyF=id)::582    std.uniq(std.sort(arr, keyF), keyF),583584  setMember(x, arr, keyF=id)::585    // TODO(dcunnin): Binary chop for O(log n) complexity586    std.length(std.setInter([x], arr, keyF)) > 0,587588  setUnion(a, b, keyF=id)::589    // NOTE: order matters, values in `a` win590    local aux(a, b, i, j, acc) =591      if i >= std.length(a) then592        acc + b[j:]593      else if j >= std.length(b) then594        acc + a[i:]595      else596        local ak = keyF(a[i]);597        local bk = keyF(b[j]);598        if ak == bk then599          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict600        else if ak < bk then601          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict602        else603          aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;604    aux(a, b, 0, 0, []),605606  setInter(a, b, keyF=id)::607    local aux(a, b, i, j, acc) =608      if i >= std.length(a) || j >= std.length(b) then609        acc610      else611        if keyF(a[i]) == keyF(b[j]) then612          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict613        else if keyF(a[i]) < keyF(b[j]) then614          aux(a, b, i + 1, j, acc) tailstrict615        else616          aux(a, b, i, j + 1, acc) tailstrict;617    aux(a, b, 0, 0, []) tailstrict,618619  setDiff(a, b, keyF=id)::620    local aux(a, b, i, j, acc) =621      if i >= std.length(a) then622        acc623      else if j >= std.length(b) then624        acc + a[i:]625      else626        if keyF(a[i]) == keyF(b[j]) then627          aux(a, b, i + 1, j + 1, acc) tailstrict628        else if keyF(a[i]) < keyF(b[j]) then629          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict630        else631          aux(a, b, i, j + 1, acc) tailstrict;632    aux(a, b, 0, 0, []) tailstrict,633634  mergePatch(target, patch)::635    if std.isObject(patch) then636      local target_object =637        if std.isObject(target) then target else {};638639      local target_fields =640        if std.isObject(target_object) then std.objectFields(target_object) else [];641642      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];643      local both_fields = std.setUnion(target_fields, std.objectFields(patch));644645      {646        [k]:647          if !std.objectHas(patch, k) then648            target_object[k]649          else if !std.objectHas(target_object, k) then650            std.mergePatch(null, patch[k]) tailstrict651          else652            std.mergePatch(target_object[k], patch[k]) tailstrict653        for k in std.setDiff(both_fields, null_fields)654      }655    else656      patch,657658  objectFields(o)::659    std.objectFieldsEx(o, false),660661  objectFieldsAll(o)::662    std.objectFieldsEx(o, true),663664  objectHas(o, f)::665    std.objectHasEx(o, f, false),666667  objectHasAll(o, f)::668    std.objectHasEx(o, f, true),669670  objectValues(o)::671    [o[k] for k in std.objectFields(o)],672673  objectValuesAll(o)::674    [o[k] for k in std.objectFieldsAll(o)],675676  equals:: $intrinsic(equals),677678  resolvePath(f, r)::679    local arr = std.split(f, '/');680    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),681682  prune(a)::683    local isContent(b) =684      if b == null then685        false686      else if std.isArray(b) then687        std.length(b) > 0688      else if std.isObject(b) then689        std.length(b) > 0690      else691        true;692    if std.isArray(a) then693      [std.prune(x) for x in a if isContent($.prune(x))]694    else if std.isObject(a) then {695      [x]: $.prune(a[x])696      for x in std.objectFields(a)697      if isContent(std.prune(a[x]))698    } else699      a,700701  findSubstr(pat, str)::702    if !std.isString(pat) then703      error 'findSubstr first parameter should be a string, got ' + std.type(pat)704    else if !std.isString(str) then705      error 'findSubstr second parameter should be a string, got ' + std.type(str)706    else707      local pat_len = std.length(pat);708      local str_len = std.length(str);709      if pat_len == 0 || str_len == 0 || pat_len > str_len then710        []711      else712        std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),713714  find(value, arr)::715    if !std.isArray(arr) then716      error 'find second parameter should be an array, got ' + std.type(arr)717    else718      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),719}