JAL-1807 still testing
[jalviewjs.git] / unused / com / stevesoft / pat / FastMulti.java
1 //\r
2 // This software is now distributed according to\r
3 // the Lesser Gnu Public License.  Please see\r
4 // http://www.gnu.org/copyleft/lesser.txt for\r
5 // the details.\r
6 //    -- Happy Computing!\r
7 //\r
8 package com.stevesoft.pat;\r
9 \r
10 import java.util.*;\r
11 \r
12 /**\r
13  * A special case of Multi, implemented when minChars().equals(maxChars()), and\r
14  * some other conditions spelled out in RegOpt.safe4fm "Safe for FastMulti." It\r
15  * avoids stack growth problems as well as being slightly faster.\r
16  */\r
17 class FastMulti extends PatternSub\r
18 {\r
19   patInt fewestMatches, mostMatches;\r
20 \r
21   public patInt minChars()\r
22   {\r
23     return sub.countMinChars().mul(fewestMatches);\r
24   }\r
25 \r
26   public patInt maxChars()\r
27   {\r
28     return sub.countMaxChars().mul(mostMatches);\r
29   }\r
30 \r
31   public boolean matchFewest = false;\r
32 \r
33   FastMulti(patInt a, patInt b, Pattern p) throws RegSyntax\r
34   {\r
35     if (p == null)\r
36     {\r
37       RegSyntaxError.endItAll("Null length pattern "\r
38               + "followed by *, +, or other Multi.");\r
39     }\r
40     fewestMatches = a;\r
41     mostMatches = b;\r
42     sub = p;\r
43     step = p.countMinChars().intValue();\r
44     sub.setParent(null);\r
45   }\r
46 \r
47   public String toString()\r
48   {\r
49     return sub.toString() + "{" + fewestMatches + "," + mostMatches + "}"\r
50             + (matchFewest ? "?" : "") + "(?# <= fast multi)"\r
51             + nextString();\r
52   }\r
53 \r
54   int step = -1;\r
55 \r
56   public int matchInternal(int pos, Pthings pt)\r
57   {\r
58     int m = -1;\r
59     int i = pos;\r
60     int endstr = pt.src.length() - step;\r
61     patInt matches = new patInt(0);\r
62     if (matchFewest)\r
63     {\r
64       if (fewestMatches.lessEq(matches))\r
65       {\r
66         int ii = nextMatch(i, pt);\r
67         if (ii >= 0)\r
68         {\r
69           return ii;\r
70         }\r
71       }\r
72       while (i >= 0 && i <= endstr)\r
73       {\r
74         i = sub.matchInternal(i, pt);\r
75         if (i >= 0)\r
76         {\r
77           matches.inc();\r
78           if (fewestMatches.lessEq(matches))\r
79           {\r
80             int ii = nextMatch(i, pt);\r
81             if (ii >= 0)\r
82             {\r
83               return ii;\r
84             }\r
85           }\r
86           if (matches.equals(mostMatches))\r
87           {\r
88             return -1;\r
89           }\r
90         }\r
91       }\r
92       return -1;\r
93     }\r
94     int nMatches = 0;\r
95     while (fewestMatches.intValue() > nMatches)\r
96     {\r
97       i = sub.matchInternal(i, pt);\r
98       if (i >= 0)\r
99       {\r
100         nMatches++;\r
101       }\r
102       else\r
103       {\r
104         return -1;\r
105       }\r
106     }\r
107     m = i;\r
108     if (mostMatches.finite())\r
109     {\r
110       while (nMatches < mostMatches.intValue())\r
111       {\r
112         i = sub.matchInternal(i, pt);\r
113         if (i >= 0)\r
114         {\r
115           m = i;\r
116           nMatches++;\r
117         }\r
118         else\r
119         {\r
120           break;\r
121         }\r
122       }\r
123     }\r
124     else\r
125     {\r
126       while (true)\r
127       {\r
128         i = sub.matchInternal(i, pt);\r
129         if (i >= 0)\r
130         {\r
131           m = i;\r
132           nMatches++;\r
133         }\r
134         else\r
135         {\r
136           break;\r
137         }\r
138       }\r
139     }\r
140     while (m >= pos)\r
141     {\r
142       int r = nextMatch(m, pt);\r
143       if (r >= 0)\r
144       {\r
145         return r;\r
146       }\r
147       m -= step;\r
148       nMatches--;\r
149       if (nMatches < fewestMatches.intValue())\r
150       {\r
151         return -1;\r
152       }\r
153     }\r
154     return -1;\r
155   }\r
156 \r
157   public Pattern clone1(Hashtable h)\r
158   {\r
159     try\r
160     {\r
161       FastMulti fm = new FastMulti(fewestMatches, mostMatches, sub.clone(h));\r
162       fm.matchFewest = matchFewest;\r
163       return fm;\r
164     } catch (RegSyntax rs)\r
165     {\r
166       return null;\r
167     }\r
168   }\r
169 }\r