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

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1999, 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.  Oracle designates this

@@ -952,10 +952,16 @@
      * @serial
      */
     private int flags;
 
     /**
+     * The temporary pattern flags used during compiling. The flags might be turn
+     * on and off by embedded flag.
+     */
+    private transient int flags0;
+
+    /**
      * Boolean indicating this Pattern is compiled; this is necessary in order
      * to lazily compile deserialized Patterns.
      */
     private transient volatile boolean compiled;
 

@@ -1135,11 +1141,11 @@
      * Returns this pattern's match flags.
      *
      * @return  The match flags specified when this pattern was compiled
      */
     public int flags() {
-        return flags;
+        return flags0;
     }
 
     /**
      * Compiles the given regular expression and attempts to match the given
      * input against it.

@@ -1367,10 +1373,13 @@
         throws java.io.IOException, ClassNotFoundException {
 
         // Read in all fields
         s.defaultReadObject();
 
+        // reset the flags
+        flags0 = flags;
+
         // Initialize counts
         capturingGroupCount = 1;
         localCount = 0;
         localTCNCount = 0;
 

@@ -1398,10 +1407,13 @@
 
         // to use UNICODE_CASE if UNICODE_CHARACTER_CLASS present
         if ((flags & UNICODE_CHARACTER_CLASS) != 0)
             flags |= UNICODE_CASE;
 
+        // 'flags' for compiling
+        flags0 = flags;
+
         // Reset group index count
         capturingGroupCount = 1;
         localCount = 0;
         localTCNCount = 0;
 

@@ -1839,11 +1851,11 @@
 
     /**
      * Indicates whether a particular flag is set or not.
      */
     private boolean has(int f) {
-        return (flags & f) != 0;
+        return (flags0 & f) != 0;
     }
 
     /**
      * Match next character, signal error if failed.
      */

@@ -2716,11 +2728,11 @@
               (ch == 0xff || ch == 0xb5 ||
                ch == 0x49 || ch == 0x69 ||    //I and i
                ch == 0x53 || ch == 0x73 ||    //S and s
                ch == 0x4b || ch == 0x6b ||    //K and k
                ch == 0xc5 || ch == 0xe5))) {  //A+ring
-            bits.add(ch, flags());
+            bits.add(ch, flags0);
             return null;
         }
         return single(ch);
     }
 

@@ -2929,11 +2941,11 @@
      */
     private Node group0() {
         boolean capturingGroup = false;
         Node head = null;
         Node tail = null;
-        int save = flags;
+        int save = flags0;
         int saveTCNCount = topClosureNodes.size();
         root = null;
         int ch = next();
         if (ch == '?') {
             ch = skip();

@@ -3030,11 +3042,11 @@
             tail = root;
             head.next = expr(tail);
         }
 
         accept(')', "Unclosed group");
-        flags = save;
+        flags0 = save;
 
         // Check for quantifiers
         Node node = closure(head);
         if (node == head) { // No closure
             root = tail;

@@ -3133,32 +3145,32 @@
     private void addFlag() {
         int ch = peek();
         for (;;) {
             switch (ch) {
             case 'i':
-                flags |= CASE_INSENSITIVE;
+                flags0 |= CASE_INSENSITIVE;
                 break;
             case 'm':
-                flags |= MULTILINE;
+                flags0 |= MULTILINE;
                 break;
             case 's':
-                flags |= DOTALL;
+                flags0 |= DOTALL;
                 break;
             case 'd':
-                flags |= UNIX_LINES;
+                flags0 |= UNIX_LINES;
                 break;
             case 'u':
-                flags |= UNICODE_CASE;
+                flags0 |= UNICODE_CASE;
                 break;
             case 'c':
-                flags |= CANON_EQ;
+                flags0 |= CANON_EQ;
                 break;
             case 'x':
-                flags |= COMMENTS;
+                flags0 |= COMMENTS;
                 break;
             case 'U':
-                flags |= (UNICODE_CHARACTER_CLASS | UNICODE_CASE);
+                flags0 |= (UNICODE_CHARACTER_CLASS | UNICODE_CASE);
                 break;
             case '-': // subFlag then fall through
                 ch = next();
                 subFlag();
             default:

@@ -3176,32 +3188,32 @@
     private void subFlag() {
         int ch = peek();
         for (;;) {
             switch (ch) {
             case 'i':
-                flags &= ~CASE_INSENSITIVE;
+                flags0 &= ~CASE_INSENSITIVE;
                 break;
             case 'm':
-                flags &= ~MULTILINE;
+                flags0 &= ~MULTILINE;
                 break;
             case 's':
-                flags &= ~DOTALL;
+                flags0 &= ~DOTALL;
                 break;
             case 'd':
-                flags &= ~UNIX_LINES;
+                flags0 &= ~UNIX_LINES;
                 break;
             case 'u':
-                flags &= ~UNICODE_CASE;
+                flags0 &= ~UNICODE_CASE;
                 break;
             case 'c':
-                flags &= ~CANON_EQ;
+                flags0 &= ~CANON_EQ;
                 break;
             case 'x':
-                flags &= ~COMMENTS;
+                flags0 &= ~COMMENTS;
                 break;
             case 'U':
-                flags &= ~(UNICODE_CHARACTER_CLASS | UNICODE_CASE);
+                flags0 &= ~(UNICODE_CHARACTER_CLASS | UNICODE_CASE);
                 break;
             default:
                 return;
             }
             ch = next();