Skip to content

Commit 6cb8d4d

Browse files
committed
Add string replace function
1 parent ce82241 commit 6cb8d4d

11 files changed

+63
-29
lines changed

stdlib/std.jsonnet

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,32 @@ limitations under the License.
9898
aux(str, delim, i2, arr, v + c) tailstrict;
9999
aux(str, c, 0, [], ""),
100100

101+
strReplace(str, from, to)::
102+
assert std.type(str) == "string";
103+
assert std.type(from) == "string";
104+
assert std.type(to) == "string";
105+
assert from != "" : "'from' string must not be zero length.";
106+
107+
// Cache for performance.
108+
local str_len = std.length(str);
109+
local from_len = std.length(from);
110+
111+
// True if from is at str[i].
112+
local found_at(i) = str[i:i + from_len] == from;
113+
114+
// Return the remainder of 'str' starting with 'start_index' where
115+
// all occurrences of 'from' after 'curr_index' are replaced with 'to'.
116+
local replace_after(start_index, curr_index, acc) =
117+
if curr_index > str_len then
118+
acc + str[start_index:curr_index]
119+
else if found_at(curr_index) then
120+
local new_index = curr_index + std.length(from);
121+
replace_after(new_index, new_index, acc + str[start_index:curr_index] + to)
122+
else
123+
replace_after(start_index, curr_index + 1, acc);
124+
125+
replace_after(0, 0, ""),
126+
101127
range(from, to)::
102128
std.makeArray(to - from + 1, function(i) i + from),
103129

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
RUNTIME ERROR: Cannot test equality of functions
2-
std.jsonnet:1014:17-42 function <anonymous>
2+
std.jsonnet:1040:17-42 function <anonymous>
33
error.equality_function.jsonnet:17:1-33
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
RUNTIME ERROR: foobar
22
error.inside_equals_array.jsonnet:18:18-32 thunk <array_element>
3-
std.jsonnet:994:41-45 thunk <b>
4-
std.jsonnet:994:33-45 function <anonymous>
5-
std.jsonnet:994:33-45 function <aux>
6-
std.jsonnet:997:29-45 function <anonymous>
7-
std.jsonnet:998:21-33
3+
std.jsonnet:1020:41-45 thunk <b>
4+
std.jsonnet:1020:33-45 function <anonymous>
5+
std.jsonnet:1020:33-45 function <aux>
6+
std.jsonnet:1023:29-45 function <anonymous>
7+
std.jsonnet:1024:21-33
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
RUNTIME ERROR: foobar
22
error.inside_equals_object.jsonnet:18:22-36 object <b>
3-
std.jsonnet:1008:62-66 thunk <b>
4-
std.jsonnet:1008:54-66 function <anonymous>
5-
std.jsonnet:1008:54-66 function <aux>
6-
std.jsonnet:1011:29-45 function <anonymous>
7-
std.jsonnet:1012:21-33
3+
std.jsonnet:1034:62-66 thunk <b>
4+
std.jsonnet:1034:54-66 function <anonymous>
5+
std.jsonnet:1034:54-66 function <aux>
6+
std.jsonnet:1037:29-45 function <anonymous>
7+
std.jsonnet:1038:21-33
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Object assertion failed.
22
error.invariant.equality.jsonnet:17:10-15 thunk <object_assert>
3-
std.jsonnet:1008:54-58 thunk <a>
4-
std.jsonnet:1008:54-66 function <anonymous>
5-
std.jsonnet:1008:54-66 function <anonymous>
6-
std.jsonnet:1012:21-33
3+
std.jsonnet:1034:54-58 thunk <a>
4+
std.jsonnet:1034:54-66 function <anonymous>
5+
std.jsonnet:1034:54-66 function <anonymous>
6+
std.jsonnet:1038:21-33
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Object assertion failed.
22
error.obj_assert.fail1.jsonnet:20:23-29 thunk <object_assert>
3-
std.jsonnet:1008:54-58 thunk <a>
4-
std.jsonnet:1008:54-66 function <anonymous>
5-
std.jsonnet:1008:54-66 function <anonymous>
6-
std.jsonnet:1012:21-33
3+
std.jsonnet:1034:54-58 thunk <a>
4+
std.jsonnet:1034:54-66 function <anonymous>
5+
std.jsonnet:1034:54-66 function <anonymous>
6+
std.jsonnet:1038:21-33
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: foo was not equal to bar
22
error.obj_assert.fail2.jsonnet:20:32-65 thunk <object_assert>
3-
std.jsonnet:1008:54-58 thunk <a>
4-
std.jsonnet:1008:54-66 function <anonymous>
5-
std.jsonnet:1008:54-66 function <anonymous>
6-
std.jsonnet:1012:21-33
3+
std.jsonnet:1034:54-58 thunk <a>
4+
std.jsonnet:1034:54-66 function <anonymous>
5+
std.jsonnet:1034:54-66 function <anonymous>
6+
std.jsonnet:1038:21-33
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
RUNTIME ERROR: Assertion failed. 1 != 2
2-
std.jsonnet:651:13-56 function <anonymous>
2+
std.jsonnet:677:13-56 function <anonymous>
33
error.sanity.jsonnet:17:1-22
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
RUNTIME ERROR: expected string but arr[1] was array
2-
std.jsonnet:167:17-95 function <aux>
3-
std.jsonnet:169:17-57 function <aux>
4-
std.jsonnet:175:13-34 function <anonymous>
2+
std.jsonnet:193:17-95 function <aux>
3+
std.jsonnet:195:17-57 function <aux>
4+
std.jsonnet:201:13-34 function <anonymous>
55
error.std_join_types1.jsonnet:17:1-26
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
RUNTIME ERROR: expected array but arr[0] was string
2-
std.jsonnet:167:17-95 function <aux>
3-
std.jsonnet:177:13-34 function <anonymous>
2+
std.jsonnet:193:17-95 function <aux>
3+
std.jsonnet:203:13-34 function <anonymous>
44
error.std_join_types2.jsonnet:17:1-31

test_suite/stdlib.jsonnet

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ std.assertEqual(std.char(97), "a") &&
159159
std.assertEqual(std.codepoint("\u0000"), 0) &&
160160
std.assertEqual(std.char(0), "\u0000") &&
161161

162+
std.assertEqual(std.strReplace('A cat walked by', 'cat', 'dog'), 'A dog walked by') &&
163+
std.assertEqual(std.strReplace('cat', 'cat', 'dog'), 'dog') &&
164+
std.assertEqual(std.strReplace('', 'cat', ''), '') &&
165+
std.assertEqual(std.strReplace('xoxoxoxox', 'xoxox3xox', 'A'), 'xoxoxoxox') &&
166+
std.assertEqual(std.strReplace('xoxoxox3xox', 'xoxox3xox', 'A'), 'xoA') &&
167+
std.assertEqual(std.strReplace('A cat is a cat', 'cat', 'dog'), 'A dog is a dog') &&
168+
std.assertEqual(std.strReplace('wishyfishyisishy', 'ish', 'and'), 'wandyfandyisandy') &&
169+
162170
std.assertEqual(std.map(function(x) x * x, []), []) &&
163171
std.assertEqual(std.map(function(x) x * x, [1, 2, 3, 4]), [1, 4, 9, 16]) &&
164172
std.assertEqual(std.map(function(x) x * x, std.filter(function(x) x > 5, std.range(1, 10))), [36, 49, 64, 81, 100]) &&

0 commit comments

Comments
 (0)