Copyright test
[jalview.git] / src / com / stevesoft / pat / DotMulti.java
1 /*******************************************************************************
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $(date) The Jalview Authors
4  *
5  * This file is part of Jalview.
6  *  
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *   
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  *******************************************************************************/
21 //
22 // This software is now distributed according to
23 // the Lesser Gnu Public License.  Please see
24 // http://www.gnu.org/copyleft/lesser.txt for
25 // the details.
26 //    -- Happy Computing!
27 //
28 package com.stevesoft.pat;
29
30 import java.util.Hashtable;
31
32 /**
33  * A special optimization of multi that is used when the common subpattern ".*"
34  * is encountered.
35  */
36 class DotMulti extends PatternSub
37 {
38   patInt fewestMatches, mostMatches;
39
40   public patInt minChars()
41   {
42     return fewestMatches;
43   }
44
45   public patInt maxChars()
46   {
47     return mostMatches;
48   }
49
50   public boolean matchFewest = false;
51
52   StringLike src = null;
53
54   int srclength = 0;
55
56   boolean dotDoesntMatchCR = true;
57
58   DotMulti(patInt a, patInt b)
59   {
60     fewestMatches = a;
61     mostMatches = b;
62   }
63
64   public String toString()
65   {
66     return ".{" + fewestMatches + "," + mostMatches + "}"
67             + (matchFewest ? "?" : "") + "(?# <= dot multi)" + nextString();
68   }
69
70   final int submatchInternal(int pos, Pthings pt)
71   {
72     if (pos < srclength)
73     {
74       if (dotDoesntMatchCR)
75       {
76         if (src.charAt(pos) != '\n')
77         {
78           return 1 + pos;
79         }
80       }
81       else
82       {
83         return 1 + pos;
84       }
85     }
86     return -1;
87   }
88
89   final static int step = 1;
90
91   static int idcount = 1;
92
93   public int matchInternal(int pos, Pthings pt)
94   {
95     int m = -1;
96     int i = pos;
97     src = pt.src;
98     srclength = src.length();
99     dotDoesntMatchCR = pt.dotDoesntMatchCR;
100     if (matchFewest)
101     {
102       int nMatches = 0;
103       while (fewestMatches.intValue() > nMatches)
104       {
105         i = submatchInternal(i, pt);
106         if (i < 0)
107         {
108           return -1;
109         }
110         nMatches++;
111       }
112       if (i < 0)
113       {
114         return -1;
115       }
116       int ii = nextMatch(i, pt);
117       if (ii >= 0)
118       {
119         return ii;
120       }
121       if (!mostMatches.finite())
122       {
123         while (i >= 0)
124         {
125           i = submatchInternal(i, pt);
126           if (i < 0)
127           {
128             return -1;
129           }
130           ii = nextMatch(i, pt);
131           if (ii >= 0)
132           {
133             return ii;
134           }
135         }
136       }
137       else
138       {
139         while (i > 0)
140         {
141           i = submatchInternal(i, pt);
142           if (i < 0)
143           {
144             return -1;
145           }
146           nMatches++;
147           if (nMatches > mostMatches.intValue())
148           {
149             return -1;
150           }
151           ii = nextMatch(i, pt);
152           if (ii >= 0)
153           {
154             return ii;
155           }
156         }
157       }
158       return -1;
159     }
160     int nMatches = 0;
161     while (fewestMatches.intValue() > nMatches)
162     {
163       i = submatchInternal(i, pt);
164       if (i >= 0)
165       {
166         nMatches++;
167       }
168       else
169       {
170         return -1;
171       }
172     }
173     m = i;
174     if (mostMatches.finite())
175     {
176       while (nMatches < mostMatches.intValue())
177       {
178         i = submatchInternal(i, pt);
179         if (i >= 0)
180         {
181           m = i;
182           nMatches++;
183         }
184         else
185         {
186           break;
187         }
188       }
189     }
190     else
191     {
192       while (true)
193       {
194         i = submatchInternal(i, pt);
195         if (i >= 0)
196         {
197           m = i;
198           nMatches++;
199         }
200         else
201         {
202           break;
203         }
204       }
205     }
206     while (m >= pos)
207     {
208       int r = nextMatch(m, pt);
209       if (r >= 0)
210       {
211         return r;
212       }
213       m -= step;
214       nMatches--;
215       if (nMatches < fewestMatches.intValue())
216       {
217         return -1;
218       }
219     }
220     return -1;
221   }
222
223   Pattern clone1(Hashtable h)
224   {
225     DotMulti dm = new DotMulti(fewestMatches, mostMatches);
226     dm.matchFewest = matchFewest;
227     return dm;
228   }
229 }