Pattern.splitAsStream JavaDoc doubt
Tagir F. Valeev
amaembo at gmail.com
Sun Dec 6 12:21:51 UTC 2015
Hello!
Currently Pattern.splitAsStream JavaDoc says [1]:
* <p> If the input sequence is mutable, it must remain constant during the
* execution of the terminal stream operation. Otherwise, the result of the
* terminal stream operation is undefined.
However in reality the sequence must remain constant from the stream
creation till the end of the terminal operation. Let's check:
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("a,b,c,d,e");
Stream<String> stream = Pattern.compile(",").splitAsStream(sb);
// Modify the CharSequence after stream creation
sb.setLength(3);
// During the terminal operation it remains constant
stream.forEach(System.out::println);
}
The result is:
a
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 3
at java.lang.AbstractStringBuilder.charAt(AbstractStringBuilder.java:210)
at java.lang.StringBuilder.charAt(StringBuilder.java:76)
...
So I feel either the JavaDoc or the implementation should be changed.
Changing the implementation to fit the JavaDoc is quite simple. See
the attached pattern-patch.txt and pattern-patch2.txt for two possible
alternatives.
What do you think?
With best regards,
Tagir Valeev.
[1] http://hg.openjdk.java.net/jdk9/dev/jdk/file/3c3a5343044c/src/java.base/share/classes/java/util/regex/Pattern.java#l5803
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: pattern-patch.txt
URL: <http://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20151206/62b426e7/pattern-patch.txt>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: pattern-patch2.txt
URL: <http://mail.openjdk.java.net/pipermail/core-libs-dev/attachments/20151206/62b426e7/pattern-patch2.txt>
More information about the core-libs-dev
mailing list