KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > regexp > RETokenChar


1 /*
2  * gnu/regexp/RETokenChar.java
3  * Copyright (C) 1998-2001 Wes Biggs
4  *
5  * This library is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published
7  * by the Free Software Foundation; either version 2.1 of the License, or
8  * (at your option) any later version.
9  *
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,cnext.ch.length);
61       
62       ch = chTemp;
63       return false;
64     } else return super.chain(next);
65   }
66
67   void dump(StringBuffer JavaDoc os) {
68     os.append(ch);
69   }
70 }
71
72
73
Popular Tags