< prev index next >

src/java.base/share/classes/java/util/regex/Pattern.java

Print this page
rev 57456 : [mq]: 8236034-Use-optimized-Ques-node-for-curly-0-1-quantifier

@@ -3241,25 +3241,32 @@
 
     static enum Qtype {
         GREEDY, LAZY, POSSESSIVE, INDEPENDENT
     }
 
-    private Node curly(Node prev, int cmin) {
+    private Qtype qtype() {
         int ch = next();
         if (ch == '?') {
             next();
-            return new Curly(prev, cmin, MAX_REPS, Qtype.LAZY);
+            return Qtype.LAZY;
         } else if (ch == '+') {
             next();
-            return new Curly(prev, cmin, MAX_REPS, Qtype.POSSESSIVE);
+            return Qtype.POSSESSIVE;
+        }
+        return Qtype.GREEDY;
         }
+
+    private Node curly(Node prev, int cmin) {
+        Qtype qtype = qtype();
+        if (qtype == Qtype.GREEDY) {
         if (prev instanceof BmpCharProperty) {
             return new BmpCharPropertyGreedy((BmpCharProperty)prev, cmin);
         } else if (prev instanceof CharProperty) {
             return new CharPropertyGreedy((CharProperty)prev, cmin);
         }
-        return new Curly(prev, cmin, MAX_REPS, Qtype.GREEDY);
+        }
+        return new Curly(prev, cmin, MAX_REPS, qtype);
     }
 
     /**
      * Processes repetition. If the next character peeked is a quantifier
      * then new nodes must be appended to handle the repetition.

@@ -3267,19 +3274,11 @@
      */
     private Node closure(Node prev) {
         int ch = peek();
         switch (ch) {
         case '?':
-            ch = next();
-            if (ch == '?') {
-                next();
-                return new Ques(prev, Qtype.LAZY);
-            } else if (ch == '+') {
-                next();
-                return new Ques(prev, Qtype.POSSESSIVE);
-            }
-            return new Ques(prev, Qtype.GREEDY);
+            return new Ques(prev, qtype());
         case '*':
             return curly(prev, 0);
         case '+':
             return curly(prev, 1);
         case '{':

@@ -3312,20 +3311,14 @@
                 }
                 if (ch != '}')
                     throw error("Unclosed counted closure");
                 if (cmax < cmin)
                     throw error("Illegal repetition range");
-                ch = peek();
-                if (ch == '?') {
-                    next();
-                    return new Curly(prev, cmin, cmax, Qtype.LAZY);
-                } else if (ch == '+') {
-                    next();
-                    return new Curly(prev, cmin, cmax, Qtype.POSSESSIVE);
-                } else {
-                    return new Curly(prev, cmin, cmax, Qtype.GREEDY);
-                }
+                unread();
+                return (cmin == 0 && cmax == 1)
+                        ? new Ques(prev, qtype())
+                        : new Curly(prev, cmin, cmax, qtype());
             } else {
                 throw error("Illegal repetition");
             }
         default:
             return prev;
< prev index next >