Graphical bits moved into files to save jar size
[jalview.git] / src / com / stevesoft / pat / DotMulti.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 import java.util.Hashtable;\r
10 \r
11 /** A special optimization of multi that is used when the\r
12   * common subpattern ".*" is encountered.\r
13   */\r
14 class DotMulti extends PatternSub {\r
15     patInt fewestMatches,mostMatches;\r
16     public patInt minChars() {\r
17         return fewestMatches;\r
18     }\r
19     public patInt maxChars() {\r
20         return mostMatches;\r
21     }\r
22     public boolean matchFewest = false;\r
23 \r
24     StringLike src=null;\r
25     int srclength=0;\r
26     boolean dotDoesntMatchCR=true;\r
27     DotMulti(patInt a,patInt b) {\r
28         fewestMatches = a;\r
29         mostMatches = b;\r
30     }\r
31     public String toString() {\r
32         return ".{"\r
33             +fewestMatches+","+mostMatches+"}"+\r
34             (matchFewest ? "?" : "")+"(?# <= dot multi)"+\r
35             nextString();\r
36     }\r
37     final int submatchInternal(int pos,Pthings pt) {\r
38         if(pos < srclength) {\r
39             if(dotDoesntMatchCR) {\r
40                 if(src.charAt(pos) != '\n')\r
41                     return 1+pos;\r
42             } else return 1+pos;\r
43         }\r
44         return -1;\r
45     }\r
46     final static int step = 1;\r
47     static int idcount = 1;\r
48     public int matchInternal(int pos,Pthings pt) {\r
49         int m=-1;\r
50         int i=pos;\r
51         src = pt.src;\r
52         srclength = src.length();\r
53         dotDoesntMatchCR = pt.dotDoesntMatchCR;\r
54         if(matchFewest) {\r
55             int nMatches = 0;\r
56             while(fewestMatches.intValue() > nMatches) {\r
57                 i=submatchInternal(i,pt);\r
58                 if(i<0) return -1;\r
59                 nMatches++;\r
60             }\r
61             if(i<0) return -1;\r
62             int ii = nextMatch(i,pt);\r
63             if(ii >= 0) return ii;\r
64             if(!mostMatches.finite()) {\r
65                 while(i >= 0) {\r
66                     i = submatchInternal(i,pt);\r
67                     if(i < 0) return -1;\r
68                     ii = nextMatch(i,pt);\r
69                     if(ii >= 0) return ii;\r
70                 }\r
71             } else {\r
72                 while(i > 0) {\r
73                     i = submatchInternal(i,pt);\r
74                     if(i < 0) return -1;\r
75                     nMatches++;\r
76                     if(nMatches > mostMatches.intValue())\r
77                         return -1;\r
78                     ii = nextMatch(i,pt);\r
79                     if(ii >= 0) return ii;\r
80                 }\r
81             }\r
82             return -1;\r
83         }\r
84         int nMatches = 0;\r
85         while(fewestMatches.intValue() > nMatches) {\r
86             i=submatchInternal(i,pt);\r
87             if(i >= 0)\r
88                 nMatches++;\r
89             else\r
90                 return -1;\r
91         }\r
92         m=i;\r
93         if(mostMatches.finite()) {\r
94             while(nMatches < mostMatches.intValue()) {\r
95                 i = submatchInternal(i,pt);\r
96                 if(i>=0) {\r
97                     m=i;\r
98                     nMatches++;\r
99                 } else break;\r
100             }\r
101         } else {\r
102             while(true) {\r
103                 i = submatchInternal(i,pt);\r
104                 if(i>=0) {\r
105                     m=i;\r
106                     nMatches++;\r
107                 } else break;\r
108             }\r
109         }\r
110         while(m >= pos) {\r
111             int r=nextMatch(m,pt);\r
112             if(r >= 0) return r;\r
113             m -= step;\r
114             nMatches--;\r
115             if(nMatches < fewestMatches.intValue())\r
116                 return -1;\r
117         }\r
118         return -1;\r
119     }\r
120     Pattern clone1(Hashtable h) {\r
121         DotMulti dm = new DotMulti(fewestMatches,mostMatches);\r
122         dm.matchFewest = matchFewest;\r
123         return dm;\r
124     }\r
125 }\r