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: *
020: * Portions, Copyright © 1991-2005 Unicode, Inc. The following applies to Unicode.
021: *
022: * COPYRIGHT AND PERMISSION NOTICE
023: *
024: * Copyright © 1991-2005 Unicode, Inc. All rights reserved. Distributed under
025: * the Terms of Use in http://www.unicode.org/copyright.html. Permission is
026: * hereby granted, free of charge, to any person obtaining a copy of the
027: * Unicode data files and any associated documentation (the "Data Files")
028: * or Unicode software and any associated documentation (the "Software")
029: * to deal in the Data Files or Software without restriction, including without
030: * limitation the rights to use, copy, modify, merge, publish, distribute,
031: * and/or sell copies of the Data Files or Software, and to permit persons
032: * to whom the Data Files or Software are furnished to do so, provided that
033: * (a) the above copyright notice(s) and this permission notice appear with
034: * all copies of the Data Files or Software, (b) both the above copyright
035: * notice(s) and this permission notice appear in associated documentation,
036: * and (c) there is clear notice in each modified Data File or in the Software
037: * as well as in the documentation associated with the Data File(s) or Software
038: * that the data or software has been modified.
039:
040: * THE DATA FILES AND SOFTWARE ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
041: * KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
042: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
043: * OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
044: * INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
045: * OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
046: * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
047: * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
048: * PERFORMANCE OF THE DATA FILES OR SOFTWARE.
049: *
050: * Except as contained in this notice, the name of a copyright holder shall
051: * not be used in advertising or otherwise to promote the sale, use or other
052: * dealings in these Data Files or Software without prior written
053: * authorization of the copyright holder.
054: *
055: * 2. Additional terms from the Database:
056: *
057: * Copyright © 1995-1999 Unicode, Inc. All Rights reserved.
058: *
059: * Disclaimer
060: *
061: * The Unicode Character Database is provided as is by Unicode, Inc.
062: * No claims are made as to fitness for any particular purpose. No warranties
063: * of any kind are expressed or implied. The recipient agrees to determine
064: * applicability of information provided. If this file has been purchased
065: * on magnetic or optical media from Unicode, Inc., the sole remedy for any claim
066: * will be exchange of defective media within 90 days of receipt. This disclaimer
067: * is applicable for all other data files accompanying the Unicode Character Database,
068: * some of which have been compiled by the Unicode Consortium, and some of which
069: * have been supplied by other sources.
070: *
071: * Limitations on Rights to Redistribute This Data
072: *
073: * Recipient is granted the right to make copies in any form for internal
074: * distribution and to freely use the information supplied in the creation of
075: * products supporting the UnicodeTM Standard. The files in
076: * the Unicode Character Database can be redistributed to third parties or other
077: * organizations (whether for profit or not) as long as this notice and the disclaimer
078: * notice are retained. Information can be extracted from these files and used
079: * in documentation or programs, as long as there is an accompanying notice
080: * indicating the source.
081: */
082:
083: package java.util.regex;
084:
085: /**
086: * This class represents low surrogate character.
087: */
088: class LowSurrogateCharSet extends JointSet {
089:
090: /*
091: * Note that we can use high and low surrogate characters
092: * that don't combine into supplementary code point.
093: * See http://www.unicode.org/reports/tr18/#Supplementary_Characters
094: */
095: private char low;
096:
097: public LowSurrogateCharSet(char low) {
098: this .low = low;
099: }
100:
101: /**
102: * Returns the next.
103: */
104: public AbstractSet getNext() {
105: return this .next;
106: }
107:
108: /**
109: * Sets next abstract set.
110: * @param next
111: * The next to set.
112: */
113: public void setNext(AbstractSet next) {
114: this .next = next;
115: }
116:
117: public int matches(int stringIndex, CharSequence testString,
118: MatchResultImpl matchResult) {
119:
120: if (stringIndex + 1 > matchResult.getRightBound()) {
121: matchResult.hitEnd = true;
122: return -1;
123: }
124:
125: char low = testString.charAt(stringIndex);
126:
127: if (stringIndex > matchResult.getLeftBound()) {
128: char high = testString.charAt(stringIndex - 1);
129:
130: /*
131: * we consider high surrogate followed by
132: * low surrogate as a codepoint
133: */
134: if (Character.isHighSurrogate(high)) {
135: return -1;
136: }
137: }
138:
139: if (this .low == low) {
140: return next.matches(stringIndex + 1, testString,
141: matchResult);
142: }
143:
144: return -1;
145: }
146:
147: public int find(int strIndex, CharSequence testString,
148: MatchResultImpl matchResult) {
149: if (testString instanceof String) {
150: String testStr = (String) testString;
151: int startStr = matchResult.getLeftBound();
152: int strLength = matchResult.getRightBound();
153:
154: while (strIndex < strLength) {
155:
156: strIndex = testStr.indexOf(low, strIndex);
157: if (strIndex < 0)
158: return -1;
159:
160: if (strIndex > startStr) {
161:
162: /*
163: * we consider high surrogate followed by
164: * low surrogate as a codepoint
165: */
166: if (Character.isHighSurrogate(testStr
167: .charAt(strIndex - 1))) {
168: strIndex++;
169: continue;
170: }
171: }
172:
173: if (next.matches(strIndex + 1, testString, matchResult) >= 0) {
174: return strIndex;
175: }
176: strIndex++;
177: }
178:
179: return -1;
180: }
181:
182: return super .find(strIndex, testString, matchResult);
183: }
184:
185: public int findBack(int strIndex, int lastIndex,
186: CharSequence testString, MatchResultImpl matchResult) {
187: if (testString instanceof String) {
188: int startStr = matchResult.getLeftBound();
189: String testStr = (String) testString;
190:
191: while (lastIndex >= strIndex) {
192: lastIndex = testStr.lastIndexOf(low, lastIndex);
193: if (lastIndex < 0 || lastIndex < strIndex) {
194: return -1;
195: }
196:
197: if (lastIndex > startStr) {
198:
199: /*
200: * we consider high surrogate followed by
201: * low surrogate as a codepoint
202: */
203: if (Character.isHighSurrogate(testStr
204: .charAt(lastIndex - 1))) {
205: lastIndex -= 2;
206: continue;
207: }
208: }
209:
210: if (next
211: .matches(lastIndex + 1, testString, matchResult) >= 0) {
212: return lastIndex;
213: }
214:
215: lastIndex--;
216: }
217:
218: return -1;
219: }
220:
221: return super .findBack(strIndex, lastIndex, testString,
222: matchResult);
223: }
224:
225: protected String getName() {
226: return "" + low;
227: }
228:
229: protected int getChar() {
230: return low;
231: }
232:
233: public boolean first(AbstractSet set) {
234: if (set instanceof CharSet) {
235: return false;
236: } else if (set instanceof RangeSet) {
237: return false;
238: } else if (set instanceof SupplRangeSet) {
239: return false;
240: } else if (set instanceof SupplCharSet) {
241: return false;
242: } else if (set instanceof HighSurrogateCharSet) {
243: return false;
244: } else if (set instanceof LowSurrogateCharSet) {
245: return ((LowSurrogateCharSet) set).low == this .low;
246: }
247:
248: return true;
249: }
250:
251: public boolean hasConsumed(MatchResultImpl matchResult) {
252: return true;
253: }
254: }
|