This is the first of a series of patches to cleanup
the regex classes before fixing PR36000.

ChangeLog:

2008-05-07  Andrew John Hughes  <[EMAIL PROTECTED]>

        Add generics to these classes.
        * gnu/java/util/regex/RETokenOneOf.java:
        Use an ArrayList and a Deque instead of
        a Vector and a Stack to remove unnecessary
        synchronisation.
        * gnu/java/util/regex/RETokenRepeated.java:
        Replace custom FindMatchControlStack class
        with a generic Deque.
        * java/util/regex/Pattern.java:
        Use a generic ArrayList instance.

-- 
Andrew :)

Support Free Java!
Contribute to GNU Classpath and the OpenJDK
http://www.gnu.org/software/classpath
http://openjdk.java.net
PGP Key: 94EFD9D8 (http://subkeys.pgp.net)
Fingerprint = F8EF F1EA 401E 2E60 15FA  7927 142C 2591 94EF D9D8
Index: gnu/java/util/regex/RETokenOneOf.java
===================================================================
RCS file: /sources/classpath/classpath/gnu/java/util/regex/RETokenOneOf.java,v
retrieving revision 1.3
diff -u -r1.3 RETokenOneOf.java
--- gnu/java/util/regex/RETokenOneOf.java       5 May 2008 20:58:08 -0000       
1.3
+++ gnu/java/util/regex/RETokenOneOf.java       7 May 2008 19:52:04 -0000
@@ -39,23 +39,25 @@
 
 import gnu.java.lang.CPStringBuilder;
 
-import java.util.Vector;
-import java.util.Stack;
+import java.util.ArrayDeque;
+import java.util.ArrayList;
+import java.util.Deque;
+import java.util.List;
 
 final class RETokenOneOf extends REToken {
-  private Vector options;
+  private final List<REToken> options;
   private boolean negative;
   // True if this RETokenOneOf is supposed to match only one character,
   // which is typically the case of a character class expression.
   private boolean matchesOneChar;
 
-  private Vector addition;
-  // This Vector addition is used to store nested character classes.
+  private final List<Object> addition;
+  // This ArrayList addition is used to store nested character classes.
   // For example, if the original expression is
   //    [2-7a-c[f-k][m-z]&&[^p-v][st]]
-  // the basic part /2-7a-c/ is stored in the Vector options, and
+  // the basic part /2-7a-c/ is stored in the ArrayList options, and
   // the additional part /[f-k][m-z]&&[^p-v][st]/ is stored in the
-  // Vector addition in the following order (Reverse Polish Notation):
+  // ArrayList addition in the following order (Reverse Polish Notation):
   //           -- The matching result of the basic part is assumed here. 
   //    [f-k]  -- REToken
   //    "|"    -- or
@@ -68,7 +70,7 @@
   //    "|"    -- or
   //    "&"    -- and
   //
-  // As it is clear from the explanation above, the Vector addition is
+  // As it is clear from the explanation above, the ArrayList addition is
   // effective only when this REToken originates from a character class
   // expression.
 
@@ -78,21 +80,20 @@
 
   RETokenOneOf(int subIndex, String optionsStr, boolean negative, boolean 
insens) {
     super(subIndex);
-    options = new Vector();
+    options = new ArrayList<REToken>();
     this.negative = negative;
     for (int i = 0; i < optionsStr.length(); i++)
-      options.addElement(new 
RETokenChar(subIndex,optionsStr.charAt(i),insens));
+      options.add(new RETokenChar(subIndex,optionsStr.charAt(i),insens));
     matchesOneChar = true;
+    addition = null;
   }
 
-  RETokenOneOf(int subIndex, Vector options, boolean negative) {
-    super(subIndex);
-    this.options = options;
-    this.negative = negative;
-    matchesOneChar = negative;
+  RETokenOneOf(int subIndex, List<REToken> options, boolean negative) {
+    this(subIndex, options, null, negative);
   }
 
-  RETokenOneOf(int subIndex, Vector options, Vector addition, boolean 
negative) {
+  RETokenOneOf(int subIndex, List<REToken> options, List<Object> addition,
+              boolean negative) {
     super(subIndex);
     this.options = options;
     this.addition = addition;
@@ -104,8 +105,8 @@
     if (matchesOneChar) return 1;
     int min = Integer.MAX_VALUE;
     int x;
-    for (int i=0; i < options.size(); i++) {
-      if ((x = ((REToken) options.elementAt(i)).getMinimumLength()) < min)
+    for (REToken t : options) {
+      if ((x = t.getMinimumLength()) < min)
        min = x;
     }
     return min;
@@ -115,8 +116,8 @@
     if (matchesOneChar) return 1;
     int max = 0;
     int x;
-    for (int i=0; i < options.size(); i++) {
-      if ((x = ((REToken) options.elementAt(i)).getMaximumLength()) > max)
+    for (REToken t : options) {
+      if ((x = t.getMaximumLength()) > max)
        max = x;
     }
     return max;
@@ -144,33 +145,31 @@
         matchP(input, tryMatch, tryOnly);
       if (addition == null) return b;
 
-      Stack stack = new Stack();
+      final Deque<Boolean> stack = new ArrayDeque<Boolean>();
       stack.push(new Boolean(b));
-      for (int i=0; i < addition.size(); i++) {
-       Object obj = addition.elementAt(i);
+      for (Object obj : addition) {
        if (obj instanceof REToken) {
          b = ((REToken)obj).match(input, (REMatch)mymatch.clone());
          stack.push(new Boolean(b));
        }
        else if (obj instanceof Boolean) {
-         stack.push(obj);
+         stack.push((Boolean) obj);
        }
        else if (obj.equals("|")) {
-         b = ((Boolean)stack.pop()).booleanValue();
-         b = ((Boolean)stack.pop()).booleanValue() || b;
+         b = stack.pop();
+         b = stack.pop() || b;
          stack.push(new Boolean(b));
        }
        else if (obj.equals("&")) {
-         b = ((Boolean)stack.pop()).booleanValue();
-         b = ((Boolean)stack.pop()).booleanValue() && b;
+         b = stack.pop();
+         b = stack.pop() && b;
          stack.push(new Boolean(b));
        }
        else {
          throw new RuntimeException("Invalid object found");
        }
       }
-      b = ((Boolean)stack.pop()).booleanValue();
-      if (b) {
+      if (stack.pop()) {
         ++mymatch.index;
         return next(input, mymatch);
       }
@@ -181,11 +180,7 @@
       if (input.charAt(mymatch.index) == CharIndexed.OUT_OF_BOUNDS) 
         return false;
 
-      REMatch newMatch = null;
-      REMatch last = null;
-      REToken tk;
-      for (int i=0; i < options.size(); i++) {
-       tk = (REToken) options.elementAt(i);
+      for (REToken tk : options) {
        REMatch tryMatch = (REMatch) mymatch.clone();
        if (tk.match(input, tryMatch)) { // match was successful
            return false;
@@ -198,9 +193,7 @@
     }
 
     private boolean matchP(CharIndexed input, REMatch mymatch, boolean 
tryOnly) {
-      REToken tk;
-      for (int i=0; i < options.size(); i++) {
-       tk = (REToken) options.elementAt(i);
+      for (REToken tk : options) {
        REMatch tryMatch = (REMatch) mymatch.clone();
        if (tk.match(input, tryMatch)) { // match was successful
          if (tryOnly) return true;
@@ -233,7 +226,7 @@
 
   private REMatch findMatch(CharIndexed input, REMatch mymatch, int 
optionIndex) {
       for (int i = optionIndex; i < options.size(); i++) {
-          REToken tk = (REToken) options.elementAt(i);
+          REToken tk = options.get(i);
          tk = (REToken) tk.clone();
          tk.chain(getNext());
           REMatch tryMatch = (REMatch) mymatch.clone();
@@ -243,13 +236,13 @@
          boolean stackPushed = false;
          if (i + 1 < options.size()) {
             tryMatch.backtrackStack.push(new BacktrackStack.Backtrack(
-              this, input, mymatch, new Integer(i + 1)));
+              this, input, mymatch, i + 1));
            stackPushed = true;
           }
-         boolean b = tk.match(input, tryMatch);
-         if (b) {
-            return tryMatch;
-         }
+         if (tk.match(input, tryMatch))
+           {
+             return tryMatch;
+           }
          if (stackPushed) tryMatch.backtrackStack.pop();
       }
       return null; 
@@ -277,7 +270,7 @@
     os.append(negative ? "[^" : "(?:");
     for (int i = 0; i < options.size(); i++) {
       if (!negative && (i > 0)) os.append('|');
-      ((REToken) options.elementAt(i)).dumpAll(os);
+      options.get(i).dumpAll(os);
     }
     os.append(negative ? ']' : ')');
   }  
Index: gnu/java/util/regex/RETokenRepeated.java
===================================================================
RCS file: 
/sources/classpath/classpath/gnu/java/util/regex/RETokenRepeated.java,v
retrieving revision 1.4
diff -u -r1.4 RETokenRepeated.java
--- gnu/java/util/regex/RETokenRepeated.java    5 May 2008 20:58:08 -0000       
1.4
+++ gnu/java/util/regex/RETokenRepeated.java    7 May 2008 19:52:04 -0000
@@ -40,7 +40,8 @@
 
 import gnu.java.lang.CPStringBuilder;
 
-import java.util.ArrayList;
+import java.util.ArrayDeque;
+import java.util.Deque;
 
 final class RETokenRepeated extends REToken {
     private REToken token;
@@ -169,18 +170,6 @@
        }
     }
 
-    private static class FindMatchControlStack extends ArrayList {
-       private void push(FindMatchControl control) {
-           add(control);
-       }
-       private FindMatchControl pop() {
-           return (FindMatchControl)remove(size()-1);
-       }
-       private boolean empty() {
-           return isEmpty();
-       }
-    }
-
     private static class FindMatchControl {
        DoablesFinder finder;
        FindMatchControl(DoablesFinder finder) {
@@ -189,11 +178,11 @@
     }
 
     private REMatch findMatch(BacktrackStack stack) {
-       return findMatch(stack, new FindMatchControlStack());
+       return findMatch(stack, new ArrayDeque<FindMatchControl>());
     }
 
     private REMatch findMatch(BacktrackStack stack,
-               FindMatchControlStack controlStack) {
+               Deque<FindMatchControl> controlStack) {
        REMatch result = null;
        StackedInfo si = null;
        CharIndexed input = null;
@@ -324,7 +313,7 @@
 
        } // MAIN_LOOP
 
-       if (controlStack.empty()) return result;
+       if (controlStack.isEmpty()) return result;
        FindMatchControl control = controlStack.pop();
        if (possessive) {
            return result;
Index: java/util/regex/Pattern.java
===================================================================
RCS file: /sources/classpath/classpath/java/util/regex/Pattern.java,v
retrieving revision 1.20
diff -u -r1.20 Pattern.java
--- java/util/regex/Pattern.java        7 Mar 2007 22:26:35 -0000       1.20
+++ java/util/regex/Pattern.java        7 May 2008 19:52:09 -0000
@@ -191,7 +191,7 @@
   public String[] split (CharSequence input, int limit)
   {
     Matcher matcher = new Matcher(this, input);
-    ArrayList list = new ArrayList();
+    ArrayList<String> list = new ArrayList<String>();
     int empties = 0;
     int count = 0;
     int start = 0;
@@ -251,9 +251,7 @@
          list.add(t);
       }
 
-    String[] output = new String [list.size()];
-    list.toArray(output);
-    return output;
+    return list.toArray(new String[list.size()]);
   }
   
   public String pattern ()

Reply via email to