1 /*******************************************************************************
2 * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3 * Copyright (C) $(date) The Jalview Authors
5 * This file is part of Jalview.
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.
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.
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 *******************************************************************************/
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
26 // -- Happy Computing!
28 package com.stevesoft.pat;
30 import java.util.Hashtable;
33 * A special case of Multi, implemented when minChars().equals(maxChars()), and
34 * some other conditions spelled out in RegOpt.safe4fm "Safe for FastMulti." It
35 * avoids stack growth problems as well as being slightly faster.
37 class FastMulti extends PatternSub
39 patInt fewestMatches, mostMatches;
41 public patInt minChars()
43 return sub.countMinChars().mul(fewestMatches);
46 public patInt maxChars()
48 return sub.countMaxChars().mul(mostMatches);
51 public boolean matchFewest = false;
53 FastMulti(patInt a, patInt b, Pattern p) throws RegSyntax
57 RegSyntaxError.endItAll("Null length pattern "
58 + "followed by *, +, or other Multi.");
63 step = p.countMinChars().intValue();
67 public String toString()
69 return sub.toString() + "{" + fewestMatches + "," + mostMatches + "}"
70 + (matchFewest ? "?" : "") + "(?# <= fast multi)"
76 public int matchInternal(int pos, Pthings pt)
80 int endstr = pt.src.length() - step;
81 patInt matches = new patInt(0);
84 if (fewestMatches.lessEq(matches))
86 int ii = nextMatch(i, pt);
92 while (i >= 0 && i <= endstr)
94 i = sub.matchInternal(i, pt);
98 if (fewestMatches.lessEq(matches))
100 int ii = nextMatch(i, pt);
106 if (matches.equals(mostMatches))
115 while (fewestMatches.intValue() > nMatches)
117 i = sub.matchInternal(i, pt);
128 if (mostMatches.finite())
130 while (nMatches < mostMatches.intValue())
132 i = sub.matchInternal(i, pt);
148 i = sub.matchInternal(i, pt);
162 int r = nextMatch(m, pt);
169 if (nMatches < fewestMatches.intValue())
177 public Pattern clone1(Hashtable h)
181 FastMulti fm = new FastMulti(fewestMatches, mostMatches, sub.clone(h));
182 fm.matchFewest = matchFewest;
184 } catch (RegSyntax rs)