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