UCharacterName.java
13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.agl.impl;
import com.adobe.agl.impl.ICUData;
import com.adobe.agl.impl.UCharacterNameReader;
import com.adobe.agl.impl.UCharacterUtility;
import com.adobe.agl.lang.UCharacter;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.MissingResourceException;
public final class UCharacterName {
public int m_groupcount_ = 0;
int m_groupsize_ = 0;
private char[] m_tokentable_;
private byte[] m_tokenstring_;
private char[] m_groupinfo_;
private byte[] m_groupstring_;
private AlgorithmName[] m_algorithm_;
private char[] m_groupoffsets_ = new char[33];
private char[] m_grouplengths_ = new char[33];
private int[] m_nameSet_ = new int[8];
private int[] m_ISOCommentSet_ = new int[8];
private StringBuffer m_utilStringBuffer_ = new StringBuffer();
private int[] m_utilIntBuffer_ = new int[2];
private static UCharacterName INSTANCE_ = null;
private static final String[] TYPE_NAMES_ = new String[]{"unassigned", "uppercase letter", "lowercase letter", "titlecase letter", "modifier letter", "other letter", "non spacing mark", "enclosing mark", "combining spacing mark", "decimal digit number", "letter number", "other number", "space separator", "line separator", "paragraph separator", "control", "format", "private use area", "surrogate", "dash punctuation", "start punctuation", "end punctuation", "connector punctuation", "other punctuation", "math symbol", "currency symbol", "modifier symbol", "other symbol", "initial punctuation", "final punctuation", "noncharacter", "lead surrogate", "trail surrogate"};
public static UCharacterName getInstance() {
if (INSTANCE_ == null) {
try {
INSTANCE_ = new UCharacterName();
}
catch (IOException e) {
throw new MissingResourceException("Could not construct UCharacterName. Missing unames.icu", "", "");
}
catch (Exception e) {
throw new MissingResourceException(e.getMessage(), "", "");
}
}
return INSTANCE_;
}
public int getCharFromName(int choice, String name) {
if (choice >= 3 || name == null || name.length() == 0) {
return -1;
}
int result = UCharacterName.getExtendedChar(name.toLowerCase(), choice);
if (result >= -1) {
return result;
}
String upperCaseName = name.toUpperCase();
if (choice != 1) {
int count = 0;
if (this.m_algorithm_ != null) {
count = this.m_algorithm_.length;
}
--count;
while (count >= 0) {
result = this.m_algorithm_[count].getChar(upperCaseName);
if (result >= 0) {
return result;
}
--count;
}
}
if (choice == 2) {
result = this.getGroupChar(upperCaseName, 0);
if (result == -1) {
result = this.getGroupChar(upperCaseName, 1);
}
} else {
result = this.getGroupChar(upperCaseName, choice);
}
return result;
}
public int getGroupLengths(int index, char[] offsets, char[] lengths) {
int length = 65535;
byte b = 0;
byte n = 0;
int stringoffset = UCharacterUtility.toInt(this.m_groupinfo_[index + 1], this.m_groupinfo_[(index *= this.m_groupsize_) + 2]);
offsets[0] = '\u0000';
int i = 0;
while (i < 32) {
b = this.m_groupstring_[stringoffset];
for (int shift = 4; shift >= 0; shift -= 4) {
n = (byte)(b >> shift & 15);
if (length == 65535 && n > 11) {
length = (char)(n - 12 << 4);
continue;
}
lengths[i] = length != 65535 ? (char)((length | n) + 12) : (char)n;
if (i < 32) {
offsets[i + 1] = (char)(offsets[i] + lengths[i]);
}
length = 65535;
++i;
}
++stringoffset;
}
return stringoffset;
}
boolean setToken(char[] token, byte[] tokenstring) {
if (token != null && tokenstring != null && token.length > 0 && tokenstring.length > 0) {
this.m_tokentable_ = token;
this.m_tokenstring_ = tokenstring;
return true;
}
return false;
}
boolean setAlgorithm(AlgorithmName[] alg) {
if (alg != null && alg.length != 0) {
this.m_algorithm_ = alg;
return true;
}
return false;
}
boolean setGroupCountSize(int count, int size) {
if (count <= 0 || size <= 0) {
return false;
}
this.m_groupcount_ = count;
this.m_groupsize_ = size;
return true;
}
boolean setGroup(char[] group, byte[] groupstring) {
if (group != null && groupstring != null && group.length > 0 && groupstring.length > 0) {
this.m_groupinfo_ = group;
this.m_groupstring_ = groupstring;
return true;
}
return false;
}
private UCharacterName() throws IOException {
InputStream is = ICUData.getRequiredStream("data/icudt40b/unames.icu");
BufferedInputStream b = new BufferedInputStream(is, 100000);
UCharacterNameReader reader = new UCharacterNameReader(b);
reader.read(this);
b.close();
}
private synchronized int getGroupChar(String name, int choice) {
for (int i = 0; i < this.m_groupcount_; ++i) {
int startgpstrindex = this.getGroupLengths(i, this.m_groupoffsets_, this.m_grouplengths_);
int result = this.getGroupChar(startgpstrindex, this.m_grouplengths_, name, choice);
if (result == -1) continue;
return this.m_groupinfo_[i * this.m_groupsize_] << 5 | result;
}
return -1;
}
private int getGroupChar(int index, char[] length, String name, int choice) {
byte b = 0;
int namelen = name.length();
for (int result = 0; result <= 32; ++result) {
int nindex = 0;
int len = length[result];
if (choice == 1) {
int oldindex = index;
index += UCharacterUtility.skipByteSubString(this.m_groupstring_, index, len, 59);
len -= index - oldindex;
}
int count = 0;
while (count < len && nindex != -1 && nindex < namelen) {
b = this.m_groupstring_[index + count];
++count;
if (b >= this.m_tokentable_.length) {
if (name.charAt(nindex++) == (b & 255)) continue;
nindex = -1;
continue;
}
char token = this.m_tokentable_[b & 255];
if (token == '\ufffe') {
token = this.m_tokentable_[b << 8 | this.m_groupstring_[index + count] & 255];
++count;
}
if (token == '\uffff') {
if (name.charAt(nindex++) == (b & 255)) continue;
nindex = -1;
continue;
}
nindex = UCharacterUtility.compareNullTermByteSubString(name, this.m_tokenstring_, nindex, token);
}
if (namelen == nindex && (count == len || this.m_groupstring_[index + count] == 59)) {
return result;
}
index += len;
}
return -1;
}
private static int getType(int ch) {
if (UCharacterUtility.isNonCharacter(ch)) {
return 30;
}
int result = UCharacter.getType(ch);
if (result == 18) {
result = ch <= 56319 ? 31 : 32;
}
return result;
}
private static int getExtendedChar(String name, int choice) {
if (name.charAt(0) == '<') {
int startIndex;
int endIndex;
if (choice == 2 && name.charAt(endIndex = name.length() - 1) == '>' && (startIndex = name.lastIndexOf(45)) >= 0) {
++startIndex;
int result = -1;
try {
result = Integer.parseInt(name.substring(startIndex, endIndex), 16);
}
catch (NumberFormatException e) {
return -1;
}
String type = name.substring(1, startIndex - 1);
int length = TYPE_NAMES_.length;
for (int i = 0; i < length; ++i) {
if (type.compareTo(TYPE_NAMES_[i]) != 0) continue;
if (UCharacterName.getType(result) != i) break;
return result;
}
}
return -1;
}
return -2;
}
static final class AlgorithmName {
private int m_rangestart_;
private int m_rangeend_;
private byte m_type_;
private byte m_variant_;
private char[] m_factor_;
private String m_prefix_;
private byte[] m_factorstring_;
private StringBuffer m_utilStringBuffer_ = new StringBuffer();
private int[] m_utilIntBuffer_ = new int[256];
AlgorithmName() {
}
boolean setInfo(int rangestart, int rangeend, byte type, byte variant) {
if (rangestart >= 0 && rangestart <= rangeend && rangeend <= 1114111 && (type == 0 || type == 1)) {
this.m_rangestart_ = rangestart;
this.m_rangeend_ = rangeend;
this.m_type_ = type;
this.m_variant_ = variant;
return true;
}
return false;
}
boolean setFactor(char[] factor) {
if (factor.length == this.m_variant_) {
this.m_factor_ = factor;
return true;
}
return false;
}
boolean setPrefix(String prefix) {
if (prefix != null && prefix.length() > 0) {
this.m_prefix_ = prefix;
return true;
}
return false;
}
boolean setFactorString(byte[] string) {
this.m_factorstring_ = string;
return true;
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
int getChar(String name) {
int prefixlen = this.m_prefix_.length();
if (name.length() < prefixlen || !this.m_prefix_.equals(name.substring(0, prefixlen))) {
return -1;
}
switch (this.m_type_) {
case 0: {
try {
int result = Integer.parseInt(name.substring(prefixlen), 16);
if (this.m_rangestart_ > result || result > this.m_rangeend_) break;
return result;
}
catch (NumberFormatException e) {
return -1;
}
}
case 1: {
for (int ch = this.m_rangestart_; ch <= this.m_rangeend_; ++ch) {
int offset = ch - this.m_rangestart_;
int[] indexes = this.m_utilIntBuffer_;
int[] arrn = this.m_utilIntBuffer_;
synchronized (arrn) {
for (int i = this.m_variant_ - 1; i > 0; --i) {
int factor = this.m_factor_[i] & 255;
indexes[i] = offset % factor;
offset /= factor;
}
indexes[0] = offset;
if (this.compareFactorString(indexes, this.m_variant_, name, prefixlen)) {
return ch;
}
continue;
}
}
break;
}
}
return -1;
}
private boolean compareFactorString(int[] index, int length, String str, int offset) {
int size = this.m_factor_.length;
if (index == null || length != size) {
return false;
}
int count = 0;
int strcount = offset;
for (int i = 0; i <= --size; ++i) {
char factor = this.m_factor_[i];
if ((strcount = UCharacterUtility.compareNullTermByteSubString(str, this.m_factorstring_, strcount, count = UCharacterUtility.skipNullTermByteSubString(this.m_factorstring_, count, index[i]))) < 0) {
return false;
}
if (i == size) continue;
count = UCharacterUtility.skipNullTermByteSubString(this.m_factorstring_, count, factor - index[i]);
}
if (strcount != str.length()) {
return false;
}
return true;
}
}
}