Dead
[official-gcc.git] / gomp-20050608-branch / libjava / classpath / gnu / regexp / RETokenOneOf.java
blob3f6e89e2103ad122adcf56fe528ce899c441ff67
1 /* gnu/regexp/RETokenOneOf.java
2 Copyright (C) 1998-2001, 2004 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
38 package gnu.regexp;
39 import java.util.Vector;
41 final class RETokenOneOf extends REToken {
42 private Vector options;
43 private boolean negative;
45 // This constructor is used for convenience when we know the set beforehand,
46 // e.g. \d --> new RETokenOneOf("0123456789",false, ..)
47 // \D --> new RETokenOneOf("0123456789",true, ..)
49 RETokenOneOf(int subIndex, String optionsStr, boolean negative, boolean insens) {
50 super(subIndex);
51 options = new Vector();
52 this.negative = negative;
53 for (int i = 0; i < optionsStr.length(); i++)
54 options.addElement(new RETokenChar(subIndex,optionsStr.charAt(i),insens));
57 RETokenOneOf(int subIndex, Vector options, boolean negative) {
58 super(subIndex);
59 this.options = options;
60 this.negative = negative;
63 int getMinimumLength() {
64 int min = Integer.MAX_VALUE;
65 int x;
66 for (int i=0; i < options.size(); i++) {
67 if ((x = ((REToken) options.elementAt(i)).getMinimumLength()) < min)
68 min = x;
70 return min;
73 boolean match(CharIndexed input, REMatch mymatch) {
74 if (negative && (input.charAt(mymatch.index) == CharIndexed.OUT_OF_BOUNDS))
75 return false;
77 REMatch newMatch = null;
78 REMatch last = null;
79 REToken tk;
80 boolean isMatch;
81 for (int i=0; i < options.size(); i++) {
82 tk = (REToken) options.elementAt(i);
83 REMatch tryMatch = (REMatch) mymatch.clone();
84 if (tk.match(input, tryMatch)) { // match was successful
85 if (negative) return false;
87 if (next(input, tryMatch)) {
88 // Add tryMatch to list of possibilities.
89 if (last == null) {
90 newMatch = tryMatch;
91 last = tryMatch;
92 } else {
93 last.next = tryMatch;
94 last = tryMatch;
96 } // next succeeds
97 } // is a match
98 } // try next option
100 if (newMatch != null) {
101 if (negative) {
102 return false;
103 } else {
104 // set contents of mymatch equal to newMatch
106 // try each one that matched
107 mymatch.assignFrom(newMatch);
108 return true;
110 } else {
111 if (negative) {
112 ++mymatch.index;
113 return next(input, mymatch);
114 } else {
115 return false;
119 // index+1 works for [^abc] lists, not for generic lookahead (--> index)
122 void dump(StringBuffer os) {
123 os.append(negative ? "[^" : "(?:");
124 for (int i = 0; i < options.size(); i++) {
125 if (!negative && (i > 0)) os.append('|');
126 ((REToken) options.elementAt(i)).dumpAll(os);
128 os.append(negative ? ']' : ')');