01: /*
02: * gnu/regexp/RETokenChar.java
03: * Copyright (C) 1998-2001 Wes Biggs
04: *
05: * This library is free software; you can redistribute it and/or modify
06: * it under the terms of the GNU Lesser General Public License as published
07: * by the Free Software Foundation; either version 2.1 of the License, or
08: * (at your option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: * GNU Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public License
16: * along with this program; if not, write to the Free Software
17: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18: */
19:
20: package gnu.regexp;
21:
22: final class RETokenChar extends REToken {
23: private char[] ch;
24: private boolean insens;
25:
26: RETokenChar(int subIndex, char c, boolean ins) {
27: super (subIndex);
28: ch = new char[1];
29: ch[0] = (insens = ins) ? Character.toLowerCase(c) : c;
30: }
31:
32: int getMinimumLength() {
33: return ch.length;
34: }
35:
36: boolean match(CharIndexed input, REMatch mymatch) {
37: int z = ch.length;
38: char c;
39: for (int i = 0; i < z; i++) {
40: c = input.charAt(mymatch.index + i);
41: if (((insens) ? Character.toLowerCase(c) : c) != ch[i]) {
42: return false;
43: }
44: }
45: mymatch.index += z;
46:
47: return next(input, mymatch);
48: }
49:
50: // Overrides REToken.chain() to optimize for strings
51: boolean chain(REToken next) {
52: if (next instanceof RETokenChar) {
53: RETokenChar cnext = (RETokenChar) next;
54: // assume for now that next can only be one character
55: int newsize = ch.length + cnext.ch.length;
56:
57: char[] chTemp = new char[newsize];
58:
59: System.arraycopy(ch, 0, chTemp, 0, ch.length);
60: System.arraycopy(cnext.ch, 0, chTemp, ch.length,
61: cnext.ch.length);
62:
63: ch = chTemp;
64: return false;
65: } else
66: return super .chain(next);
67: }
68:
69: void dump(StringBuffer os) {
70: os.append(ch);
71: }
72: }
|