Clover icon

Coverage Report

  1. Project Clover database Mon Nov 18 2024 09:56:54 GMT
  2. Package com.stevesoft.pat

File FastMulti.java

 

Coverage histogram

../../../img/srcFileCovDistChart7.png
29% of files have more coverage

Code metrics

38
64
6
1
170
148
27
0.42
10.67
6
4.5

Classes

Class Line # Actions
FastMulti 17 64 27
0.638888963.9%
 

Contributing tests

This file is covered by 85 tests. .

Source view

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.Hashtable;
11   
12    /**
13    * A special case of Multi, implemented when minChars().equals(maxChars()), and
14    * some other conditions spelled out in RegOpt.safe4fm "Safe for FastMulti." It
15    * avoids stack growth problems as well as being slightly faster.
16    */
 
17    class FastMulti extends PatternSub
18    {
19    patInt fewestMatches, mostMatches;
20   
 
21  342 toggle public patInt minChars()
22    {
23  342 return sub.countMinChars().mul(fewestMatches);
24    }
25   
 
26  342 toggle public patInt maxChars()
27    {
28  342 return sub.countMaxChars().mul(mostMatches);
29    }
30   
31    public boolean matchFewest = false;
32   
 
33  5079 toggle FastMulti(patInt a, patInt b, Pattern p) throws RegSyntax
34    {
35  5079 if (p == null)
36    {
37  0 RegSyntaxError.endItAll(
38    "Null length pattern " + "followed by *, +, or other Multi.");
39    }
40  5079 fewestMatches = a;
41  5079 mostMatches = b;
42  5079 sub = p;
43  5079 step = p.countMinChars().intValue();
44  5079 sub.setParent(null);
45    }
46   
 
47  0 toggle public String toString()
48    {
49  0 return sub.toString() + "{" + fewestMatches + "," + mostMatches + "}"
50  0 + (matchFewest ? "?" : "") + "(?# <= fast multi)"
51    + nextString();
52    }
53   
54    int step = -1;
55   
 
56  43833 toggle public int matchInternal(int pos, Pthings pt)
57    {
58  43833 int m = -1;
59  43833 int i = pos;
60  43833 int endstr = pt.src.length() - step;
61  43833 patInt matches = new patInt(0);
62  43833 if (matchFewest)
63    {
64  0 if (fewestMatches.lessEq(matches))
65    {
66  0 int ii = nextMatch(i, pt);
67  0 if (ii >= 0)
68    {
69  0 return ii;
70    }
71    }
72  0 while (i >= 0 && i <= endstr)
73    {
74  0 i = sub.matchInternal(i, pt);
75  0 if (i >= 0)
76    {
77  0 matches.inc();
78  0 if (fewestMatches.lessEq(matches))
79    {
80  0 int ii = nextMatch(i, pt);
81  0 if (ii >= 0)
82    {
83  0 return ii;
84    }
85    }
86  0 if (matches.equals(mostMatches))
87    {
88  0 return -1;
89    }
90    }
91    }
92  0 return -1;
93    }
94  43833 int nMatches = 0;
95  61756 while (fewestMatches.intValue() > nMatches)
96    {
97  21934 i = sub.matchInternal(i, pt);
98  21934 if (i >= 0)
99    {
100  17923 nMatches++;
101    }
102    else
103    {
104  4011 return -1;
105    }
106    }
107  39822 m = i;
108  39822 if (mostMatches.finite())
109    {
110  3211 while (nMatches < mostMatches.intValue())
111    {
112  1591 i = sub.matchInternal(i, pt);
113  1591 if (i >= 0)
114    {
115  1570 m = i;
116  1570 nMatches++;
117    }
118    else
119    {
120  21 break;
121    }
122    }
123    }
124    else
125    {
126  38181 while (true)
127    {
128  246175 i = sub.matchInternal(i, pt);
129  246175 if (i >= 0)
130    {
131  207994 m = i;
132  207994 nMatches++;
133    }
134    else
135    {
136  38181 break;
137    }
138    }
139    }
140  73991 while (m >= pos)
141    {
142  73991 int r = nextMatch(m, pt);
143  73991 if (r >= 0)
144    {
145  15101 return r;
146    }
147  58890 m -= step;
148  58890 nMatches--;
149  58890 if (nMatches < fewestMatches.intValue())
150    {
151  24721 return -1;
152    }
153    }
154  0 return -1;
155    }
156   
 
157  1212 toggle public Pattern clone1(Hashtable h)
158    {
159  1212 try
160    {
161  1212 FastMulti fm = new FastMulti(fewestMatches, mostMatches,
162    sub.clone(h));
163  1212 fm.matchFewest = matchFewest;
164  1212 return fm;
165    } catch (RegSyntax rs)
166    {
167  0 return null;
168    }
169    }
170    }