Utility.java
9.06 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
/*
* Decompiled with CFR 0_118.
*/
package com.adobe.agl.impl;
import com.adobe.agl.impl.UCharacterProperty;
import com.adobe.agl.lang.UCharacter;
import com.adobe.agl.text.UTF16;
public final class Utility {
public static String LINE_SEPARATOR = System.getProperty("line.separator");
static final char[] HEX_DIGIT = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
private static final char[] UNESCAPE_MAP = new char[]{'a', '\u0007', 'b', '\b', 'e', '\u001b', 'f', '\f', 'n', '\n', 'r', '\r', 't', '\t', 'v', '\u000b'};
static final char[] DIGITS = new char[]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
private static final Integer[] INT_CONST = new Integer[64];
public static final String escape(String s) {
int c;
StringBuffer buf = new StringBuffer();
for (int i = 0; i < s.length(); i += UTF16.getCharCount((int)c)) {
c = UTF16.charAt(s, i);
if (c >= 32 && c <= 127) {
if (c == 92) {
buf.append("\\\\");
continue;
}
buf.append((char)c);
continue;
}
boolean four = c <= 65535;
buf.append(four ? "\\u" : "\\U");
Utility.hex(c, four ? 4 : 8, buf);
}
return buf.toString();
}
public static int unescapeAt(String s, int[] offset16) {
int dig;
int result = 0;
int n = 0;
int minDig = 0;
int maxDig = 0;
int bitsPerDigit = 4;
boolean braces = false;
int offset = offset16[0];
int length = s.length();
if (offset < 0 || offset >= length) {
return -1;
}
int c = UTF16.charAt(s, offset);
offset += UTF16.getCharCount(c);
switch (c) {
case 117: {
maxDig = 4;
minDig = 4;
break;
}
case 85: {
maxDig = 8;
minDig = 8;
break;
}
case 120: {
minDig = 1;
if (offset < length && UTF16.charAt(s, offset) == 123) {
++offset;
braces = true;
maxDig = 8;
break;
}
maxDig = 2;
break;
}
default: {
dig = UCharacter.digit(c, 8);
if (dig < 0) break;
minDig = 1;
maxDig = 3;
n = 1;
bitsPerDigit = 3;
result = dig;
}
}
if (minDig != 0) {
while (offset < length && n < maxDig && (dig = UCharacter.digit(c = UTF16.charAt(s, offset), bitsPerDigit == 3 ? 8 : 16)) >= 0) {
result = result << bitsPerDigit | dig;
offset += UTF16.getCharCount(c);
++n;
}
if (n < minDig) {
return -1;
}
if (braces) {
if (c != 125) {
return -1;
}
++offset;
}
if (result < 0 || result >= 1114112) {
return -1;
}
if (offset < length && UTF16.isLeadSurrogate((char)result)) {
int ahead = offset + 1;
c = s.charAt(offset);
if (c == 92 && ahead < length) {
int[] o = new int[]{ahead};
c = Utility.unescapeAt(s, o);
ahead = o[0];
}
if (UTF16.isTrailSurrogate((char)c)) {
offset = ahead;
result = UCharacterProperty.getRawSupplementary((char)result, (char)c);
}
}
offset16[0] = offset;
return result;
}
for (int i = 0; i < UNESCAPE_MAP.length; i += 2) {
if (c == UNESCAPE_MAP[i]) {
offset16[0] = offset;
return UNESCAPE_MAP[i + 1];
}
if (c < UNESCAPE_MAP[i]) break;
}
if (c == 99 && offset < length) {
c = UTF16.charAt(s, offset);
offset16[0] = offset + UTF16.getCharCount(c);
return 31 & c;
}
offset16[0] = offset;
return c;
}
public static String hex(char ch) {
StringBuffer temp = new StringBuffer();
return Utility.hex(ch, temp).toString();
}
public static String hex(String s) {
StringBuffer temp = new StringBuffer();
return Utility.hex(s, temp).toString();
}
public static StringBuffer hex(char ch, StringBuffer output) {
return Utility.appendNumber(output, ch, 16, 4);
}
public static StringBuffer hex(int ch, int width, StringBuffer output) {
return Utility.appendNumber(output, ch, 16, width);
}
public static String hex(int ch, int width) {
StringBuffer buf = new StringBuffer();
return Utility.appendNumber(buf, ch, 16, width).toString();
}
public static String hex(long i, int places) {
boolean negative;
String result;
if (i == Long.MIN_VALUE) {
return "-8000000000000000";
}
boolean bl = negative = i < 0;
if (negative) {
i = - i;
}
if ((result = Long.toString(i, 16).toUpperCase()).length() < places) {
result = "0000000000000000".substring(result.length(), places) + result;
}
if (negative) {
return "" + '-' + result;
}
return result;
}
public static String hex(long ch) {
return Utility.hex(ch, 4);
}
public static StringBuffer hex(String s, StringBuffer result) {
for (int i = 0; i < s.length(); ++i) {
if (i != 0) {
result.append(',');
}
Utility.hex(s.charAt(i), result);
}
return result;
}
public static int skipWhitespace(String str, int pos) {
int c;
while (pos < str.length() && UCharacterProperty.isRuleWhiteSpace(c = UTF16.charAt(str, pos))) {
pos += UTF16.getCharCount(c);
}
return pos;
}
public static String deleteRuleWhiteSpace(String str) {
int ch;
StringBuffer buf = new StringBuffer();
for (int i = 0; i < str.length(); i += UTF16.getCharCount((int)ch)) {
ch = UTF16.charAt(str, i);
if (UCharacterProperty.isRuleWhiteSpace(ch)) continue;
UTF16.append(buf, ch);
}
return buf.toString();
}
private static void recursiveAppendNumber(StringBuffer result, int n, int radix, int minDigits) {
int digit = n % radix;
if (n >= radix || minDigits > 1) {
Utility.recursiveAppendNumber(result, n / radix, radix, minDigits - 1);
}
result.append(DIGITS[digit]);
}
public static StringBuffer appendNumber(StringBuffer result, int n, int radix, int minDigits) throws IllegalArgumentException {
if (radix < 2 || radix > 36) {
throw new IllegalArgumentException("Illegal radix " + radix);
}
int abs = n;
if (n < 0) {
abs = - n;
result.append("-");
}
Utility.recursiveAppendNumber(result, abs, radix, minDigits);
return result;
}
public static boolean isUnprintable(int c) {
return c < 32 || c > 126;
}
public static boolean escapeUnprintable(StringBuffer result, int c) {
if (Utility.isUnprintable(c)) {
result.append('\\');
if ((c & -65536) != 0) {
result.append('U');
result.append(DIGITS[15 & c >> 28]);
result.append(DIGITS[15 & c >> 24]);
result.append(DIGITS[15 & c >> 20]);
result.append(DIGITS[15 & c >> 16]);
} else {
result.append('u');
}
result.append(DIGITS[15 & c >> 12]);
result.append(DIGITS[15 & c >> 8]);
result.append(DIGITS[15 & c >> 4]);
result.append(DIGITS[15 & c]);
return true;
}
return false;
}
public static void getChars(StringBuffer src, int srcBegin, int srcEnd, char[] dst, int dstBegin) {
if (srcBegin == srcEnd) {
return;
}
src.getChars(srcBegin, srcEnd, dst, dstBegin);
}
public static final int compareUnsigned(int source, int target) {
if ((source -= Integer.MIN_VALUE) < (target -= Integer.MIN_VALUE)) {
return -1;
}
if (source > target) {
return 1;
}
return 0;
}
public static Integer integerValueOf(int val) {
if (0 <= val && val < 64) {
return INT_CONST[val];
}
return new Integer(val);
}
static {
for (int i = 0; i < 64; ++i) {
Utility.INT_CONST[i] = new Integer(i);
}
}
}