0N/A/*
2362N/A * Copyright (c) 1995, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.misc;
0N/A
0N/A/**
0N/A * A class to represent a regular expression. Only handles '*'s.
0N/A * @author James Gosling
0N/A */
0N/A
0N/Apublic class Regexp {
0N/A /** if true then the matching process ignores case. */
0N/A public boolean ignoreCase;
0N/A
0N/A /*
0N/A * regular expressions are carved into three regions: a constant string
0N/A * prefix, a constant string suffix, and a series of floating strings in
0N/A * between. In the input regular expression, they are separated by *s
0N/A */
0N/A public String exp;
0N/A public String prefix, suffix;
0N/A public boolean exact;
0N/A public int prefixLen, suffixLen, totalLen;
0N/A public String mids[];
0N/A
0N/A /** Create a new regular expression object. The regular expression
0N/A is a series of constant strings separated by *s. For example:
0N/A <dl>
0N/A <dt>*.gif <dd>Matches any string that ends in ".gif".
0N/A <dt>/tmp/* <dd>Matches any string that starts with "/tmp/".
0N/A <dt>/tmp/*.gif <dd>Matches any string that starts with "/tmp/" and ends
0N/A with ".gif".
0N/A <dt>/tmp/*new*.gif <dd>Matches any string that starts with "/tmp/"
0N/A and ends with ".gif" and has "new" somewhere in between.
0N/A </dl>
0N/A */
0N/A public Regexp (String s) {
0N/A exp = s;
0N/A int firstst = s.indexOf('*');
0N/A int lastst = s.lastIndexOf('*');
0N/A if (firstst < 0) {
0N/A totalLen = s.length();
0N/A exact = true; // no * s
0N/A } else {
0N/A prefixLen = firstst;
0N/A if (firstst == 0)
0N/A prefix = null;
0N/A else
0N/A prefix = s.substring(0, firstst);
0N/A suffixLen = s.length() - lastst - 1;
0N/A if (suffixLen == 0)
0N/A suffix = null;
0N/A else
0N/A suffix = s.substring(lastst + 1);
0N/A int nmids = 0;
0N/A int pos = firstst;
0N/A while (pos < lastst && pos >= 0) {
0N/A nmids++;
0N/A pos = s.indexOf('*', pos + 1);
0N/A }
0N/A totalLen = prefixLen + suffixLen;
0N/A if (nmids > 0) {
0N/A mids = new String[nmids];
0N/A pos = firstst;
0N/A for (int i = 0; i < nmids; i++) {
0N/A pos++;
0N/A int npos = s.indexOf('*', pos);
0N/A if (pos < npos) {
0N/A mids[i] = s.substring(pos, npos);
0N/A totalLen += mids[i].length();
0N/A }
0N/A pos = npos;
0N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A /** Returns true iff the String s matches this regular expression. */
0N/A final boolean matches(String s) {
0N/A return matches(s, 0, s.length());
0N/A }
0N/A
0N/A /** Returns true iff the substring of s from offset for len characters
0N/A matches this regular expression. */
0N/A boolean matches(String s, int offset, int len) {
0N/A if (exact)
0N/A return len == totalLen &&
0N/A exp.regionMatches(ignoreCase, 0, s, offset, len);
0N/A if (len < totalLen)
0N/A return false;
0N/A if (prefixLen > 0 &&
0N/A !prefix.regionMatches(ignoreCase,
0N/A 0, s, offset, prefixLen)
0N/A ||
0N/A suffixLen > 0 &&
0N/A !suffix.regionMatches(ignoreCase,
0N/A 0, s, offset + len - suffixLen,
0N/A suffixLen))
0N/A return false;
0N/A if (mids == null)
0N/A return true;
0N/A int nmids = mids.length;
0N/A int spos = offset + prefixLen;
0N/A int limit = offset+len-suffixLen;
0N/A for (int i = 0; i<nmids; i++) {
0N/A String ms = mids[i];
0N/A int ml = ms.length();
0N/A while (spos+ml<=limit &&
0N/A !ms.regionMatches(ignoreCase,
0N/A 0, s, spos, ml))
0N/A spos++;
0N/A if (spos+ml>limit)
0N/A return false;
0N/A spos+=ml;
0N/A }
0N/A return true;
0N/A }
0N/A
0N/A}