author | alanb |
Thu, 01 Dec 2016 08:57:53 +0000 | |
changeset 42338 | a60f280f803c |
parent 35783 | 2690535d72cc |
permissions | -rw-r--r-- |
5610 | 1 |
/* |
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
2 |
* Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved. |
5610 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
6374
e214162c907e
6982137: Rebranding pass 2 - missed copyright changes
ohair
parents:
5610
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
5610 | 8 |
* particular file as subject to the "Classpath" exception as provided |
6374
e214162c907e
6982137: Rebranding pass 2 - missed copyright changes
ohair
parents:
5610
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
5610 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
6374
e214162c907e
6982137: Rebranding pass 2 - missed copyright changes
ohair
parents:
5610
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
e214162c907e
6982137: Rebranding pass 2 - missed copyright changes
ohair
parents:
5610
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
e214162c907e
6982137: Rebranding pass 2 - missed copyright changes
ohair
parents:
5610
diff
changeset
|
23 |
* questions. |
5610 | 24 |
*/ |
25 |
||
26 |
package java.lang; |
|
27 |
||
28 |
import java.io.DataInputStream; |
|
29 |
import java.io.InputStream; |
|
30 |
import java.lang.ref.SoftReference; |
|
31 |
import java.util.Arrays; |
|
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
32 |
import java.util.Locale; |
5610 | 33 |
import java.util.zip.InflaterInputStream; |
34 |
import java.security.AccessController; |
|
35 |
import java.security.PrivilegedAction; |
|
36 |
||
37 |
class CharacterName { |
|
38 |
||
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
39 |
private static SoftReference<CharacterName> refCharName; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
40 |
|
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
41 |
// codepoint -> bkIndex -> lookup -> offset/len |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
42 |
private final byte[] strPool; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
43 |
private final int[] lookup; // code point -> offset/len in strPool |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
44 |
private final int[] bkIndices; // code point -> lookup index |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
45 |
|
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
46 |
// name -> hash -> hsIndices -> cpEntries -> code point |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
47 |
private final int[] cpEntries; // code points that have name in strPool |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
48 |
private final int[] hsIndices; // chain heads, hash indices into "cps" |
5610 | 49 |
|
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
50 |
private CharacterName() { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
51 |
try (DataInputStream dis = new DataInputStream(new InflaterInputStream( |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
52 |
AccessController.doPrivileged(new PrivilegedAction<>() { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
53 |
public InputStream run() { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
54 |
return getClass().getResourceAsStream("uniName.dat"); |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
55 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
56 |
})))) { |
5610 | 57 |
|
58 |
int total = dis.readInt(); |
|
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
59 |
int bkNum = dis.readInt(); |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
60 |
int cpNum = dis.readInt(); |
5610 | 61 |
int cpEnd = dis.readInt(); |
62 |
byte ba[] = new byte[cpEnd]; |
|
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
63 |
lookup = new int[bkNum * 256]; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
64 |
bkIndices = new int[(Character.MAX_CODE_POINT + 1) >> 8]; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
65 |
strPool = new byte[total - cpEnd]; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
66 |
cpEntries = new int[cpNum * 3]; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
67 |
hsIndices = new int[(cpNum / 2) | 1]; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
68 |
Arrays.fill(bkIndices, -1); |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
69 |
Arrays.fill(hsIndices, -1); |
5610 | 70 |
dis.readFully(ba); |
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
71 |
dis.readFully(strPool); |
5610 | 72 |
|
73 |
int nameOff = 0; |
|
74 |
int cpOff = 0; |
|
75 |
int cp = 0; |
|
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
76 |
int bk = -1; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
77 |
int prevBk = -1; // prev bkNo; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
78 |
int idx = 0; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
79 |
int next = -1; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
80 |
int hash = 0; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
81 |
int hsh = 0; |
5610 | 82 |
do { |
83 |
int len = ba[cpOff++] & 0xff; |
|
84 |
if (len == 0) { |
|
85 |
len = ba[cpOff++] & 0xff; |
|
86 |
// always big-endian |
|
87 |
cp = ((ba[cpOff++] & 0xff) << 16) | |
|
88 |
((ba[cpOff++] & 0xff) << 8) | |
|
89 |
((ba[cpOff++] & 0xff)); |
|
90 |
} else { |
|
91 |
cp++; |
|
92 |
} |
|
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
93 |
// cp -> name |
5610 | 94 |
int hi = cp >> 8; |
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
95 |
if (prevBk != hi) { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
96 |
bk++; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
97 |
bkIndices[hi] = bk; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
98 |
prevBk = hi; |
5610 | 99 |
} |
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
100 |
lookup[(bk << 8) + (cp & 0xff)] = (nameOff << 8) | len; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
101 |
// name -> cp |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
102 |
hash = hashN(strPool, nameOff, len); |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
103 |
hsh = (hash & 0x7fffffff) % hsIndices.length; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
104 |
next = hsIndices[hsh]; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
105 |
hsIndices[hsh] = idx; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
106 |
idx = addCp(idx, hash, next, cp); |
5610 | 107 |
nameOff += len; |
108 |
} while (cpOff < cpEnd); |
|
109 |
} catch (Exception x) { |
|
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
7803
diff
changeset
|
110 |
throw new InternalError(x.getMessage(), x); |
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
111 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
112 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
113 |
|
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
114 |
private static final int hashN(byte[] a, int off, int len) { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
115 |
int h = 1; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
116 |
while (len-- > 0) { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
117 |
h = 31 * h + a[off++]; |
5610 | 118 |
} |
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
119 |
return h; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
120 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
121 |
|
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
122 |
private int addCp(int idx, int hash, int next, int cp) { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
123 |
cpEntries[idx++] = hash; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
124 |
cpEntries[idx++] = next; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
125 |
cpEntries[idx++] = cp; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
126 |
return idx; |
5610 | 127 |
} |
128 |
||
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
129 |
private int getCpHash(int idx) { return cpEntries[idx]; } |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
130 |
private int getCpNext(int idx) { return cpEntries[idx + 1]; } |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
131 |
private int getCp(int idx) { return cpEntries[idx + 2]; } |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
132 |
|
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
133 |
public static CharacterName getInstance() { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
134 |
SoftReference<CharacterName> ref = refCharName; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
135 |
CharacterName cname = null; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
136 |
if (ref == null || (cname = ref.get()) == null) { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
137 |
cname = new CharacterName(); |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
138 |
refCharName = new SoftReference<>(cname); |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
139 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
140 |
return cname; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
141 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
142 |
|
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
143 |
public String getName(int cp) { |
5610 | 144 |
int off = 0; |
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
145 |
int bk = bkIndices[cp >> 8]; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
146 |
if (bk == -1 || (off = lookup[(bk << 8) + (cp & 0xff)]) == 0) |
5610 | 147 |
return null; |
11275 | 148 |
@SuppressWarnings("deprecation") |
149 |
String result = new String(strPool, 0, off >>> 8, off & 0xff); // ASCII |
|
150 |
return result; |
|
5610 | 151 |
} |
35783
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
152 |
|
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
153 |
public int getCodePoint(String name) { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
154 |
byte[] bname = name.getBytes(java.nio.charset.StandardCharsets.ISO_8859_1); |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
155 |
int hsh = hashN(bname, 0, bname.length); |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
156 |
int idx = hsIndices[(hsh & 0x7fffffff) % hsIndices.length]; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
157 |
while (idx != -1) { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
158 |
if (getCpHash(idx) == hsh) { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
159 |
int cp = getCp(idx); |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
160 |
int off = -1; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
161 |
int bk = bkIndices[cp >> 8]; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
162 |
if (bk != -1 && (off = lookup[(bk << 8) + (cp & 0xff)]) != 0) { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
163 |
int len = off & 0xff; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
164 |
off = off >>> 8; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
165 |
if (bname.length == len) { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
166 |
int i = 0; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
167 |
while (i < len && bname[i] == strPool[off++]) { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
168 |
i++; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
169 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
170 |
if (i == len) { |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
171 |
return cp; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
172 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
173 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
174 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
175 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
176 |
idx = getCpNext(idx); |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
177 |
} |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
178 |
return -1; |
2690535d72cc
7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
29986
diff
changeset
|
179 |
} |
5610 | 180 |
} |