Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8069325

Pattern.splitAsStream does not return input if it is empty and there is no match

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P4
    • 9
    • 8u25
    • core-libs
    • b52
    • x86
    • windows_2003

    Backports

      Description

        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 ----------

        Attachments

          Issue Links

            Activity

              People

                psandoz Paul Sandoz
                webbuggrp Webbug Group
                Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: