Skip to content

Commit

Permalink
Issue #130: Fix bug with String.prototype.split(RegExp).
Browse files Browse the repository at this point in the history
The "empty string at end of last match" should only trigger if the match
is empty!
  • Loading branch information
ccxvii committed Jan 26, 2024
1 parent d2697fd commit 40556b3
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions jsstring.c
Original file line number Diff line number Diff line change
Expand Up @@ -661,12 +661,14 @@ static void Sp_split_regexp(js_State *J)
js_newarray(J);
len = 0;

if (limit == 0)
return;

e = text + strlen(text);

/* splitting the empty string */
if (e == text) {
if (js_doregexec(J, re->prog, text, &m, 0)) {
if (len == limit) return;
js_pushliteral(J, "");
js_setindex(J, -2, 0);
}
Expand All @@ -682,7 +684,7 @@ static void Sp_split_regexp(js_State *J)
c = m.sub[0].ep;

/* empty string at end of last match */
if (b == p) {
if (b == c && b == p) {
++a;
continue;
}
Expand Down Expand Up @@ -714,6 +716,9 @@ static void Sp_split_string(js_State *J)

js_newarray(J);

if (limit == 0)
return;

n = strlen(sep);

/* empty string */
Expand Down

0 comments on commit 40556b3

Please sign in to comment.