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
6 // -- Happy Computing!
\r
8 package com.stevesoft.pat;
\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
17 class FastMulti extends PatternSub
\r
19 patInt fewestMatches, mostMatches;
\r
21 public patInt minChars()
\r
23 return sub.countMinChars().mul(fewestMatches);
\r
26 public patInt maxChars()
\r
28 return sub.countMaxChars().mul(mostMatches);
\r
31 public boolean matchFewest = false;
\r
33 FastMulti(patInt a, patInt b, Pattern p) throws RegSyntax
\r
37 RegSyntaxError.endItAll("Null length pattern "
\r
38 + "followed by *, +, or other Multi.");
\r
43 step = p.countMinChars().intValue();
\r
44 sub.setParent(null);
\r
47 public String toString()
\r
49 return sub.toString() + "{" + fewestMatches + "," + mostMatches + "}"
\r
50 + (matchFewest ? "?" : "") + "(?# <= fast multi)"
\r
56 public int matchInternal(int pos, Pthings pt)
\r
60 int endstr = pt.src.length() - step;
\r
61 patInt matches = new patInt(0);
\r
64 if (fewestMatches.lessEq(matches))
\r
66 int ii = nextMatch(i, pt);
\r
72 while (i >= 0 && i <= endstr)
\r
74 i = sub.matchInternal(i, pt);
\r
78 if (fewestMatches.lessEq(matches))
\r
80 int ii = nextMatch(i, pt);
\r
86 if (matches.equals(mostMatches))
\r
95 while (fewestMatches.intValue() > nMatches)
\r
97 i = sub.matchInternal(i, pt);
\r
108 if (mostMatches.finite())
\r
110 while (nMatches < mostMatches.intValue())
\r
112 i = sub.matchInternal(i, pt);
\r
128 i = sub.matchInternal(i, pt);
\r
142 int r = nextMatch(m, pt);
\r
149 if (nMatches < fewestMatches.intValue())
\r
157 public Pattern clone1(Hashtable h)
\r
161 FastMulti fm = new FastMulti(fewestMatches, mostMatches, sub.clone(h));
\r
162 fm.matchFewest = matchFewest;
\r
164 } catch (RegSyntax rs)
\r