Constant STDLIB_STR
Source pub const STDLIB_STR: &str = "{\n local std = self,\n local id = std.id,\n\n # Those functions aren\'t normally located in stdlib\n length:: $intrinsic(length),\n type:: $intrinsic(type),\n makeArray:: $intrinsic(makeArray),\n codepoint:: $intrinsic(codepoint),\n objectFieldsEx:: $intrinsic(objectFieldsEx),\n objectHasEx:: $intrinsic(objectHasEx),\n primitiveEquals:: $intrinsic(primitiveEquals),\n modulo:: $intrinsic(modulo),\n floor:: $intrinsic(floor),\n ceil:: $intrinsic(ceil),\n extVar:: $intrinsic(extVar),\n native:: $intrinsic(native),\n filter:: $intrinsic(filter),\n char:: $intrinsic(char),\n encodeUTF8:: $intrinsic(encodeUTF8),\n decodeUTF8:: $intrinsic(decodeUTF8),\n md5:: $intrinsic(md5),\n trace:: $intrinsic(trace),\n id:: $intrinsic(id),\n parseJson:: $intrinsic(parseJson),\n\n log:: $intrinsic(log),\n pow:: $intrinsic(pow),\n sqrt:: $intrinsic(sqrt),\n\n sin:: $intrinsic(sin),\n cos:: $intrinsic(cos),\n tan:: $intrinsic(tan),\n asin:: $intrinsic(asin),\n acos:: $intrinsic(acos),\n atan:: $intrinsic(atan),\n\n exp:: $intrinsic(exp),\n mantissa:: $intrinsic(mantissa),\n exponent:: $intrinsic(exponent),\n\n isString(v):: std.type(v) == \'string\',\n isNumber(v):: std.type(v) == \'number\',\n isBoolean(v):: std.type(v) == \'boolean\',\n isObject(v):: std.type(v) == \'object\',\n isArray(v):: std.type(v) == \'array\',\n isFunction(v):: std.type(v) == \'function\',\n\n toString(a)::\n if std.type(a) == \'string\' then a else \'\' + a,\n\n substr:: $intrinsic(substr),\n\n startsWith(a, b)::\n if std.length(a) < std.length(b) then\n false\n else\n std.substr(a, 0, std.length(b)) == b,\n\n endsWith(a, b)::\n if std.length(a) < std.length(b) then\n false\n else\n std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,\n\n lstripChars(str, chars)::\n if std.length(str) > 0 && std.member(chars, str[0]) then\n std.lstripChars(str[1:], chars)\n else\n str,\n\n rstripChars(str, chars)::\n local len = std.length(str);\n if len > 0 && std.member(chars, str[len - 1]) then\n std.rstripChars(str[:len - 1], chars)\n else\n str,\n\n stripChars(str, chars)::\n std.lstripChars(std.rstripChars(str, chars), chars),\n\n stringChars(str)::\n std.makeArray(std.length(str), function(i) str[i]),\n\n local parse_nat(str, base) =\n assert base > 0 && base <= 16 : \'integer base %d invalid\' % base;\n // These codepoints are in ascending order:\n local zero_code = std.codepoint(\'0\');\n local upper_a_code = std.codepoint(\'A\');\n local lower_a_code = std.codepoint(\'a\');\n local addDigit(aggregate, char) =\n local code = std.codepoint(char);\n local digit = if code >= lower_a_code then\n code - lower_a_code + 10\n else if code >= upper_a_code then\n code - upper_a_code + 10\n else\n code - zero_code;\n assert digit >= 0 && digit < base : \'%s is not a base %d integer\' % [str, base];\n base * aggregate + digit;\n std.foldl(addDigit, std.stringChars(str), 0),\n\n parseInt(str)::\n assert std.isString(str) : \'Expected string, got \' + std.type(str);\n assert std.length(str) > 0 && str != \'-\' : \'Not an integer: \"%s\"\' % [str];\n if str[0] == \'-\' then\n -parse_nat(str[1:], 10)\n else\n parse_nat(str, 10),\n\n parseOctal(str)::\n assert std.isString(str) : \'Expected string, got \' + std.type(str);\n assert std.length(str) > 0 : \'Not an octal number: \"\"\';\n parse_nat(str, 8),\n\n parseHex(str)::\n assert std.isString(str) : \'Expected string, got \' + std.type(str);\n assert std.length(str) > 0 : \'Not hexadecimal: \"\"\';\n parse_nat(str, 16),\n\n split(str, c)::\n assert std.isString(str) : \'std.split first parameter should be a string, got \' + std.type(str);\n assert std.isString(c) : \'std.split second parameter should be a string, got \' + std.type(c);\n assert std.length(c) == 1 : \'std.split second parameter should have length 1, got \' + std.length(c);\n std.splitLimit(str, c, -1),\n\n splitLimit:: $intrinsic(splitLimit),\n\n strReplace:: $intrinsic(strReplace),\n\n asciiUpper(str)::\n local cp = std.codepoint;\n local up_letter(c) = if cp(c) >= 97 && cp(c) < 123 then\n std.char(cp(c) - 32)\n else\n c;\n std.join(\'\', std.map(up_letter, std.stringChars(str))),\n\n asciiLower(str)::\n local cp = std.codepoint;\n local down_letter(c) = if cp(c) >= 65 && cp(c) < 91 then\n std.char(cp(c) + 32)\n else\n c;\n std.join(\'\', std.map(down_letter, std.stringChars(str))),\n\n range:: $intrinsic(range),\n\n repeat(what, count)::\n local joiner =\n if std.isString(what) then \'\'\n else if std.isArray(what) then []\n else error \'std.repeat first argument must be an array or a string\';\n std.join(joiner, std.makeArray(count, function(i) what)),\n\n slice:: $intrinsic(slice),\n\n member(arr, x)::\n if std.isArray(arr) then\n std.count(arr, x) > 0\n else if std.isString(arr) then\n std.length(std.findSubstr(x, arr)) > 0\n else error \'std.member first argument must be an array or a string\',\n\n count(arr, x):: std.length(std.filter(function(v) v == x, arr)),\n\n mod:: $intrinsic(mod),\n\n map:: $intrinsic(map),\n\n mapWithIndex(func, arr)::\n if !std.isFunction(func) then\n error (\'std.mapWithIndex first param must be function, got \' + std.type(func))\n else if !std.isArray(arr) && !std.isString(arr) then\n error (\'std.mapWithIndex second param must be array, got \' + std.type(arr))\n else\n std.makeArray(std.length(arr), function(i) func(i, arr[i])),\n\n mapWithKey(func, obj)::\n if !std.isFunction(func) then\n error (\'std.mapWithKey first param must be function, got \' + std.type(func))\n else if !std.isObject(obj) then\n error (\'std.mapWithKey second param must be object, got \' + std.type(obj))\n else\n { [k]: func(k, obj[k]) for k in std.objectFields(obj) },\n\n flatMap:: $intrinsic(flatMap),\n\n join:: $intrinsic(join),\n\n lines(arr)::\n std.join(\'\\n\', arr + [\'\']),\n\n deepJoin(arr)::\n if std.isString(arr) then\n arr\n else if std.isArray(arr) then\n std.join(\'\', [std.deepJoin(x) for x in arr])\n else\n error \'Expected string or array, got %s\' % std.type(arr),\n\n\n format:: $intrinsic(format),\n\n foldr:: $intrinsic(foldr),\n\n foldl:: $intrinsic(foldl),\n\n filterMap(filter_func, map_func, arr)::\n if !std.isFunction(filter_func) then\n error (\'std.filterMap first param must be function, got \' + std.type(filter_func))\n else if !std.isFunction(map_func) then\n error (\'std.filterMap second param must be function, got \' + std.type(map_func))\n else if !std.isArray(arr) then\n error (\'std.filterMap third param must be array, got \' + std.type(arr))\n else\n std.map(map_func, std.filter(filter_func, arr)),\n\n assertEqual(a, b)::\n if a == b then\n true\n else\n error \'Assertion failed. \' + a + \' != \' + b,\n\n abs(n)::\n if !std.isNumber(n) then\n error \'std.abs expected number, got \' + std.type(n)\n else\n if n > 0 then n else -n,\n\n sign(n)::\n if !std.isNumber(n) then\n error \'std.sign expected number, got \' + std.type(n)\n else\n if n > 0 then\n 1\n else if n < 0 then\n -1\n else 0,\n\n max(a, b)::\n if !std.isNumber(a) then\n error \'std.max first param expected number, got \' + std.type(a)\n else if !std.isNumber(b) then\n error \'std.max second param expected number, got \' + std.type(b)\n else\n if a > b then a else b,\n\n min(a, b)::\n if !std.isNumber(a) then\n error \'std.min first param expected number, got \' + std.type(a)\n else if !std.isNumber(b) then\n error \'std.min second param expected number, got \' + std.type(b)\n else\n if a < b then a else b,\n\n clamp(x, minVal, maxVal)::\n if x < minVal then minVal\n else if x > maxVal then maxVal\n else x,\n\n flattenArrays(arrs)::\n std.foldl(function(a, b) a + b, arrs, []),\n\n manifestIni(ini)::\n local body_lines(body) =\n std.join([], [\n local value_or_values = body[k];\n if std.isArray(value_or_values) then\n [\'%s = %s\' % [k, value] for value in value_or_values]\n else\n [\'%s = %s\' % [k, value_or_values]]\n\n for k in std.objectFields(body)\n ]);\n\n local section_lines(sname, sbody) = [\'[%s]\' % [sname]] + body_lines(sbody),\n main_body = if std.objectHas(ini, \'main\') then body_lines(ini.main) else [],\n all_sections = [\n section_lines(k, ini.sections[k])\n for k in std.objectFields(ini.sections)\n ];\n std.join(\'\\n\', main_body + std.flattenArrays(all_sections) + [\'\']),\n\n manifestToml(value):: std.manifestTomlEx(value, \' \'),\n\n manifestTomlEx(value, indent)::\n local\n escapeStringToml = std.escapeStringJson,\n escapeKeyToml(key) =\n local bare_allowed = std.set(std.stringChars(\'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-\'));\n if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),\n isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),\n isSection(v) = std.isObject(v) || isTableArray(v),\n renderValue(v, indexedPath, inline, cindent) =\n if v == true then\n \'true\'\n else if v == false then\n \'false\'\n else if v == null then\n error \'Tried to manifest \"null\" at \' + indexedPath\n else if std.isNumber(v) then\n \'\' + v\n else if std.isString(v) then\n escapeStringToml(v)\n else if std.isFunction(v) then\n error \'Tried to manifest function at \' + indexedPath\n else if std.isArray(v) then\n if std.length(v) == 0 then\n \'[]\'\n else\n local range = std.range(0, std.length(v) - 1);\n local new_indent = if inline then \'\' else cindent + indent;\n local separator = if inline then \' \' else \'\\n\';\n local lines = [\'[\' + separator]\n + std.join([\',\' + separator],\n [\n [new_indent + renderValue(v[i], indexedPath + [i], true, \'\')]\n for i in range\n ])\n + [separator + (if inline then \'\' else cindent) + \']\'];\n std.join(\'\', lines)\n else if std.isObject(v) then\n local lines = [\'{ \']\n + std.join([\', \'],\n [\n [escapeKeyToml(k) + \' = \' + renderValue(v[k], indexedPath + [k], true, \'\')]\n for k in std.objectFields(v)\n ])\n + [\' }\'];\n std.join(\'\', lines),\n renderTableInternal(v, path, indexedPath, cindent) =\n local kvp = std.flattenArrays([\n [cindent + escapeKeyToml(k) + \' = \' + renderValue(v[k], indexedPath + [k], false, cindent)]\n for k in std.objectFields(v)\n if !isSection(v[k])\n ]);\n local sections = [std.join(\'\\n\', kvp)] + [\n (\n if std.isObject(v[k]) then\n renderTable(v[k], path + [k], indexedPath + [k], cindent)\n else\n renderTableArray(v[k], path + [k], indexedPath + [k], cindent)\n )\n for k in std.objectFields(v)\n if isSection(v[k])\n ];\n std.join(\'\\n\\n\', sections),\n renderTable(v, path, indexedPath, cindent) =\n cindent + \'[\' + std.join(\'.\', std.map(escapeKeyToml, path)) + \']\'\n + (if v == {} then \'\' else \'\\n\')\n + renderTableInternal(v, path, indexedPath, cindent + indent),\n renderTableArray(v, path, indexedPath, cindent) =\n local range = std.range(0, std.length(v) - 1);\n local sections = [\n (cindent + \'[[\' + std.join(\'.\', std.map(escapeKeyToml, path)) + \']]\'\n + (if v[i] == {} then \'\' else \'\\n\')\n + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))\n for i in range\n ];\n std.join(\'\\n\\n\', sections);\n if std.isObject(value) then\n renderTableInternal(value, [], [], \'\')\n else\n error \'TOML body must be an object. Got \' + std.type(value),\n\n escapeStringJson:: $intrinsic(escapeStringJson),\n\n escapeStringPython(str)::\n std.escapeStringJson(str),\n\n escapeStringBash(str_)::\n local str = std.toString(str_);\n local trans(ch) =\n if ch == \"\'\" then\n \"\'\\\"\'\\\"\'\"\n else\n ch;\n \"\'%s\'\" % std.join(\'\', [trans(ch) for ch in std.stringChars(str)]),\n\n escapeStringDollars(str_)::\n local str = std.toString(str_);\n local trans(ch) =\n if ch == \'$\' then\n \'$$\'\n else\n ch;\n std.foldl(function(a, b) a + trans(b), std.stringChars(str), \'\'),\n\n manifestJson(value):: std.manifestJsonEx(value, \' \'),\n\n manifestJsonEx:: $intrinsic(manifestJsonEx),\n\n manifestYamlDoc(value, indent_array_in_object=false)::\n local aux(v, path, cindent) =\n if v == true then\n \'true\'\n else if v == false then\n \'false\'\n else if v == null then\n \'null\'\n else if std.isNumber(v) then\n \'\' + v\n else if std.isString(v) then\n local len = std.length(v);\n if len == 0 then\n \'\"\"\'\n else if v[len - 1] == \'\\n\' then\n local split = std.split(v, \'\\n\');\n std.join(\'\\n\' + cindent + \' \', [\'|\'] + split[0:std.length(split) - 1])\n else\n std.escapeStringJson(v)\n else if std.isFunction(v) then\n error \'Tried to manifest function at \' + path\n else if std.isArray(v) then\n if std.length(v) == 0 then\n \'[]\'\n else\n local params(value) =\n if std.isArray(value) && std.length(value) > 0 then {\n // While we could avoid the new line, it yields YAML that is\n // hard to read, e.g.:\n // - - - 1\n // - 2\n // - - 3\n // - 4\n new_indent: cindent + \' \',\n space: \'\\n\' + self.new_indent,\n } else if std.isObject(value) && std.length(value) > 0 then {\n new_indent: cindent + \' \',\n // In this case we can start on the same line as the - because the indentation\n // matches up then. The converse is not true, because fields are not always\n // 1 character long.\n space: \' \',\n } else {\n // In this case, new_indent is only used in the case of multi-line strings.\n new_indent: cindent,\n space: \' \',\n };\n local range = std.range(0, std.length(v) - 1);\n local parts = [\n \'-\' + param.space + aux(v[i], path + [i], param.new_indent)\n for i in range\n for param in [params(v[i])]\n ];\n std.join(\'\\n\' + cindent, parts)\n else if std.isObject(v) then\n if std.length(v) == 0 then\n \'{}\'\n else\n local params(value) =\n if std.isArray(value) && std.length(value) > 0 then {\n // Not indenting allows e.g.\n // ports:\n // - 80\n // instead of\n // ports:\n // - 80\n new_indent: if indent_array_in_object then cindent + \' \' else cindent,\n space: \'\\n\' + self.new_indent,\n } else if std.isObject(value) && std.length(value) > 0 then {\n new_indent: cindent + \' \',\n space: \'\\n\' + self.new_indent,\n } else {\n // In this case, new_indent is only used in the case of multi-line strings.\n new_indent: cindent,\n space: \' \',\n };\n local lines = [\n std.escapeStringJson(k) + \':\' + param.space + aux(v[k], path + [k], param.new_indent)\n for k in std.objectFields(v)\n for param in [params(v[k])]\n ];\n std.join(\'\\n\' + cindent, lines);\n aux(value, [], \'\'),\n\n manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::\n if !std.isArray(value) then\n error \'manifestYamlStream only takes arrays, got \' + std.type(value)\n else\n \'---\\n\' + std.join(\n \'\\n---\\n\', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]\n ) + if c_document_end then \'\\n...\\n\' else \'\\n\',\n\n\n manifestPython(v)::\n if std.isObject(v) then\n local fields = [\n \'%s: %s\' % [std.escapeStringPython(k), std.manifestPython(v[k])]\n for k in std.objectFields(v)\n ];\n \'{%s}\' % [std.join(\', \', fields)]\n else if std.isArray(v) then\n \'[%s]\' % [std.join(\', \', [std.manifestPython(v2) for v2 in v])]\n else if std.isString(v) then\n \'%s\' % [std.escapeStringPython(v)]\n else if std.isFunction(v) then\n error \'cannot manifest function\'\n else if std.isNumber(v) then\n std.toString(v)\n else if v == true then\n \'True\'\n else if v == false then\n \'False\'\n else if v == null then\n \'None\',\n\n manifestPythonVars(conf)::\n local vars = [\'%s = %s\' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];\n std.join(\'\\n\', vars + [\'\']),\n\n manifestXmlJsonml(value)::\n if !std.isArray(value) then\n error \'Expected a JSONML value (an array), got %s\' % std.type(value)\n else\n local aux(v) =\n if std.isString(v) then\n v\n else\n local tag = v[0];\n local has_attrs = std.length(v) > 1 && std.isObject(v[1]);\n local attrs = if has_attrs then v[1] else {};\n local children = if has_attrs then v[2:] else v[1:];\n local attrs_str =\n std.join(\'\', [\' %s=\"%s\"\' % [k, attrs[k]] for k in std.objectFields(attrs)]);\n std.deepJoin([\'<\', tag, attrs_str, \'>\', [aux(x) for x in children], \'</\', tag, \'>\']);\n\n aux(value),\n\n local base64_table = \'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\',\n local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },\n\n base64:: $intrinsic(base64),\n\n base64DecodeBytes:: $intrinsic(base64DecodeBytes),\n\n base64Decode:: $intrinsic(base64Decode),\n\n reverse:: $intrinsic(reverse),\n\n sortImpl:: $intrinsic(sortImpl),\n\n sort(arr, keyF=id)::\n std.sortImpl(arr, keyF),\n\n uniq(arr, keyF=id)::\n local f(a, b) =\n if std.length(a) == 0 then\n [b]\n else if keyF(a[std.length(a) - 1]) == keyF(b) then\n a\n else\n a + [b];\n std.foldl(f, arr, []),\n\n set(arr, keyF=id)::\n std.uniq(std.sort(arr, keyF), keyF),\n\n setMember(x, arr, keyF=id)::\n // TODO(dcunnin): Binary chop for O(log n) complexity\n std.length(std.setInter([x], arr, keyF)) > 0,\n\n setUnion(a, b, keyF=id)::\n // NOTE: order matters, values in `a` win\n local aux(a, b, i, j, acc) =\n if i >= std.length(a) then\n acc + b[j:]\n else if j >= std.length(b) then\n acc + a[i:]\n else\n local ak = keyF(a[i]);\n local bk = keyF(b[j]);\n if ak == bk then\n aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict\n else if ak < bk then\n aux(a, b, i + 1, j, acc + [a[i]]) tailstrict\n else\n aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;\n aux(a, b, 0, 0, []),\n\n setInter(a, b, keyF=id)::\n local aux(a, b, i, j, acc) =\n if i >= std.length(a) || j >= std.length(b) then\n acc\n else\n if keyF(a[i]) == keyF(b[j]) then\n aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict\n else if keyF(a[i]) < keyF(b[j]) then\n aux(a, b, i + 1, j, acc) tailstrict\n else\n aux(a, b, i, j + 1, acc) tailstrict;\n aux(a, b, 0, 0, []) tailstrict,\n\n setDiff(a, b, keyF=id)::\n local aux(a, b, i, j, acc) =\n if i >= std.length(a) then\n acc\n else if j >= std.length(b) then\n acc + a[i:]\n else\n if keyF(a[i]) == keyF(b[j]) then\n aux(a, b, i + 1, j + 1, acc) tailstrict\n else if keyF(a[i]) < keyF(b[j]) then\n aux(a, b, i + 1, j, acc + [a[i]]) tailstrict\n else\n aux(a, b, i, j + 1, acc) tailstrict;\n aux(a, b, 0, 0, []) tailstrict,\n\n mergePatch(target, patch)::\n if std.isObject(patch) then\n local target_object =\n if std.isObject(target) then target else {};\n\n local target_fields =\n if std.isObject(target_object) then std.objectFields(target_object) else [];\n\n local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];\n local both_fields = std.setUnion(target_fields, std.objectFields(patch));\n\n {\n [k]:\n if !std.objectHas(patch, k) then\n target_object[k]\n else if !std.objectHas(target_object, k) then\n std.mergePatch(null, patch[k]) tailstrict\n else\n std.mergePatch(target_object[k], patch[k]) tailstrict\n for k in std.setDiff(both_fields, null_fields)\n }\n else\n patch,\n\n objectFields(o)::\n std.objectFieldsEx(o, false),\n\n objectFieldsAll(o)::\n std.objectFieldsEx(o, true),\n\n objectHas(o, f)::\n std.objectHasEx(o, f, false),\n\n objectHasAll(o, f)::\n std.objectHasEx(o, f, true),\n\n objectValues(o)::\n [o[k] for k in std.objectFields(o)],\n\n objectValuesAll(o)::\n [o[k] for k in std.objectFieldsAll(o)],\n\n equals:: $intrinsic(equals),\n\n resolvePath(f, r)::\n local arr = std.split(f, \'/\');\n std.join(\'/\', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),\n\n prune(a)::\n local isContent(b) =\n if b == null then\n false\n else if std.isArray(b) then\n std.length(b) > 0\n else if std.isObject(b) then\n std.length(b) > 0\n else\n true;\n if std.isArray(a) then\n [std.prune(x) for x in a if isContent($.prune(x))]\n else if std.isObject(a) then {\n [x]: $.prune(a[x])\n for x in std.objectFields(a)\n if isContent(std.prune(a[x]))\n } else\n a,\n\n findSubstr(pat, str)::\n if !std.isString(pat) then\n error \'findSubstr first parameter should be a string, got \' + std.type(pat)\n else if !std.isString(str) then\n error \'findSubstr second parameter should be a string, got \' + std.type(str)\n else\n local pat_len = std.length(pat);\n local str_len = std.length(str);\n if pat_len == 0 || str_len == 0 || pat_len > str_len then\n []\n else\n std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),\n\n find(value, arr)::\n if !std.isArray(arr) then\n error \'find second parameter should be an array, got \' + std.type(arr)\n else\n std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),\n}\n";