8176029: Linebreak matcher is not equivalent to the pattern as stated in javadoc
authorsherman
Thu, 09 Mar 2017 12:39:34 -0800
changeset 44122 65a14579a2ae
parent 44121 3ed4e4e86d3f
child 44123 3a090845d178
8176029: Linebreak matcher is not equivalent to the pattern as stated in javadoc Reviewed-by: rriggs
jdk/src/java.base/share/classes/java/util/regex/Pattern.java
jdk/test/java/util/regex/RegExTest.java
--- a/jdk/src/java.base/share/classes/java/util/regex/Pattern.java	Thu Mar 09 08:58:58 2017 -0800
+++ b/jdk/src/java.base/share/classes/java/util/regex/Pattern.java	Thu Mar 09 12:39:34 2017 -0800
@@ -3867,8 +3867,10 @@
                     return next.match(matcher, i + 1, seq);
                 if (ch == 0x0D) {
                     i++;
-                    if (i < matcher.to && seq.charAt(i) == 0x0A)
-                        i++;
+                    if (i < matcher.to && seq.charAt(i) == 0x0A &&
+                        next.match(matcher, i + 1, seq)) {
+                        return true;
+                    }
                     return next.match(matcher, i, seq);
                 }
             } else {
--- a/jdk/test/java/util/regex/RegExTest.java	Thu Mar 09 08:58:58 2017 -0800
+++ b/jdk/test/java/util/regex/RegExTest.java	Thu Mar 09 12:39:34 2017 -0800
@@ -35,6 +35,7 @@
  * 8027645 8035076 8039124 8035975 8074678 6854417 8143854 8147531 7071819
  * 8151481 4867170 7080302 6728861 6995635 6736245 4916384
  * 6328855 6192895 6345469 6988218 6693451 7006761 8140212 8143282 8158482
+ * 8176029
  *
  * @library /lib/testlibrary
  * @build jdk.testlibrary.*
@@ -4567,10 +4568,15 @@
         String linebreaks = new String (new char[] {
             0x0A, 0x0B, 0x0C, 0x0D, 0x85, 0x2028, 0x2029 });
         String crnl = "\r\n";
-        if (!Pattern.compile("\\R+").matcher(linebreaks).matches() ||
-            !Pattern.compile("\\R").matcher(crnl).matches() ||
-            Pattern.compile("\\R\\R").matcher(crnl).matches())
-            failCount++;
+        if (!(Pattern.compile("\\R+").matcher(linebreaks).matches() &&
+              Pattern.compile("\\R").matcher(crnl).matches() &&
+              Pattern.compile("\\Rabc").matcher(crnl + "abc").matches() &&
+              Pattern.compile("\\Rabc").matcher("\rabc").matches() &&
+              Pattern.compile("\\R\\R").matcher(crnl).matches() &&  // backtracking
+              Pattern.compile("\\R\\n").matcher(crnl).matches()) && // backtracking
+              !Pattern.compile("((?<!\\R)\\s)*").matcher(crnl).matches()) { // #8176029
+            failCount++;
+        }
         report("linebreakTest");
     }