Tagir,

It would seem most useful to update the implementation to
match the current spec. To that end, your first patch looks
like the most appropriate change, pattern-patch.txt.

-Chris.

On 06/12/15 12:21, Tagir F. Valeev wrote:
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

Reply via email to