OSDN Git Service

PR c++/28559
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / regexp / RETokenPOSIX.java
1 /* gnu/regexp/RETokenPOSIX.java
2    Copyright (C) 2006 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
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
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
80     int getMinimumLength() {
81         return 1;
82     }
83
84     int getMaximumLength() {
85         return 1;
86     }
87
88     REMatch matchThis(CharIndexed input, REMatch mymatch) {
89       char ch = input.charAt(mymatch.index);
90       boolean retval = matchOneChar(ch);
91       if (retval) {
92         ++mymatch.index;
93         return mymatch;
94       }
95       return null;
96     }
97
98     boolean matchOneChar(char ch) {
99     if (ch == CharIndexed.OUT_OF_BOUNDS)
100       return false;
101     
102     boolean retval = false;
103     switch (type) {
104     case ALNUM:
105         // Note that there is some debate over whether '_' should be included
106         retval = Character.isLetterOrDigit(ch) || (ch == '_');
107         break;
108     case ALPHA:
109         retval = Character.isLetter(ch);
110         break;
111     case BLANK:
112         retval = ((ch == ' ') || (ch == '\t'));
113         break;
114     case CNTRL:
115         retval = Character.isISOControl(ch);
116         break;
117     case DIGIT:
118         retval = Character.isDigit(ch);
119         break;
120     case GRAPH:
121         retval = (!(Character.isWhitespace(ch) || Character.isISOControl(ch)));
122         break;
123     case LOWER:
124         retval = ((insens && Character.isLetter(ch)) || Character.isLowerCase(ch));
125         break;
126     case PRINT:
127         retval = (!(Character.isWhitespace(ch) || Character.isISOControl(ch)))
128             || (ch == ' ');
129         break;
130     case PUNCT:
131         // This feels sloppy, especially for non-U.S. locales.
132         retval = ("`~!@#$%^&*()-_=+[]{}\\|;:'\"/?,.<>".indexOf(ch)!=-1);
133         break;
134     case SPACE:
135         retval = Character.isWhitespace(ch);
136         break;
137     case UPPER:
138         retval = ((insens && Character.isLetter(ch)) || Character.isUpperCase(ch));
139         break;
140     case XDIGIT:
141         retval = (Character.isDigit(ch) || ("abcdefABCDEF".indexOf(ch)!=-1));
142         break;
143     }
144
145     if (negated) retval = !retval;
146     return retval;
147   }
148
149   boolean returnsFixedLengthMatches() { return true; }
150
151   int findFixedLengthMatches(CharIndexed input, REMatch mymatch, int max) {
152       int index = mymatch.index;
153       int numRepeats = 0;
154       while (true) {
155         if (numRepeats >= max) break;
156         char ch = input.charAt(index++);
157         if (! matchOneChar(ch)) break;
158         numRepeats++;
159       }
160       return numRepeats;
161   }
162
163   void dump(StringBuffer os) {
164     if (negated) os.append('^');
165     os.append("[:" + s_nameTable[type] + ":]");
166   }
167 }