| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package net.sf.saxon.regex; |
| 9 |
|
| 10 |
import net.sf.saxon.tree.util.FastStringBuffer; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
public abstract class UnicodeString implements CharSequence, Comparable<UnicodeString> { |
| 19 |
|
| 20 |
int cachedHash = -1; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 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 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 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 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 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 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 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 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
public abstract UnicodeString uSubstring(int beginIndex, int endIndex); |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public abstract int uIndexOf(int search, int start); |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public abstract int uCharAt(int pos); |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public abstract int uLength(); |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public abstract boolean isEnd(int pos); |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
@Override |
| 183 |
public int hashCode() { |
| 184 |
|
| 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 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 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 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 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 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 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 |
} |