Skip to content

Commit b86b599

Browse files
Merge pull request #117 from python-processing-unit/main
gh-116: Fix SPLIT handling of empty delimiter.
2 parents 414f754 + f97af22 commit b86b599

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

src/builtins.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4952,6 +4952,9 @@ static Value builtin_split(Interpreter* interp, Value* args, int argc, Expr** ar
49524952
if (argc >= 2) {
49534953
EXPECT_STR(args[1], "SPLIT", interp, line, col);
49544954
sep = args[1].as.s;
4955+
if (sep[0] == '\0') {
4956+
RUNTIME_ERROR(interp, "SPLIT expects a non-empty delimiter", line, col);
4957+
}
49554958
}
49564959
const char* s = args[0].as.s;
49574960
// 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
49844987
free(piece);
49854988
cur = found + seplen;
49864989
}
4987-
// last piece
4988-
if (*cur != '\0') {
4989-
if (count + 1 > cap) { cap *= 2; items = realloc(items, sizeof(Value) * cap); }
4990-
items[count++] = value_str(cur);
4991-
}
4990+
// last piece, including empty trailing fields
4991+
size_t len = strlen(cur);
4992+
char* piece = malloc(len + 1);
4993+
memcpy(piece, cur, len);
4994+
piece[len] = '\0';
4995+
if (count + 1 > cap) { cap *= 2; items = realloc(items, sizeof(Value) * cap); }
4996+
items[count++] = value_str(piece);
4997+
free(piece);
49924998
free(copy);
4993-
if (count == 0) {
4994-
free(items);
4995-
return value_tns_new(TYPE_STR, 1, (const size_t[]){0});
4996-
}
49974999
size_t shape[1] = { count };
49985000
Value out = value_tns_from_values(TYPE_STR, 1, shape, items, count);
49995001
for (size_t i = 0; i < count; i++) value_free(items[i]);

0 commit comments

Comments
 (0)