01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: /**
19: * @author Nikolay A. Kuznetsov
20: * @version $Revision: 1.8.2.2 $
21: */package java.util.regex;
22:
23: /**
24: * Composite (i.e. {n,m}) quantifier node over the leaf nodes ("a{n,m}")
25: *
26: * @author Nikolay A. Kuznetsov
27: * @version $Revision: 1.8.2.2 $
28: */
29: class CompositeQuantifierSet extends LeafQuantifierSet {
30:
31: protected Quantifier quantifier = null;
32:
33: public CompositeQuantifierSet(Quantifier quant, LeafSet innerSet,
34: AbstractSet next, int type) {
35: super (innerSet, next, type);
36: this .quantifier = quant;
37: }
38:
39: public int matches(int stringIndex, CharSequence testString,
40: MatchResultImpl matchResult) {
41: int min = quantifier.min();
42: int max = quantifier.max();
43: int i = 0;
44:
45: for (; i < min; i++) {
46:
47: if (stringIndex + leaf.charCount() > matchResult
48: .getRightBound()) {
49: matchResult.hitEnd = true;
50: return -1;
51: }
52:
53: int shift = leaf.accepts(stringIndex, testString);
54: if (shift < 1) {
55: return -1;
56: }
57: stringIndex += shift;
58: }
59:
60: for (; i < max; i++) {
61: int shift;
62: if (stringIndex + leaf.charCount() > matchResult
63: .getRightBound()
64: || (shift = leaf.accepts(stringIndex, testString)) < 1) {
65: break;
66: }
67: stringIndex += shift;
68: }
69:
70: for (; i >= min; i--) {
71: int shift = next.matches(stringIndex, testString,
72: matchResult);
73: if (shift >= 0) {
74: return shift;
75: }
76: stringIndex -= leaf.charCount();
77: }
78: return -1;
79:
80: }
81:
82: public void reset() {
83: quantifier.resetCounter();
84: }
85:
86: protected String getName() {
87: return quantifier.toString();
88: }
89:
90: void setQuantifier(Quantifier quant) {
91: this.quantifier = quant;
92: }
93: }
|