8069325: Pattern.splitAsStream does not return input if it is empty and there is no match
authorpsandoz
Fri, 13 Feb 2015 11:13:30 +0100
changeset 28964 f4a570561f34
parent 28963 8498cdb7c54b
child 28965 9468d534ab8c
8069325: Pattern.splitAsStream does not return input if it is empty and there is no match Reviewed-by: sherman
jdk/src/java.base/share/classes/java/util/regex/Pattern.java
jdk/test/java/util/regex/PatternStreamTest.java
--- a/jdk/src/java.base/share/classes/java/util/regex/Pattern.java	Fri Feb 13 11:13:27 2015 +0100
+++ b/jdk/src/java.base/share/classes/java/util/regex/Pattern.java	Fri Feb 13 11:13:30 2015 +0100
@@ -5819,6 +5819,10 @@
 
             MatcherIterator() {
                 this.matcher = matcher(input);
+                // If the input is an empty string then the result can only be a
+                // stream of the input.  Induce that by setting the empty
+                // element count to 1
+                this.emptyElementCount = input.length() == 0 ? 1 : 0;
             }
 
             public String next() {
--- a/jdk/test/java/util/regex/PatternStreamTest.java	Fri Feb 13 11:13:27 2015 +0100
+++ b/jdk/test/java/util/regex/PatternStreamTest.java	Fri Feb 13 11:13:30 2015 +0100
@@ -114,9 +114,20 @@
         data.add(new Object[]{description, input, pattern, expected});
 
 
+        description = "Empty input";
         input = "";
         pattern = Pattern.compile("\u56da");
         expected = new ArrayList<>();
+        expected.add("");
+
+        data.add(new Object[]{description, input, pattern, expected});
+
+
+        description = "Empty input with empty pattern";
+        input = "";
+        pattern = Pattern.compile("");
+        expected = new ArrayList<>();
+        expected.add("");
 
         data.add(new Object[]{description, input, pattern, expected});