-
Bug
-
Resolution: Fixed
-
P4
-
8u25
-
b52
-
x86
-
windows_2003
Issue | Fix Version | Assignee | Priority | Status | Resolution | Resolved In Build |
---|---|---|---|---|---|---|
JDK-8084508 | emb-9 | Paul Sandoz | P4 | Resolved | Fixed | team |
FULL PRODUCT VERSION :
A DESCRIPTION OF THE PROBLEM :
The documentation for Pattern.splitAsStream states:
>If this pattern does not match any subsequence of the input then the resulting stream has just one element, namely the input sequence in string form.
This does not hold true when the input is the empty string. When the input is the empty string and the pattern does not match, the resulting stream has *no* elements. It should return the empty string as its one output element.
I believe it is the implementation that is buggy, rather than the documentation that is incorrect, because Pattern.splitAsStream ought to be equivalent to its older sibling Pattern.split. The documentation for Pattern.split is equivalent, and that method does behave as the documentation says, returning the empty string in its output.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.util.regex.Pattern;
class SplitAsStreamTest {
public static void main(String[] args) {
// main problem:
System.out.println(Pattern.compile(",").split("").length); // expect 1, output 1
System.out.println(Pattern.compile(",").splitAsStream("").count()); // expect 1, output 0
// some other test strings to compare split and splitAsStream:
String[] ss = {
"1,2,3,4",
"1",
"",
",",
",,",
",,3",
"1,,4",
"1,,",
};
Pattern p = Pattern.compile(",");
for (String s : ss) {
System.out.println("----------------------------");
System.out.println("\"" + s + "\":");
dump(p.split(s));
dump(p.splitAsStream(s).toArray(i -> new String[i]));
}
}
static void dump(String[] array) {
System.out.print("[");
for (int i = 0; i < array.length; i++) {
if (i > 0) System.out.print(", ");
System.out.print("\"" + array[i] + "\"");
}
System.out.println("]");
}
}
---------- END SOURCE ----------
A DESCRIPTION OF THE PROBLEM :
The documentation for Pattern.splitAsStream states:
>If this pattern does not match any subsequence of the input then the resulting stream has just one element, namely the input sequence in string form.
This does not hold true when the input is the empty string. When the input is the empty string and the pattern does not match, the resulting stream has *no* elements. It should return the empty string as its one output element.
I believe it is the implementation that is buggy, rather than the documentation that is incorrect, because Pattern.splitAsStream ought to be equivalent to its older sibling Pattern.split. The documentation for Pattern.split is equivalent, and that method does behave as the documentation says, returning the empty string in its output.
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.util.regex.Pattern;
class SplitAsStreamTest {
public static void main(String[] args) {
// main problem:
System.out.println(Pattern.compile(",").split("").length); // expect 1, output 1
System.out.println(Pattern.compile(",").splitAsStream("").count()); // expect 1, output 0
// some other test strings to compare split and splitAsStream:
String[] ss = {
"1,2,3,4",
"1",
"",
",",
",,",
",,3",
"1,,4",
"1,,",
};
Pattern p = Pattern.compile(",");
for (String s : ss) {
System.out.println("----------------------------");
System.out.println("\"" + s + "\":");
dump(p.split(s));
dump(p.splitAsStream(s).toArray(i -> new String[i]));
}
}
static void dump(String[] array) {
System.out.print("[");
for (int i = 0; i < array.length; i++) {
if (i > 0) System.out.print(", ");
System.out.print("\"" + array[i] + "\"");
}
System.out.println("]");
}
}
---------- END SOURCE ----------
- backported by
-
JDK-8084508 Pattern.splitAsStream does not return input if it is empty and there is no match
-
- Resolved
-
- duplicates
-
JDK-8199771 Pattern.splitAsStream returns empty stream when splitting empty, should have 1 element per docs
-
- Closed
-