Skip to content
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

Implement Java 8 String.split() semantics #10054

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 && matchObj.asArray()[0].length() == 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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package com.google.gwt.emultest.java.lang;

import com.google.gwt.junit.client.GWTTestCase;
import com.google.gwt.testing.TestUtils;

import java.util.Locale;

Expand Down Expand Up @@ -245,15 +244,30 @@ public void testSplit() {
"b", "", ":and:f"});
compareList("0:", "boo:and:foo".split(":", 0), new String[] {
"boo", "and", "foo"});
}
// issue 2742
compareList("issue2742", new String[] {}, "/".split("/", 0));

// Splitting an empty string should result in an array containing a single
// empty string.
String[] s = "".split(",");
assertTrue(s != null);
assertTrue(s.length == 1);
assertTrue(s[0] != null);
assertTrue(s[0].length() == 0);

s = "abcada".split("a");
assertTrue(s != null);
assertEquals(3, s.length);
assertEquals("", s[0]);
assertEquals("bc", s[1]);
assertEquals("d", s[2]); }

public void testSplit_emptyExpr() {
// TODO(rluble): implement JDK8 string.split semantics and fix test.
// See issue 8913.
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"};
String[] expected = new String[] {"a", "b", "c", "x", "x", "d", "e", "x", "f", "x"};
compareList("emptyRegexSplit", expected, "abcxxdexfx".split(""));

String[] arr = ",".split(",");
assertEquals(0, arr.length);
}

public void testStartsWith() {
Expand Down
21 changes: 14 additions & 7 deletions user/test/com/google/gwt/emultest/java/lang/StringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -781,23 +781,30 @@ public void testSplit() {

// Splitting an empty string should result in an array containing a single
// empty string.
String[] s = "".split(",");
String[] s = hideFromCompiler("").split(",");
assertTrue(s != null);
assertTrue(s.length == 1);
assertTrue(s[0] != null);
assertTrue(s[0].length() == 0);

s = hideFromCompiler("abcada").split("a");
assertTrue(s != null);
assertEquals(3, s.length);
assertEquals("", s[0]);
assertEquals("bc", s[1]);
assertEquals("d", s[2]);
}

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(""));

String[] arr = hideFromCompiler(",").split(",");
assertEquals(0, arr.length);
}

public void testStartsWith() {
String haystack = "abcdefghi";
String haystack = hideFromCompiler("abcdefghi");
assertTrue(haystack.startsWith("abc"));
assertTrue(haystack.startsWith("bc", 1));
assertTrue(haystack.startsWith(haystack));
Expand Down
Loading