Skip to content

Commit

Permalink
Support Java 8+ semantics for String.split()
Browse files Browse the repository at this point in the history
  • Loading branch information
niloc132 committed Dec 8, 2024
1 parent c2229e7 commit 6079779
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
12 changes: 12 additions & 0 deletions user/super/com/google/gwt/emul/java/lang/String.java
Original file line number Diff line number Diff line change
Expand Up @@ -647,10 +647,22 @@ public String[] split(String regex, int maxMatch) {
// subgroup handling
NativeRegExp.Match matchObj = compiled.exec(trail);
if (matchObj == null || trail == "" || (count == (maxMatch - 1) && maxMatch > 0)) {
// At the end of the string, or we have performed the maximum number of matches,
// record the remaining string and break
out[count] = trail;
break;
} else {
int matchIndex = matchObj.getIndex();

if (lastTrail == null && matchIndex == 0) {
// As of Java 8, we should discard the first zero-length match if it is the beginning of
// the string. Do not increment the count, and do not add to the output array.
trail = trail.substring(matchIndex + matchObj.asArray()[0].length(), trail.length());
compiled.lastIndex = 0;
lastTrail = trail;
continue;
}

out[count] = trail.substring(0, matchIndex);
trail = trail.substring(matchIndex + matchObj.asArray()[0].length(), trail.length());
// Force the compiled pattern to reset internal state
Expand Down
7 changes: 2 additions & 5 deletions user/test/com/google/gwt/emultest/java/lang/StringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -789,11 +789,8 @@ public void testSplit() {
}

public void testSplit_emptyExpr() {
// TODO(rluble): implement JDK8 string.split semantics and fix test.
String[] expected = (TestUtils.getJdkVersion() > 7) ?
new String[] {"a", "b", "c", "x", "x", "d", "e", "x", "f", "x"} :
new String[] {"", "a", "b", "c", "x", "x", "d", "e", "x", "f", "x"};
compareList("emptyRegexSplit", expected, "abcxxdexfx".split(""));
String[] expected = new String[] {"a", "b", "c", "x", "x", "d", "e", "x", "f", "x"};
compareList("emptyRegexSplit", expected, hideFromCompiler("abcxxdexfx").split(""));
}

public void testStartsWith() {
Expand Down

0 comments on commit 6079779

Please sign in to comment.