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

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  ceil:: $intrinsic(ceil),16  log:: $intrinsic(log),17  pow:: $intrinsic(pow),18  extVar:: $intrinsic(extVar),19  native:: $intrinsic(native),20  filter:: $intrinsic(filter),21  char:: $intrinsic(char),22  encodeUTF8:: $intrinsic(encodeUTF8),23  md5:: $intrinsic(md5),24  trace:: $intrinsic(trace),25  id:: $intrinsic(id),26  parseJson:: $intrinsic(parseJson),2728  isString(v):: std.type(v) == 'string',29  isNumber(v):: std.type(v) == 'number',30  isBoolean(v):: std.type(v) == 'boolean',31  isObject(v):: std.type(v) == 'object',32  isArray(v):: std.type(v) == 'array',33  isFunction(v):: std.type(v) == 'function',3435  toString(a)::36    if std.type(a) == 'string' then a else '' + a,3738  substr(str, from, len)::39    assert std.isString(str) : 'substr first parameter should be a string, got ' + std.type(str);40    assert std.isNumber(from) : 'substr second parameter should be a string, got ' + std.type(from);41    assert std.isNumber(len) : 'substr third parameter should be a string, got ' + std.type(len);42    assert len >= 0 : 'substr third parameter should be greater than zero, got ' + len;43    std.join('', std.makeArray(std.max(0, std.min(len, std.length(str) - from)), function(i) str[i + from])),4445  startsWith(a, b)::46    if std.length(a) < std.length(b) then47      false48    else49      std.substr(a, 0, std.length(b)) == b,5051  endsWith(a, b)::52    if std.length(a) < std.length(b) then53      false54    else55      std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,5657  lstripChars(str, chars)::58    if std.length(str) > 0 && std.member(chars, str[0]) then59      std.lstripChars(str[1:], chars)60    else61      str,6263  rstripChars(str, chars)::64    local len = std.length(str);65    if len > 0 && std.member(chars, str[len - 1]) then66      std.rstripChars(str[:len - 1], chars)67    else68      str,6970  stripChars(str, chars)::71    std.lstripChars(std.rstripChars(str, chars), chars),7273  stringChars(str)::74    std.makeArray(std.length(str), function(i) str[i]),7576  local parse_nat(str, base) =77    assert base > 0 && base <= 16 : 'integer base %d invalid' % base;78    // These codepoints are in ascending order:79    local zero_code = std.codepoint('0');80    local upper_a_code = std.codepoint('A');81    local lower_a_code = std.codepoint('a');82    local addDigit(aggregate, char) =83      local code = std.codepoint(char);84      local digit = if code >= lower_a_code then85        code - lower_a_code + 1086      else if code >= upper_a_code then87        code - upper_a_code + 1088      else89        code - zero_code;90      assert digit >= 0 && digit < base : '%s is not a base %d integer' % [str, base];91      base * aggregate + digit;92    std.foldl(addDigit, std.stringChars(str), 0),9394  parseInt(str)::95    assert std.isString(str) : 'Expected string, got ' + std.type(str);96    assert std.length(str) > 0 && str != '-' : 'Not an integer: "%s"' % [str];97    if str[0] == '-' then98      -parse_nat(str[1:], 10)99    else100      parse_nat(str, 10),101102  parseOctal(str)::103    assert std.isString(str) : 'Expected string, got ' + std.type(str);104    assert std.length(str) > 0 : 'Not an octal number: ""';105    parse_nat(str, 8),106107  parseHex(str)::108    assert std.isString(str) : 'Expected string, got ' + std.type(str);109    assert std.length(str) > 0 : 'Not hexadecimal: ""';110    parse_nat(str, 16),111112  split(str, c)::113    assert std.isString(str) : 'std.split first parameter should be a string, got ' + std.type(str);114    assert std.isString(c) : 'std.split second parameter should be a string, got ' + std.type(c);115    assert std.length(c) == 1 : 'std.split second parameter should have length 1, got ' + std.length(c);116    std.splitLimit(str, c, -1),117118  splitLimit(str, c, maxsplits)::119    assert std.isString(str) : 'std.splitLimit first parameter should be a string, got ' + std.type(str);120    assert std.isString(c) : 'std.splitLimit second parameter should be a string, got ' + std.type(c);121    assert std.length(c) == 1 : 'std.splitLimit second parameter should have length 1, got ' + std.length(c);122    assert std.isNumber(maxsplits) : 'std.splitLimit third parameter should be a number, got ' + std.type(maxsplits);123    local aux(str, delim, i, arr, v) =124      local c = str[i];125      local i2 = i + 1;126      if i >= std.length(str) then127        arr + [v]128      else if c == delim && (maxsplits == -1 || std.length(arr) < maxsplits) then129        aux(str, delim, i2, arr + [v], '') tailstrict130      else131        aux(str, delim, i2, arr, v + c) tailstrict;132    aux(str, c, 0, [], ''),133134  strReplace:: $intrinsic(strReplace),135136  asciiUpper(str)::137    local cp = std.codepoint;138    local up_letter(c) = if cp(c) >= 97 && cp(c) < 123 then139      std.char(cp(c) - 32)140    else141      c;142    std.join('', std.map(up_letter, std.stringChars(str))),143144  asciiLower(str)::145    local cp = std.codepoint;146    local down_letter(c) = if cp(c) >= 65 && cp(c) < 91 then147      std.char(cp(c) + 32)148    else149      c;150    std.join('', std.map(down_letter, std.stringChars(str))),151152  range:: $intrinsic(range),153154  repeat(what, count)::155    local joiner =156      if std.isString(what) then ''157      else if std.isArray(what) then []158      else error 'std.repeat first argument must be an array or a string';159    std.join(joiner, std.makeArray(count, function(i) what)),160161  slice:: $intrinsic(slice),162163  member(arr, x)::164    if std.isArray(arr) then165      std.count(arr, x) > 0166    else if std.isString(arr) then167      std.length(std.findSubstr(x, arr)) > 0168    else error 'std.member first argument must be an array or a string',169170  count(arr, x):: std.length(std.filter(function(v) v == x, arr)),171172  mod:: $intrinsic(mod),173174  map:: $intrinsic(map),175176  mapWithIndex(func, arr)::177    if !std.isFunction(func) then178      error ('std.mapWithIndex first param must be function, got ' + std.type(func))179    else if !std.isArray(arr) && !std.isString(arr) then180      error ('std.mapWithIndex second param must be array, got ' + std.type(arr))181    else182      std.makeArray(std.length(arr), function(i) func(i, arr[i])),183184  mapWithKey(func, obj)::185    if !std.isFunction(func) then186      error ('std.mapWithKey first param must be function, got ' + std.type(func))187    else if !std.isObject(obj) then188      error ('std.mapWithKey second param must be object, got ' + std.type(obj))189    else190      { [k]: func(k, obj[k]) for k in std.objectFields(obj) },191192  flatMap:: $intrinsic(flatMap),193194  join:: $intrinsic(join),195196  lines(arr)::197    std.join('\n', arr + ['']),198199  deepJoin(arr)::200    if std.isString(arr) then201      arr202    else if std.isArray(arr) then203      std.join('', [std.deepJoin(x) for x in arr])204    else205      error 'Expected string or array, got %s' % std.type(arr),206207208  format:: $intrinsic(format),209210  foldr:: $intrinsic(foldr),211212  foldl:: $intrinsic(foldl),213214  filterMap(filter_func, map_func, arr)::215    if !std.isFunction(filter_func) then216      error ('std.filterMap first param must be function, got ' + std.type(filter_func))217    else if !std.isFunction(map_func) then218      error ('std.filterMap second param must be function, got ' + std.type(map_func))219    else if !std.isArray(arr) then220      error ('std.filterMap third param must be array, got ' + std.type(arr))221    else222      std.map(map_func, std.filter(filter_func, arr)),223224  assertEqual(a, b)::225    if a == b then226      true227    else228      error 'Assertion failed. ' + a + ' != ' + b,229230  abs(n)::231    if !std.isNumber(n) then232      error 'std.abs expected number, got ' + std.type(n)233    else234      if n > 0 then n else -n,235236  sign(n)::237    if !std.isNumber(n) then238      error 'std.sign expected number, got ' + std.type(n)239    else240      if n > 0 then241        1242      else if n < 0 then243        -1244      else 0,245246  max(a, b)::247    if !std.isNumber(a) then248      error 'std.max first param expected number, got ' + std.type(a)249    else if !std.isNumber(b) then250      error 'std.max second param expected number, got ' + std.type(b)251    else252      if a > b then a else b,253254  min(a, b)::255    if !std.isNumber(a) then256      error 'std.min first param expected number, got ' + std.type(a)257    else if !std.isNumber(b) then258      error 'std.min second param expected number, got ' + std.type(b)259    else260      if a < b then a else b,261262  clamp(x, minVal, maxVal)::263    if x < minVal then minVal264    else if x > maxVal then maxVal265    else x,266267  flattenArrays(arrs)::268    std.foldl(function(a, b) a + b, arrs, []),269270  manifestIni(ini)::271    local body_lines(body) =272      std.join([], [273        local value_or_values = body[k];274        if std.isArray(value_or_values) then275          ['%s = %s' % [k, value] for value in value_or_values]276        else277          ['%s = %s' % [k, value_or_values]]278279        for k in std.objectFields(body)280      ]);281282    local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),283          main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],284          all_sections = [285      section_lines(k, ini.sections[k])286      for k in std.objectFields(ini.sections)287    ];288    std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),289290  manifestToml(value):: std.manifestTomlEx(value, '  '),291292  manifestTomlEx(value, indent)::293    local294      escapeStringToml = std.escapeStringJson,295      escapeKeyToml(key) =296        local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));297        if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),298      isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),299      isSection(v) = std.isObject(v) || isTableArray(v),300      renderValue(v, indexedPath, inline, cindent) =301        if v == true then302          'true'303        else if v == false then304          'false'305        else if v == null then306          error 'Tried to manifest "null" at ' + indexedPath307        else if std.isNumber(v) then308          '' + v309        else if std.isString(v) then310          escapeStringToml(v)311        else if std.isFunction(v) then312          error 'Tried to manifest function at ' + indexedPath313        else if std.isArray(v) then314          if std.length(v) == 0 then315            '[]'316          else317            local range = std.range(0, std.length(v) - 1);318            local new_indent = if inline then '' else cindent + indent;319            local separator = if inline then ' ' else '\n';320            local lines = ['[' + separator]321                          + std.join([',' + separator],322                                     [323                                       [new_indent + renderValue(v[i], indexedPath + [i], true, '')]324                                       for i in range325                                     ])326                          + [separator + (if inline then '' else cindent) + ']'];327            std.join('', lines)328        else if std.isObject(v) then329          local lines = ['{ ']330                        + std.join([', '],331                                   [332                                     [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]333                                     for k in std.objectFields(v)334                                   ])335                        + [' }'];336          std.join('', lines),337      renderTableInternal(v, path, indexedPath, cindent) =338        local kvp = std.flattenArrays([339          [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]340          for k in std.objectFields(v)341          if !isSection(v[k])342        ]);343        local sections = [std.join('\n', kvp)] + [344          (345            if std.isObject(v[k]) then346              renderTable(v[k], path + [k], indexedPath + [k], cindent)347            else348              renderTableArray(v[k], path + [k], indexedPath + [k], cindent)349          )350          for k in std.objectFields(v)351          if isSection(v[k])352        ];353        std.join('\n\n', sections),354      renderTable(v, path, indexedPath, cindent) =355        cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'356        + (if v == {} then '' else '\n')357        + renderTableInternal(v, path, indexedPath, cindent + indent),358      renderTableArray(v, path, indexedPath, cindent) =359        local range = std.range(0, std.length(v) - 1);360        local sections = [361          (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'362           + (if v[i] == {} then '' else '\n')363           + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))364          for i in range365        ];366        std.join('\n\n', sections);367    if std.isObject(value) then368      renderTableInternal(value, [], [], '')369    else370      error 'TOML body must be an object. Got ' + std.type(value),371372  escapeStringJson:: $intrinsic(escapeStringJson),373374  escapeStringPython(str)::375    std.escapeStringJson(str),376377  escapeStringBash(str_)::378    local str = std.toString(str_);379    local trans(ch) =380      if ch == "'" then381        "'\"'\"'"382      else383        ch;384    "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),385386  escapeStringDollars(str_)::387    local str = std.toString(str_);388    local trans(ch) =389      if ch == '$' then390        '$$'391      else392        ch;393    std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),394395  manifestJson(value):: std.manifestJsonEx(value, '    '),396397  manifestJsonEx:: $intrinsic(manifestJsonEx),398399  manifestYamlDoc(value, indent_array_in_object=false)::400    local aux(v, path, cindent) =401      if v == true then402        'true'403      else if v == false then404        'false'405      else if v == null then406        'null'407      else if std.isNumber(v) then408        '' + v409      else if std.isString(v) then410        local len = std.length(v);411        if len == 0 then412          '""'413        else if v[len - 1] == '\n' then414          local split = std.split(v, '\n');415          std.join('\n' + cindent + '  ', ['|'] + split[0:std.length(split) - 1])416        else417          std.escapeStringJson(v)418      else if std.isFunction(v) then419        error 'Tried to manifest function at ' + path420      else if std.isArray(v) then421        if std.length(v) == 0 then422          '[]'423        else424          local params(value) =425            if std.isArray(value) && std.length(value) > 0 then {426              // While we could avoid the new line, it yields YAML that is427              // hard to read, e.g.:428              // - - - 1429              //     - 2430              //   - - 3431              //     - 4432              new_indent: cindent + '  ',433              space: '\n' + self.new_indent,434            } else if std.isObject(value) && std.length(value) > 0 then {435              new_indent: cindent + '  ',436              // In this case we can start on the same line as the - because the indentation437              // matches up then.  The converse is not true, because fields are not always438              // 1 character long.439              space: ' ',440            } else {441              // In this case, new_indent is only used in the case of multi-line strings.442              new_indent: cindent,443              space: ' ',444            };445          local range = std.range(0, std.length(v) - 1);446          local parts = [447            '-' + param.space + aux(v[i], path + [i], param.new_indent)448            for i in range449            for param in [params(v[i])]450          ];451          std.join('\n' + cindent, parts)452      else if std.isObject(v) then453        if std.length(v) == 0 then454          '{}'455        else456          local params(value) =457            if std.isArray(value) && std.length(value) > 0 then {458              // Not indenting allows e.g.459              // ports:460              // - 80461              // instead of462              // ports:463              //   - 80464              new_indent: if indent_array_in_object then cindent + '  ' else cindent,465              space: '\n' + self.new_indent,466            } else if std.isObject(value) && std.length(value) > 0 then {467              new_indent: cindent + '  ',468              space: '\n' + self.new_indent,469            } else {470              // In this case, new_indent is only used in the case of multi-line strings.471              new_indent: cindent,472              space: ' ',473            };474          local lines = [475            std.escapeStringJson(k) + ':' + param.space + aux(v[k], path + [k], param.new_indent)476            for k in std.objectFields(v)477            for param in [params(v[k])]478          ];479          std.join('\n' + cindent, lines);480    aux(value, [], ''),481482  manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::483    if !std.isArray(value) then484      error 'manifestYamlStream only takes arrays, got ' + std.type(value)485    else486      '---\n' + std.join(487        '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]488      ) + if c_document_end then '\n...\n' else '\n',489490491  manifestPython(v)::492    if std.isObject(v) then493      local fields = [494        '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]495        for k in std.objectFields(v)496      ];497      '{%s}' % [std.join(', ', fields)]498    else if std.isArray(v) then499      '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]500    else if std.isString(v) then501      '%s' % [std.escapeStringPython(v)]502    else if std.isFunction(v) then503      error 'cannot manifest function'504    else if std.isNumber(v) then505      std.toString(v)506    else if v == true then507      'True'508    else if v == false then509      'False'510    else if v == null then511      'None',512513  manifestPythonVars(conf)::514    local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];515    std.join('\n', vars + ['']),516517  manifestXmlJsonml(value)::518    if !std.isArray(value) then519      error 'Expected a JSONML value (an array), got %s' % std.type(value)520    else521      local aux(v) =522        if std.isString(v) then523          v524        else525          local tag = v[0];526          local has_attrs = std.length(v) > 1 && std.isObject(v[1]);527          local attrs = if has_attrs then v[1] else {};528          local children = if has_attrs then v[2:] else v[1:];529          local attrs_str =530            std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);531          std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);532533      aux(value),534535  local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',536  local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },537538  base64:: $intrinsic(base64),539540  base64DecodeBytes(str)::541    if std.length(str) % 4 != 0 then542      error 'Not a base64 encoded string "%s"' % str543    else544      local aux(str, i, r) =545        if i >= std.length(str) then546          r547        else548          // all 6 bits of i, 2 MSB of i+1549          local n1 = [base64_inv[str[i]] << 2 | (base64_inv[str[i + 1]] >> 4)];550          // 4 LSB of i+1, 4MSB of i+2551          local n2 =552            if str[i + 2] == '=' then []553            else [(base64_inv[str[i + 1]] & 15) << 4 | (base64_inv[str[i + 2]] >> 2)];554          // 2 LSB of i+2, all 6 bits of i+3555          local n3 =556            if str[i + 3] == '=' then []557            else [(base64_inv[str[i + 2]] & 3) << 6 | base64_inv[str[i + 3]]];558          aux(str, i + 4, r + n1 + n2 + n3) tailstrict;559      aux(str, 0, []),560561  base64Decode(str)::562    local bytes = std.base64DecodeBytes(str);563    std.join('', std.map(function(b) std.char(b), bytes)),564565  reverse:: $intrinsic(reverse),566567  sortImpl:: $intrinsic(sortImpl),568569  sort(arr, keyF=id)::570    std.sortImpl(arr, keyF),571572  uniq(arr, keyF=id)::573    local f(a, b) =574      if std.length(a) == 0 then575        [b]576      else if keyF(a[std.length(a) - 1]) == keyF(b) then577        a578      else579        a + [b];580    std.foldl(f, arr, []),581582  set(arr, keyF=id)::583    std.uniq(std.sort(arr, keyF), keyF),584585  setMember(x, arr, keyF=id)::586    // TODO(dcunnin): Binary chop for O(log n) complexity587    std.length(std.setInter([x], arr, keyF)) > 0,588589  setUnion(a, b, keyF=id)::590    // NOTE: order matters, values in `a` win591    local aux(a, b, i, j, acc) =592      if i >= std.length(a) then593        acc + b[j:]594      else if j >= std.length(b) then595        acc + a[i:]596      else597        local ak = keyF(a[i]);598        local bk = keyF(b[j]);599        if ak == bk then600          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict601        else if ak < bk then602          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict603        else604          aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;605    aux(a, b, 0, 0, []),606607  setInter(a, b, keyF=id)::608    local aux(a, b, i, j, acc) =609      if i >= std.length(a) || j >= std.length(b) then610        acc611      else612        if keyF(a[i]) == keyF(b[j]) then613          aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict614        else if keyF(a[i]) < keyF(b[j]) then615          aux(a, b, i + 1, j, acc) tailstrict616        else617          aux(a, b, i, j + 1, acc) tailstrict;618    aux(a, b, 0, 0, []) tailstrict,619620  setDiff(a, b, keyF=id)::621    local aux(a, b, i, j, acc) =622      if i >= std.length(a) then623        acc624      else if j >= std.length(b) then625        acc + a[i:]626      else627        if keyF(a[i]) == keyF(b[j]) then628          aux(a, b, i + 1, j + 1, acc) tailstrict629        else if keyF(a[i]) < keyF(b[j]) then630          aux(a, b, i + 1, j, acc + [a[i]]) tailstrict631        else632          aux(a, b, i, j + 1, acc) tailstrict;633    aux(a, b, 0, 0, []) tailstrict,634635  mergePatch(target, patch)::636    if std.isObject(patch) then637      local target_object =638        if std.isObject(target) then target else {};639640      local target_fields =641        if std.isObject(target_object) then std.objectFields(target_object) else [];642643      local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];644      local both_fields = std.setUnion(target_fields, std.objectFields(patch));645646      {647        [k]:648          if !std.objectHas(patch, k) then649            target_object[k]650          else if !std.objectHas(target_object, k) then651            std.mergePatch(null, patch[k]) tailstrict652          else653            std.mergePatch(target_object[k], patch[k]) tailstrict654        for k in std.setDiff(both_fields, null_fields)655      }656    else657      patch,658659  objectFields(o)::660    std.objectFieldsEx(o, false),661662  objectFieldsAll(o)::663    std.objectFieldsEx(o, true),664665  objectHas(o, f)::666    std.objectHasEx(o, f, false),667668  objectHasAll(o, f)::669    std.objectHasEx(o, f, true),670671  objectValues(o)::672    [o[k] for k in std.objectFields(o)],673674  objectValuesAll(o)::675    [o[k] for k in std.objectFieldsAll(o)],676677  equals:: $intrinsic(equals),678679  resolvePath(f, r)::680    local arr = std.split(f, '/');681    std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),682683  prune(a)::684    local isContent(b) =685      if b == null then686        false687      else if std.isArray(b) then688        std.length(b) > 0689      else if std.isObject(b) then690        std.length(b) > 0691      else692        true;693    if std.isArray(a) then694      [std.prune(x) for x in a if isContent($.prune(x))]695    else if std.isObject(a) then {696      [x]: $.prune(a[x])697      for x in std.objectFields(a)698      if isContent(std.prune(a[x]))699    } else700      a,701702  findSubstr(pat, str)::703    if !std.isString(pat) then704      error 'findSubstr first parameter should be a string, got ' + std.type(pat)705    else if !std.isString(str) then706      error 'findSubstr second parameter should be a string, got ' + std.type(str)707    else708      local pat_len = std.length(pat);709      local str_len = std.length(str);710      if pat_len == 0 || str_len == 0 || pat_len > str_len then711        []712      else713        std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),714715  find(value, arr)::716    if !std.isArray(arr) then717      error 'find second parameter should be an array, got ' + std.type(arr)718    else719      std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),720}