Imported GNU Classpath 0.20
[official-gcc.git] / libjava / classpath / java / util / regex / Pattern.java
blobd39f1cfb04de985f179bda6872449c53c2fddc09
1 /* Pattern.java -- Compiled regular expression ready to be applied.
2 Copyright (C) 2002, 2004, 2005 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
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.
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.
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.
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.
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. */
38 package java.util.regex;
40 import gnu.regexp.RE;
41 import gnu.regexp.REException;
42 import gnu.regexp.RESyntax;
44 import java.io.Serializable;
45 import java.util.ArrayList;
48 /**
49 * Compiled regular expression ready to be applied.
51 * @since 1.4
53 public final class Pattern implements Serializable
55 private static final long serialVersionUID = 5073258162644648461L;
57 public static final int CANON_EQ = 128;
58 public static final int CASE_INSENSITIVE = 2;
59 public static final int COMMENTS = 4;
60 public static final int DOTALL = 32;
61 public static final int MULTILINE = 8;
62 public static final int UNICODE_CASE = 64;
63 public static final int UNIX_LINES = 1;
65 private final String regex;
66 private final int flags;
68 private final RE re;
70 private Pattern (String regex, int flags)
71 throws PatternSyntaxException
73 this.regex = regex;
74 this.flags = flags;
76 int gnuFlags = 0;
77 if ((flags & CASE_INSENSITIVE) != 0)
78 gnuFlags |= RE.REG_ICASE;
79 if ((flags & MULTILINE) != 0)
80 gnuFlags |= RE.REG_MULTILINE;
81 if ((flags & DOTALL) != 0)
82 gnuFlags |= RE.REG_DOT_NEWLINE;
83 // not yet supported:
84 // if ((flags & UNICODE_CASE) != 0) gnuFlags =
85 // if ((flags & CANON_EQ) != 0) gnuFlags =
87 RESyntax syntax = RESyntax.RE_SYNTAX_JAVA_1_4;
88 if ((flags & UNIX_LINES) != 0)
90 // Use a syntax set with \n for linefeeds?
91 syntax = new RESyntax(syntax);
92 syntax.setLineSeparator("\n");
95 if ((flags & COMMENTS) != 0)
97 // Use a syntax with support for comments?
102 this.re = new RE(regex, gnuFlags, syntax);
104 catch (REException e)
106 PatternSyntaxException pse;
107 pse = new PatternSyntaxException(e.getMessage(),
108 regex, e.getPosition());
109 pse.initCause(e);
110 throw pse;
114 // package private accessor method
115 RE getRE()
117 return re;
121 * @param regex The regular expression
123 * @exception PatternSyntaxException If the expression's syntax is invalid
125 public static Pattern compile (String regex)
126 throws PatternSyntaxException
128 return compile(regex, 0);
132 * @param regex The regular expression
133 * @param flags The match flags, a bit mask
135 * @exception PatternSyntaxException If the expression's syntax is invalid
136 * @exception IllegalArgumentException If bit values other than those
137 * corresponding to the defined match flags are set in flags
139 public static Pattern compile (String regex, int flags)
140 throws PatternSyntaxException
142 // FIXME: check which flags are really accepted
143 if ((flags & ~0xEF) != 0)
144 throw new IllegalArgumentException ();
146 return new Pattern (regex, flags);
149 public int flags ()
151 return this.flags;
155 * @param regex The regular expression
156 * @param input The character sequence to be matched
158 * @exception PatternSyntaxException If the expression's syntax is invalid
160 public static boolean matches (String regex, CharSequence input)
162 return compile(regex).matcher(input).matches();
166 * @param input The character sequence to be matched
168 public Matcher matcher (CharSequence input)
170 return new Matcher(this, input);
174 * @param input The character sequence to be matched
176 public String[] split (CharSequence input)
178 return split(input, 0);
182 * @param input The character sequence to be matched
183 * @param limit The result threshold
185 public String[] split (CharSequence input, int limit)
187 Matcher matcher = new Matcher(this, input);
188 ArrayList list = new ArrayList();
189 int empties = 0;
190 int count = 0;
191 int start = 0;
192 int end;
193 boolean matched = matcher.find();
195 while (matched && (limit <= 0 || count < limit - 1))
197 ++count;
198 end = matcher.start();
199 if (start == end)
200 empties++;
201 else
203 while (empties > 0)
205 list.add("");
206 empties--;
209 String text = input.subSequence(start, end).toString();
210 list.add(text);
212 start = matcher.end();
213 matched = matcher.find();
216 // We matched nothing.
217 if (!matched && count == 0)
218 return new String[] { input.toString() };
220 // Is the last token empty?
221 boolean emptyLast = (start == input.length());
223 // Can/Must we add empties or an extra last token at the end?
224 if (list.size() < limit || limit < 0 || (limit == 0 && !emptyLast))
226 if (limit > list.size())
228 int max = limit - list.size();
229 empties = (empties > max) ? max : empties;
231 while (empties > 0)
233 list.add("");
234 empties--;
238 // last token at end
239 if (limit != 0 || (limit == 0 && !emptyLast))
241 String t = input.subSequence(start, input.length()).toString();
242 if ("".equals(t) && limit == 0)
243 ; // Don't add.
244 else
245 list.add(t);
248 String[] output = new String [list.size()];
249 list.toArray(output);
250 return output;
253 public String pattern ()
255 return regex;