difftreelog
Merge pull request #124 from pawelbeza/missing-std-features
in: master
10 files changed
Cargo.lockdiffbeforeafterboth--- a/Cargo.lock
+++ b/Cargo.lock
@@ -375,7 +375,9 @@
"serde",
"serde_json",
"serde_yaml_with_quirks",
+ "sha1",
"sha2",
+ "sha3",
"structdump",
]
@@ -388,6 +390,15 @@
]
[[package]]
+name = "keccak"
+version = "0.1.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8f6d5ed8676d904364de097082f4e7d240b571b67989ced0240f08b7f966f940"
+dependencies = [
+ "cpufeatures",
+]
+
+[[package]]
name = "libc"
version = "0.2.139"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -676,6 +687,17 @@
]
[[package]]
+name = "sha1"
+version = "0.10.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f04293dc80c3993519f2d7f6f511707ee7094fe0c6d3406feb330cdb3540eba3"
+dependencies = [
+ "cfg-if",
+ "cpufeatures",
+ "digest",
+]
+
+[[package]]
name = "sha2"
version = "0.10.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -687,6 +709,16 @@
]
[[package]]
+name = "sha3"
+version = "0.10.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "75872d278a8f37ef87fa0ddbda7802605cb18344497949862c0d4dcb291eba60"
+dependencies = [
+ "digest",
+ "keccak",
+]
+
+[[package]]
name = "smallvec"
version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
crates/jrsonnet-stdlib/Cargo.tomldiffbeforeafterboth--- a/crates/jrsonnet-stdlib/Cargo.toml
+++ b/crates/jrsonnet-stdlib/Cargo.toml
@@ -32,8 +32,12 @@
# std.md5
md5 = "0.7.0"
+# std.sha1
+sha1 = "0.10.5"
# std.sha256, std.sha512
sha2 = "0.10.6"
+# std.sha3
+sha3 = "0.10.8"
# std.base64
base64 = "0.21.0"
# std.parseJson
crates/jrsonnet-stdlib/src/arrays.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/arrays.rs
+++ b/crates/jrsonnet-stdlib/src/arrays.rs
@@ -253,3 +253,26 @@
}
Ok(Val::Num(arr.iter().sum::<f64>() / (arr.len() as f64)))
}
+
+#[builtin]
+pub fn builtin_remove_at(
+ arr: ArrValue,
+ index: usize,
+) -> Result<ArrValue> {
+ let newArrLeft = arr.clone().slice(None, Some(index), None);
+ let newArrRight = arr.clone().slice(Some(index + 1), None, None);
+ return Ok(ArrValue::extended(
+ newArrLeft.unwrap_or(ArrValue::empty()),
+ newArrRight.unwrap_or(ArrValue::empty()))
+ );
+}
+
+#[builtin]
+pub fn builtin_remove(arr: ArrValue, elem: Val) -> Result<ArrValue> {
+ for (index, item) in arr.iter().enumerate() {
+ if equals(&item?, &elem)? {
+ return builtin_remove_at(arr.clone(), index)
+ }
+ }
+ Ok(arr)
+}
crates/jrsonnet-stdlib/src/hash.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/hash.rs
+++ b/crates/jrsonnet-stdlib/src/hash.rs
@@ -16,3 +16,15 @@
use sha2::digest::Digest;
format!("{:x}", sha2::Sha512::digest(s.as_bytes()))
}
+
+#[builtin]
+pub fn builtin_sha1(s: IStr) -> String {
+ use sha1::digest::Digest;
+ format!("{:x}", sha1::Sha1::digest(s.as_bytes()))
+}
+
+#[builtin]
+pub fn builtin_sha3(s: IStr) -> String {
+ use sha3::digest::Digest;
+ format!("{:x}", sha3::Sha3_512::digest(s.as_bytes()))
+}
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -83,6 +83,8 @@
("contains", builtin_member::INST),
("count", builtin_count::INST),
("avg", builtin_avg::INST),
+ ("removeAt", builtin_remove_at::INST),
+ ("remove", builtin_remove::INST),
// Math
("abs", builtin_abs::INST),
("sign", builtin_sign::INST),
@@ -104,11 +106,17 @@
("exp", builtin_exp::INST),
("mantissa", builtin_mantissa::INST),
("exponent", builtin_exponent::INST),
+ ("round", builtin_round::INST),
+ ("isEven", builtin_is_even::INST),
+ ("isOdd", builtin_is_odd::INST),
+ ("isInteger", builtin_is_integer::INST),
+ ("isDecimal", builtin_is_decimal::INST),
// Operator
("mod", builtin_mod::INST),
("primitiveEquals", builtin_primitive_equals::INST),
("equals", builtin_equals::INST),
("xor", builtin_xor::INST),
+ ("xnor", builtin_xnor::INST),
("format", builtin_format::INST),
// Sort
("sort", builtin_sort::INST),
@@ -118,8 +126,10 @@
("maxArray", builtin_max_array::INST),
// Hash
("md5", builtin_md5::INST),
+ ("sha1", builtin_sha1::INST),
("sha256", builtin_sha256::INST),
("sha512", builtin_sha512::INST),
+ ("sha3", builtin_sha3::INST),
// Encoding
("encodeUTF8", builtin_encode_utf8::INST),
("decodeUTF8", builtin_decode_utf8::INST),
@@ -129,6 +139,7 @@
// Objects
("objectFieldsEx", builtin_object_fields_ex::INST),
("objectHasEx", builtin_object_has_ex::INST),
+ ("objectRemoveKey", builtin_object_remove_key::INST),
// Manifest
("escapeStringJson", builtin_escape_string_json::INST),
("manifestJsonEx", builtin_manifest_json_ex::INST),
@@ -142,6 +153,8 @@
("substr", builtin_substr::INST),
("char", builtin_char::INST),
("strReplace", builtin_str_replace::INST),
+ ("isEmpty", builtin_is_empty::INST),
+ ("equalsIgnoreCase", builtin_equals_ignore_case::INST),
("splitLimit", builtin_splitlimit::INST),
("asciiUpper", builtin_ascii_upper::INST),
("asciiLower", builtin_ascii_lower::INST),
crates/jrsonnet-stdlib/src/math.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/math.rs
+++ b/crates/jrsonnet-stdlib/src/math.rs
@@ -114,3 +114,28 @@
pub fn builtin_exponent(x: f64) -> i16 {
frexp(x).1
}
+
+#[builtin]
+pub fn builtin_round(x: f64) -> f64 {
+ x.round()
+}
+
+#[builtin]
+pub fn builtin_is_even(x: f64) -> bool {
+ builtin_round(x) % 2.0 == 0.0
+}
+
+#[builtin]
+pub fn builtin_is_odd(x: f64) -> bool {
+ builtin_round(x) % 2.0 == 1.0
+}
+
+#[builtin]
+pub fn builtin_is_integer(x: f64) -> bool {
+ builtin_round(x) == x
+}
+
+#[builtin]
+pub fn builtin_is_decimal(x: f64) -> bool {
+ builtin_round(x) != x
+}
crates/jrsonnet-stdlib/src/objects.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/objects.rs
+++ b/crates/jrsonnet-stdlib/src/objects.rs
@@ -1,9 +1,10 @@
use jrsonnet_evaluator::{
function::builtin,
val::{StrValue, Val},
- IStr, ObjValue,
+ IStr, ObjValue, ObjValueBuilder,
};
+
#[builtin]
pub fn builtin_object_fields_ex(
obj: ObjValue,
@@ -27,3 +28,16 @@
pub fn builtin_object_has_ex(obj: ObjValue, fname: IStr, hidden: bool) -> bool {
obj.has_field_ex(fname, hidden)
}
+
+#[builtin]
+pub fn builtin_object_remove_key(obj: ObjValue, key: IStr) -> ObjValue {
+ let mut new_obj = ObjValueBuilder::with_capacity(obj.len() - 1);
+ for (k, v) in obj.iter() {
+ if k == key {
+ continue
+ }
+ new_obj.member(k).value_unchecked(v.unwrap())
+ }
+
+ new_obj.build()
+}
crates/jrsonnet-stdlib/src/operator.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/operator.rs
+++ b/crates/jrsonnet-stdlib/src/operator.rs
@@ -39,6 +39,11 @@
}
#[builtin]
+pub fn builtin_xnor(x: bool, y: bool) -> bool {
+ x == y
+}
+
+#[builtin]
pub fn builtin_format(str: IStr, vals: Val) -> Result<String> {
std_format(&str, vals)
}
crates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth1{2 local std = self,3 local id = std.id,45 thisFile:: error 'std.thisFile is deprecated, to enable its support in jrsonnet - recompile it with "legacy-this-file" support.\nThis will slow down stdlib caching a bit, though',67 toString(a):: '' + a,89 lstripChars(str, chars)::10 if std.length(str) > 0 && std.member(chars, str[0]) then11 std.lstripChars(str[1:], chars)12 else13 str,1415 rstripChars(str, chars)::16 local len = std.length(str);17 if len > 0 && std.member(chars, str[len - 1]) then18 std.rstripChars(str[:len - 1], chars)19 else20 str,2122 stripChars(str, chars)::23 std.lstripChars(std.rstripChars(str, chars), chars),2425 stringChars(str)::26 std.makeArray(std.length(str), function(i) str[i]),2728 splitLimitR(str, c, maxsplits)::29 if maxsplits == -1 then30 std.splitLimit(str, c, -1)31 else32 local revStr(str) = std.join('', std.reverse(std.stringChars(str)));33 std.map(function(e) revStr(e), std.reverse(std.splitLimit(revStr(str), revStr(c), maxsplits))),3435 split(str, c):: std.splitLimit(str, c, -1),3637 mapWithIndex(func, arr)::38 if !std.isFunction(func) then39 error ('std.mapWithIndex first param must be function, got ' + std.type(func))40 else if !std.isArray(arr) && !std.isString(arr) then41 error ('std.mapWithIndex second param must be array, got ' + std.type(arr))42 else43 std.makeArray(std.length(arr), function(i) func(i, arr[i])),4445 mapWithKey(func, obj)::46 if !std.isFunction(func) then47 error ('std.mapWithKey first param must be function, got ' + std.type(func))48 else if !std.isObject(obj) then49 error ('std.mapWithKey second param must be object, got ' + std.type(obj))50 else51 { [k]: func(k, obj[k]) for k in std.objectFields(obj) },5253 lines(arr)::54 std.join('\n', arr + ['']),5556 deepJoin(arr)::57 if std.isString(arr) then58 arr59 else if std.isArray(arr) then60 std.join('', [std.deepJoin(x) for x in arr])61 else62 error 'Expected string or array, got %s' % std.type(arr),6364 filterMap(filter_func, map_func, arr)::65 if !std.isFunction(filter_func) then66 error ('std.filterMap first param must be function, got ' + std.type(filter_func))67 else if !std.isFunction(map_func) then68 error ('std.filterMap second param must be function, got ' + std.type(map_func))69 else if !std.isArray(arr) then70 error ('std.filterMap third param must be array, got ' + std.type(arr))71 else72 std.map(map_func, std.filter(filter_func, arr)),7374 assertEqual(a, b)::75 if a == b then76 true77 else78 error 'Assertion failed. ' + a + ' != ' + b,7980 clamp(x, minVal, maxVal)::81 if x < minVal then minVal82 else if x > maxVal then maxVal83 else x,8485 flattenArrays(arrs)::86 std.foldl(function(a, b) a + b, arrs, []),8788 manifestIni(ini)::89 local body_lines(body) =90 std.join([], [91 local value_or_values = body[k];92 if std.isArray(value_or_values) then93 ['%s = %s' % [k, value] for value in value_or_values]94 else95 ['%s = %s' % [k, value_or_values]]9697 for k in std.objectFields(body)98 ]);99100 local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),101 main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],102 all_sections = [103 section_lines(k, ini.sections[k])104 for k in std.objectFields(ini.sections)105 ];106 std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),107108 manifestToml(value):: std.manifestTomlEx(value, ' '),109110 escapeStringPython(str)::111 std.escapeStringJson(str),112113 escapeStringBash(str_)::114 local str = std.toString(str_);115 local trans(ch) =116 if ch == "'" then117 "'\"'\"'"118 else119 ch;120 "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),121122 escapeStringDollars(str_)::123 local str = std.toString(str_);124 local trans(ch) =125 if ch == '$' then126 '$$'127 else128 ch;129 std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),130131 local xml_escapes = {132 '<': '<',133 '>': '>',134 '&': '&',135 '"': '"',136 "'": ''',137 },138139 escapeStringXML(str_)::140 local str = std.toString(str_);141 std.join('', [std.get(xml_escapes, ch, ch) for ch in std.stringChars(str)]),142143 manifestJson(value):: std.manifestJsonEx(value, ' ') tailstrict,144145 manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),146147 manifestYamlStream(value, indent_array_in_object=false, c_document_end=true, quote_keys=true)::148 if !std.isArray(value) then149 error 'manifestYamlStream only takes arrays, got ' + std.type(value)150 else151 '---\n' + std.join(152 '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object, quote_keys) for e in value]153 ) + if c_document_end then '\n...\n' else '\n',154155 manifestPython(v)::156 if std.isObject(v) then157 local fields = [158 '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]159 for k in std.objectFields(v)160 ];161 '{%s}' % [std.join(', ', fields)]162 else if std.isArray(v) then163 '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]164 else if std.isString(v) then165 '%s' % [std.escapeStringPython(v)]166 else if std.isFunction(v) then167 error 'cannot manifest function'168 else if std.isNumber(v) then169 std.toString(v)170 else if v == true then171 'True'172 else if v == false then173 'False'174 else if v == null then175 'None',176177 manifestPythonVars(conf)::178 local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];179 std.join('\n', vars + ['']),180181 manifestXmlJsonml(value)::182 if !std.isArray(value) then183 error 'Expected a JSONML value (an array), got %s' % std.type(value)184 else185 local aux(v) =186 if std.isString(v) then187 v188 else189 local tag = v[0];190 local has_attrs = std.length(v) > 1 && std.isObject(v[1]);191 local attrs = if has_attrs then v[1] else {};192 local children = if has_attrs then v[2:] else v[1:];193 local attrs_str =194 std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);195 std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);196197 aux(value),198199 setUnion(a, b, keyF=id)::200 // NOTE: order matters, values in `a` win201 local aux(a, b, i, j, acc) =202 if i >= std.length(a) then203 acc + b[j:]204 else if j >= std.length(b) then205 acc + a[i:]206 else207 local ak = keyF(a[i]);208 local bk = keyF(b[j]);209 if ak == bk then210 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict211 else if ak < bk then212 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict213 else214 aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;215 aux(a, b, 0, 0, []),216217 setDiff(a, b, keyF=id)::218 local aux(a, b, i, j, acc) =219 if i >= std.length(a) then220 acc221 else if j >= std.length(b) then222 acc + a[i:]223 else224 if keyF(a[i]) == keyF(b[j]) then225 aux(a, b, i + 1, j + 1, acc) tailstrict226 else if keyF(a[i]) < keyF(b[j]) then227 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict228 else229 aux(a, b, i, j + 1, acc) tailstrict;230 aux(a, b, 0, 0, []) tailstrict,231232 mergePatch(target, patch)::233 if std.isObject(patch) then234 local target_object =235 if std.isObject(target) then target else {};236237 local target_fields =238 if std.isObject(target_object) then std.objectFields(target_object) else [];239240 local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];241 local both_fields = std.setUnion(target_fields, std.objectFields(patch));242243 {244 [k]:245 if !std.objectHas(patch, k) then246 target_object[k]247 else if !std.objectHas(target_object, k) then248 std.mergePatch(null, patch[k]) tailstrict249 else250 std.mergePatch(target_object[k], patch[k]) tailstrict251 for k in std.setDiff(both_fields, null_fields)252 }253 else254 patch,255256 get(o, f, default=null, inc_hidden=true)::257 if std.objectHasEx(o, f, inc_hidden) then o[f] else default,258259 objectFields(o)::260 std.objectFieldsEx(o, false),261262 objectFieldsAll(o)::263 std.objectFieldsEx(o, true),264265 objectHas(o, f)::266 std.objectHasEx(o, f, false),267268 objectHasAll(o, f)::269 std.objectHasEx(o, f, true),270271 objectValues(o)::272 [o[k] for k in std.objectFields(o)],273274 objectValuesAll(o)::275 [o[k] for k in std.objectFieldsAll(o)],276277 resolvePath(f, r)::278 local arr = std.split(f, '/');279 std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),280281 prune(a)::282 local isContent(b) =283 if b == null then284 false285 else if std.isArray(b) then286 std.length(b) > 0287 else if std.isObject(b) then288 std.length(b) > 0289 else290 true;291 if std.isArray(a) then292 [std.prune(x) for x in a if isContent($.prune(x))]293 else if std.isObject(a) then {294 [x]: $.prune(a[x])295 for x in std.objectFields(a)296 if isContent(std.prune(a[x]))297 } else298 a,299300 find(value, arr)::301 if !std.isArray(arr) then302 error 'find second parameter should be an array, got ' + std.type(arr)303 else304 std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),305306 // Compat307 __compare_array(arr1, arr2)::308 assert std.isArray(arr1) && std.isArray(arr2);309 std.__compare(arr1, arr2),310 __array_less(arr1, arr2):: std.__compare_array(arr1, arr2) == -1,311 __array_greater(arr1, arr2):: std.__compare_array(arr1, arr2) == 1,312 __array_less_or_equal(arr1, arr2):: std.__compare_array(arr1, arr2) <= 0,313 __array_greater_or_equal(arr1, arr2):: std.__compare_array(arr1, arr2) >= 0,314}1{2 local std = self,3 local id = std.id,45 thisFile:: error 'std.thisFile is deprecated, to enable its support in jrsonnet - recompile it with "legacy-this-file" support.\nThis will slow down stdlib caching a bit, though',67 toString(a):: '' + a,89 lstripChars(str, chars)::10 if std.length(str) > 0 && std.member(chars, str[0]) then11 std.lstripChars(str[1:], chars)12 else13 str,1415 rstripChars(str, chars)::16 local len = std.length(str);17 if len > 0 && std.member(chars, str[len - 1]) then18 std.rstripChars(str[:len - 1], chars)19 else20 str,2122 stripChars(str, chars)::23 std.lstripChars(std.rstripChars(str, chars), chars),2425 stringChars(str)::26 std.makeArray(std.length(str), function(i) str[i]),2728 splitLimitR(str, c, maxsplits)::29 if maxsplits == -1 then30 std.splitLimit(str, c, -1)31 else32 local revStr(str) = std.join('', std.reverse(std.stringChars(str)));33 std.map(function(e) revStr(e), std.reverse(std.splitLimit(revStr(str), revStr(c), maxsplits))),3435 split(str, c):: std.splitLimit(str, c, -1),3637 mapWithIndex(func, arr)::38 if !std.isFunction(func) then39 error ('std.mapWithIndex first param must be function, got ' + std.type(func))40 else if !std.isArray(arr) && !std.isString(arr) then41 error ('std.mapWithIndex second param must be array, got ' + std.type(arr))42 else43 std.makeArray(std.length(arr), function(i) func(i, arr[i])),4445 mapWithKey(func, obj)::46 if !std.isFunction(func) then47 error ('std.mapWithKey first param must be function, got ' + std.type(func))48 else if !std.isObject(obj) then49 error ('std.mapWithKey second param must be object, got ' + std.type(obj))50 else51 { [k]: func(k, obj[k]) for k in std.objectFields(obj) },5253 lines(arr)::54 std.join('\n', arr + ['']),5556 deepJoin(arr)::57 if std.isString(arr) then58 arr59 else if std.isArray(arr) then60 std.join('', [std.deepJoin(x) for x in arr])61 else62 error 'Expected string or array, got %s' % std.type(arr),6364 filterMap(filter_func, map_func, arr)::65 if !std.isFunction(filter_func) then66 error ('std.filterMap first param must be function, got ' + std.type(filter_func))67 else if !std.isFunction(map_func) then68 error ('std.filterMap second param must be function, got ' + std.type(map_func))69 else if !std.isArray(arr) then70 error ('std.filterMap third param must be array, got ' + std.type(arr))71 else72 std.map(map_func, std.filter(filter_func, arr)),7374 assertEqual(a, b)::75 if a == b then76 true77 else78 error 'Assertion failed. ' + a + ' != ' + b,7980 clamp(x, minVal, maxVal)::81 if x < minVal then minVal82 else if x > maxVal then maxVal83 else x,8485 flattenArrays(arrs)::86 std.foldl(function(a, b) a + b, arrs, []),8788 manifestIni(ini)::89 local body_lines(body) =90 std.join([], [91 local value_or_values = body[k];92 if std.isArray(value_or_values) then93 ['%s = %s' % [k, value] for value in value_or_values]94 else95 ['%s = %s' % [k, value_or_values]]9697 for k in std.objectFields(body)98 ]);99100 local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),101 main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],102 all_sections = [103 section_lines(k, ini.sections[k])104 for k in std.objectFields(ini.sections)105 ];106 std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),107108 manifestToml(value):: std.manifestTomlEx(value, ' '),109110 escapeStringPython(str)::111 std.escapeStringJson(str),112113 escapeStringBash(str_)::114 local str = std.toString(str_);115 local trans(ch) =116 if ch == "'" then117 "'\"'\"'"118 else119 ch;120 "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),121122 escapeStringDollars(str_)::123 local str = std.toString(str_);124 local trans(ch) =125 if ch == '$' then126 '$$'127 else128 ch;129 std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),130131 local xml_escapes = {132 '<': '<',133 '>': '>',134 '&': '&',135 '"': '"',136 "'": ''',137 },138139 escapeStringXML(str_)::140 local str = std.toString(str_);141 std.join('', [std.get(xml_escapes, ch, ch) for ch in std.stringChars(str)]),142143 manifestJson(value):: std.manifestJsonEx(value, ' ') tailstrict,144145 manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),146147 manifestYamlStream(value, indent_array_in_object=false, c_document_end=true, quote_keys=true)::148 if !std.isArray(value) then149 error 'manifestYamlStream only takes arrays, got ' + std.type(value)150 else151 '---\n' + std.join(152 '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object, quote_keys) for e in value]153 ) + if c_document_end then '\n...\n' else '\n',154155 manifestPython(v)::156 if std.isObject(v) then157 local fields = [158 '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]159 for k in std.objectFields(v)160 ];161 '{%s}' % [std.join(', ', fields)]162 else if std.isArray(v) then163 '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]164 else if std.isString(v) then165 '%s' % [std.escapeStringPython(v)]166 else if std.isFunction(v) then167 error 'cannot manifest function'168 else if std.isNumber(v) then169 std.toString(v)170 else if v == true then171 'True'172 else if v == false then173 'False'174 else if v == null then175 'None',176177 manifestPythonVars(conf)::178 local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];179 std.join('\n', vars + ['']),180181 manifestXmlJsonml(value)::182 if !std.isArray(value) then183 error 'Expected a JSONML value (an array), got %s' % std.type(value)184 else185 local aux(v) =186 if std.isString(v) then187 v188 else189 local tag = v[0];190 local has_attrs = std.length(v) > 1 && std.isObject(v[1]);191 local attrs = if has_attrs then v[1] else {};192 local children = if has_attrs then v[2:] else v[1:];193 local attrs_str =194 std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);195 std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);196197 aux(value),198199 setUnion(a, b, keyF=id)::200 // NOTE: order matters, values in `a` win201 local aux(a, b, i, j, acc) =202 if i >= std.length(a) then203 acc + b[j:]204 else if j >= std.length(b) then205 acc + a[i:]206 else207 local ak = keyF(a[i]);208 local bk = keyF(b[j]);209 if ak == bk then210 aux(a, b, i + 1, j + 1, acc + [a[i]]) tailstrict211 else if ak < bk then212 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict213 else214 aux(a, b, i, j + 1, acc + [b[j]]) tailstrict;215 aux(a, b, 0, 0, []),216217 setDiff(a, b, keyF=id)::218 local aux(a, b, i, j, acc) =219 if i >= std.length(a) then220 acc221 else if j >= std.length(b) then222 acc + a[i:]223 else224 if keyF(a[i]) == keyF(b[j]) then225 aux(a, b, i + 1, j + 1, acc) tailstrict226 else if keyF(a[i]) < keyF(b[j]) then227 aux(a, b, i + 1, j, acc + [a[i]]) tailstrict228 else229 aux(a, b, i, j + 1, acc) tailstrict;230 aux(a, b, 0, 0, []) tailstrict,231232 mergePatch(target, patch)::233 if std.isObject(patch) then234 local target_object =235 if std.isObject(target) then target else {};236237 local target_fields =238 if std.isObject(target_object) then std.objectFields(target_object) else [];239240 local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];241 local both_fields = std.setUnion(target_fields, std.objectFields(patch));242243 {244 [k]:245 if !std.objectHas(patch, k) then246 target_object[k]247 else if !std.objectHas(target_object, k) then248 std.mergePatch(null, patch[k]) tailstrict249 else250 std.mergePatch(target_object[k], patch[k]) tailstrict251 for k in std.setDiff(both_fields, null_fields)252 }253 else254 patch,255256 get(o, f, default=null, inc_hidden=true)::257 if std.objectHasEx(o, f, inc_hidden) then o[f] else default,258259 objectFields(o)::260 std.objectFieldsEx(o, false),261262 objectFieldsAll(o)::263 std.objectFieldsEx(o, true),264265 objectHas(o, f)::266 std.objectHasEx(o, f, false),267268 objectHasAll(o, f)::269 std.objectHasEx(o, f, true),270271 objectValues(o)::272 [o[k] for k in std.objectFields(o)],273274 objectValuesAll(o)::275 [o[k] for k in std.objectFieldsAll(o)],276277 objectKeysValues(o)::278 [{ key: k, value: o[k] } for k in std.objectFields(o)],279 280 objectKeysValuesAll(o)::281 [{ key: k, value: o[k] } for k in std.objectFieldsAll(o)],282283 resolvePath(f, r)::284 local arr = std.split(f, '/');285 std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),286287 prune(a)::288 local isContent(b) =289 if b == null then290 false291 else if std.isArray(b) then292 std.length(b) > 0293 else if std.isObject(b) then294 std.length(b) > 0295 else296 true;297 if std.isArray(a) then298 [std.prune(x) for x in a if isContent($.prune(x))]299 else if std.isObject(a) then {300 [x]: $.prune(a[x])301 for x in std.objectFields(a)302 if isContent(std.prune(a[x]))303 } else304 a,305306 find(value, arr)::307 if !std.isArray(arr) then308 error 'find second parameter should be an array, got ' + std.type(arr)309 else310 std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),311312 // Compat313 __compare_array(arr1, arr2)::314 assert std.isArray(arr1) && std.isArray(arr2);315 std.__compare(arr1, arr2),316 __array_less(arr1, arr2):: std.__compare_array(arr1, arr2) == -1,317 __array_greater(arr1, arr2):: std.__compare_array(arr1, arr2) == 1,318 __array_less_or_equal(arr1, arr2):: std.__compare_array(arr1, arr2) <= 0,319 __array_greater_or_equal(arr1, arr2):: std.__compare_array(arr1, arr2) >= 0,320}crates/jrsonnet-stdlib/src/strings.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/strings.rs
+++ b/crates/jrsonnet-stdlib/src/strings.rs
@@ -28,6 +28,16 @@
}
#[builtin]
+pub fn builtin_is_empty(str: String) -> bool {
+ str.is_empty()
+}
+
+#[builtin]
+pub fn builtin_equals_ignore_case(x: String, y: String) -> bool {
+ x.to_ascii_lowercase() == y.to_ascii_lowercase()
+}
+
+#[builtin]
pub fn builtin_splitlimit(str: IStr, c: IStr, maxsplits: Either![usize, M1]) -> ArrValue {
use Either2::*;
match maxsplits {