Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/java.base/share/classes/java/lang/ConditionalSpecialCasing.java
67794 views
1
/*
2
* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
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
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
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
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package java.lang;
27
28
import java.text.BreakIterator;
29
import java.util.HashSet;
30
import java.util.Hashtable;
31
import java.util.Iterator;
32
import java.util.Locale;
33
import sun.text.Normalizer;
34
35
36
/**
37
* This is a utility class for {@code String.toLowerCase()} and
38
* {@code String.toUpperCase()}, that handles special casing with
39
* conditions. In other words, it handles the mappings with conditions
40
* that are defined in
41
* <a href="http://www.unicode.org/Public/UNIDATA/SpecialCasing.txt">Special
42
* Casing Properties</a> file.
43
* <p>
44
* Note that the unconditional case mappings (including 1:M mappings)
45
* are handled in {@code Character.toLower/UpperCase()}.
46
*/
47
final class ConditionalSpecialCasing {
48
49
// context conditions.
50
static final int FINAL_CASED = 1;
51
static final int AFTER_SOFT_DOTTED = 2;
52
static final int MORE_ABOVE = 3;
53
static final int AFTER_I = 4;
54
static final int NOT_BEFORE_DOT = 5;
55
56
// combining class definitions
57
static final int COMBINING_CLASS_ABOVE = 230;
58
59
// Special case mapping entries
60
static Entry[] entry = {
61
//# ================================================================================
62
//# Conditional mappings
63
//# ================================================================================
64
new Entry(0x03A3, new char[]{0x03C2}, new char[]{0x03A3}, null, FINAL_CASED), // # GREEK CAPITAL LETTER SIGMA
65
new Entry(0x0130, new char[]{0x0069, 0x0307}, new char[]{0x0130}, null, 0), // # LATIN CAPITAL LETTER I WITH DOT ABOVE
66
67
//# ================================================================================
68
//# Locale-sensitive mappings
69
//# ================================================================================
70
//# Lithuanian
71
new Entry(0x0307, new char[]{0x0307}, new char[]{}, "lt", AFTER_SOFT_DOTTED), // # COMBINING DOT ABOVE
72
new Entry(0x0049, new char[]{0x0069, 0x0307}, new char[]{0x0049}, "lt", MORE_ABOVE), // # LATIN CAPITAL LETTER I
73
new Entry(0x004A, new char[]{0x006A, 0x0307}, new char[]{0x004A}, "lt", MORE_ABOVE), // # LATIN CAPITAL LETTER J
74
new Entry(0x012E, new char[]{0x012F, 0x0307}, new char[]{0x012E}, "lt", MORE_ABOVE), // # LATIN CAPITAL LETTER I WITH OGONEK
75
new Entry(0x00CC, new char[]{0x0069, 0x0307, 0x0300}, new char[]{0x00CC}, "lt", 0), // # LATIN CAPITAL LETTER I WITH GRAVE
76
new Entry(0x00CD, new char[]{0x0069, 0x0307, 0x0301}, new char[]{0x00CD}, "lt", 0), // # LATIN CAPITAL LETTER I WITH ACUTE
77
new Entry(0x0128, new char[]{0x0069, 0x0307, 0x0303}, new char[]{0x0128}, "lt", 0), // # LATIN CAPITAL LETTER I WITH TILDE
78
79
//# ================================================================================
80
//# Turkish and Azeri
81
new Entry(0x0130, new char[]{0x0069}, new char[]{0x0130}, "tr", 0), // # LATIN CAPITAL LETTER I WITH DOT ABOVE
82
new Entry(0x0130, new char[]{0x0069}, new char[]{0x0130}, "az", 0), // # LATIN CAPITAL LETTER I WITH DOT ABOVE
83
new Entry(0x0307, new char[]{}, new char[]{0x0307}, "tr", AFTER_I), // # COMBINING DOT ABOVE
84
new Entry(0x0307, new char[]{}, new char[]{0x0307}, "az", AFTER_I), // # COMBINING DOT ABOVE
85
new Entry(0x0049, new char[]{0x0131}, new char[]{0x0049}, "tr", NOT_BEFORE_DOT), // # LATIN CAPITAL LETTER I
86
new Entry(0x0049, new char[]{0x0131}, new char[]{0x0049}, "az", NOT_BEFORE_DOT), // # LATIN CAPITAL LETTER I
87
new Entry(0x0069, new char[]{0x0069}, new char[]{0x0130}, "tr", 0), // # LATIN SMALL LETTER I
88
new Entry(0x0069, new char[]{0x0069}, new char[]{0x0130}, "az", 0) // # LATIN SMALL LETTER I
89
};
90
91
// A hash table that contains the above entries
92
static Hashtable<Integer, HashSet<Entry>> entryTable = new Hashtable<>();
93
static {
94
// create hashtable from the entry
95
for (Entry cur : entry) {
96
Integer cp = cur.getCodePoint();
97
HashSet<Entry> set = entryTable.get(cp);
98
if (set == null) {
99
set = new HashSet<>();
100
entryTable.put(cp, set);
101
}
102
set.add(cur);
103
}
104
}
105
106
static int toLowerCaseEx(String src, int index, Locale locale) {
107
char[] result = lookUpTable(src, index, locale, true);
108
109
if (result != null) {
110
if (result.length == 1) {
111
return result[0];
112
} else {
113
return Character.ERROR;
114
}
115
} else {
116
// default to Character class' one
117
return Character.toLowerCase(src.codePointAt(index));
118
}
119
}
120
121
static int toUpperCaseEx(String src, int index, Locale locale) {
122
char[] result = lookUpTable(src, index, locale, false);
123
124
if (result != null) {
125
if (result.length == 1) {
126
return result[0];
127
} else {
128
return Character.ERROR;
129
}
130
} else {
131
// default to Character class' one
132
return Character.toUpperCaseEx(src.codePointAt(index));
133
}
134
}
135
136
static char[] toLowerCaseCharArray(String src, int index, Locale locale) {
137
return lookUpTable(src, index, locale, true);
138
}
139
140
static char[] toUpperCaseCharArray(String src, int index, Locale locale) {
141
char[] result = lookUpTable(src, index, locale, false);
142
if (result != null) {
143
return result;
144
} else {
145
return Character.toUpperCaseCharArray(src.codePointAt(index));
146
}
147
}
148
149
private static char[] lookUpTable(String src, int index, Locale locale, boolean bLowerCasing) {
150
HashSet<Entry> set = entryTable.get(src.codePointAt(index));
151
char[] ret = null;
152
153
if (set != null) {
154
Iterator<Entry> iter = set.iterator();
155
String currentLang = locale.getLanguage();
156
while (iter.hasNext()) {
157
Entry entry = iter.next();
158
String conditionLang = entry.getLanguage();
159
if (((conditionLang == null) || (conditionLang.equals(currentLang))) &&
160
isConditionMet(src, index, locale, entry.getCondition())) {
161
ret = bLowerCasing ? entry.getLowerCase() : entry.getUpperCase();
162
if (conditionLang != null) {
163
break;
164
}
165
}
166
}
167
}
168
169
return ret;
170
}
171
172
private static boolean isConditionMet(String src, int index, Locale locale, int condition) {
173
return switch (condition) {
174
case FINAL_CASED -> isFinalCased(src, index, locale);
175
case AFTER_SOFT_DOTTED -> isAfterSoftDotted(src, index);
176
case MORE_ABOVE -> isMoreAbove(src, index);
177
case AFTER_I -> isAfterI(src, index);
178
case NOT_BEFORE_DOT -> !isBeforeDot(src, index);
179
default -> true;
180
};
181
}
182
183
/**
184
* Implements the "Final_Cased" condition
185
*
186
* Specification: Within the closest word boundaries containing C, there is a cased
187
* letter before C, and there is no cased letter after C.
188
*
189
* Regular Expression:
190
* Before C: [{cased==true}][{wordBoundary!=true}]*
191
* After C: !([{wordBoundary!=true}]*[{cased}])
192
*/
193
private static boolean isFinalCased(String src, int index, Locale locale) {
194
BreakIterator wordBoundary = BreakIterator.getWordInstance(locale);
195
wordBoundary.setText(src);
196
int ch;
197
198
// Look for a preceding 'cased' letter
199
for (int i = index; (i >= 0) && !wordBoundary.isBoundary(i);
200
i -= Character.charCount(ch)) {
201
202
ch = src.codePointBefore(i);
203
if (isCased(ch)) {
204
205
int len = src.length();
206
// Check that there is no 'cased' letter after the index
207
for (i = index + Character.charCount(src.codePointAt(index));
208
(i < len) && !wordBoundary.isBoundary(i);
209
i += Character.charCount(ch)) {
210
211
ch = src.codePointAt(i);
212
if (isCased(ch)) {
213
return false;
214
}
215
}
216
217
return true;
218
}
219
}
220
221
return false;
222
}
223
224
/**
225
* Implements the "After_I" condition
226
*
227
* Specification: The last preceding base character was an uppercase I,
228
* and there is no intervening combining character class 230 (ABOVE).
229
*
230
* Regular Expression:
231
* Before C: [I]([{cc!=230}&{cc!=0}])*
232
*/
233
private static boolean isAfterI(String src, int index) {
234
int ch;
235
int cc;
236
237
// Look for the last preceding base character
238
for (int i = index; i > 0; i -= Character.charCount(ch)) {
239
240
ch = src.codePointBefore(i);
241
242
if (ch == 'I') {
243
return true;
244
} else {
245
cc = Normalizer.getCombiningClass(ch);
246
if ((cc == 0) || (cc == COMBINING_CLASS_ABOVE)) {
247
return false;
248
}
249
}
250
}
251
252
return false;
253
}
254
255
/**
256
* Implements the "After_Soft_Dotted" condition
257
*
258
* Specification: The last preceding character with combining class
259
* of zero before C was Soft_Dotted, and there is no intervening
260
* combining character class 230 (ABOVE).
261
*
262
* Regular Expression:
263
* Before C: [{Soft_Dotted==true}]([{cc!=230}&{cc!=0}])*
264
*/
265
private static boolean isAfterSoftDotted(String src, int index) {
266
int ch;
267
int cc;
268
269
// Look for the last preceding character
270
for (int i = index; i > 0; i -= Character.charCount(ch)) {
271
272
ch = src.codePointBefore(i);
273
274
if (isSoftDotted(ch)) {
275
return true;
276
} else {
277
cc = Normalizer.getCombiningClass(ch);
278
if ((cc == 0) || (cc == COMBINING_CLASS_ABOVE)) {
279
return false;
280
}
281
}
282
}
283
284
return false;
285
}
286
287
/**
288
* Implements the "More_Above" condition
289
*
290
* Specification: C is followed by one or more characters of combining
291
* class 230 (ABOVE) in the combining character sequence.
292
*
293
* Regular Expression:
294
* After C: [{cc!=0}]*[{cc==230}]
295
*/
296
private static boolean isMoreAbove(String src, int index) {
297
int ch;
298
int cc;
299
int len = src.length();
300
301
// Look for a following ABOVE combining class character
302
for (int i = index + Character.charCount(src.codePointAt(index));
303
i < len; i += Character.charCount(ch)) {
304
305
ch = src.codePointAt(i);
306
cc = Normalizer.getCombiningClass(ch);
307
308
if (cc == COMBINING_CLASS_ABOVE) {
309
return true;
310
} else if (cc == 0) {
311
return false;
312
}
313
}
314
315
return false;
316
}
317
318
/**
319
* Implements the "Before_Dot" condition
320
*
321
* Specification: C is followed by {@code U+0307 COMBINING DOT ABOVE}.
322
* Any sequence of characters with a combining class that is
323
* neither 0 nor 230 may intervene between the current character
324
* and the combining dot above.
325
*
326
* Regular Expression:
327
* After C: ([{cc!=230}&{cc!=0}])*[\u0307]
328
*/
329
private static boolean isBeforeDot(String src, int index) {
330
int ch;
331
int cc;
332
int len = src.length();
333
334
// Look for a following COMBINING DOT ABOVE
335
for (int i = index + Character.charCount(src.codePointAt(index));
336
i < len; i += Character.charCount(ch)) {
337
338
ch = src.codePointAt(i);
339
340
if (ch == '\u0307') {
341
return true;
342
} else {
343
cc = Normalizer.getCombiningClass(ch);
344
if ((cc == 0) || (cc == COMBINING_CLASS_ABOVE)) {
345
return false;
346
}
347
}
348
}
349
350
return false;
351
}
352
353
/**
354
* Examines whether a character is 'cased'.
355
*
356
* A character C is defined to be 'cased' if and only if at least one of
357
* following are true for C: uppercase==true, or lowercase==true, or
358
* general_category==titlecase_letter.
359
*
360
* The uppercase and lowercase property values are specified in the data
361
* file DerivedCoreProperties.txt in the Unicode Character Database.
362
*/
363
private static boolean isCased(int ch) {
364
int type = Character.getType(ch);
365
if (type == Character.LOWERCASE_LETTER ||
366
type == Character.UPPERCASE_LETTER ||
367
type == Character.TITLECASE_LETTER) {
368
return true;
369
} else {
370
// Check for Other_Lowercase and Other_Uppercase
371
//
372
if ((ch >= 0x02B0) && (ch <= 0x02B8)) {
373
// MODIFIER LETTER SMALL H..MODIFIER LETTER SMALL Y
374
return true;
375
} else if ((ch >= 0x02C0) && (ch <= 0x02C1)) {
376
// MODIFIER LETTER GLOTTAL STOP..MODIFIER LETTER REVERSED GLOTTAL STOP
377
return true;
378
} else if ((ch >= 0x02E0) && (ch <= 0x02E4)) {
379
// MODIFIER LETTER SMALL GAMMA..MODIFIER LETTER SMALL REVERSED GLOTTAL STOP
380
return true;
381
} else if (ch == 0x0345) {
382
// COMBINING GREEK YPOGEGRAMMENI
383
return true;
384
} else if (ch == 0x037A) {
385
// GREEK YPOGEGRAMMENI
386
return true;
387
} else if ((ch >= 0x1D2C) && (ch <= 0x1D61)) {
388
// MODIFIER LETTER CAPITAL A..MODIFIER LETTER SMALL CHI
389
return true;
390
} else if ((ch >= 0x2160) && (ch <= 0x217F)) {
391
// ROMAN NUMERAL ONE..ROMAN NUMERAL ONE THOUSAND
392
// SMALL ROMAN NUMERAL ONE..SMALL ROMAN NUMERAL ONE THOUSAND
393
return true;
394
} else if ((ch >= 0x24B6) && (ch <= 0x24E9)) {
395
// CIRCLED LATIN CAPITAL LETTER A..CIRCLED LATIN CAPITAL LETTER Z
396
// CIRCLED LATIN SMALL LETTER A..CIRCLED LATIN SMALL LETTER Z
397
return true;
398
} else {
399
return false;
400
}
401
}
402
}
403
404
private static boolean isSoftDotted(int ch) {
405
switch (ch) {
406
case 0x0069: // Soft_Dotted # L& LATIN SMALL LETTER I
407
case 0x006A: // Soft_Dotted # L& LATIN SMALL LETTER J
408
case 0x012F: // Soft_Dotted # L& LATIN SMALL LETTER I WITH OGONEK
409
case 0x0268: // Soft_Dotted # L& LATIN SMALL LETTER I WITH STROKE
410
case 0x0456: // Soft_Dotted # L& CYRILLIC SMALL LETTER BYELORUSSIAN-UKRAINIAN I
411
case 0x0458: // Soft_Dotted # L& CYRILLIC SMALL LETTER JE
412
case 0x1D62: // Soft_Dotted # L& LATIN SUBSCRIPT SMALL LETTER I
413
case 0x1E2D: // Soft_Dotted # L& LATIN SMALL LETTER I WITH TILDE BELOW
414
case 0x1ECB: // Soft_Dotted # L& LATIN SMALL LETTER I WITH DOT BELOW
415
case 0x2071: // Soft_Dotted # L& SUPERSCRIPT LATIN SMALL LETTER I
416
return true;
417
default:
418
return false;
419
}
420
}
421
422
/**
423
* An internal class that represents an entry in the Special Casing Properties.
424
*/
425
static class Entry {
426
int ch;
427
char [] lower;
428
char [] upper;
429
String lang;
430
int condition;
431
432
Entry(int ch, char[] lower, char[] upper, String lang, int condition) {
433
this.ch = ch;
434
this.lower = lower;
435
this.upper = upper;
436
this.lang = lang;
437
this.condition = condition;
438
}
439
440
int getCodePoint() {
441
return ch;
442
}
443
444
char[] getLowerCase() {
445
return lower;
446
}
447
448
char[] getUpperCase() {
449
return upper;
450
}
451
452
String getLanguage() {
453
return lang;
454
}
455
456
int getCondition() {
457
return condition;
458
}
459
}
460
}
461
462