fix(strings): address split_n review comments - #83
Open
anakrish wants to merge 2 commits into
Open
Conversation
strings.split_n splits a string into at most n pieces (positive n) or returns the last |n| pieces from a full split (negative n), using str::splitn for correct remainder semantics. Also fixes the long-standing typo in split() where the function registered its name as "replace" instead of "split", which produced misleading error messages (e.g. when split_n delegates to split for the negative-n tail path). - Register strings.split_n with arity 3 - Positive n: use str::splitn; handle empty-delimiter char-by-char with remainder to match OPA semantics - Negative n: full split, then take the last |n| pieces - Non-integer n in non-strict mode returns undefined (strict: error) - n == 0 returns empty array; n overflowing i64 positive treated as no limit (full split) - enforce_limit() on each piece to bound memory growth - Fix split() name string: "replace" -> "split" - Add YAML regression test covering all major code paths Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Fix fallback for n > i64::MAX: pass only first two args to split() instead of
all three; split() expects exactly 2 operands and ensure_args_count was rejecting
the extra one.
- Add enforce_limit() in empty-delimiter branch to prevent unbounded memory growth:
call it in the per-character map closure and before the final remainder push.
- Add YAML tests: split_n("abcd", "", 3) → ["a","b","cd"] (remainder)
and split_n("a,b,c", ",", 1) → ["a,b,c"] (n=1 returns whole string).
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
Addresses review comments on PR #78 for the
strings.split_nbuiltin:noverflowsi64, the fallback tosplit()was passing all three arguments (string,delimiter,n).split()expects exactly two args;ensure_args_countwas rejecting the call. Fixed by passing only¶ms[..2]and&args[..2].n < chars.len(), the map collecting per-character parts did not callenforce_limit(), allowing unbounded memory growth from adversarial inputs. Fixed by callingenforce_limit()?inside the closure and once more before pushing the final remainder part.split_n("abcd", "", 3)→["a", "b", "cd"](remainder collects leftover chars) andsplit_n("a,b,c", ",", 1)→["a,b,c"](n=1 returns whole string).Note: the
unkonwn→unknowntypo fix lives on branchfix/time-duration-d-w-y(separate PR).