< prev index next >
src/java.base/share/classes/sun/util/locale/LocaleMatcher.java
Print this page
*** 363,373 ****
// Special language range ("*") is ignored in lookup.
if (range.equals("*")) {
continue;
}
! String rangeForRegex = range.replaceAll("\\x2A", "\\\\p{Alnum}*");
while (rangeForRegex.length() > 0) {
for (String tag : tags) {
tag = tag.toLowerCase(Locale.ROOT);
if (tag.matches(rangeForRegex)
&& !shouldIgnoreLookupMatch(zeroRanges, tag)) {
--- 363,373 ----
// Special language range ("*") is ignored in lookup.
if (range.equals("*")) {
continue;
}
! String rangeForRegex = range.replace("*", "\\p{Alnum}*");
while (rangeForRegex.length() > 0) {
for (String tag : tags) {
tag = tag.toLowerCase(Locale.ROOT);
if (tag.matches(rangeForRegex)
&& !shouldIgnoreLookupMatch(zeroRanges, tag)) {
*** 397,407 ****
// Special language range ("*") is ignored in lookup.
if (range.equals("*")) {
continue;
}
! String rangeForRegex = range.replaceAll("\\x2A", "\\\\p{Alnum}*");
while (rangeForRegex.length() > 0) {
if (tag.matches(rangeForRegex)) {
return true;
}
// Truncate from the end....
--- 397,407 ----
// Special language range ("*") is ignored in lookup.
if (range.equals("*")) {
continue;
}
! String rangeForRegex = range.replace("*", "\\p{Alnum}*");
while (rangeForRegex.length() > 0) {
if (tag.matches(rangeForRegex)) {
return true;
}
// Truncate from the end....
*** 445,455 ****
return -1; // no q=0 range exists
}
public static List<LanguageRange> parse(String ranges) {
! ranges = ranges.replaceAll(" ", "").toLowerCase(Locale.ROOT);
if (ranges.startsWith("accept-language:")) {
ranges = ranges.substring(16); // delete unnecessary prefix
}
String[] langRanges = ranges.split(",");
--- 445,455 ----
return -1; // no q=0 range exists
}
public static List<LanguageRange> parse(String ranges) {
! ranges = ranges.replace(" ", "").toLowerCase(Locale.ROOT);
if (ranges.startsWith("accept-language:")) {
ranges = ranges.substring(16); // delete unnecessary prefix
}
String[] langRanges = ranges.split(",");
*** 534,558 ****
}
return list;
}
private static String[] getEquivalentsForLanguage(String range) {
String r = range;
while (r.length() > 0) {
if (LocaleEquivalentMaps.singleEquivMap.containsKey(r)) {
String equiv = LocaleEquivalentMaps.singleEquivMap.get(r);
// Return immediately for performance if the first matching
// subtag is found.
! return new String[] {range.replaceFirst(r, equiv)};
} else if (LocaleEquivalentMaps.multiEquivsMap.containsKey(r)) {
String[] equivs = LocaleEquivalentMaps.multiEquivsMap.get(r);
for (int i = 0; i < equivs.length; i++) {
! equivs[i] = range.replaceFirst(r, equivs[i]);
}
! return equivs;
}
// Truncate the last subtag simply.
int index = r.lastIndexOf('-');
if (index == -1) {
--- 534,576 ----
}
return list;
}
+ /**
+ * A faster alternative approach to String.replaceFirst(), if the given
+ * string is a literal String, not a regex.
+ */
+ private static String replaceFirstSubStringMatch(String range,
+ String substr, String replacement) {
+ int pos = range.indexOf(substr);
+ if (pos == -1) {
+ return range;
+ } else {
+ return range.substring(0, pos) + replacement
+ + range.substring(pos + substr.length());
+ }
+ }
+
private static String[] getEquivalentsForLanguage(String range) {
String r = range;
while (r.length() > 0) {
if (LocaleEquivalentMaps.singleEquivMap.containsKey(r)) {
String equiv = LocaleEquivalentMaps.singleEquivMap.get(r);
// Return immediately for performance if the first matching
// subtag is found.
! return new String[]{replaceFirstSubStringMatch(range,
! r, equiv)};
} else if (LocaleEquivalentMaps.multiEquivsMap.containsKey(r)) {
String[] equivs = LocaleEquivalentMaps.multiEquivsMap.get(r);
+ String[] result = new String[equivs.length];
for (int i = 0; i < equivs.length; i++) {
! result[i] = replaceFirstSubStringMatch(range,
! r, equivs[i]);
}
! return result;
}
// Truncate the last subtag simply.
int index = r.lastIndexOf('-');
if (index == -1) {
*** 576,586 ****
continue;
}
int len = index + subtag.length();
if (range.length() == len || range.charAt(len) == '-') {
! return range.replaceFirst(subtag, LocaleEquivalentMaps.regionVariantEquivMap.get(subtag));
}
}
}
return null;
--- 594,606 ----
continue;
}
int len = index + subtag.length();
if (range.length() == len || range.charAt(len) == '-') {
! return replaceFirstSubStringMatch(range, subtag,
! LocaleEquivalentMaps.regionVariantEquivMap
! .get(subtag));
}
}
}
return null;
< prev index next >