LocaleUtils.toLocale(String) cannot parse four segments

This commit is contained in:
Gary Gregory 2024-07-19 11:35:33 -04:00
parent 8f579b4f30
commit 0f4dee8451
3 changed files with 15 additions and 11 deletions

View File

@ -49,6 +49,7 @@ The <action> type attribute can be add,update,fix,remove.
<!-- FIX --> <!-- FIX -->
<action type="fix" dev="ggregory" due-to="Gary Gregory">Reimplement StopWatch internals to use java.time.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Reimplement StopWatch internals to use java.time.</action>
<action issue="LANG-1745" type="fix" dev="ggregory" due-to="Wang Hailong, Gary Gregory">RandomStringUtils.random() with a negative character index should throw IllegalArgumentException.</action> <action issue="LANG-1745" type="fix" dev="ggregory" due-to="Wang Hailong, Gary Gregory">RandomStringUtils.random() with a negative character index should throw IllegalArgumentException.</action>
<action issue="LANG-1741" type="fix" dev="ggregory" due-to="Wang Hailong, Gary Gregory">LocaleUtils.toLocale(String) cannot parse four segments.</action>
<!-- ADD --> <!-- ADD -->
<action type="add" dev="ggregory" due-to="Gary Gregory">Add StopWatch.getSplitDuration() and deprecate getSplitTime().</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add StopWatch.getSplitDuration() and deprecate getSplitTime().</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add StopWatch.getStartInstant() and deprecate getStartTime().</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add StopWatch.getStartInstant() and deprecate getStartTime().</action>

View File

@ -249,28 +249,30 @@ public class LocaleUtils {
} }
/** /**
* Tries to parse a locale from the given String. * Tries to parse a Locale from the given String.
* <p>
* See {@Link Locale} for the format.
* </p>
* *
* @param str the String to parse a locale from. * @param str the String to parse as a Locale.
* @return a Locale instance parsed from the given String. * @return a Locale parsed from the given String.
* @throws IllegalArgumentException if the given String can not be parsed. * @throws IllegalArgumentException if the given String can not be parsed.
* @see Locale
*/ */
private static Locale parseLocale(final String str) { private static Locale parseLocale(final String str) {
if (isISO639LanguageCode(str)) { if (isISO639LanguageCode(str)) {
return new Locale(str); return new Locale(str);
} }
final int limit = 3;
final String[] segments = str.indexOf(UNDERSCORE) != -1 final char separator = str.indexOf(UNDERSCORE) != -1 ? UNDERSCORE : DASH;
? str.split(String.valueOf(UNDERSCORE), -1) final String[] segments = str.split(String.valueOf(separator), 3);
: str.split(String.valueOf(DASH), -1);
final String language = segments[0]; final String language = segments[0];
if (segments.length == 2) { if (segments.length == 2) {
final String country = segments[1]; final String country = segments[1];
if (isISO639LanguageCode(language) && isISO3166CountryCode(country) || if (isISO639LanguageCode(language) && isISO3166CountryCode(country) || isNumericAreaCode(country)) {
isNumericAreaCode(country)) {
return new Locale(language, country); return new Locale(language, country);
} }
} else if (segments.length == 3) { } else if (segments.length == limit) {
final String country = segments[1]; final String country = segments[1];
final String variant = segments[2]; final String variant = segments[2];
if (isISO639LanguageCode(language) && if (isISO639LanguageCode(language) &&

View File

@ -538,9 +538,10 @@ public class LocaleUtilsTest extends AbstractLangTest {
assertValidToLocale("us_EN_a", "us", "EN", "A"); assertValidToLocale("us_EN_a", "us", "EN", "A");
assertValidToLocale("us_EN_SFsafdFDsdfF", "us", "EN", "SFSAFDFDSDFF"); assertValidToLocale("us_EN_SFsafdFDsdfF", "us", "EN", "SFSAFDFDSDFF");
} }
assertThrows(IllegalArgumentException.class, () -> LocaleUtils.toLocale("us_EN-a"), "Should fail as no consistent delimiter"); assertThrows(IllegalArgumentException.class, () -> LocaleUtils.toLocale("us_EN-a"), "Should fail as no consistent delimiter");
assertThrows(IllegalArgumentException.class, () -> LocaleUtils.toLocale("uu_UU_"), "Must be 3, 5 or 7+ in length"); assertThrows(IllegalArgumentException.class, () -> LocaleUtils.toLocale("uu_UU_"), "Must be 3, 5 or 7+ in length");
// LANG-1741
assertEquals(new Locale("en", "001", "US_POSIX"), LocaleUtils.toLocale("en_001_US_POSIX"));
} }
/** /**