jdk/src/share/classes/sun/misc/Regexp.java
author alanb
Tue, 08 Feb 2011 19:31:44 +0000
changeset 8189 5fd29d2cbc4b
parent 5506 202f599c92aa
permissions -rw-r--r--
4421494: infinite loop while parsing double literal Reviewed-by: darcy, alanb Contributed-by: dmitry.nadezhin@oracle.com
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, 2004, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * A class to represent a regular expression.  Only handles '*'s.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @author  James Gosling
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
public class Regexp {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    /** if true then the matching process ignores case. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    public boolean ignoreCase;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
     * regular expressions are carved into three regions: a constant string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
     * prefix, a constant string suffix, and a series of floating strings in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
     * between.  In the input regular expression, they are separated by *s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    public String exp;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    public String prefix, suffix;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    public boolean exact;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    public int prefixLen, suffixLen, totalLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    public String mids[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /** Create a new regular expression object.  The regular expression
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        is a series of constant strings separated by *s.  For example:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        <dt>*.gif       <dd>Matches any string that ends in ".gif".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        <dt>/tmp/*      <dd>Matches any string that starts with "/tmp/".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        <dt>/tmp/*.gif  <dd>Matches any string that starts with "/tmp/" and ends
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                        with ".gif".
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        <dt>/tmp/*new*.gif <dd>Matches any string that starts with "/tmp/"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                        and ends with ".gif" and has "new" somewhere in between.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public Regexp (String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        exp = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        int firstst = s.indexOf('*');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        int lastst = s.lastIndexOf('*');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (firstst < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            totalLen = s.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            exact = true;       // no * s
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            prefixLen = firstst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            if (firstst == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                prefix = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                prefix = s.substring(0, firstst);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            suffixLen = s.length() - lastst - 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            if (suffixLen == 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                suffix = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                suffix = s.substring(lastst + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            int nmids = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            int pos = firstst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            while (pos < lastst && pos >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                nmids++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                pos = s.indexOf('*', pos + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            totalLen = prefixLen + suffixLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            if (nmids > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                mids = new String[nmids];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                pos = firstst;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                for (int i = 0; i < nmids; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                    pos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    int npos = s.indexOf('*', pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    if (pos < npos) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                        mids[i] = s.substring(pos, npos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                        totalLen += mids[i].length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                    pos = npos;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    /** Returns true iff the String s matches this regular expression. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    final boolean matches(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        return matches(s, 0, s.length());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    /** Returns true iff the substring of s from offset for len characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        matches this regular expression. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    boolean matches(String s, int offset, int len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        if (exact)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            return len == totalLen &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                exp.regionMatches(ignoreCase, 0, s, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (len < totalLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (prefixLen > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                !prefix.regionMatches(ignoreCase,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                                      0, s, offset, prefixLen)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                suffixLen > 0 &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                !suffix.regionMatches(ignoreCase,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                                      0, s, offset + len - suffixLen,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                      suffixLen))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        if (mids == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        int nmids = mids.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        int spos = offset + prefixLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        int limit = offset+len-suffixLen;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        for (int i = 0; i<nmids; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            String ms = mids[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            int ml = ms.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            while (spos+ml<=limit &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                   !ms.regionMatches(ignoreCase,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                                     0, s, spos, ml))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                spos++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if (spos+ml>limit)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            spos+=ml;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
}