49
49
import org.graalvm.collections.EconomicSet;
50
50
import org.graalvm.collections.MapCursor;
51
51
52
+ import com.oracle.truffle.regex.charset.UnicodeProperties.NameMatchingMode;
52
53
import com.oracle.truffle.regex.charset.UnicodePropertyDataDiff.CodePointSetDiff;
53
54
54
55
public class UnicodePropertyData {
55
56
57
+ private record Aliases(
58
+ EconomicMap<String, String> prop,
59
+ EconomicMap<String, String> gc,
60
+ EconomicMap<String, String> sc,
61
+ EconomicMap<String, String> blk) {
62
+
63
+ private Aliases transform(NameMatchingMode nameMatchingMode) {
64
+ return new Aliases(
65
+ transformMap(prop, nameMatchingMode),
66
+ transformMap(gc, nameMatchingMode),
67
+ transformMap(sc, nameMatchingMode),
68
+ transformMap(blk, nameMatchingMode));
69
+ }
70
+
71
+ private static EconomicMap<String, String> transformMap(EconomicMap<String, String> source, NameMatchingMode nameMatchingMode) {
72
+ EconomicMap<String, String> target = EconomicMap.create(source.size());
73
+ MapCursor<String, String> cursor = source.getEntries();
74
+ while (cursor.advance()) {
75
+ String transformedKey = nameMatchingMode.normalize(cursor.getKey());
76
+ assert !target.containsKey(transformedKey) || target.get(transformedKey).equals(cursor.getValue());
77
+ target.put(transformedKey, cursor.getValue());
78
+ }
79
+ return target;
80
+ }
81
+ }
82
+
56
83
private final EconomicMap<String, CodePointSet> properties;
57
84
protected final EconomicMap<String, ClassSetContents> emoji;
58
- protected final EconomicMap<String, String> propAliases;
59
- protected final EconomicMap<String, String> gcAliases;
60
- protected final EconomicMap<String, String> scAliases;
61
- protected final EconomicMap<String, String> blkAliases;
62
85
private ClassSetContents rgiEmoji;
63
- private EconomicMap<String, String> propAliasesCaseInsensitive;
64
- private EconomicMap<String, String> gcAliasesCaseInsensitive;
65
- private EconomicMap<String, String> scAliasesCaseInsensitive;
66
- private EconomicMap<String, String> blkAliasesCaseInsensitive;
86
+ private final Aliases[] aliases = new Aliases[NameMatchingMode.values().length];
67
87
68
88
UnicodePropertyData(
69
89
EconomicMap<String, CodePointSet> properties,
@@ -74,10 +94,7 @@ public class UnicodePropertyData {
74
94
EconomicMap<String, String> blkAliases) {
75
95
this.properties = properties;
76
96
this.emoji = emoji;
77
- this.propAliases = propAliases;
78
- this.gcAliases = gcAliases;
79
- this.scAliases = scAliases;
80
- this.blkAliases = blkAliases;
97
+ aliases[NameMatchingMode.exact.ordinal()] = new Aliases(propAliases, gcAliases, scAliases, blkAliases);
81
98
}
82
99
83
100
CodePointSet retrieveProperty(String propertySpec) {
@@ -118,64 +135,50 @@ protected ClassSetContents getRGIEmoji() {
118
135
return rgiEmoji;
119
136
}
120
137
121
- String lookupPropertyAlias(String alias, boolean caseInsensitive) {
122
- String name = propAliases.get(alias);
123
- if (name == null && caseInsensitive) {
124
- return lookupPropertyAliasCaseInsensitive(alias);
125
- }
126
- return name;
138
+ Aliases getExactAliases() {
139
+ return aliases[NameMatchingMode.exact.ordinal()];
127
140
}
128
141
129
- private String lookupPropertyAliasCaseInsensitive(String alias) {
130
- if (propAliasesCaseInsensitive == null) {
131
- propAliasesCaseInsensitive = createCaseInsensitiveMap(propAliases);
142
+ Aliases getAliases(NameMatchingMode nameMatchingMode) {
143
+ Aliases lookup = aliases[nameMatchingMode.ordinal()];
144
+ if (lookup == null) {
145
+ assert nameMatchingMode != NameMatchingMode.exact;
146
+ lookup = getExactAliases().transform(nameMatchingMode);
147
+ aliases[nameMatchingMode.ordinal()] = lookup;
132
148
}
133
- return propAliasesCaseInsensitive.get(alias.toLowerCase()) ;
149
+ return lookup ;
134
150
}
135
151
136
- String lookupGeneralCategoryAlias (String alias, boolean caseInsensitive ) {
137
- String name = gcAliases .get(alias);
138
- if (name == null && caseInsensitive ) {
139
- return lookupGcAliasCaseInsensitive( alias);
152
+ String lookupPropertyAlias (String alias, NameMatchingMode nameMatchingMode ) {
153
+ String name = getExactAliases().prop .get(alias);
154
+ if (name == null && nameMatchingMode != NameMatchingMode.exact ) {
155
+ return getAliases(nameMatchingMode).prop.get(nameMatchingMode.normalize( alias) );
140
156
}
141
157
return name;
142
158
}
143
159
144
- private String lookupGcAliasCaseInsensitive(String alias) {
145
- if (gcAliasesCaseInsensitive == null) {
146
- gcAliasesCaseInsensitive = createCaseInsensitiveMap(gcAliases);
147
- }
148
- return gcAliasesCaseInsensitive.get(alias.toLowerCase());
149
- }
150
-
151
- String lookupScriptAlias(String alias, boolean caseInsensitive) {
152
- String name = scAliases.get(alias);
153
- if (name == null && caseInsensitive) {
154
- return lookupScAliasCaseInsensitive(alias);
160
+ String lookupGeneralCategoryAlias(String alias, NameMatchingMode nameMatchingMode) {
161
+ String name = getExactAliases().gc.get(alias);
162
+ if (name == null && nameMatchingMode != NameMatchingMode.exact) {
163
+ return getAliases(nameMatchingMode).gc.get(nameMatchingMode.normalize(alias));
155
164
}
156
165
return name;
157
166
}
158
167
159
- private String lookupScAliasCaseInsensitive(String alias) {
160
- if (scAliasesCaseInsensitive == null) {
161
- scAliasesCaseInsensitive = createCaseInsensitiveMap(scAliases);
162
- }
163
- return scAliasesCaseInsensitive.get(alias.toLowerCase());
164
- }
165
-
166
- String lookupBlockAlias(String alias, boolean caseInsensitive) {
167
- String name = blkAliases.get(alias);
168
- if (name == null && caseInsensitive) {
169
- return lookupBlkAliasCaseInsensitive(alias);
168
+ String lookupScriptAlias(String alias, NameMatchingMode nameMatchingMode) {
169
+ String name = getExactAliases().sc.get(alias);
170
+ if (name == null && nameMatchingMode != NameMatchingMode.exact) {
171
+ return getAliases(nameMatchingMode).sc.get(nameMatchingMode.normalize(alias));
170
172
}
171
173
return name;
172
174
}
173
175
174
- private String lookupBlkAliasCaseInsensitive(String alias) {
175
- if (blkAliasesCaseInsensitive == null) {
176
- blkAliasesCaseInsensitive = createCaseInsensitiveMap(blkAliases);
176
+ String lookupBlockAlias(String alias, NameMatchingMode nameMatchingMode) {
177
+ String name = getExactAliases().blk.get(alias);
178
+ if (name == null && nameMatchingMode != NameMatchingMode.exact) {
179
+ return getAliases(nameMatchingMode).blk.get(nameMatchingMode.normalize(alias));
177
180
}
178
- return blkAliasesCaseInsensitive.get(alias.toLowerCase()) ;
181
+ return name ;
179
182
}
180
183
181
184
private static EconomicSet<String> stringSet(String... strings) {
@@ -186,17 +189,6 @@ private static EconomicSet<String> stringSet(String... strings) {
186
189
return set;
187
190
}
188
191
189
- private static EconomicMap<String, String> createCaseInsensitiveMap(EconomicMap<String, String> source) {
190
- EconomicMap<String, String> target = EconomicMap.create(source.size());
191
- MapCursor<String, String> cursor = source.getEntries();
192
- while (cursor.advance()) {
193
- String lowerCaseKey = cursor.getKey().toLowerCase();
194
- assert !target.containsKey(lowerCaseKey);
195
- target.put(lowerCaseKey, cursor.getValue());
196
- }
197
- return target;
198
- }
199
-
200
192
/* GENERATED CODE BEGIN - KEEP THIS MARKER FOR AUTOMATIC UPDATES */
201
193
202
194
private static final EconomicMap<String, String> PROPERTY_ALIASES = EconomicMap.create(124);
0 commit comments