Skip to content

feat(builtins): add strings.split_n; fix split error-message name - #798

Open
Anand Krishnamoorthi (anakrish) wants to merge 2 commits into
microsoft:mainfrom
anakrish:feat/strings-split-n
Open

feat(builtins): add strings.split_n; fix split error-message name#798
Anand Krishnamoorthi (anakrish) wants to merge 2 commits into
microsoft:mainfrom
anakrish:feat/strings-split-n

Conversation

@anakrish

Copy link
Copy Markdown
Collaborator

Adds strings.split_n and fixes a typo in split().

strings.split_n (new builtin, arity 3):

  • Positive n: splits into at most n pieces, last piece holds remainder (str::splitn)
  • Negative n: full split then returns last |n| pieces
  • n == 0: returns empty array; non-integer n returns undefined in non-strict mode
  • enforce_limit() on each piece to bound memory growth

Bug fix — split(): name was registered as "replace" instead of "split", producing misleading error messages. Required for split_n negative-n path correctness.

docs/builtins.md: added strings.count and strings.split_n entries.

Tests: new tests/interpreter/cases/builtins/strings/split_n.yaml covering positive limit, exact limit, limit-exceeds-parts, zero limit, negative limit tail, empty string, non-integer n (undefined), wrong arg count.

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

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