Skip to content

fix(strings): address split_n review comments - #83

Open
anakrish wants to merge 2 commits into
mainfrom
feat/strings-split-n
Open

fix(strings): address split_n review comments#83
anakrish wants to merge 2 commits into
mainfrom
feat/strings-split-n

Conversation

@anakrish

Copy link
Copy Markdown
Owner

Addresses review comments on PR #78 for the strings.split_n builtin:

  • Fallback fix: When n overflows i64, the fallback to split() was passing all three arguments (string, delimiter, n). split() expects exactly two args; ensure_args_count was rejecting the call. Fixed by passing only &params[..2] and &args[..2].
  • enforce_limit in empty-delimiter branch: When the delimiter is empty and n < chars.len(), the map collecting per-character parts did not call enforce_limit(), allowing unbounded memory growth from adversarial inputs. Fixed by calling enforce_limit()? inside the closure and once more before pushing the final remainder part.
  • YAML tests added: split_n("abcd", "", 3)["a", "b", "cd"] (remainder collects leftover chars) and split_n("a,b,c", ",", 1)["a,b,c"] (n=1 returns whole string).

Note: the unkonwnunknown typo fix lives on branch fix/time-duration-d-w-y (separate PR).

anakrish and others added 2 commits August 28, 2026 11:17
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>
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