difftreelog
perf native implementation of std.splitLimitR (#163)
in: master
* native implementation of std.splitLimitR * style: fix formatting ---------
5 files changed
crates/jrsonnet-stdlib/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/lib.rs
+++ b/crates/jrsonnet-stdlib/src/lib.rs
@@ -178,6 +178,7 @@
("isEmpty", builtin_is_empty::INST),
("equalsIgnoreCase", builtin_equals_ignore_case::INST),
("splitLimit", builtin_splitlimit::INST),
+ ("splitLimitR", builtin_splitlimitr::INST),
("asciiUpper", builtin_ascii_upper::INST),
("asciiLower", builtin_ascii_lower::INST),
("findSubstr", builtin_find_substr::INST),
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 lstripChars(str, chars)::8 if std.length(str) > 0 && std.member(chars, str[0]) then9 std.lstripChars(str[1:], chars)10 else11 str,1213 rstripChars(str, chars)::14 local len = std.length(str);15 if len > 0 && std.member(chars, str[len - 1]) then16 std.rstripChars(str[:len - 1], chars)17 else18 str,1920 stripChars(str, chars)::21 std.lstripChars(std.rstripChars(str, chars), chars),2223 splitLimitR(str, c, maxsplits)::24 if maxsplits == -1 then25 std.splitLimit(str, c, -1)26 else27 local revStr(str) = std.join('', std.reverse(std.stringChars(str)));28 std.map(function(e) revStr(e), std.reverse(std.splitLimit(revStr(str), revStr(c), maxsplits))),2930 split(str, c):: std.splitLimit(str, c, -1),3132 mapWithIndex(func, arr)::33 if !std.isFunction(func) then34 error ('std.mapWithIndex first param must be function, got ' + std.type(func))35 else if !std.isArray(arr) && !std.isString(arr) then36 error ('std.mapWithIndex second param must be array, got ' + std.type(arr))37 else38 std.makeArray(std.length(arr), function(i) func(i, arr[i])),3940 mapWithKey(func, obj)::41 if !std.isFunction(func) then42 error ('std.mapWithKey first param must be function, got ' + std.type(func))43 else if !std.isObject(obj) then44 error ('std.mapWithKey second param must be object, got ' + std.type(obj))45 else46 { [k]: func(k, obj[k]) for k in std.objectFields(obj) },4748 lines(arr)::49 std.join('\n', arr + ['']),5051 deepJoin(arr)::52 if std.isString(arr) then53 arr54 else if std.isArray(arr) then55 std.join('', [std.deepJoin(x) for x in arr])56 else57 error 'Expected string or array, got %s' % std.type(arr),5859 assertEqual(a, b)::60 if a == b then61 true62 else63 error 'Assertion failed. ' + a + ' != ' + b,6465 clamp(x, minVal, maxVal)::66 if x < minVal then minVal67 else if x > maxVal then maxVal68 else x,6970 manifestIni(ini)::71 local body_lines(body) =72 std.join([], [73 local value_or_values = body[k];74 if std.isArray(value_or_values) then75 ['%s = %s' % [k, value] for value in value_or_values]76 else77 ['%s = %s' % [k, value_or_values]]7879 for k in std.objectFields(body)80 ]);8182 local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),83 main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],84 all_sections = [85 section_lines(k, ini.sections[k])86 for k in std.objectFields(ini.sections)87 ];88 std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),8990 manifestToml(value):: std.manifestTomlEx(value, ' '),9192 escapeStringPython(str)::93 std.escapeStringJson(str),9495 escapeStringBash(str_)::96 local str = std.toString(str_);97 local trans(ch) =98 if ch == "'" then99 "'\"'\"'"100 else101 ch;102 "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),103104 escapeStringDollars(str_)::105 local str = std.toString(str_);106 local trans(ch) =107 if ch == '$' then108 '$$'109 else110 ch;111 std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),112113 local xml_escapes = {114 '<': '<',115 '>': '>',116 '&': '&',117 '"': '"',118 "'": ''',119 },120121 escapeStringXML(str_)::122 local str = std.toString(str_);123 std.join('', [std.get(xml_escapes, ch, ch) for ch in std.stringChars(str)]),124125 manifestJson(value):: std.manifestJsonEx(value, ' ') tailstrict,126127 manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),128129 manifestYamlStream(value, indent_array_in_object=false, c_document_end=true, quote_keys=true)::130 if !std.isArray(value) then131 error 'manifestYamlStream only takes arrays, got ' + std.type(value)132 else133 '---\n' + std.join(134 '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object, quote_keys) for e in value]135 ) + if c_document_end then '\n...\n' else '\n',136137 manifestPython(v)::138 if std.isObject(v) then139 local fields = [140 '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]141 for k in std.objectFields(v)142 ];143 '{%s}' % [std.join(', ', fields)]144 else if std.isArray(v) then145 '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]146 else if std.isString(v) then147 '%s' % [std.escapeStringPython(v)]148 else if std.isFunction(v) then149 error 'cannot manifest function'150 else if std.isNumber(v) then151 std.toString(v)152 else if v == true then153 'True'154 else if v == false then155 'False'156 else if v == null then157 'None',158159 manifestPythonVars(conf)::160 local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];161 std.join('\n', vars + ['']),162163 manifestXmlJsonml(value)::164 if !std.isArray(value) then165 error 'Expected a JSONML value (an array), got %s' % std.type(value)166 else167 local aux(v) =168 if std.isString(v) then169 v170 else171 local tag = v[0];172 local has_attrs = std.length(v) > 1 && std.isObject(v[1]);173 local attrs = if has_attrs then v[1] else {};174 local children = if has_attrs then v[2:] else v[1:];175 local attrs_str =176 std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);177 std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);178179 aux(value),180181 mergePatch(target, patch)::182 if std.isObject(patch) then183 local target_object =184 if std.isObject(target) then target else {};185186 local target_fields =187 if std.isObject(target_object) then std.objectFields(target_object) else [];188189 local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];190 local both_fields = std.setUnion(target_fields, std.objectFields(patch));191192 {193 [k]:194 if !std.objectHas(patch, k) then195 target_object[k]196 else if !std.objectHas(target_object, k) then197 std.mergePatch(null, patch[k]) tailstrict198 else199 std.mergePatch(target_object[k], patch[k]) tailstrict200 for k in std.setDiff(both_fields, null_fields)201 }202 else203 patch,204205 get(o, f, default=null, inc_hidden=true)::206 if std.objectHasEx(o, f, inc_hidden) then o[f] else default,207208 resolvePath(f, r)::209 local arr = std.split(f, '/');210 std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),211212 find(value, arr)::213 if !std.isArray(arr) then214 error 'find second parameter should be an array, got ' + std.type(arr)215 else216 std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),217218 // Compat219 __compare_array(arr1, arr2)::220 assert std.isArray(arr1) && std.isArray(arr2);221 std.__compare(arr1, arr2),222 __array_less(arr1, arr2):: std.__compare_array(arr1, arr2) == -1,223 __array_greater(arr1, arr2):: std.__compare_array(arr1, arr2) == 1,224 __array_less_or_equal(arr1, arr2):: std.__compare_array(arr1, arr2) <= 0,225 __array_greater_or_equal(arr1, arr2):: std.__compare_array(arr1, arr2) >= 0,226}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 lstripChars(str, chars)::8 if std.length(str) > 0 && std.member(chars, str[0]) then9 std.lstripChars(str[1:], chars)10 else11 str,1213 rstripChars(str, chars)::14 local len = std.length(str);15 if len > 0 && std.member(chars, str[len - 1]) then16 std.rstripChars(str[:len - 1], chars)17 else18 str,1920 stripChars(str, chars)::21 std.lstripChars(std.rstripChars(str, chars), chars),2223 split(str, c):: std.splitLimit(str, c, -1),2425 mapWithIndex(func, arr)::26 if !std.isFunction(func) then27 error ('std.mapWithIndex first param must be function, got ' + std.type(func))28 else if !std.isArray(arr) && !std.isString(arr) then29 error ('std.mapWithIndex second param must be array, got ' + std.type(arr))30 else31 std.makeArray(std.length(arr), function(i) func(i, arr[i])),3233 mapWithKey(func, obj)::34 if !std.isFunction(func) then35 error ('std.mapWithKey first param must be function, got ' + std.type(func))36 else if !std.isObject(obj) then37 error ('std.mapWithKey second param must be object, got ' + std.type(obj))38 else39 { [k]: func(k, obj[k]) for k in std.objectFields(obj) },4041 lines(arr)::42 std.join('\n', arr + ['']),4344 deepJoin(arr)::45 if std.isString(arr) then46 arr47 else if std.isArray(arr) then48 std.join('', [std.deepJoin(x) for x in arr])49 else50 error 'Expected string or array, got %s' % std.type(arr),5152 assertEqual(a, b)::53 if a == b then54 true55 else56 error 'Assertion failed. ' + a + ' != ' + b,5758 clamp(x, minVal, maxVal)::59 if x < minVal then minVal60 else if x > maxVal then maxVal61 else x,6263 manifestIni(ini)::64 local body_lines(body) =65 std.join([], [66 local value_or_values = body[k];67 if std.isArray(value_or_values) then68 ['%s = %s' % [k, value] for value in value_or_values]69 else70 ['%s = %s' % [k, value_or_values]]7172 for k in std.objectFields(body)73 ]);7475 local section_lines(sname, sbody) = ['[%s]' % [sname]] + body_lines(sbody),76 main_body = if std.objectHas(ini, 'main') then body_lines(ini.main) else [],77 all_sections = [78 section_lines(k, ini.sections[k])79 for k in std.objectFields(ini.sections)80 ];81 std.join('\n', main_body + std.flattenArrays(all_sections) + ['']),8283 manifestToml(value):: std.manifestTomlEx(value, ' '),8485 escapeStringPython(str)::86 std.escapeStringJson(str),8788 escapeStringBash(str_)::89 local str = std.toString(str_);90 local trans(ch) =91 if ch == "'" then92 "'\"'\"'"93 else94 ch;95 "'%s'" % std.join('', [trans(ch) for ch in std.stringChars(str)]),9697 escapeStringDollars(str_)::98 local str = std.toString(str_);99 local trans(ch) =100 if ch == '$' then101 '$$'102 else103 ch;104 std.foldl(function(a, b) a + trans(b), std.stringChars(str), ''),105106 local xml_escapes = {107 '<': '<',108 '>': '>',109 '&': '&',110 '"': '"',111 "'": ''',112 },113114 escapeStringXML(str_)::115 local str = std.toString(str_);116 std.join('', [std.get(xml_escapes, ch, ch) for ch in std.stringChars(str)]),117118 manifestJson(value):: std.manifestJsonEx(value, ' ') tailstrict,119120 manifestJsonMinified(value):: std.manifestJsonEx(value, '', '', ':'),121122 manifestYamlStream(value, indent_array_in_object=false, c_document_end=true, quote_keys=true)::123 if !std.isArray(value) then124 error 'manifestYamlStream only takes arrays, got ' + std.type(value)125 else126 '---\n' + std.join(127 '\n---\n', [std.manifestYamlDoc(e, indent_array_in_object, quote_keys) for e in value]128 ) + if c_document_end then '\n...\n' else '\n',129130 manifestPython(v)::131 if std.isObject(v) then132 local fields = [133 '%s: %s' % [std.escapeStringPython(k), std.manifestPython(v[k])]134 for k in std.objectFields(v)135 ];136 '{%s}' % [std.join(', ', fields)]137 else if std.isArray(v) then138 '[%s]' % [std.join(', ', [std.manifestPython(v2) for v2 in v])]139 else if std.isString(v) then140 '%s' % [std.escapeStringPython(v)]141 else if std.isFunction(v) then142 error 'cannot manifest function'143 else if std.isNumber(v) then144 std.toString(v)145 else if v == true then146 'True'147 else if v == false then148 'False'149 else if v == null then150 'None',151152 manifestPythonVars(conf)::153 local vars = ['%s = %s' % [k, std.manifestPython(conf[k])] for k in std.objectFields(conf)];154 std.join('\n', vars + ['']),155156 manifestXmlJsonml(value)::157 if !std.isArray(value) then158 error 'Expected a JSONML value (an array), got %s' % std.type(value)159 else160 local aux(v) =161 if std.isString(v) then162 v163 else164 local tag = v[0];165 local has_attrs = std.length(v) > 1 && std.isObject(v[1]);166 local attrs = if has_attrs then v[1] else {};167 local children = if has_attrs then v[2:] else v[1:];168 local attrs_str =169 std.join('', [' %s="%s"' % [k, attrs[k]] for k in std.objectFields(attrs)]);170 std.deepJoin(['<', tag, attrs_str, '>', [aux(x) for x in children], '</', tag, '>']);171172 aux(value),173174 mergePatch(target, patch)::175 if std.isObject(patch) then176 local target_object =177 if std.isObject(target) then target else {};178179 local target_fields =180 if std.isObject(target_object) then std.objectFields(target_object) else [];181182 local null_fields = [k for k in std.objectFields(patch) if patch[k] == null];183 local both_fields = std.setUnion(target_fields, std.objectFields(patch));184185 {186 [k]:187 if !std.objectHas(patch, k) then188 target_object[k]189 else if !std.objectHas(target_object, k) then190 std.mergePatch(null, patch[k]) tailstrict191 else192 std.mergePatch(target_object[k], patch[k]) tailstrict193 for k in std.setDiff(both_fields, null_fields)194 }195 else196 patch,197198 get(o, f, default=null, inc_hidden=true)::199 if std.objectHasEx(o, f, inc_hidden) then o[f] else default,200201 resolvePath(f, r)::202 local arr = std.split(f, '/');203 std.join('/', std.makeArray(std.length(arr) - 1, function(i) arr[i]) + [r]),204205 find(value, arr)::206 if !std.isArray(arr) then207 error 'find second parameter should be an array, got ' + std.type(arr)208 else209 std.filter(function(i) arr[i] == value, std.range(0, std.length(arr) - 1)),210211 // Compat212 __compare_array(arr1, arr2)::213 assert std.isArray(arr1) && std.isArray(arr2);214 std.__compare(arr1, arr2),215 __array_less(arr1, arr2):: std.__compare_array(arr1, arr2) == -1,216 __array_greater(arr1, arr2):: std.__compare_array(arr1, arr2) == 1,217 __array_less_or_equal(arr1, arr2):: std.__compare_array(arr1, arr2) <= 0,218 __array_greater_or_equal(arr1, arr2):: std.__compare_array(arr1, arr2) >= 0,219}crates/jrsonnet-stdlib/src/strings.rsdiffbeforeafterboth--- a/crates/jrsonnet-stdlib/src/strings.rs
+++ b/crates/jrsonnet-stdlib/src/strings.rs
@@ -47,6 +47,25 @@
}
#[builtin]
+pub fn builtin_splitlimitr(str: IStr, c: IStr, maxsplits: Either![usize, M1]) -> ArrValue {
+ use Either2::*;
+ match maxsplits {
+ A(n) =>
+ // rsplitn does not implement DoubleEndedIterator so collect into
+ // a temporary vec
+ {
+ str.rsplitn(n + 1, &c as &str)
+ .map(Val::string)
+ .collect::<Vec<_>>()
+ .into_iter()
+ .rev()
+ .collect()
+ }
+ B(_) => str.split(&c as &str).map(Val::string).collect(),
+ }
+}
+
+#[builtin]
pub fn builtin_ascii_upper(str: IStr) -> String {
str.to_ascii_uppercase()
}
tests/golden/builtin_strings.jsonnetdiffbeforeafterboth--- /dev/null
+++ b/tests/golden/builtin_strings.jsonnet
@@ -0,0 +1,7 @@
+local str = 'ab::cd::ef';
+{
+ split: std.split(str, '::'),
+ splitlimit: std.splitLimit(str, '::', 1),
+ splitlimitRNoLimit: std.splitLimit(str, '::', -1),
+ splitlimitR: std.splitLimitR(str, '::', 1),
+}
tests/golden/builtin_strings.jsonnet.goldendiffbeforeafterboth--- /dev/null
+++ b/tests/golden/builtin_strings.jsonnet.golden
@@ -0,0 +1,20 @@
+{
+ "split": [
+ "ab",
+ "cd",
+ "ef"
+ ],
+ "splitlimit": [
+ "ab",
+ "cd::ef"
+ ],
+ "splitlimitR": [
+ "ab::cd",
+ "ef"
+ ],
+ "splitlimitRNoLimit": [
+ "ab",
+ "cd",
+ "ef"
+ ]
+}
\ No newline at end of file