Debian Codegraph @

net/sf/saxon/regex/UnicodeString.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.regex;
9
10 import net.sf.saxon.tree.util.FastStringBuffer;
11
12 /**
13 * An abstract class that efficiently handles Unicode strings including
14 * non-BMP characters; it has three subclasses, respectively handling
15 * strings whose maximum character code is 255, 65535, or 1114111.
16 */
17
18 public abstract class UnicodeString implements CharSequence, Comparable<UnicodeString> {
19
20 int cachedHash = -1;
21
22 /**
23 * Make a UnicodeString for a given CharSequence
24 *
25 * @param in the input CharSequence
26 * @return a UnicodeString using an appropriate implementation class
27 */
28
29 public static UnicodeString makeUnicodeString(CharSequence in) {
30 if (in instanceof UnicodeString) {
31 return (UnicodeString)in;
32 }
33 if (in.length() == 0) {
34 return EmptyString.THE_INSTANCE;
35 }
36 int width = getMaxWidth(in);
37 if (width == 1) {
38 return new LatinString(in);
39 } else if (width == 2) {
40 return new BMPString(in);
41 } else {
42 return new GeneralUnicodeString(in);
43 }
44 }
45
46 /**
47 * Make a UnicodeString for a given array of codepoints
48 *
49 * @param in the input CharSequence
50 * @return a UnicodeString using an appropriate implementation class
51 */
52
53 public static UnicodeString makeUnicodeString(int[] in) {
54 for (int ch : in) {
55 if (ch > 65535) {
56 return new GeneralUnicodeString(in, 0, in.length);
57 }
58 }
59 FastStringBuffer fsb = new FastStringBuffer(in.length);
60 for (int ch : in) {
61 fsb.append((char) ch);
62 }
63 return new BMPString(fsb);
64 }
65
66 /**
67 * Test whether a CharSequence contains Unicode codepoints outside the BMP range
68 *
69 * @param value the string to be tested
70 * @return true if the string contains non-BMP codepoints
71 */
72
73 public static boolean containsSurrogatePairs(CharSequence value) {
74 if (value instanceof BMPString || value instanceof LatinString || value instanceof EmptyString) {
75 return false;
76 } else if (value instanceof GeneralUnicodeString) {
77 GeneralUnicodeString gus = (GeneralUnicodeString)value;
78 for (int i = 0; i < gus.uLength(); i++) {
79 if (gus.uCharAt(i) >= 65535) {
80 return true;
81 }
82 }
83 return false;
84 } else {
85 for (int i = 0; i < value.length(); i++) {
86 int c = (int) value.charAt(i);
87 if (c >= 55296 && c <= 56319) {
88 return true;
89 }
90 }
91 return false;
92 }
93 }
94
95 /**
96 * Get the maximum width of codepoints in this string, in bytes
97 * @param value the string to be examined
98 * @return 1, 2, or 4 depending on whether the string contains characters
99 * with codepoints &gt;255 and/or &gt;65535
100 */
101
102 public static int getMaxWidth(CharSequence value) {
103 if (value instanceof LatinString || value instanceof EmptyString) {
104 return 1;
105 }
106 if (value instanceof BMPString) {
107 return 2;
108 }
109 if (value instanceof GeneralUnicodeString) {
110 return 4;
111 }
112 boolean nonLatin = false;
113 for (int i = 0; i < value.length(); i++) {
114 int c = (int) value.charAt(i);
115 if (c > 255) {
116 nonLatin = true;
117 }
118 if (c >= 55296 && c <= 56319) {
119 return 4;
120 }
121 }
122 return nonLatin ? 2 : 1;
123 }
124
125 /**
126 * Get a substring of this string
127 *
128 * @param beginIndex the index of the first character to be included (counting
129 * codepoints, not 16-bit characters)
130 * @param endIndex the index of the first character to be NOT included (counting
131 * codepoints, not 16-bit characters)
132 * @return a substring
133 * @throws IndexOutOfBoundsException if the selection goes off the start or end of the string
134 * (this function follows the semantics of String.substring(), not the XPath semantics)
135 */
136 public abstract UnicodeString uSubstring(int beginIndex, int endIndex);
137
138 /**
139 * Get the first match for a given character
140 *
141 * @param search the character to look for
142 * @param start the first position to look
143 * @return the position of the first occurrence of the sought character, or -1 if not found
144 */
145
146 public abstract int uIndexOf(int search, int start);
147
148 /**
149 * Get the character at a specified position
150 *
151 * @param pos the index of the required character (counting
152 * codepoints, not 16-bit characters)
153 * @return a character (Unicode codepoint) at the specified position.
154 */
155
156 public abstract int uCharAt(int pos);
157
158 /**
159 * Get the length of the string, in Unicode codepoints
160 *
161 * @return the number of codepoints in the string
162 */
163
164 public abstract int uLength();
165
166 /**
167 * Ask whether a given position is at (or beyond) the end of the string
168 *
169 * @param pos the index of the required character (counting
170 * codepoints, not 16-bit characters)
171 * @return <tt>true</tt> iff if the specified index is after the end of the character stream
172 */
173
174 public abstract boolean isEnd(int pos);
175
176 /**
177 * Implementations of UnicodeString can be compared with each other, but not with
178 * other implementations of CharSequence
179 * @return a hashCode that distinuishes this UnicodeString from others
180 */
181
182 @Override
183 public int hashCode() {
184 // Same result as String#hashCode() in the case where all characters are BMP chars
185 if (cachedHash == -1) {
186 int h = 0;
187 for (int i = 0; i < uLength(); i++) {
188 h = 31 * h + uCharAt(i);
189 }
190 cachedHash = h;
191 }
192 return cachedHash;
193 }
194
195 /**
196 * Implementations of UnicodeString can be compared with each other, but not with
197 * other implementations of CharSequence
198 * @param obj the object to be compared
199 * @return true if obj is a UnicodeString containing the same codepoints
200 */
201
202 @Override
203 public boolean equals(Object obj) {
204 if (!(obj instanceof UnicodeString)) {
205 return false;
206 }
207 if (uLength() != ((UnicodeString)obj).uLength()) {
208 return false;
209 }
210 for (int i=0; i<uLength(); i++) {
211 if (uCharAt(i) != ((UnicodeString)obj).uCharAt(i)) {
212 return false;
213 }
214 }
215 return true;
216 }
217
218 /**
219 * Compare two unicode strings in codepoint collating sequence
220 *
221 * @param other the object to be compared
222 * @return less than 0, 0, or greater than 0 depending on the ordering of the two strings
223 */
224
225 public int compareTo(UnicodeString other) {
226 int alen = uLength();
227 int blen = other.uLength();
228 int i = 0;
229 int j = 0;
230 while (true) {
231 if (i == alen) {
232 if (j == blen) {
233 return 0;
234 } else {
235 return -1;
236 }
237 }
238 if (j == blen) {
239 return +1;
240 }
241
242 int nexta = uCharAt(i++);
243 int nextb = other.uCharAt(j++);
244 int c = nexta - nextb;
245 if (c != 0) {
246 return c;
247 }
248 }
249 }
250
251
252 /**
253 * Get the codepoints of the string as a byte array, allocating three
254 * bytes per character. (Max unicode codepoint = x10FFFF)
255 *
256 * @return a byte array that can act as a collation key
257 */
258
259 public final byte[] getCodepointCollationKey() {
260 int len = uLength();
261 byte[] result = new byte[len * 3];
262 for (int i = 0, j = 0; i < len; i++) {
263 int c = uCharAt(i);
264 result[j++] = (byte) (c >> 16);
265 result[j++] = (byte) (c >> 8);
266 result[j++] = (byte) c;
267 }
268 return result;
269 }
270 }