difftreelog
Merge remote-tracking branch 'origin/master' into gcmodule
in: master
7 files changed
crates/jrsonnet-evaluator/src/builtin/manifest.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/manifest.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/manifest.rs
@@ -19,6 +19,8 @@
pub struct ManifestJsonOptions<'s> {
pub padding: &'s str,
pub mtype: ManifestType,
+ pub newline: &'s str,
+ pub key_val_sep: &'s str,
}
pub fn manifest_json_ex(val: &Val, options: &ManifestJsonOptions<'_>) -> Result<String> {
@@ -49,7 +51,7 @@
buf.push('[');
if !items.is_empty() {
if mtype != ManifestType::ToString && mtype != ManifestType::Minify {
- buf.push('\n');
+ buf.push_str(options.newline);
}
let old_len = cur_padding.len();
@@ -60,7 +62,7 @@
if mtype == ManifestType::ToString {
buf.push(' ');
} else if mtype != ManifestType::Minify {
- buf.push('\n');
+ buf.push_str(options.newline);
}
}
buf.push_str(cur_padding);
@@ -69,7 +71,7 @@
cur_padding.truncate(old_len);
if mtype != ManifestType::ToString && mtype != ManifestType::Minify {
- buf.push('\n');
+ buf.push_str(options.newline);
buf.push_str(cur_padding);
}
} else if mtype == ManifestType::Std {
@@ -86,7 +88,7 @@
let fields = obj.fields();
if !fields.is_empty() {
if mtype != ManifestType::ToString && mtype != ManifestType::Minify {
- buf.push('\n');
+ buf.push_str(options.newline);
}
let old_len = cur_padding.len();
@@ -97,12 +99,12 @@
if mtype == ManifestType::ToString {
buf.push(' ');
} else if mtype != ManifestType::Minify {
- buf.push('\n');
+ buf.push_str(options.newline);
}
}
buf.push_str(cur_padding);
escape_string_json_buf(&field, buf);
- buf.push_str(": ");
+ buf.push_str(options.key_val_sep);
push_description_frame(
|| format!("field <{}> manifestification", field.clone()),
|| {
@@ -115,7 +117,7 @@
cur_padding.truncate(old_len);
if mtype != ManifestType::ToString && mtype != ManifestType::Minify {
- buf.push('\n');
+ buf.push_str(options.newline);
buf.push_str(cur_padding);
}
} else if mtype == ManifestType::Std {
crates/jrsonnet-evaluator/src/builtin/mod.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/builtin/mod.rs
+++ b/crates/jrsonnet-evaluator/src/builtin/mod.rs
@@ -1,6 +1,5 @@
use crate::function::StaticBuiltin;
-use crate::typed::{Any, Null, PositiveF64, VecVal, M1};
-use crate::{self as jrsonnet_evaluator, Either, ObjValue};
+use crate::typed::{Any, PositiveF64, VecVal, M1};
use crate::{
builtin::manifest::{manifest_yaml_ex, ManifestYamlOptions},
equals,
@@ -10,6 +9,7 @@
typed::{Either2, Either4},
with_state, ArrValue, Context, FuncVal, IndexableVal, Val,
};
+use crate::{Either, ObjValue};
use format::{format_arr, format_obj};
use gcmodule::Cc;
use jrsonnet_interner::IStr;
@@ -145,7 +145,7 @@
fn builtin_length(x: Either![IStr, VecVal, ObjValue, Cc<FuncVal>]) -> Result<usize> {
use Either4::*;
Ok(match x {
- A(x) => x.len(),
+ A(x) => x.chars().count(),
B(x) => x.0.len(),
C(x) => x
.fields_visibility()
@@ -566,12 +566,21 @@
}
#[jrsonnet_macros::builtin]
-fn builtin_manifest_json_ex(value: Any, indent: IStr) -> Result<String> {
+fn builtin_manifest_json_ex(
+ value: Any,
+ indent: IStr,
+ newline: Option<IStr>,
+ key_val_sep: Option<IStr>,
+) -> Result<String> {
+ let newline = newline.as_deref().unwrap_or("\n");
+ let key_val_sep = key_val_sep.as_deref().unwrap_or(": ");
manifest_json_ex(
&value.0,
&ManifestJsonOptions {
padding: &indent,
mtype: ManifestType::Std,
+ newline,
+ key_val_sep,
},
)
}
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -5,6 +5,9 @@
clippy::ptr_arg
)]
+// For jrsonnet-macros
+extern crate self as jrsonnet_evaluator;
+
mod builtin;
mod ctx;
mod dynamic;
@@ -976,6 +979,14 @@
}
#[test]
+ fn json_minified() {
+ assert_json!(
+ r#"std.manifestJsonMinified({a:3, b:4, c:6})"#,
+ r#""{\"a\":3,\"b\":4,\"c\":6}""#
+ );
+ }
+
+ #[test]
fn parse_json() {
assert_json!(
r#"std.parseJson('{"a": -1,"b": 1,"c": 3.141,"d": []}')"#,
crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -19,7 +19,7 @@
($($ty:ty)*) => {$(
impl Typed for $ty {
const TYPE: &'static ComplexValType =
- &ComplexValType::BoundedNumber(Some(<$ty>::MIN as f64), Some(<$ty>::MAX as f64));
+ &ComplexValType::BoundedNumber(Some(Self::MIN as f64), Some(Self::MAX as f64));
}
impl TryFrom<Val> for $ty {
type Error = LocError;
crates/jrsonnet-evaluator/src/val.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/val.rs
+++ b/crates/jrsonnet-evaluator/src/val.rs
@@ -400,6 +400,8 @@
&ManifestJsonOptions {
padding: "",
mtype: ManifestType::ToString,
+ newline: "\n",
+ key_val_sep: ": ",
},
)?
.into(),
@@ -484,6 +486,8 @@
} else {
ManifestType::Manifest
},
+ newline: "\n",
+ key_val_sep: ": ",
},
)
.map(|s| s.into())
@@ -496,6 +500,8 @@
&ManifestJsonOptions {
padding: &" ".repeat(padding),
mtype: ManifestType::Std,
+ newline: "\n",
+ key_val_sep: ": ",
},
)
.map(|s| s.into())
crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -105,7 +105,7 @@
if let Some(opt_ty) = extract_type_from_option(&t.ty) {
quote! {{
if let Some(value) = parsed.get(#ident) {
- Some(jrsonnet_evaluator::push_description_frame(
+ Some(::jrsonnet_evaluator::push_description_frame(
|| format!("argument <{}> evaluation", #ident),
|| <#opt_ty>::try_from(value.evaluate()?),
)?)
@@ -117,7 +117,7 @@
quote! {{
let value = parsed.get(#ident).unwrap();
- jrsonnet_evaluator::push_description_frame(
+ ::jrsonnet_evaluator::push_description_frame(
|| format!("argument <{}> evaluation", #ident),
|| <#ty>::try_from(value.evaluate()?),
)?
@@ -136,7 +136,7 @@
#[derive(Clone, Copy, gcmodule::Trace)]
#vis struct #name {}
const _: () = {
- use jrsonnet_evaluator::function::{Builtin, StaticBuiltin, BuiltinParam, ArgsLike};
+ use ::jrsonnet_evaluator::function::{Builtin, StaticBuiltin, BuiltinParam, ArgsLike};
const PARAMS: &'static [BuiltinParam] = &[
#(#params),*
];
@@ -156,7 +156,7 @@
PARAMS
}
fn call(&self, context: Context, loc: Option<&ExprLocation>, args: &dyn ArgsLike) -> Result<Val> {
- let parsed = jrsonnet_evaluator::function::parse_builtin_call(context, &PARAMS, args, false)?;
+ let parsed = ::jrsonnet_evaluator::function::parse_builtin_call(context, &PARAMS, args, false)?;
let result: #result = #name(#(#args),*);
let result = result?;
crates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth1{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 decodeUTF8:: $intrinsic(decodeUTF8),22 md5:: $intrinsic(md5),23 trace:: $intrinsic(trace),24 id:: $intrinsic(id),25 parseJson:: $intrinsic(parseJson),26 parseYaml:: $intrinsic(parseYaml),2728 log:: $intrinsic(log),29 pow:: $intrinsic(pow),30 sqrt:: $intrinsic(sqrt),3132 sin:: $intrinsic(sin),33 cos:: $intrinsic(cos),34 tan:: $intrinsic(tan),35 asin:: $intrinsic(asin),36 acos:: $intrinsic(acos),37 atan:: $intrinsic(atan),3839 exp:: $intrinsic(exp),40 mantissa:: $intrinsic(mantissa),41 exponent:: $intrinsic(exponent),4243 isString(v):: std.type(v) == 'string',44 isNumber(v):: std.type(v) == 'number',45 isBoolean(v):: std.type(v) == 'boolean',46 isObject(v):: std.type(v) == 'object',47 isArray(v):: std.type(v) == 'array',48 isFunction(v):: std.type(v) == 'function',4950 toString(a)::51 if std.type(a) == 'string' then a else '' + a,5253 substr:: $intrinsic(substr),5455 startsWith(a, b)::56 if std.length(a) < std.length(b) then57 false58 else59 std.substr(a, 0, std.length(b)) == b,6061 endsWith(a, b)::62 if std.length(a) < std.length(b) then63 false64 else65 std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,6667 lstripChars(str, chars)::68 if std.length(str) > 0 && std.member(chars, str[0]) then69 std.lstripChars(str[1:], chars)70 else71 str,7273 rstripChars(str, chars)::74 local len = std.length(str);75 if len > 0 && std.member(chars, str[len - 1]) then76 std.rstripChars(str[:len - 1], chars)77 else78 str,7980 stripChars(str, chars)::81 std.lstripChars(std.rstripChars(str, chars), chars),8283 stringChars(str)::84 std.makeArray(std.length(str), function(i) str[i]),8586 local parse_nat(str, base) =87 assert base > 0 && base <= 16 : 'integer base %d invalid' % base;88 // These codepoints are in ascending order:89 local zero_code = std.codepoint('0');90 local upper_a_code = std.codepoint('A');91 local lower_a_code = std.codepoint('a');92 local addDigit(aggregate, char) =93 local code = std.codepoint(char);94 local digit = if code >= lower_a_code then95 code - lower_a_code + 1096 else if code >= upper_a_code then97 code - upper_a_code + 1098 else99 code - zero_code;100 assert digit >= 0 && digit < base : '%s is not a base %d integer' % [str, base];101 base * aggregate + digit;102 std.foldl(addDigit, std.stringChars(str), 0),103104 parseInt(str)::105 assert std.isString(str) : 'Expected string, got ' + std.type(str);106 assert std.length(str) > 0 && str != '-' : 'Not an integer: "%s"' % [str];107 if str[0] == '-' then108 -parse_nat(str[1:], 10)109 else110 parse_nat(str, 10),111112 parseOctal(str)::113 assert std.isString(str) : 'Expected string, got ' + std.type(str);114 assert std.length(str) > 0 : 'Not an octal number: ""';115 parse_nat(str, 8),116117 parseHex(str)::118 assert std.isString(str) : 'Expected string, got ' + std.type(str);119 assert std.length(str) > 0 : 'Not hexadecimal: ""';120 parse_nat(str, 16),121122 split(str, c)::123 assert std.isString(str) : 'std.split first parameter should be a string, got ' + std.type(str);124 assert std.isString(c) : 'std.split second parameter should be a string, got ' + std.type(c);125 assert std.length(c) == 1 : 'std.split second parameter should have length 1, got ' + std.length(c);126 std.splitLimit(str, c, -1),127128 splitLimit:: $intrinsic(splitLimit),129130 strReplace:: $intrinsic(strReplace),131132 asciiUpper:: $intrinsic(asciiUpper),133134 asciiLower:: $intrinsic(asciiLower),135136 range:: $intrinsic(range),137138 repeat(what, count)::139 local joiner =140 if std.isString(what) then ''141 else if std.isArray(what) then []142 else error 'std.repeat first argument must be an array or a string';143 std.join(joiner, std.makeArray(count, function(i) what)),144145 slice:: $intrinsic(slice),146147 member:: $intrinsic(member),148149 count:: $intrinsic(count),150151 mod:: $intrinsic(mod),152153 map:: $intrinsic(map),154155 mapWithIndex(func, arr)::156 if !std.isFunction(func) then157 error ('std.mapWithIndex first param must be function, got ' + std.type(func))158 else if !std.isArray(arr) && !std.isString(arr) then159 error ('std.mapWithIndex second param must be array, got ' + std.type(arr))160 else161 std.makeArray(std.length(arr), function(i) func(i, arr[i])),162163 mapWithKey(func, obj)::164 if !std.isFunction(func) then165 error ('std.mapWithKey first param must be function, got ' + std.type(func))166 else if !std.isObject(obj) then167 error ('std.mapWithKey second param must be object, got ' + std.type(obj))168 else169 { [k]: func(k, obj[k]) for k in std.objectFields(obj) },170171 flatMap:: $intrinsic(flatMap),172173 join:: $intrinsic(join),174175 lines(arr)::176 std.join('\n', arr + ['']),177178 deepJoin(arr)::179 if std.isString(arr) then180 arr181 else if std.isArray(arr) then182 std.join('', [std.deepJoin(x) for x in arr])183 else184 error 'Expected string or array, got %s' % std.type(arr),185186187 format:: $intrinsic(format),188189 foldr:: $intrinsic(foldr),190191 foldl:: $intrinsic(foldl),192193 filterMap(filter_func, map_func, arr)::194 if !std.isFunction(filter_func) then195 error ('std.filterMap first param must be function, got ' + std.type(filter_func))196 else if !std.isFunction(map_func) then197 error ('std.filterMap second param must be function, got ' + std.type(map_func))198 else if !std.isArray(arr) then199 error ('std.filterMap third param must be array, got ' + std.type(arr))200 else201 std.map(map_func, std.filter(filter_func, arr)),202203 assertEqual(a, b)::204 if a == b then205 true206 else207 error 'Assertion failed. ' + a + ' != ' + b,208209 abs(n)::210 if !std.isNumber(n) then211 error 'std.abs expected number, got ' + std.type(n)212 else213 if n > 0 then n else -n,214215 sign(n)::216 if !std.isNumber(n) then217 error 'std.sign expected number, got ' + std.type(n)218 else219 if n > 0 then220 1221 else if n < 0 then222 -1223 else 0,224225 max(a, b)::226 if !std.isNumber(a) then227 error 'std.max first param expected number, got ' + std.type(a)228 else if !std.isNumber(b) then229 error 'std.max second param expected number, got ' + std.type(b)230 else231 if a > b then a else b,232233 min(a, b)::234 if !std.isNumber(a) then235 error 'std.min first param expected number, got ' + std.type(a)236 else if !std.isNumber(b) then237 error 'std.min second param expected number, got ' + std.type(b)238 else239 if a < b then a else b,240241 clamp(x, minVal, maxVal)::242 if x < minVal then minVal243 else if x > maxVal then maxVal244 else x,245246 flattenArrays(arrs)::247 std.foldl(function(a, b) a + b, arrs, []),248249 manifestIni(ini)::250 local body_lines(body) =251 std.join([], [252 local value_or_values = body[k];253 if std.isArray(value_or_values) then254 ['%s = %s' % [k, value] for value in value_or_values]255 else256 ['%s = %s' % [k, value_or_values]]257258 for k in std.objectFields(body)259 ]);260261 local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),262 main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],263 all_sections = [264 section_lines(k, ini.sections[k])265 for k in std.objectFields(ini.sections)266 ];267 std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),268269 manifestToml(value):: std.manifestTomlEx(value, ' '),270271 manifestTomlEx(value, indent)::272 local273 escapeStringToml = std.escapeStringJson,274 escapeKeyToml(key) =275 local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));276 if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),277 isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),278 isSection(v) = std.isObject(v) || isTableArray(v),279 renderValue(v, indexedPath, inline, cindent) =280 if v == true then281 'true'282 else if v == false then283 'false'284 else if v == null then285 error 'Tried to manifest "null" at ' + indexedPath286 else if std.isNumber(v) then287 '' + v288 else if std.isString(v) then289 escapeStringToml(v)290 else if std.isFunction(v) then291 error 'Tried to manifest function at ' + indexedPath292 else if std.isArray(v) then293 if std.length(v) == 0 then294 '[]'295 else296 local range = std.range(0, std.length(v) - 1);297 local new_indent = if inline then '' else cindent + indent;298 local separator = if inline then ' ' else '\n';299 local lines = ['[' + separator]300 + std.join([',' + separator],301 [302 [new_indent + renderValue(v[i], indexedPath + [i], true, '')]303 for i in range304 ])305 + [separator + (if inline then '' else cindent) + ']'];306 std.join('', lines)307 else if std.isObject(v) then308 local lines = ['{ ']309 + std.join([', '],310 [311 [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]312 for k in std.objectFields(v)313 ])314 + [' }'];315 std.join('', lines),316 renderTableInternal(v, path, indexedPath, cindent) =317 local kvp = std.flattenArrays([318 [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]319 for k in std.objectFields(v)320 if !isSection(v[k])321 ]);322 local sections = [std.join('\n', kvp)] + [323 (324 if std.isObject(v[k]) then325 renderTable(v[k], path + [k], indexedPath + [k], cindent)326 else327 renderTableArray(v[k], path + [k], indexedPath + [k], cindent)328 )329 for k in std.objectFields(v)330 if isSection(v[k])331 ];332 std.join('\n\n', sections),333 renderTable(v, path, indexedPath, cindent) =334 cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'335 + (if v == {} then '' else '\n')336 + renderTableInternal(v, path, indexedPath, cindent + indent),337 renderTableArray(v, path, indexedPath, cindent) =338 local range = std.range(0, std.length(v) - 1);339 local sections = [340 (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'341 + (if v[i] == {} then '' else '\n')342 + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))343 for i in range344 ];345 std.join('\n\n', sections);346 if std.isObject(value) then347 renderTableInternal(value, [], [], '')348 else349 error 'TOML body must be an object. Got ' + std.type(value),350351 escapeStringJson:: $intrinsic(escapeStringJson),352353 escapeStringPython(str)::354 std.escapeStringJson(str),355356 escapeStringBash(str_)::357 local str = std.toString(str_);358 local trans(ch) =359 if ch == "'" then360 "'\"'\"'"361 else362 ch;363 "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),364365 escapeStringDollars(str_)::366 local str = std.toString(str_);367 local trans(ch) =368 if ch == '$' then369 '$$'370 else371 ch;372 std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),373374 manifestJson(value):: std.manifestJsonEx(value, ' ') tailstrict,375376 manifestJsonEx:: $intrinsic(manifestJsonEx),377378 manifestYamlDoc:: $intrinsic(manifestYamlDoc),379380 manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::381 if !std.isArray(value) then382 error 'manifestYamlStream only takes arrays, got ' + std.type(value)383 else384 '---\n' + std.join(385 '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]386 ) + if c_document_end then '\n...\n' else '\n',387388389 manifestPython(v)::390 if std.isObject(v) then391 local fields = [392 '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]393 for k in std.objectFields(v)394 ];395 '{%s}' % [std.join(', ', fields)]396 else if std.isArray(v) then397 '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]398 else if std.isString(v) then399 '%s' % [std.escapeStringPython(v)]400 else if std.isFunction(v) then401 error 'cannot manifest function'402 else if std.isNumber(v) then403 std.toString(v)404 else if v == true then405 'True'406 else if v == false then407 'False'408 else if v == null then409 'None',410411 manifestPythonVars(conf)::412 local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];413 std.join('\n', vars + ['']),414415 manifestXmlJsonml(value)::416 if !std.isArray(value) then417 error 'Expected a JSONML value (an array), got %s' % std.type(value)418 else419 local aux(v) =420 if std.isString(v) then421 v422 else423 local tag = v[0];424 local has_attrs = std.length(v) > 1 && std.isObject(v[1]);425 local attrs = if has_attrs then v[1] else {};426 local children = if has_attrs then v[2:] else v[1:];427 local attrs_str =428 std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);429 std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);430431 aux(value),432433 local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',434 local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },435436 base64:: $intrinsic(base64),437438 base64DecodeBytes:: $intrinsic(base64DecodeBytes),439440 base64Decode:: $intrinsic(base64Decode),441442 reverse:: $intrinsic(reverse),443444 sort:: $intrinsic(sort),445446 uniq(arr, keyF=id)::447 local f(a, b) =448 if std.length(a) == 0 then449 [b]450 else if keyF(a[std.length(a) - 1]) == keyF(b) then451 a452 else453 a + [b];454 std.foldl(f, arr, []),455456 set(arr, keyF=id)::457 std.uniq(std.sort(arr, keyF), keyF),458459 setMember(x, arr, keyF=id)::460 // TODO(dcunnin): Binary chop for O(log n) complexity461 std.length(std.setInter([x], arr, keyF)) > 0,462463 setUnion(a, b, keyF=id)::464 // NOTE: order matters, values in `a` win465 local aux(a, b, i, j, acc) =466 if i >= std.length(a) then467 acc + b[j:]468 else if j >= std.length(b) then469 acc + a[i:]470 else471 local ak = keyF(a[i]);472 local bk = keyF(b[j]);473 if ak == bk then474 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict475 else if ak < bk then476 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict477 else478 aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;479 aux(a, b, 0, 0, []),480481 setInter(a, b, keyF=id)::482 local aux(a, b, i, j, acc) =483 if i >= std.length(a) || j >= std.length(b) then484 acc485 else486 if keyF(a[i]) == keyF(b[j]) then487 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict488 else if keyF(a[i]) < keyF(b[j]) then489 aux(a, b, i + 1, j, acc) tailstrict490 else491 aux(a, b, i, j + 1, acc) tailstrict;492 aux(a, b, 0, 0, []) tailstrict,493494 setDiff(a, b, keyF=id)::495 local aux(a, b, i, j, acc) =496 if i >= std.length(a) then497 acc498 else if j >= std.length(b) then499 acc + a[i:]500 else501 if keyF(a[i]) == keyF(b[j]) then502 aux(a, b, i + 1, j + 1, acc) tailstrict503 else if keyF(a[i]) < keyF(b[j]) then504 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict505 else506 aux(a, b, i, j + 1, acc) tailstrict;507 aux(a, b, 0, 0, []) tailstrict,508509 mergePatch(target, patch)::510 if std.isObject(patch) then511 local target_object =512 if std.isObject(target) then target else {};513514 local target_fields =515 if std.isObject(target_object) then std.objectFields(target_object) else [];516517 local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];518 local both_fields = std.setUnion(target_fields, std.objectFields(patch));519520 {521 [k]:522 if !std.objectHas(patch, k) then523 target_object[k]524 else if !std.objectHas(target_object, k) then525 std.mergePatch(null, patch[k]) tailstrict526 else527 std.mergePatch(target_object[k], patch[k]) tailstrict528 for k in std.setDiff(both_fields, null_fields)529 }530 else531 patch,532533 objectFields(o)::534 std.objectFieldsEx(o, false),535536 objectFieldsAll(o)::537 std.objectFieldsEx(o, true),538539 objectHas(o, f)::540 std.objectHasEx(o, f, false),541542 objectHasAll(o, f)::543 std.objectHasEx(o, f, true),544545 objectValues(o)::546 [o[k] for k in std.objectFields(o)],547548 objectValuesAll(o)::549 [o[k] for k in std.objectFieldsAll(o)],550551 equals:: $intrinsic(equals),552553 resolvePath(f, r)::554 local arr = std.split(f, '/');555 std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),556557 prune(a)::558 local isContent(b) =559 if b == null then560 false561 else if std.isArray(b) then562 std.length(b) > 0563 else if std.isObject(b) then564 std.length(b) > 0565 else566 true;567 if std.isArray(a) then568 [std.prune(x) for x in a if isContent($.prune(x))]569 else if std.isObject(a) then {570 [x]: $.prune(a[x])571 for x in std.objectFields(a)572 if isContent(std.prune(a[x]))573 } else574 a,575576 findSubstr(pat, str)::577 if !std.isString(pat) then578 error 'findSubstr first parameter should be a string, got ' + std.type(pat)579 else if !std.isString(str) then580 error 'findSubstr second parameter should be a string, got ' + std.type(str)581 else582 local pat_len = std.length(pat);583 local str_len = std.length(str);584 if pat_len == 0 || str_len == 0 || pat_len > str_len then585 []586 else587 std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),588589 find(value, arr)::590 if !std.isArray(arr) then591 error 'find second parameter should be an array, got ' + std.type(arr)592 else593 std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),594}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 decodeUTF8:: $intrinsic(decodeUTF8),22 md5:: $intrinsic(md5),23 trace:: $intrinsic(trace),24 id:: $intrinsic(id),25 parseJson:: $intrinsic(parseJson),26 parseYaml:: $intrinsic(parseYaml),2728 log:: $intrinsic(log),29 pow:: $intrinsic(pow),30 sqrt:: $intrinsic(sqrt),3132 sin:: $intrinsic(sin),33 cos:: $intrinsic(cos),34 tan:: $intrinsic(tan),35 asin:: $intrinsic(asin),36 acos:: $intrinsic(acos),37 atan:: $intrinsic(atan),3839 exp:: $intrinsic(exp),40 mantissa:: $intrinsic(mantissa),41 exponent:: $intrinsic(exponent),4243 isString(v):: std.type(v) == 'string',44 isNumber(v):: std.type(v) == 'number',45 isBoolean(v):: std.type(v) == 'boolean',46 isObject(v):: std.type(v) == 'object',47 isArray(v):: std.type(v) == 'array',48 isFunction(v):: std.type(v) == 'function',4950 toString(a)::51 if std.type(a) == 'string' then a else '' + a,5253 substr:: $intrinsic(substr),5455 startsWith(a, b)::56 if std.length(a) < std.length(b) then57 false58 else59 std.substr(a, 0, std.length(b)) == b,6061 endsWith(a, b)::62 if std.length(a) < std.length(b) then63 false64 else65 std.substr(a, std.length(a) - std.length(b), std.length(b)) == b,6667 lstripChars(str, chars)::68 if std.length(str) > 0 && std.member(chars, str[0]) then69 std.lstripChars(str[1:], chars)70 else71 str,7273 rstripChars(str, chars)::74 local len = std.length(str);75 if len > 0 && std.member(chars, str[len - 1]) then76 std.rstripChars(str[:len - 1], chars)77 else78 str,7980 stripChars(str, chars)::81 std.lstripChars(std.rstripChars(str, chars), chars),8283 stringChars(str)::84 std.makeArray(std.length(str), function(i) str[i]),8586 local parse_nat(str, base) =87 assert base > 0 && base <= 16 : 'integer base %d invalid' % base;88 // These codepoints are in ascending order:89 local zero_code = std.codepoint('0');90 local upper_a_code = std.codepoint('A');91 local lower_a_code = std.codepoint('a');92 local addDigit(aggregate, char) =93 local code = std.codepoint(char);94 local digit = if code >= lower_a_code then95 code - lower_a_code + 1096 else if code >= upper_a_code then97 code - upper_a_code + 1098 else99 code - zero_code;100 assert digit >= 0 && digit < base : '%s is not a base %d integer' % [str, base];101 base * aggregate + digit;102 std.foldl(addDigit, std.stringChars(str), 0),103104 parseInt(str)::105 assert std.isString(str) : 'Expected string, got ' + std.type(str);106 assert std.length(str) > 0 && str != '-' : 'Not an integer: "%s"' % [str];107 if str[0] == '-' then108 -parse_nat(str[1:], 10)109 else110 parse_nat(str, 10),111112 parseOctal(str)::113 assert std.isString(str) : 'Expected string, got ' + std.type(str);114 assert std.length(str) > 0 : 'Not an octal number: ""';115 parse_nat(str, 8),116117 parseHex(str)::118 assert std.isString(str) : 'Expected string, got ' + std.type(str);119 assert std.length(str) > 0 : 'Not hexadecimal: ""';120 parse_nat(str, 16),121122 split(str, c)::123 assert std.isString(str) : 'std.split first parameter should be a string, got ' + std.type(str);124 assert std.isString(c) : 'std.split second parameter should be a string, got ' + std.type(c);125 assert std.length(c) == 1 : 'std.split second parameter should have length 1, got ' + std.length(c);126 std.splitLimit(str, c, -1),127128 splitLimit:: $intrinsic(splitLimit),129130 strReplace:: $intrinsic(strReplace),131132 asciiUpper:: $intrinsic(asciiUpper),133134 asciiLower:: $intrinsic(asciiLower),135136 range:: $intrinsic(range),137138 repeat(what, count)::139 local joiner =140 if std.isString(what) then ''141 else if std.isArray(what) then []142 else error 'std.repeat first argument must be an array or a string';143 std.join(joiner, std.makeArray(count, function(i) what)),144145 slice:: $intrinsic(slice),146147 member:: $intrinsic(member),148149 count:: $intrinsic(count),150151 mod:: $intrinsic(mod),152153 map:: $intrinsic(map),154155 mapWithIndex(func, arr)::156 if !std.isFunction(func) then157 error ('std.mapWithIndex first param must be function, got ' + std.type(func))158 else if !std.isArray(arr) && !std.isString(arr) then159 error ('std.mapWithIndex second param must be array, got ' + std.type(arr))160 else161 std.makeArray(std.length(arr), function(i) func(i, arr[i])),162163 mapWithKey(func, obj)::164 if !std.isFunction(func) then165 error ('std.mapWithKey first param must be function, got ' + std.type(func))166 else if !std.isObject(obj) then167 error ('std.mapWithKey second param must be object, got ' + std.type(obj))168 else169 { [k]: func(k, obj[k]) for k in std.objectFields(obj) },170171 flatMap:: $intrinsic(flatMap),172173 join:: $intrinsic(join),174175 lines(arr)::176 std.join('\n', arr + ['']),177178 deepJoin(arr)::179 if std.isString(arr) then180 arr181 else if std.isArray(arr) then182 std.join('', [std.deepJoin(x) for x in arr])183 else184 error 'Expected string or array, got %s' % std.type(arr),185186187 format:: $intrinsic(format),188189 foldr:: $intrinsic(foldr),190191 foldl:: $intrinsic(foldl),192193 filterMap(filter_func, map_func, arr)::194 if !std.isFunction(filter_func) then195 error ('std.filterMap first param must be function, got ' + std.type(filter_func))196 else if !std.isFunction(map_func) then197 error ('std.filterMap second param must be function, got ' + std.type(map_func))198 else if !std.isArray(arr) then199 error ('std.filterMap third param must be array, got ' + std.type(arr))200 else201 std.map(map_func, std.filter(filter_func, arr)),202203 assertEqual(a, b)::204 if a == b then205 true206 else207 error 'Assertion failed. ' + a + ' != ' + b,208209 abs(n)::210 if !std.isNumber(n) then211 error 'std.abs expected number, got ' + std.type(n)212 else213 if n > 0 then n else -n,214215 sign(n)::216 if !std.isNumber(n) then217 error 'std.sign expected number, got ' + std.type(n)218 else219 if n > 0 then220 1221 else if n < 0 then222 -1223 else 0,224225 max(a, b)::226 if !std.isNumber(a) then227 error 'std.max first param expected number, got ' + std.type(a)228 else if !std.isNumber(b) then229 error 'std.max second param expected number, got ' + std.type(b)230 else231 if a > b then a else b,232233 min(a, b)::234 if !std.isNumber(a) then235 error 'std.min first param expected number, got ' + std.type(a)236 else if !std.isNumber(b) then237 error 'std.min second param expected number, got ' + std.type(b)238 else239 if a < b then a else b,240241 clamp(x, minVal, maxVal)::242 if x < minVal then minVal243 else if x > maxVal then maxVal244 else x,245246 flattenArrays(arrs)::247 std.foldl(function(a, b) a + b, arrs, []),248249 manifestIni(ini)::250 local body_lines(body) =251 std.join([], [252 local value_or_values = body[k];253 if std.isArray(value_or_values) then254 ['%s = %s' % [k, value] for value in value_or_values]255 else256 ['%s = %s' % [k, value_or_values]]257258 for k in std.objectFields(body)259 ]);260261 local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),262 main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],263 all_sections = [264 section_lines(k, ini.sections[k])265 for k in std.objectFields(ini.sections)266 ];267 std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),268269 manifestToml(value):: std.manifestTomlEx(value, ' '),270271 manifestTomlEx(value, indent)::272 local273 escapeStringToml = std.escapeStringJson,274 escapeKeyToml(key) =275 local bare_allowed = std.set(std.stringChars('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-'));276 if std.setUnion(std.set(std.stringChars(key)), bare_allowed) == bare_allowed then key else escapeStringToml(key),277 isTableArray(v) = std.isArray(v) && std.length(v) > 0 && std.foldl(function(a, b) a && std.isObject(b), v, true),278 isSection(v) = std.isObject(v) || isTableArray(v),279 renderValue(v, indexedPath, inline, cindent) =280 if v == true then281 'true'282 else if v == false then283 'false'284 else if v == null then285 error 'Tried to manifest "null" at ' + indexedPath286 else if std.isNumber(v) then287 '' + v288 else if std.isString(v) then289 escapeStringToml(v)290 else if std.isFunction(v) then291 error 'Tried to manifest function at ' + indexedPath292 else if std.isArray(v) then293 if std.length(v) == 0 then294 '[]'295 else296 local range = std.range(0, std.length(v) - 1);297 local new_indent = if inline then '' else cindent + indent;298 local separator = if inline then ' ' else '\n';299 local lines = ['[' + separator]300 + std.join([',' + separator],301 [302 [new_indent + renderValue(v[i], indexedPath + [i], true, '')]303 for i in range304 ])305 + [separator + (if inline then '' else cindent) + ']'];306 std.join('', lines)307 else if std.isObject(v) then308 local lines = ['{ ']309 + std.join([', '],310 [311 [escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], true, '')]312 for k in std.objectFields(v)313 ])314 + [' }'];315 std.join('', lines),316 renderTableInternal(v, path, indexedPath, cindent) =317 local kvp = std.flattenArrays([318 [cindent + escapeKeyToml(k) + ' = ' + renderValue(v[k], indexedPath + [k], false, cindent)]319 for k in std.objectFields(v)320 if !isSection(v[k])321 ]);322 local sections = [std.join('\n', kvp)] + [323 (324 if std.isObject(v[k]) then325 renderTable(v[k], path + [k], indexedPath + [k], cindent)326 else327 renderTableArray(v[k], path + [k], indexedPath + [k], cindent)328 )329 for k in std.objectFields(v)330 if isSection(v[k])331 ];332 std.join('\n\n', sections),333 renderTable(v, path, indexedPath, cindent) =334 cindent + '[' + std.join('.', std.map(escapeKeyToml, path)) + ']'335 + (if v == {} then '' else '\n')336 + renderTableInternal(v, path, indexedPath, cindent + indent),337 renderTableArray(v, path, indexedPath, cindent) =338 local range = std.range(0, std.length(v) - 1);339 local sections = [340 (cindent + '[[' + std.join('.', std.map(escapeKeyToml, path)) + ']]'341 + (if v[i] == {} then '' else '\n')342 + renderTableInternal(v[i], path, indexedPath + [i], cindent + indent))343 for i in range344 ];345 std.join('\n\n', sections);346 if std.isObject(value) then347 renderTableInternal(value, [], [], '')348 else349 error 'TOML body must be an object. Got ' + std.type(value),350351 escapeStringJson:: $intrinsic(escapeStringJson),352353 escapeStringPython(str)::354 std.escapeStringJson(str),355356 escapeStringBash(str_)::357 local str = std.toString(str_);358 local trans(ch) =359 if ch == "'" then360 "'\"'\"'"361 else362 ch;363 "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),364365 escapeStringDollars(str_)::366 local str = std.toString(str_);367 local trans(ch) =368 if ch == '$' then369 '$$'370 else371 ch;372 std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),373374 manifestJson(value):: std.manifestJsonEx(value, ' ') tailstrict,375376 manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),377378 manifestJsonEx:: $intrinsic(manifestJsonEx),379380 manifestYamlDoc:: $intrinsic(manifestYamlDoc),381382 manifestYamlStream(value, indent_array_in_object=false, c_document_end=true)::383 if !std.isArray(value) then384 error 'manifestYamlStream only takes arrays, got ' + std.type(value)385 else386 '---\n' + std.join(387 '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object) for e in value]388 ) + if c_document_end then '\n...\n' else '\n',389390391 manifestPython(v)::392 if std.isObject(v) then393 local fields = [394 '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]395 for k in std.objectFields(v)396 ];397 '{%s}' % [std.join(', ', fields)]398 else if std.isArray(v) then399 '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]400 else if std.isString(v) then401 '%s' % [std.escapeStringPython(v)]402 else if std.isFunction(v) then403 error 'cannot manifest function'404 else if std.isNumber(v) then405 std.toString(v)406 else if v == true then407 'True'408 else if v == false then409 'False'410 else if v == null then411 'None',412413 manifestPythonVars(conf)::414 local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];415 std.join('\n', vars + ['']),416417 manifestXmlJsonml(value)::418 if !std.isArray(value) then419 error 'Expected a JSONML value (an array), got %s' % std.type(value)420 else421 local aux(v) =422 if std.isString(v) then423 v424 else425 local tag = v[0];426 local has_attrs = std.length(v) > 1 && std.isObject(v[1]);427 local attrs = if has_attrs then v[1] else {};428 local children = if has_attrs then v[2:] else v[1:];429 local attrs_str =430 std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);431 std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);432433 aux(value),434435 local base64_table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',436 local base64_inv = { [base64_table[i]]: i for i in std.range(0, 63) },437438 base64:: $intrinsic(base64),439440 base64DecodeBytes:: $intrinsic(base64DecodeBytes),441442 base64Decode:: $intrinsic(base64Decode),443444 reverse:: $intrinsic(reverse),445446 sort:: $intrinsic(sort),447448 uniq(arr, keyF=id)::449 local f(a, b) =450 if std.length(a) == 0 then451 [b]452 else if keyF(a[std.length(a) - 1]) == keyF(b) then453 a454 else455 a + [b];456 std.foldl(f, arr, []),457458 set(arr, keyF=id)::459 std.uniq(std.sort(arr, keyF), keyF),460461 setMember(x, arr, keyF=id)::462 // TODO(dcunnin): Binary chop for O(log n) complexity463 std.length(std.setInter([x], arr, keyF)) > 0,464465 setUnion(a, b, keyF=id)::466 // NOTE: order matters, values in `a` win467 local aux(a, b, i, j, acc) =468 if i >= std.length(a) then469 acc + b[j:]470 else if j >= std.length(b) then471 acc + a[i:]472 else473 local ak = keyF(a[i]);474 local bk = keyF(b[j]);475 if ak == bk then476 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict477 else if ak < bk then478 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict479 else480 aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;481 aux(a, b, 0, 0, []),482483 setInter(a, b, keyF=id)::484 local aux(a, b, i, j, acc) =485 if i >= std.length(a) || j >= std.length(b) then486 acc487 else488 if keyF(a[i]) == keyF(b[j]) then489 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict490 else if keyF(a[i]) < keyF(b[j]) then491 aux(a, b, i + 1, j, acc) tailstrict492 else493 aux(a, b, i, j + 1, acc) tailstrict;494 aux(a, b, 0, 0, []) tailstrict,495496 setDiff(a, b, keyF=id)::497 local aux(a, b, i, j, acc) =498 if i >= std.length(a) then499 acc500 else if j >= std.length(b) then501 acc + a[i:]502 else503 if keyF(a[i]) == keyF(b[j]) then504 aux(a, b, i + 1, j + 1, acc) tailstrict505 else if keyF(a[i]) < keyF(b[j]) then506 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict507 else508 aux(a, b, i, j + 1, acc) tailstrict;509 aux(a, b, 0, 0, []) tailstrict,510511 mergePatch(target, patch)::512 if std.isObject(patch) then513 local target_object =514 if std.isObject(target) then target else {};515516 local target_fields =517 if std.isObject(target_object) then std.objectFields(target_object) else [];518519 local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];520 local both_fields = std.setUnion(target_fields, std.objectFields(patch));521522 {523 [k]:524 if !std.objectHas(patch, k) then525 target_object[k]526 else if !std.objectHas(target_object, k) then527 std.mergePatch(null, patch[k]) tailstrict528 else529 std.mergePatch(target_object[k], patch[k]) tailstrict530 for k in std.setDiff(both_fields, null_fields)531 }532 else533 patch,534535 get(o, f, default = null, inc_hidden = true)::536 if std.objectHasEx(o, f, inc_hidden) then o[f] else default,537538 objectFields(o)::539 std.objectFieldsEx(o, false),540541 objectFieldsAll(o)::542 std.objectFieldsEx(o, true),543544 objectHas(o, f)::545 std.objectHasEx(o, f, false),546547 objectHasAll(o, f)::548 std.objectHasEx(o, f, true),549550 objectValues(o)::551 [o[k] for k in std.objectFields(o)],552553 objectValuesAll(o)::554 [o[k] for k in std.objectFieldsAll(o)],555556 equals:: $intrinsic(equals),557558 resolvePath(f, r)::559 local arr = std.split(f, '/');560 std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),561562 prune(a)::563 local isContent(b) =564 if b == null then565 false566 else if std.isArray(b) then567 std.length(b) > 0568 else if std.isObject(b) then569 std.length(b) > 0570 else571 true;572 if std.isArray(a) then573 [std.prune(x) for x in a if isContent($.prune(x))]574 else if std.isObject(a) then {575 [x]: $.prune(a[x])576 for x in std.objectFields(a)577 if isContent(std.prune(a[x]))578 } else579 a,580581 findSubstr(pat, str)::582 if !std.isString(pat) then583 error 'findSubstr first parameter should be a string, got ' + std.type(pat)584 else if !std.isString(str) then585 error 'findSubstr second parameter should be a string, got ' + std.type(str)586 else587 local pat_len = std.length(pat);588 local str_len = std.length(str);589 if pat_len == 0 || str_len == 0 || pat_len > str_len then590 []591 else592 std.filter(function(i) str[i:i + pat_len] == pat, std.range(0, str_len - pat_len)),593594 find(value, arr)::595 if !std.isArray(arr) then596 error 'find second parameter should be an array, got ' + std.type(arr)597 else598 std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),599}