1 19 20 package gnu.regexp; 21 import java.util.Vector ; 22 23 final class RETokenRepeated extends REToken { 24 private REToken token; 25 private int min,max; 26 private boolean stingy; 27 28 RETokenRepeated(int subIndex, REToken token, int min, int max) { 29 super(subIndex); 30 this.token = token; 31 this.min = min; 32 this.max = max; 33 } 34 35 36 void makeStingy() { 37 stingy = true; 38 } 39 40 41 boolean isStingy() { 42 return stingy; 43 } 44 45 50 int getMinimumLength() { 51 return (min * token.getMinimumLength()); 52 } 53 54 58 62 boolean match(CharIndexed input, REMatch mymatch) { 63 int numRepeats = 0; 65 66 REMatch newMatch = mymatch; 68 REMatch last = null; 69 REMatch current; 70 71 Vector positions = new Vector (); 74 positions.addElement(newMatch); 75 76 REMatch doables; 78 REMatch doablesLast; 79 REMatch recurrent; 80 81 do { 82 if (stingy && (numRepeats >= min)) { 84 REMatch result = matchRest(input, newMatch); 85 if (result != null) { 86 mymatch.assignFrom(result); 87 return true; 88 } 89 } 90 91 doables = null; 92 doablesLast = null; 93 94 for (current = newMatch; current != null; current = current.next) { 96 recurrent = (REMatch) current.clone(); 97 if (token.match(input, recurrent)) { 98 if (doables == null) { 100 doables = recurrent; 101 doablesLast = recurrent; 102 } else { 103 doablesLast.next = recurrent; 106 } 107 while (doablesLast.next != null) { 109 doablesLast = doablesLast.next; 110 } 111 } 112 } 113 if (doables == null) break; 115 116 newMatch = doables; 118 119 ++numRepeats; 121 122 positions.addElement(newMatch); 123 } while (numRepeats < max); 124 125 if (numRepeats < min) return false; 127 128 int posIndex = positions.size(); 130 131 REMatch allResults = null; 134 REMatch allResultsLast = null; 135 136 REMatch results = null; 137 while (--posIndex >= min) { 138 newMatch = (REMatch) positions.elementAt(posIndex); 139 results = matchRest(input, newMatch); 140 if (results != null) { 141 if (allResults == null) { 142 allResults = results; 143 allResultsLast = results; 144 } else { 145 allResultsLast.next = results; 148 } 149 while (allResultsLast.next != null) { 151 allResultsLast = allResultsLast.next; 152 } 153 } 154 } 156 if (allResults != null) { 157 mymatch.assignFrom(allResults); return true; 159 } 160 return false; 162 } 163 164 private REMatch matchRest(CharIndexed input, final REMatch newMatch) { 165 REMatch current, single; 166 REMatch doneIndex = null; 167 REMatch doneIndexLast = null; 168 for (current = newMatch; current != null; current = current.next) { 170 single = (REMatch) current.clone(); 172 if (next(input, single)) { 173 if (doneIndex == null) { 175 doneIndex = single; 176 doneIndexLast = single; 177 } else { 178 doneIndexLast.next = single; 179 } 180 while (doneIndexLast.next != null) { 182 doneIndexLast = doneIndexLast.next; 183 } 184 } 185 } 186 return doneIndex; 187 } 188 189 void dump(StringBuffer os) { 190 os.append("(?:"); 191 token.dumpAll(os); 192 os.append(')'); 193 if ((max == Integer.MAX_VALUE) && (min <= 1)) 194 os.append( (min == 0) ? '*' : '+' ); 195 else if ((min == 0) && (max == 1)) 196 os.append('?'); 197 else { 198 os.append('{').append(min); 199 if (max > min) { 200 os.append(','); 201 if (max != Integer.MAX_VALUE) os.append(max); 202 } 203 os.append('}'); 204 } 205 if (stingy) os.append('?'); 206 } 207 } 208 | Popular Tags |