JAL-1807 Bob's JalviewJS prototype first commit
[jalviewjs.git] / unused / com / stevesoft / pat / DotMulti.java
1 //
2 // This software is now distributed according to
3 // the Lesser Gnu Public License.  Please see
4 // http://www.gnu.org/copyleft/lesser.txt for
5 // the details.
6 //    -- Happy Computing!
7 //
8 package com.stevesoft.pat;
9
10 import java.util.*;
11
12 /**
13  * A special optimization of multi that is used when the common subpattern ".*"
14  * is encountered.
15  */
16 class DotMulti extends PatternSub
17 {
18   patInt fewestMatches, mostMatches;
19
20   public patInt minChars()
21   {
22     return fewestMatches;
23   }
24
25   public patInt maxChars()
26   {
27     return mostMatches;
28   }
29
30   public boolean matchFewest = false;
31
32   StringLike src = null;
33
34   int srclength = 0;
35
36   boolean dotDoesntMatchCR = true;
37
38   DotMulti(patInt a, patInt b)
39   {
40     fewestMatches = a;
41     mostMatches = b;
42   }
43
44   public String toString()
45   {
46     return ".{" + fewestMatches + "," + mostMatches + "}"
47             + (matchFewest ? "?" : "") + "(?# <= dot multi)" + nextString();
48   }
49
50   final int submatchInternal(int pos, Pthings pt)
51   {
52     if (pos < srclength)
53     {
54       if (dotDoesntMatchCR)
55       {
56         if (src.charAt(pos) != '\n')
57         {
58           return 1 + pos;
59         }
60       }
61       else
62       {
63         return 1 + pos;
64       }
65     }
66     return -1;
67   }
68
69   final static int step = 1;
70
71   static int idcount = 1;
72
73   public int matchInternal(int pos, Pthings pt)
74   {
75     int m = -1;
76     int i = pos;
77     src = pt.src;
78     srclength = src.length();
79     dotDoesntMatchCR = pt.dotDoesntMatchCR;
80     if (matchFewest)
81     {
82       int nMatches = 0;
83       while (fewestMatches.intValue() > nMatches)
84       {
85         i = submatchInternal(i, pt);
86         if (i < 0)
87         {
88           return -1;
89         }
90         nMatches++;
91       }
92       if (i < 0)
93       {
94         return -1;
95       }
96       int ii = nextMatch(i, pt);
97       if (ii >= 0)
98       {
99         return ii;
100       }
101       if (!mostMatches.finite())
102       {
103         while (i >= 0)
104         {
105           i = submatchInternal(i, pt);
106           if (i < 0)
107           {
108             return -1;
109           }
110           ii = nextMatch(i, pt);
111           if (ii >= 0)
112           {
113             return ii;
114           }
115         }
116       }
117       else
118       {
119         while (i > 0)
120         {
121           i = submatchInternal(i, pt);
122           if (i < 0)
123           {
124             return -1;
125           }
126           nMatches++;
127           if (nMatches > mostMatches.intValue())
128           {
129             return -1;
130           }
131           ii = nextMatch(i, pt);
132           if (ii >= 0)
133           {
134             return ii;
135           }
136         }
137       }
138       return -1;
139     }
140     int nMatches = 0;
141     while (fewestMatches.intValue() > nMatches)
142     {
143       i = submatchInternal(i, pt);
144       if (i >= 0)
145       {
146         nMatches++;
147       }
148       else
149       {
150         return -1;
151       }
152     }
153     m = i;
154     if (mostMatches.finite())
155     {
156       while (nMatches < mostMatches.intValue())
157       {
158         i = submatchInternal(i, pt);
159         if (i >= 0)
160         {
161           m = i;
162           nMatches++;
163         }
164         else
165         {
166           break;
167         }
168       }
169     }
170     else
171     {
172       while (true)
173       {
174         i = submatchInternal(i, pt);
175         if (i >= 0)
176         {
177           m = i;
178           nMatches++;
179         }
180         else
181         {
182           break;
183         }
184       }
185     }
186     while (m >= pos)
187     {
188       int r = nextMatch(m, pt);
189       if (r >= 0)
190       {
191         return r;
192       }
193       m -= step;
194       nMatches--;
195       if (nMatches < fewestMatches.intValue())
196       {
197         return -1;
198       }
199     }
200     return -1;
201   }
202
203   Pattern clone1(Hashtable h)
204   {
205     DotMulti dm = new DotMulti(fewestMatches, mostMatches);
206     dm.matchFewest = matchFewest;
207     return dm;
208   }
209 }