regularexpression.java

来自「JAVA的一些源码 JAVA2 STANDARD EDITION DEVELO」· Java 代码 · 共 1,511 行 · 第 1/5 页

JAVA
1,511
字号
                    int ret = this. matchCharArray (con, op.next, offset, dx, opts);                    if (ret >= 0)  return ret;                    op = op.getChild();                }                break;            case Op.UNION:                for (int i = 0;  i < op.size();  i ++) {                    int ret = this. matchCharArray (con, op.elementAt(i), offset, dx, opts);                    if (DEBUG) {                        System.err.println("UNION: "+i+", ret="+ret);                    }                    if (ret >= 0)  return ret;                }                return -1;            case Op.CAPTURE:                int refno = op.getData();                if (con.match != null && refno > 0) {                    int save = con.match.getBeginning(refno);                    con.match.setBeginning(refno, offset);                    int ret = this. matchCharArray (con, op.next, offset, dx, opts);                    if (ret < 0)  con.match.setBeginning(refno, save);                    return ret;                } else if (con.match != null && refno < 0) {                    int index = -refno;                    int save = con.match.getEnd(index);                    con.match.setEnd(index, offset);                    int ret = this. matchCharArray (con, op.next, offset, dx, opts);                    if (ret < 0)  con.match.setEnd(index, save);                    return ret;                }                op = op.next;                break;            case Op.LOOKAHEAD:                if (0 > this. matchCharArray (con, op.getChild(), offset, 1, opts))  return -1;                op = op.next;                break;            case Op.NEGATIVELOOKAHEAD:                if (0 <= this. matchCharArray (con, op.getChild(), offset, 1, opts))  return -1;                op = op.next;                break;            case Op.LOOKBEHIND:                if (0 > this. matchCharArray (con, op.getChild(), offset, -1, opts))  return -1;                op = op.next;                break;            case Op.NEGATIVELOOKBEHIND:                if (0 <= this. matchCharArray (con, op.getChild(), offset, -1, opts))  return -1;                op = op.next;                break;            case Op.INDEPENDENT:                {                    int ret = this. matchCharArray (con, op.getChild(), offset, dx, opts);                    if (ret < 0)  return ret;                    offset = ret;                    op = op.next;                }                break;            case Op.MODIFIER:                {                    int localopts = opts;                    localopts |= op.getData();                    localopts &= ~op.getData2();                    //System.err.println("MODIFIER: "+Integer.toString(opts, 16)+" -> "+Integer.toString(localopts, 16));                    int ret = this. matchCharArray (con, op.getChild(), offset, dx, localopts);                    if (ret < 0)  return ret;                    offset = ret;                    op = op.next;                }                break;            case Op.CONDITION:                {                    Op.ConditionOp cop = (Op.ConditionOp)op;                    boolean matchp = false;                    if (cop.refNumber > 0) {                        if (cop.refNumber >= this.nofparen)                            throw new RuntimeException("Internal Error: Reference number must be more than zero: "+cop.refNumber);                        matchp = con.match.getBeginning(cop.refNumber) >= 0                                 && con.match.getEnd(cop.refNumber) >= 0;                    } else {                        matchp = 0 <= this. matchCharArray (con, cop.condition, offset, dx, opts);                    }                    if (matchp) {                        op = cop.yes;                    } else if (cop.no != null) {                        op = cop.no;                    } else {                        op = cop.next;                    }                }                break;            default:                throw new RuntimeException("Unknown operation type: "+op.type);            } // switch (op.type)        } // while    }    private static final int getPreviousWordType(char[]  target, int begin, int end,                                                 int offset, int opts) {        int ret = getWordType(target, begin, end, --offset, opts);        while (ret == WT_IGNORE)            ret = getWordType(target, begin, end, --offset, opts);        return ret;    }    private static final int getWordType(char[]  target, int begin, int end,                                         int offset, int opts) {        if (offset < begin || offset >= end)  return WT_OTHER;        return getWordType0( target [  offset ] , opts);    }    private static final boolean regionMatches(char[]  target, int offset, int limit,                                               String part, int partlen) {        if (offset < 0)  return false;        if (limit-offset < partlen)            return false;        int i = 0;        while (partlen-- > 0) {            if ( target [  offset++ ]  != part.charAt(i++))                return false;        }        return true;    }    private static final boolean regionMatches(char[]  target, int offset, int limit,                                               int offset2, int partlen) {        if (offset < 0)  return false;        if (limit-offset < partlen)            return false;        int i = offset2;        while (partlen-- > 0) {            if ( target [  offset++ ]  !=  target [  i++ ] )                return false;        }        return true;    }/** * @see java.lang.String#regionMatches */    private static final boolean regionMatchesIgnoreCase(char[]  target, int offset, int limit,                                                         String part, int partlen) {        if (offset < 0)  return false;        if (limit-offset < partlen)            return false;        int i = 0;        while (partlen-- > 0) {            char ch1 =  target [  offset++ ] ;            char ch2 = part.charAt(i++);            if (ch1 == ch2)                continue;            char uch1 = Character.toUpperCase(ch1);            char uch2 = Character.toUpperCase(ch2);            if (uch1 == uch2)                continue;            if (Character.toLowerCase(uch1) != Character.toLowerCase(uch2))                return false;        }        return true;    }    private static final boolean regionMatchesIgnoreCase(char[]  target, int offset, int limit,                                                         int offset2, int partlen) {        if (offset < 0)  return false;        if (limit-offset < partlen)            return false;        int i = offset2;        while (partlen-- > 0) {            char ch1 =  target [  offset++ ] ;            char ch2 =  target [  i++ ] ;            if (ch1 == ch2)                continue;            char uch1 = Character.toUpperCase(ch1);            char uch2 = Character.toUpperCase(ch2);            if (uch1 == uch2)                continue;            if (Character.toLowerCase(uch1) != Character.toLowerCase(uch2))                return false;        }        return true;    }    /**     * Checks whether the <var>target</var> text <strong>contains</strong> this pattern or not.     *     * @return true if the target is matched to this regular expression.     */    public boolean matches(String  target) {        return this.matches(target, 0,  target .length() , (Match)null);    }    /**     * Checks whether the <var>target</var> text <strong>contains</strong> this pattern     * in specified range or not.     *     * @param start Start offset of the range.     * @param end  End offset +1 of the range.     * @return true if the target is matched to this regular expression.     */    public boolean matches(String  target, int start, int end) {        return this.matches(target, start, end, (Match)null);    }    /**     * Checks whether the <var>target</var> text <strong>contains</strong> this pattern or not.     *     * @param match A Match instance for storing matching result.     * @return Offset of the start position in <VAR>target</VAR>; or -1 if not match.     */    public boolean matches(String  target, Match match) {        return this.matches(target, 0,  target .length() , match);    }    /**     * Checks whether the <var>target</var> text <strong>contains</strong> this pattern     * in specified range or not.     *     * @param start Start offset of the range.     * @param end  End offset +1 of the range.     * @param match A Match instance for storing matching result.     * @return Offset of the start position in <VAR>target</VAR>; or -1 if not match.     */    public boolean matches(String  target, int start, int end, Match match) {        synchronized (this) {            if (this.operations == null)                this.prepare();            if (this.context == null)                this.context = new Context();        }        Context con = null;        synchronized (this.context) {            con = this.context.inuse ? new Context() : this.context;            con.reset(target, start, end, this.numberOfClosures);        }        if (match != null) {            match.setNumberOfGroups(this.nofparen);            match.setSource(target);        } else if (this.hasBackReferences) {            match = new Match();            match.setNumberOfGroups(this.nofparen);            // Need not to call setSource() because            // a caller can not access this match instance.        }        con.match = match;        if (RegularExpression.isSet(this.options, XMLSCHEMA_MODE)) {            if (DEBUG) {                System.err.println("target string="+target);            }            int matchEnd = this. matchString (con, this.operations, con.start, 1, this.options);            if (DEBUG) {                System.err.println("matchEnd="+matchEnd);                System.err.println("con.limit="+con.limit);            }            if (matchEnd == con.limit) {                if (con.match != null) {                    con.match.setBeginning(0, con.start);                    con.match.setEnd(0, matchEnd);                }                con.inuse = false;                return true;            }            return false;        }        /*         * The pattern has only fixed string.         * The engine uses Boyer-Moore.         */        if (this.fixedStringOnly) {            //System.err.println("DEBUG: fixed-only: "+this.fixedString);            int o = this.fixedStringTable.matches(target, con.start, con.limit);            if (o >= 0) {                if (con.match != null) {                    con.match.setBeginning(0, o);                    con.match.setEnd(0, o+this.fixedString.length());                }                con.inuse = false;                return true;            }            con.inuse = false;            return false;        }        /*         * The pattern contains a fixed string.         * The engine checks with Boyer-Moore whether the text contains the f

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?