提交 3425cb63 编写于 作者: P psandoz

8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split()...

8024341: j.u.regex.Pattern.splitAsStream() doesn't correspond to split() method if using an example from the spec
Reviewed-by: alanb
上级 3309e6da
...@@ -5755,7 +5755,8 @@ NEXT: while (i <= last) { ...@@ -5755,7 +5755,8 @@ NEXT: while (i <= last) {
* input sequence that is terminated by another subsequence that matches * input sequence that is terminated by another subsequence that matches
* this pattern or is terminated by the end of the input sequence. The * this pattern or is terminated by the end of the input sequence. The
* substrings in the stream are in the order in which they occur in the * substrings in the stream are in the order in which they occur in the
* input. * input. Trailing empty strings will be discarded and not encountered in
* the stream.
* *
* <p> If this pattern does not match any subsequence of the input then * <p> If this pattern does not match any subsequence of the input then
* the resulting stream has just one element, namely the input sequence in * the resulting stream has just one element, namely the input sequence in
...@@ -5781,6 +5782,8 @@ NEXT: while (i <= last) { ...@@ -5781,6 +5782,8 @@ NEXT: while (i <= last) {
private int current; private int current;
// null if the next element, if any, needs to obtained // null if the next element, if any, needs to obtained
private String nextElement; private String nextElement;
// > 0 if there are N next empty elements
private int emptyElementCount;
MatcherIterator() { MatcherIterator() {
this.matcher = matcher(input); this.matcher = matcher(input);
...@@ -5790,26 +5793,46 @@ NEXT: while (i <= last) { ...@@ -5790,26 +5793,46 @@ NEXT: while (i <= last) {
if (!hasNext()) if (!hasNext())
throw new NoSuchElementException(); throw new NoSuchElementException();
if (emptyElementCount == 0) {
String n = nextElement; String n = nextElement;
nextElement = null; nextElement = null;
return n; return n;
} else {
emptyElementCount--;
return "";
}
} }
public boolean hasNext() { public boolean hasNext() {
if (nextElement != null) if (nextElement != null || emptyElementCount > 0)
return true; return true;
if (current == input.length()) if (current == input.length())
return false; return false;
if (matcher.find()) { // Consume the next matching element
// Count sequence of matching empty elements
while (matcher.find()) {
nextElement = input.subSequence(current, matcher.start()).toString(); nextElement = input.subSequence(current, matcher.start()).toString();
current = matcher.end(); current = matcher.end();
if (!nextElement.isEmpty()) {
return true;
} else { } else {
emptyElementCount++;
}
}
// Consume last matching element
nextElement = input.subSequence(current, input.length()).toString(); nextElement = input.subSequence(current, input.length()).toString();
current = input.length(); current = input.length();
} if (!nextElement.isEmpty()) {
return true; return true;
} else {
// Ignore a terminal sequence of matching empty elements
emptyElementCount = 0;
nextElement = null;
return false;
}
} }
} }
return StreamSupport.stream(Spliterators.spliteratorUnknownSize( return StreamSupport.stream(Spliterators.spliteratorUnknownSize(
......
...@@ -23,10 +23,11 @@ ...@@ -23,10 +23,11 @@
/** /**
* @test * @test
* @bug 8016846 8024341
* @summary Unit tests for wrapping classes should delegate to default methods * @summary Unit tests for wrapping classes should delegate to default methods
* @library ../stream/bootlib * @library ../stream/bootlib
* @build java.util.stream.OpTestCase * @build java.util.stream.OpTestCase
* @run testng/othervm PatternTest * @run testng/othervm PatternStreamTest
*/ */
import org.testng.annotations.DataProvider; import org.testng.annotations.DataProvider;
...@@ -42,7 +43,7 @@ import java.util.stream.Stream; ...@@ -42,7 +43,7 @@ import java.util.stream.Stream;
import java.util.stream.TestData; import java.util.stream.TestData;
@Test @Test
public class PatternTest extends OpTestCase { public class PatternStreamTest extends OpTestCase {
@DataProvider(name = "Stream<String>") @DataProvider(name = "Stream<String>")
public static Object[][] makeStreamTestData() { public static Object[][] makeStreamTestData() {
...@@ -132,6 +133,38 @@ public class PatternTest extends OpTestCase { ...@@ -132,6 +133,38 @@ public class PatternTest extends OpTestCase {
expected.add("different"); expected.add("different");
expected.add("separators"); expected.add("separators");
description = "Repeated separators within and at end";
input = "boo:and:foo";
pattern = Pattern.compile("o");
expected = new ArrayList<>();
expected.add("b");
expected.add("");
expected.add(":and:f");
description = "Many repeated separators within and at end";
input = "booooo:and:fooooo";
pattern = Pattern.compile("o");
expected = new ArrayList<>();
expected.add("b");
expected.add("");
expected.add("");
expected.add("");
expected.add("");
expected.add(":and:f");
description = "Many repeated separators before last match";
input = "fooooo:";
pattern = Pattern.compile("o");
expected = new ArrayList<>();
expected.add("f");
expected.add("");
expected.add("");
expected.add("");
expected.add("");
expected.add(":");
data.add(new Object[] {description, input, pattern, expected}); data.add(new Object[] {description, input, pattern, expected});
return data.toArray(new Object[0][]); return data.toArray(new Object[0][]);
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册