PR c++/28559
[official-gcc.git] / libjava / classpath / gnu / regexp / REMatchEnumeration.java
blobf164a69af4f467e142c042d3182a5d19a79cc08c
1 /* gnu/regexp/REMatchEnumeration.java
2 Copyright (C) 1998-2001, 2004 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 gnu.regexp;
39 import java.io.Serializable;
40 import java.util.Enumeration;
41 import java.util.NoSuchElementException;
43 /**
44 * An REMatchEnumeration enumerates regular expression matches over a
45 * given input text. You obtain a reference to an enumeration using
46 * the <code>getMatchEnumeration()</code> methods on an instance of
47 * RE.
49 * <P>
51 * REMatchEnumeration does lazy computation; that is, it will not
52 * search for a match until it needs to. If you'd rather just get all
53 * the matches at once in a big array, use the
54 * <code>getAllMatches()</code> methods on RE. However, using an
55 * enumeration can help speed performance when the entire text does
56 * not need to be searched immediately.
58 * <P>
60 * The enumerated type is especially useful when searching on a Reader
61 * or InputStream, because the InputStream read position cannot be
62 * guaranteed after calling <code>getMatch()</code> (see the
63 * description of that method for an explanation of why). Enumeration
64 * also saves a lot of overhead required when calling
65 * <code>getMatch()</code> multiple times.
67 * @author <A HREF="mailto:wes@cacas.org">Wes Biggs</A>
69 public class REMatchEnumeration implements Enumeration, Serializable {
70 private static final int YES = 1;
71 private static final int MAYBE = 0;
72 private static final int NO = -1;
74 private int more;
75 private REMatch match;
76 private RE expr;
77 private CharIndexed input;
78 private int eflags;
79 private int index;
81 // Package scope constructor is used by RE.getMatchEnumeration()
82 REMatchEnumeration(RE expr, CharIndexed input, int index, int eflags) {
83 more = MAYBE;
84 this.expr = expr;
85 this.input = input;
86 this.index = index;
87 this.eflags = eflags;
90 /** Returns true if there are more matches in the input text. */
91 public boolean hasMoreElements() {
92 return hasMoreMatches(null);
95 /** Returns true if there are more matches in the input text. */
96 public boolean hasMoreMatches() {
97 return hasMoreMatches(null);
100 /** Returns true if there are more matches in the input text.
101 * Saves the text leading up to the match (or to the end of the input)
102 * in the specified buffer.
104 public boolean hasMoreMatches(StringBuffer buffer) {
105 if (more == MAYBE) {
106 match = expr.getMatchImpl(input,index,eflags,buffer);
107 if (match != null) {
108 input.move((match.end[0] > 0) ? match.end[0] : 1);
110 index = (match.end[0] > 0) ? match.end[0] + match.offset : index + 1;
111 more = YES;
112 } else more = NO;
114 return (more == YES);
117 /** Returns the next match in the input text. */
118 public Object nextElement() throws NoSuchElementException {
119 return nextMatch();
122 /**
123 * Returns the next match in the input text. This method is provided
124 * for convenience to avoid having to explicitly cast the return value
125 * to class REMatch.
127 public REMatch nextMatch() throws NoSuchElementException {
128 if (hasMoreElements()) {
129 more = (input.isValid()) ? MAYBE : NO;
130 return match;
132 throw new NoSuchElementException();