fix: std.stripChars/lstripChars/rstripChars accept array parameter#967
Open
He-Pin wants to merge 1 commit into
Open
fix: std.stripChars/lstripChars/rstripChars accept array parameter#967He-Pin wants to merge 1 commit into
He-Pin wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
std.stripChars,std.lstripChars, andstd.rstripCharsrejected arrays as thecharsparameter, while go-jsonnet, jsonnet-cpp, and jrsonnet all accept arrays. Whencharsis 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, andstd.rstripCharsinStd.scalato accept an array as thecharsparameter. Whencharsis 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.
std.lstripChars("forward", [1, "f", [], "o", "d", "for"])"rward""rward"std.rstripChars("cool just cool", ["o", "l", 12.2323443])"cool just c""cool just c"std.stripChars("UwU Lel Stosh", ["h", "U", "s", {}, [], null, "w", [1,2,3]])" Lel Sto"" Lel Sto"std.stripChars("abchelloabc", ["abc"])"abchelloabc""abchelloabc"std.stripChars("abchelloabc", ["a", "bc"])"bchelloabc""bchelloabc"std.stripChars("1hello1", [1])"1hello1""1hello1"Test plan
builtin_strings_string.jsonnetpasses (15 test cases)