001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Nikolay A. Kuznetsov
020: * @version $Revision: 1.12.2.2 $
021: */package java.util.regex;
022:
023: /**
024: * Represents node accepting single character.
025: *
026: * @author Nikolay A. Kuznetsov
027: * @version $Revision: 1.12.2.2 $
028: */
029: class CharSet extends LeafSet {
030:
031: private char ch = 0;
032:
033: public CharSet(char ch) {
034: this .ch = ch;
035: }
036:
037: public int charCount() {
038: return 1;
039: }
040:
041: public int accepts(int strIndex, CharSequence testString) {
042: return (this .ch == testString.charAt(strIndex)) ? 1 : -1;
043: }
044:
045: public int find(int strIndex, CharSequence testString,
046: MatchResultImpl matchResult) {
047: if (testString instanceof String) {
048: String testStr = (String) testString;
049: int strLength = matchResult.getRightBound();
050:
051: while (strIndex < strLength) {
052: strIndex = testStr.indexOf(ch, strIndex);
053: if (strIndex < 0)
054: return -1;
055: if (next.matches(strIndex + 1, testString, matchResult) >= 0) {
056: return strIndex;
057: }
058: strIndex++;
059: }
060:
061: return -1;
062: }
063:
064: return super .find(strIndex, testString, matchResult);
065: }
066:
067: public int findBack(int strIndex, int lastIndex,
068: CharSequence testString, MatchResultImpl matchResult) {
069: if (testString instanceof String) {
070: String testStr = (String) testString;
071:
072: while (lastIndex >= strIndex) {
073: lastIndex = testStr.lastIndexOf(ch, lastIndex);
074: if (lastIndex < 0 || lastIndex < strIndex) {
075: return -1;
076: }
077:
078: if (next
079: .matches(lastIndex + 1, testString, matchResult) >= 0) {
080: return lastIndex;
081: }
082:
083: lastIndex--;
084: }
085:
086: return -1;
087: }
088:
089: return super .findBack(strIndex, lastIndex, testString,
090: matchResult);
091: }
092:
093: protected String getName() {
094: return "" + ch; //$NON-NLS-1$
095: }
096:
097: protected char getChar() {
098: return ch;
099: }
100:
101: public boolean first(AbstractSet set) {
102: if (set instanceof CharSet) {
103: return ((CharSet) set).getChar() == ch;
104: } else if (set instanceof RangeSet) {
105: return ((RangeSet) set).accepts(0, Character.toString(ch)) > 0;
106: } else if (set instanceof SupplRangeSet) {
107: return ((SupplRangeSet) set).contains(ch);
108: } else if (set instanceof SupplCharSet) {
109: return false;
110: }
111:
112: return true;
113: }
114: }
|