1 /* gnu/regexp/RETokenPOSIX.java
2    Copyright (C) 1998-2001, 2004 Free Software Foundation, Inc.
3 
4 This file is part of GNU Classpath.
5 
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.
10 
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.
15 
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.
20 
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.
25 
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. */
37 
38 
39 package gnu.regexp;
40 
41 final class RETokenPOSIX extends REToken {
42   int type;
43   boolean insens;
44   boolean negated;
45 
46   static final int  ALNUM = 0;
47   static final int  ALPHA = 1;
48   static final int  BLANK = 2;
49   static final int  CNTRL = 3;
50   static final int  DIGIT = 4;
51   static final int  GRAPH = 5;
52   static final int  LOWER = 6;
53   static final int  PRINT = 7;
54   static final int  PUNCT = 8;
55   static final int  SPACE = 9;
56   static final int  UPPER = 10;
57   static final int XDIGIT = 11;
58 
59   // Array indices correspond to constants defined above.
60   static final String[] s_nameTable =  {
61     "alnum", "alpha", "blank", "cntrl", "digit", "graph", "lower",
62     "print", "punct", "space", "upper", "xdigit"
63   };
64 
65   // The RE constructor uses this to look up the constant for a string
intValue(String key)66   static int intValue(String key) {
67     for (int i = 0; i < s_nameTable.length; i++) {
68       if (s_nameTable[i].equals(key)) return i;
69     }
70     return -1;
71   }
72 
RETokenPOSIX(int subIndex, int type, boolean insens, boolean negated)73   RETokenPOSIX(int subIndex, int type, boolean insens, boolean negated) {
74     super(subIndex);
75     this.type = type;
76     this.insens = insens;
77     this.negated = negated;
78   }
79 
getMinimumLength()80     int getMinimumLength() {
81 	return 1;
82     }
83 
match(CharIndexed input, REMatch mymatch)84     boolean match(CharIndexed input, REMatch mymatch) {
85     char ch = input.charAt(mymatch.index);
86     if (ch == CharIndexed.OUT_OF_BOUNDS)
87       return false;
88 
89     boolean retval = false;
90     switch (type) {
91     case ALNUM:
92 	// Note that there is some debate over whether '_' should be included
93 	retval = Character.isLetterOrDigit(ch) || (ch == '_');
94 	break;
95     case ALPHA:
96 	retval = Character.isLetter(ch);
97 	break;
98     case BLANK:
99 	retval = ((ch == ' ') || (ch == '\t'));
100 	break;
101     case CNTRL:
102 	retval = Character.isISOControl(ch);
103 	break;
104     case DIGIT:
105 	retval = Character.isDigit(ch);
106 	break;
107     case GRAPH:
108 	retval = (!(Character.isWhitespace(ch) || Character.isISOControl(ch)));
109 	break;
110     case LOWER:
111 	retval = ((insens && Character.isLetter(ch)) || Character.isLowerCase(ch));
112 	break;
113     case PRINT:
114 	retval = (!(Character.isWhitespace(ch) || Character.isISOControl(ch)))
115 	    || (ch == ' ');
116 	break;
117     case PUNCT:
118 	// This feels sloppy, especially for non-U.S. locales.
119 	retval = ("`~!@#$%^&*()-_=+[]{}\\|;:'\"/?,.<>".indexOf(ch)!=-1);
120 	break;
121     case SPACE:
122 	retval = Character.isWhitespace(ch);
123 	break;
124     case UPPER:
125 	retval = ((insens && Character.isLetter(ch)) || Character.isUpperCase(ch));
126 	break;
127     case XDIGIT:
128 	retval = (Character.isDigit(ch) || ("abcdefABCDEF".indexOf(ch)!=-1));
129 	break;
130     }
131 
132     if (negated) retval = !retval;
133     if (retval) {
134 	++mymatch.index;
135 	return next(input, mymatch);
136     }
137     else return false;
138   }
139 
dump(StringBuffer os)140   void dump(StringBuffer os) {
141     if (negated) os.append('^');
142     os.append("[:" + s_nameTable[type] + ":]");
143   }
144 }
145