OSDN Git Service

libjava/ChangeLog:
[pf3gnuchains/gcc-fork.git] / libjava / classpath / gnu / java / util / regex / 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.java.util.regex;
40
41 import gnu.java.lang.CPStringBuilder;
42
43 final class RETokenPOSIX extends REToken
44 {
45   int type;
46   boolean insens;
47   boolean negated;
48
49   static final int ALNUM = 0;
50   static final int ALPHA = 1;
51   static final int BLANK = 2;
52   static final int CNTRL = 3;
53   static final int DIGIT = 4;
54   static final int GRAPH = 5;
55   static final int LOWER = 6;
56   static final int PRINT = 7;
57   static final int PUNCT = 8;
58   static final int SPACE = 9;
59   static final int UPPER = 10;
60   static final int XDIGIT = 11;
61
62   // Array indices correspond to constants defined above.
63   static final String[] s_nameTable = {
64     "alnum", "alpha", "blank", "cntrl", "digit", "graph", "lower",
65     "print", "punct", "space", "upper", "xdigit"
66   };
67
68   // The RE constructor uses this to look up the constant for a string
69   static int intValue (String key)
70   {
71     for (int i = 0; i < s_nameTable.length; i++)
72       {
73         if (s_nameTable[i].equals (key))
74           return i;
75       }
76     return -1;
77   }
78
79   RETokenPOSIX (int subIndex, int type, boolean insens, boolean negated)
80   {
81     super (subIndex);
82     this.type = type;
83     this.insens = insens;
84     this.negated = negated;
85   }
86
87   int getMinimumLength ()
88   {
89     return 1;
90   }
91
92   int getMaximumLength ()
93   {
94     return 1;
95   }
96
97   REMatch matchThis (CharIndexed input, REMatch mymatch)
98   {
99     char ch = input.charAt (mymatch.index);
100     boolean retval = matchOneChar (ch);
101     if (retval)
102       {
103         ++mymatch.index;
104         return mymatch;
105       }
106     return null;
107   }
108
109   boolean matchOneChar (char ch)
110   {
111     if (ch == CharIndexed.OUT_OF_BOUNDS)
112       return false;
113
114     boolean retval = false;
115     switch (type)
116       {
117       case ALNUM:
118         // Note that there is some debate over whether '_' should be included
119         retval = Character.isLetterOrDigit (ch) || (ch == '_');
120         break;
121       case ALPHA:
122         retval = Character.isLetter (ch);
123         break;
124       case BLANK:
125         retval = ((ch == ' ') || (ch == '\t'));
126         break;
127       case CNTRL:
128         retval = Character.isISOControl (ch);
129         break;
130       case DIGIT:
131         retval = Character.isDigit (ch);
132         break;
133       case GRAPH:
134         retval =
135           (!(Character.isWhitespace (ch) || Character.isISOControl (ch)));
136         break;
137       case LOWER:
138         retval = ((insens && Character.isLetter (ch))
139                   || Character.isLowerCase (ch));
140         break;
141       case PRINT:
142         retval =
143           (!(Character.isWhitespace (ch) || Character.isISOControl (ch)))
144           || (ch == ' ');
145         break;
146       case PUNCT:
147         // This feels sloppy, especially for non-U.S. locales.
148         retval = ("`~!@#$%^&*()-_=+[]{}\\|;:'\"/?,.<>".indexOf (ch) != -1);
149         break;
150       case SPACE:
151         retval = Character.isWhitespace (ch);
152         break;
153       case UPPER:
154         retval = ((insens && Character.isLetter (ch))
155                   || Character.isUpperCase (ch));
156         break;
157       case XDIGIT:
158         retval = (Character.isDigit (ch)
159                   || ("abcdefABCDEF".indexOf (ch) != -1));
160         break;
161       }
162
163     if (negated)
164       retval = !retval;
165     return retval;
166   }
167
168   boolean returnsFixedLengthMatches ()
169   {
170     return true;
171   }
172
173   int findFixedLengthMatches (CharIndexed input, REMatch mymatch, int max)
174   {
175     int index = mymatch.index;
176     int numRepeats = 0;
177     while (true)
178       {
179         if (numRepeats >= max)
180           break;
181         char ch = input.charAt (index++);
182         if (!matchOneChar (ch))
183           break;
184         numRepeats++;
185       }
186     return numRepeats;
187   }
188
189   void dump (CPStringBuilder os)
190   {
191     if (negated)
192       os.append ('^');
193     os.append ("[:" + s_nameTable[type] + ":]");
194   }
195 }