Formatting
[jalview.git] / src / com / stevesoft / pat / FastMulti.java
index 2a38281..53130fa 100755 (executable)
 //    -- Happy Computing!\r
 //\r
 package com.stevesoft.pat;\r
-import java.util.Hashtable;\r
+\r
+import java.util.*;\r
 \r
 /** A special case of Multi, implemented when minChars().equals(maxChars()),\r
-  * and some other conditions spelled out in RegOpt.safe4fm "Safe for\r
-  * FastMulti."  It avoids stack growth problems as well as being slightly\r
-  * faster.\r
-  */\r
-class FastMulti extends PatternSub {\r
-    patInt fewestMatches,mostMatches;\r
-    public patInt minChars() {\r
-        return sub.countMinChars().mul(fewestMatches);\r
-    }\r
-    public patInt maxChars() {\r
-        return sub.countMaxChars().mul(mostMatches);\r
-    }\r
-    public boolean matchFewest = false;\r
+ * and some other conditions spelled out in RegOpt.safe4fm "Safe for\r
+ * FastMulti."  It avoids stack growth problems as well as being slightly\r
+ * faster.\r
+ */\r
+class FastMulti\r
+    extends PatternSub\r
+{\r
+  patInt fewestMatches, mostMatches;\r
+  public patInt minChars()\r
+  {\r
+    return sub.countMinChars().mul(fewestMatches);\r
+  }\r
 \r
-    FastMulti(patInt a,patInt b,Pattern p) throws RegSyntax {\r
-        if(p == null) RegSyntaxError.endItAll("Null length pattern "+\r
-                "followed by *, +, or other Multi.");\r
-        fewestMatches = a;\r
-        mostMatches = b;\r
-        sub = p;\r
-        step = p.countMinChars().intValue();\r
-        sub.setParent(null);\r
-    }\r
-    public String toString() {\r
-        return sub.toString()+"{"\r
-            +fewestMatches+","+mostMatches+"}"+\r
-            (matchFewest ? "?" : "")+"(?# <= fast multi)"+\r
-            nextString();\r
+  public patInt maxChars()\r
+  {\r
+    return sub.countMaxChars().mul(mostMatches);\r
+  }\r
+\r
+  public boolean matchFewest = false;\r
+\r
+  FastMulti(patInt a, patInt b, Pattern p)\r
+      throws RegSyntax\r
+  {\r
+    if (p == null)\r
+    {\r
+      RegSyntaxError.endItAll("Null length pattern " +\r
+                              "followed by *, +, or other Multi.");\r
     }\r
-    int step = -1;\r
-    public int matchInternal(int pos,Pthings pt) {\r
-        int m=-1;\r
-        int i=pos;\r
-        int endstr = pt.src.length()-step;\r
-        patInt matches = new patInt(0);\r
-        if(matchFewest) {\r
-            if(fewestMatches.lessEq(matches)) {\r
-                int ii = nextMatch(i,pt);\r
-                if(ii >= 0) return ii;\r
-            }\r
-            while(i >= 0 && i <= endstr) {\r
-                i=sub.matchInternal(i,pt);\r
-                if(i >= 0) {\r
-                    matches.inc();\r
-                    if(fewestMatches.lessEq(matches)) {\r
-                        int ii = nextMatch(i,pt);\r
-                        if(ii >= 0) return ii;\r
-                    }\r
-                    if(matches.equals(mostMatches))\r
-                        return -1;\r
-                }\r
+    fewestMatches = a;\r
+    mostMatches = b;\r
+    sub = p;\r
+    step = p.countMinChars().intValue();\r
+    sub.setParent(null);\r
+  }\r
+\r
+  public String toString()\r
+  {\r
+    return sub.toString() + "{"\r
+        + fewestMatches + "," + mostMatches + "}" +\r
+        (matchFewest ? "?" : "") + "(?# <= fast multi)" +\r
+        nextString();\r
+  }\r
+\r
+  int step = -1;\r
+  public int matchInternal(int pos, Pthings pt)\r
+  {\r
+    int m = -1;\r
+    int i = pos;\r
+    int endstr = pt.src.length() - step;\r
+    patInt matches = new patInt(0);\r
+    if (matchFewest)\r
+    {\r
+      if (fewestMatches.lessEq(matches))\r
+      {\r
+        int ii = nextMatch(i, pt);\r
+        if (ii >= 0)\r
+        {\r
+          return ii;\r
+        }\r
+      }\r
+      while (i >= 0 && i <= endstr)\r
+      {\r
+        i = sub.matchInternal(i, pt);\r
+        if (i >= 0)\r
+        {\r
+          matches.inc();\r
+          if (fewestMatches.lessEq(matches))\r
+          {\r
+            int ii = nextMatch(i, pt);\r
+            if (ii >= 0)\r
+            {\r
+              return ii;\r
             }\r
+          }\r
+          if (matches.equals(mostMatches))\r
+          {\r
             return -1;\r
+          }\r
         }\r
-        int nMatches = 0;\r
-        while(fewestMatches.intValue() > nMatches) {\r
-            i=sub.matchInternal(i,pt);\r
-            if(i >= 0)\r
-                nMatches++;\r
-            else\r
-                return -1;\r
+      }\r
+      return -1;\r
+    }\r
+    int nMatches = 0;\r
+    while (fewestMatches.intValue() > nMatches)\r
+    {\r
+      i = sub.matchInternal(i, pt);\r
+      if (i >= 0)\r
+      {\r
+        nMatches++;\r
+      }\r
+      else\r
+      {\r
+        return -1;\r
+      }\r
+    }\r
+    m = i;\r
+    if (mostMatches.finite())\r
+    {\r
+      while (nMatches < mostMatches.intValue())\r
+      {\r
+        i = sub.matchInternal(i, pt);\r
+        if (i >= 0)\r
+        {\r
+          m = i;\r
+          nMatches++;\r
         }\r
-        m=i;\r
-        if(mostMatches.finite()) {\r
-            while(nMatches < mostMatches.intValue()) {\r
-                i = sub.matchInternal(i,pt);\r
-                if(i>=0) {\r
-                    m=i;\r
-                    nMatches++;\r
-                } else break;\r
-            }\r
-        } else {\r
-            while(true) {\r
-                i = sub.matchInternal(i,pt);\r
-                if(i>=0) {\r
-                    m=i;\r
-                    nMatches++;\r
-                } else break;\r
-            }\r
+        else\r
+        {\r
+          break;\r
+        }\r
+      }\r
+    }\r
+    else\r
+    {\r
+      while (true)\r
+      {\r
+        i = sub.matchInternal(i, pt);\r
+        if (i >= 0)\r
+        {\r
+          m = i;\r
+          nMatches++;\r
         }\r
-        while(m >= pos) {\r
-            int r=nextMatch(m,pt);\r
-            if(r >= 0) return r;\r
-            m -= step;\r
-            nMatches--;\r
-            if(nMatches < fewestMatches.intValue())\r
-                return -1;\r
+        else\r
+        {\r
+          break;\r
         }\r
+      }\r
+    }\r
+    while (m >= pos)\r
+    {\r
+      int r = nextMatch(m, pt);\r
+      if (r >= 0)\r
+      {\r
+        return r;\r
+      }\r
+      m -= step;\r
+      nMatches--;\r
+      if (nMatches < fewestMatches.intValue())\r
+      {\r
         return -1;\r
+      }\r
     }\r
-    public Pattern clone1(Hashtable h) {\r
-        try {\r
-            FastMulti fm = new FastMulti(fewestMatches,mostMatches,sub.clone(h));\r
-            fm.matchFewest = matchFewest;\r
-            return fm;\r
-        } catch(RegSyntax rs) {\r
-            return null;\r
-        }\r
+    return -1;\r
+  }\r
+\r
+  public Pattern clone1(Hashtable h)\r
+  {\r
+    try\r
+    {\r
+      FastMulti fm = new FastMulti(fewestMatches, mostMatches, sub.clone(h));\r
+      fm.matchFewest = matchFewest;\r
+      return fm;\r
+    }\r
+    catch (RegSyntax rs)\r
+    {\r
+      return null;\r
     }\r
+  }\r
 }\r