-
Notifications
You must be signed in to change notification settings - Fork 0
gh-116: Fix SPLIT handling of empty delimiter. #117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4952,6 +4952,9 @@ static Value builtin_split(Interpreter* interp, Value* args, int argc, Expr** ar | |||||||||||||||||||
| if (argc >= 2) { | ||||||||||||||||||||
| EXPECT_STR(args[1], "SPLIT", interp, line, col); | ||||||||||||||||||||
| sep = args[1].as.s; | ||||||||||||||||||||
| if (sep[0] == '\0') { | ||||||||||||||||||||
| RUNTIME_ERROR(interp, "SPLIT expects a non-empty delimiter", line, col); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| } | ||||||||||||||||||||
| const char* s = args[0].as.s; | ||||||||||||||||||||
| // simple separator: if sep==NULL split on whitespace, else split on sep exactly | ||||||||||||||||||||
|
|
@@ -4984,16 +4987,15 @@ static Value builtin_split(Interpreter* interp, Value* args, int argc, Expr** ar | |||||||||||||||||||
| free(piece); | ||||||||||||||||||||
| cur = found + seplen; | ||||||||||||||||||||
| } | ||||||||||||||||||||
| // last piece | ||||||||||||||||||||
| if (*cur != '\0') { | ||||||||||||||||||||
| if (count + 1 > cap) { cap *= 2; items = realloc(items, sizeof(Value) * cap); } | ||||||||||||||||||||
| items[count++] = value_str(cur); | ||||||||||||||||||||
| } | ||||||||||||||||||||
| // last piece, including empty trailing fields | ||||||||||||||||||||
| size_t len = strlen(cur); | ||||||||||||||||||||
| char* piece = malloc(len + 1); | ||||||||||||||||||||
| memcpy(piece, cur, len); | ||||||||||||||||||||
| piece[len] = '\0'; | ||||||||||||||||||||
| if (count + 1 > cap) { cap *= 2; items = realloc(items, sizeof(Value) * cap); } | ||||||||||||||||||||
| items[count++] = value_str(piece); | ||||||||||||||||||||
| free(piece); | ||||||||||||||||||||
|
Comment on lines
+4991
to
+4997
|
||||||||||||||||||||
| size_t len = strlen(cur); | |
| char* piece = malloc(len + 1); | |
| memcpy(piece, cur, len); | |
| piece[len] = '\0'; | |
| if (count + 1 > cap) { cap *= 2; items = realloc(items, sizeof(Value) * cap); } | |
| items[count++] = value_str(piece); | |
| free(piece); | |
| if (count + 1 > cap) { cap *= 2; items = realloc(items, sizeof(Value) * cap); } | |
| items[count++] = value_str(cur); |
Copilot
AI
Apr 1, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR title mentions fixing empty-delimiter handling, but this hunk also changes SPLIT semantics by always appending the final field (including empty trailing fields, and changing the result for an empty input string). If that broader behavior change is intentional, consider reflecting it in the PR description/title and/or documenting it in the SPLIT comment/spec so the change is discoverable for users.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change introduces new SPLIT behaviors that should be covered by the existing builtin test suite (tests/test2.pre): (1) empty delimiter now raises a runtime error, and (2) empty trailing fields are now preserved (e.g., splitting "a," by "," should include a final empty string). Adding explicit assertions will prevent regressions and clarify intended semantics (including the "" input case).