Skip to content

fix: std.stripChars/lstripChars/rstripChars accept array parameter#967

Open
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:fix/stripchars-array-param
Open

fix: std.stripChars/lstripChars/rstripChars accept array parameter#967
He-Pin wants to merge 1 commit into
databricks:masterfrom
He-Pin:fix/stripchars-array-param

Conversation

@He-Pin

@He-Pin He-Pin commented Jun 18, 2026

Copy link
Copy Markdown
Contributor

Motivation

std.stripChars, std.lstripChars, and std.rstripChars rejected arrays as the chars parameter, while go-jsonnet, jsonnet-cpp, and jrsonnet all accept arrays. When chars is an array, only single-character string elements are used for stripping; non-string elements and multi-character strings are silently ignored.

Modification

Modified std.stripChars, std.lstripChars, and std.rstripChars in Std.scala to accept an array as the chars parameter. When chars is an array, filter to single-character string elements only and join them into the strip character set.

Result

The three strip functions now match go-jsonnet behavior for array inputs.

Test case go-jsonnet v0.22.0 sjsonnet (before) sjsonnet (after)
std.lstripChars("forward", [1, "f", [], "o", "d", "for"]) "rward" error "rward"
std.rstripChars("cool just cool", ["o", "l", 12.2323443]) "cool just c" error "cool just c"
std.stripChars("UwU Lel Stosh", ["h", "U", "s", {}, [], null, "w", [1,2,3]]) " Lel Sto" error " Lel Sto"
std.stripChars("abchelloabc", ["abc"]) "abchelloabc" error "abchelloabc"
std.stripChars("abchelloabc", ["a", "bc"]) "bchelloabc" error "bchelloabc"
std.stripChars("1hello1", [1]) "1hello1" error "1hello1"

Test plan

  • All existing tests pass (48 unit + 15 file test groups)
  • jrsonnet golden test builtin_strings_string.jsonnet passes (15 test cases)
  • Verified against go-jsonnet v0.22.0, jsonnet-cpp v0.22.0, and jrsonnet v0.5.0-pre99

Motivation:
go-jsonnet allows the `chars` parameter of std.stripChars, std.lstripChars,
and std.rstripChars to be either a string or an array. When an array is
passed, only single-character string elements are used for stripping;
non-string elements and multi-character strings are silently ignored.
sjsonnet previously threw a type error on array input.

Modification:
Added a `charsToString` helper that accepts both Val.Str and Val.Arr.
For arrays, it collects only string elements that are exactly one codepoint
long and concatenates them into the strip character set.

Result:
std.stripChars/lstripChars/rstripChars now accept array parameters,
matching go-jsonnet behavior exactly across all edge cases including
non-string elements, multi-character strings, empty strings, and null.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant