diff options
author | Jo-Philipp Wich <jo@mein.io> | 2022-09-05 12:06:53 +0200 |
---|---|---|
committer | Jo-Philipp Wich <jo@mein.io> | 2022-09-05 12:06:53 +0200 |
commit | 5cdddd32ef3df78be7cb187446235a04491036b5 (patch) | |
tree | a0b81ae65dcc4a9598ef3bf1ae4ba4c1bce01520 /tests/custom/03_stdlib/33_replace | |
parent | 68b5a1eb5c8e35b9d299879df00b8130ce164cad (diff) |
lib: add limit support to split() and replace()
Extend the split() and replace() functions to accept an additional optional
`limit` argument which limits the amount of split operations / substitutions
performed by these functions.
Signed-off-by: Jo-Philipp Wich <jo@mein.io>
Diffstat (limited to 'tests/custom/03_stdlib/33_replace')
-rw-r--r-- | tests/custom/03_stdlib/33_replace | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/custom/03_stdlib/33_replace b/tests/custom/03_stdlib/33_replace index b662ae8..388959f 100644 --- a/tests/custom/03_stdlib/33_replace +++ b/tests/custom/03_stdlib/33_replace @@ -205,3 +205,37 @@ In [anonymous function](), line 2, byte 40: -- End -- + + +An optional limit parameter controls the maximum amount of replacements. + +-- Testcase -- +{% + printf("%.J\n", [ + // negative limit performs no substitution + replace("aaaaa", "a", "x", -1), + + // zero limit performs no substitution + replace("aaaaa", "a", "x", 0), + + // positive limit + replace("aaaaa", "a", "x", 3), + + // same rules apply to regex replaces: + replace("foo bar baz", /[ao]/g, "x", -1), + replace("foo bar baz", /[ao]/g, "x", 0), + replace("foo bar baz", /[ao]/g, "x", 3), + ]); +%} +-- End -- + +-- Expect stdout -- +[ + "aaaaa", + "aaaaa", + "xxxaa", + "foo bar baz", + "foo bar baz", + "fxx bxr baz" +] +-- End -- |