jdk/src/share/classes/sun/misc/RegexpPool.java
author chegar
Mon, 08 Apr 2013 06:15:18 +0100
changeset 18223 35a5c2462991
parent 5506 202f599c92aa
permissions -rw-r--r--
8008593: Better URLClassLoader resource management Reviewed-by: alanb, sherman, hawtin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1995, 2001, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.misc;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * A class to represent a pool of regular expressions.  A string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * can be matched against the whole pool all at once.  It is much
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * faster than doing individual regular expression matches one-by-one.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * @see java.misc.RegexpTarget
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author  James Gosling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class RegexpPool {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private RegexpNode prefixMachine = new RegexpNode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private RegexpNode suffixMachine = new RegexpNode();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static final int BIG = 0x7FFFFFFF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private int lastDepth = BIG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public RegexpPool () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
     * Add a regular expression to the pool of regular expressions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * @param   re  The regular expression to add to the pool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            For now, only handles strings that either begin or end with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            a '*'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * @param   ret The object to be returned when this regular expression is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            matched.  If ret is an instance of the RegexpTarget class, ret.found
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            is called with the string fragment that matched the '*' as its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @exception REException error
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public void add(String re, Object ret) throws REException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        add(re, ret, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     * Replace the target for the regular expression with a different
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * @param   re  The regular expression to be replaced in the pool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     *      For now, only handles strings that either begin or end with
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     *      a '*'.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * @param   ret The object to be returned when this regular expression is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *      matched.  If ret is an instance of the RegexpTarget class, ret.found
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *      is called with the string fragment that matched the '*' as its
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *      parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public void replace(String re, Object ret) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            add(re, ret, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        } catch(Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            // should never occur if replace is true
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * Delete the regular expression and its target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param re The regular expression to be deleted from the pool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     *           must begin or end with a '*'
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * @return target - the old target.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public Object delete(String re) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        Object o = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        RegexpNode p = prefixMachine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        RegexpNode best = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        int len = re.length() - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        boolean prefix = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        if (!re.startsWith("*") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            !re.endsWith("*"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            len++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if (len <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        /* March forward through the prefix machine */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        for (i = 0; p != null; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            if (p.result != null && p.depth < BIG
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
                && (!p.exact || i == len)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                best = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (i >= len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            p = p.find(re.charAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        /* march backward through the suffix machine */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        p = suffixMachine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        for (i = len; --i >= 0 && p != null;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (p.result != null && p.depth < BIG) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                prefix = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                best = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            p = p.find(re.charAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // delete only if there is an exact match
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        if (prefix) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (re.equals(best.re)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                o = best.result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                best.result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            if (re.equals(best.re)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                o = best.result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                best.result = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /** Search for a match to a string & return the object associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        with it with the match.  When multiple regular expressions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        would match the string, the best match is returned first.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        The next best match is returned the next time matchNext is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        @param s    The string to match against the regular expressions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    in the pool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        @return     null on failure, otherwise the object associated with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                    the regular expression when it was added to the pool.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                    If the object is an instance of RegexpTarget, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                    the return value is the result from calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    return.found(string_that_matched_wildcard).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    public Object match(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        return matchAfter(s, BIG);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /** Identical to match except that it will only find matches to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        regular expressions that were added to the pool <i>after</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        the last regular expression that matched in the last call
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        to match() or matchNext() */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    public Object matchNext(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return matchAfter(s, lastDepth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    private void add(String re, Object ret, boolean replace) throws REException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        int len = re.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        RegexpNode p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        if (re.charAt(0) == '*') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            p = suffixMachine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            while (len > 1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                p = p.add(re.charAt(--len));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
            boolean exact = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            if (re.charAt(len - 1) == '*')
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                len--;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                exact = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
            p = prefixMachine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            for (int i = 0; i < len; i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                p = p.add(re.charAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
            p.exact = exact;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        if (p.result != null && !replace)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            throw new REException(re + " is a duplicate");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        p.re = re;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        p.result = ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    private Object matchAfter(String s, int lastMatchDepth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        RegexpNode p = prefixMachine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        RegexpNode best = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        int bst = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        int bend = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        int len = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        if (len <= 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        /* March forward through the prefix machine */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        for (i = 0; p != null; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            if (p.result != null && p.depth < lastMatchDepth
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                && (!p.exact || i == len)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                lastDepth = p.depth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                best = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                bst = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                bend = len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            if (i >= len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            p = p.find(s.charAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        /* march backward through the suffix machine */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        p = suffixMachine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        for (i = len; --i >= 0 && p != null;) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            if (p.result != null && p.depth < lastMatchDepth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                lastDepth = p.depth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
                best = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                bst = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
                bend = i+1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            p = p.find(s.charAt(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        Object o = best.result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        if (o != null && o instanceof RegexpTarget)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            o = ((RegexpTarget) o).found(s.substring(bst, bend));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        return o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    /** Resets the pool so that the next call to matchNext looks
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        at all regular expressions in the pool.  match(s); is equivalent
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        to reset(); matchNext(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        <p><b>Multithreading note:</b> reset/nextMatch leave state in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        regular expression pool.  If multiple threads could be using this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        pool this way, they should be syncronized to avoid race hazards.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        match() was done in such a way that there are no such race
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        hazards: multiple threads can be matching in the same pool
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        simultaneously. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    public void reset() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        lastDepth = BIG;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    /** Print this pool to standard output */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public void print(PrintStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        out.print("Regexp pool:\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        if (suffixMachine.firstchild != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            out.print(" Suffix machine: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            suffixMachine.firstchild.print(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
            out.print("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        if (prefixMachine.firstchild != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            out.print(" Prefix machine: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            prefixMachine.firstchild.print(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
            out.print("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
/* A node in a regular expression finite state machine. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
class RegexpNode {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    char c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    RegexpNode firstchild;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
    RegexpNode nextsibling;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    int depth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    boolean exact;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    Object result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    String re = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    RegexpNode () {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        c = '#';
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        depth = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
    RegexpNode (char C, int depth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        c = C;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        this.depth = depth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    RegexpNode add(char C) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        RegexpNode p = firstchild;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        if (p == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
            p = new RegexpNode (C, depth+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
            while (p != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                if (p.c == C)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
                    p = p.nextsibling;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            p = new RegexpNode (C, depth+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            p.nextsibling = firstchild;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        firstchild = p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    RegexpNode find(char C) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        for (RegexpNode p = firstchild;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                p != null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                p = p.nextsibling)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            if (p.c == C)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                return p;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    void print(PrintStream out) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        if (nextsibling != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            RegexpNode p = this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            out.print("(");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
            while (p != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                out.write(p.c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                if (p.firstchild != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    p.firstchild.print(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                p = p.nextsibling;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                out.write(p != null ? '|' : ')');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            out.write(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            if (firstchild != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                firstchild.print(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
}