-
Bug
-
Resolution: Duplicate
-
P4
-
None
-
8u162
-
x86_64
-
windows_10
FULL PRODUCT VERSION :
$ java -version
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 10.0.16299.309]
A DESCRIPTION OF THE PROBLEM :
When giving Pattern.splitAsStream() an empty string, and when the pattern does not match, it should return a Stream of 1 element (the empty string) per the docs:
"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."
but it returns an empty Stream (Stream with no elements).
Pattern.split("") works correctly in that it returns an array with one element (the input string).
Pattern.splitAsStream() works correctly when given a non-empty string that does not match.
Pattern.splitAsStream() does not work correctly when given an empty string.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
see executable program below
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
see executable program below
ACTUAL -
see executable program below
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.util.regex.*;
/*
javac TestPatternSplit.java && java -ea TestPatternSplit
Exception in thread "main" java.lang.AssertionError: 1 == .splitAsStream("").count()
at TestPatternSplit.main(TestPatternSplit.java:25)
*/
public class TestPatternSplit {
public static void main(String[] args) {
Pattern delim = Pattern.compile("\\s+");
// From docs: https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html
// "If this pattern does not match any subsequence of the input then the resulting array has just one element, namely the input sequence in string form."
assert 1 == delim.split("").length : "1 == .split(\"\").length";
// "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."
assert 1 == delim.splitAsStream("foo").count() : "1 == .splitAsStream(\"\").count()";
assert 1 == delim.splitAsStream("").count() : "1 == .splitAsStream(\"\").count()"; // fails
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Pattern.compile("\\s+").splitAsStream("") works correctly in:
$ java -version
java version "9.0.4"
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)
Workaround:
Stream.of(Pattern.compile("\\s+").split(""));
$ java -version
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 10.0.16299.309]
A DESCRIPTION OF THE PROBLEM :
When giving Pattern.splitAsStream() an empty string, and when the pattern does not match, it should return a Stream of 1 element (the empty string) per the docs:
"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."
but it returns an empty Stream (Stream with no elements).
Pattern.split("") works correctly in that it returns an array with one element (the input string).
Pattern.splitAsStream() works correctly when given a non-empty string that does not match.
Pattern.splitAsStream() does not work correctly when given an empty string.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
see executable program below
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
see executable program below
ACTUAL -
see executable program below
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.util.regex.*;
/*
javac TestPatternSplit.java && java -ea TestPatternSplit
Exception in thread "main" java.lang.AssertionError: 1 == .splitAsStream("").count()
at TestPatternSplit.main(TestPatternSplit.java:25)
*/
public class TestPatternSplit {
public static void main(String[] args) {
Pattern delim = Pattern.compile("\\s+");
// From docs: https://docs.oracle.com/javase/8/docs/api/java/util/regex/Pattern.html
// "If this pattern does not match any subsequence of the input then the resulting array has just one element, namely the input sequence in string form."
assert 1 == delim.split("").length : "1 == .split(\"\").length";
// "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."
assert 1 == delim.splitAsStream("foo").count() : "1 == .splitAsStream(\"\").count()";
assert 1 == delim.splitAsStream("").count() : "1 == .splitAsStream(\"\").count()"; // fails
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Pattern.compile("\\s+").splitAsStream("") works correctly in:
$ java -version
java version "9.0.4"
Java(TM) SE Runtime Environment (build 9.0.4+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.4+11, mixed mode)
Workaround:
Stream.of(Pattern.compile("\\s+").split(""));
- duplicates
-
JDK-8069325 Pattern.splitAsStream does not return input if it is empty and there is no match
-
- Resolved
-