Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LANG-1172: Support dash as a delimiter in locales #766

Merged
merged 3 commits into from
Mar 6, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/main/java/org/apache/commons/lang3/LocaleUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
* @since 2.2
*/
public class LocaleUtils {
private static final char UNDERSCORE = '_';
private static final char DASH = '-';

// class to avoid synchronization (Init on demand)
static class SyncAvoid {
Expand Down Expand Up @@ -248,7 +250,9 @@ private static Locale parseLocale(final String str) {
return new Locale(str);
}

final String[] segments = str.split("_", -1);
final String[] segments = str.indexOf(DASH) != -1
? str.split(String.valueOf(DASH), -1)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it matter that dash is tested before underscore?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's been a while since I wrote this code, but I don't think so. Should be possible to switch them around without issues.

Copy link
Member Author

@c-w c-w Dec 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the unit tests still pass with the comparison swapped. I made the change in 0319d84. It's probably the right thing to optimize for the previous delimiter as that'll be the vast majority of use-cases.

: str.split(String.valueOf(UNDERSCORE), -1);
final String language = segments[0];
if (segments.length == 2) {
final String country = segments[1];
Expand Down Expand Up @@ -289,6 +293,7 @@ public static Locale toLocale(final Locale locale) {
* LocaleUtils.toLocale("") = new Locale("", "")
* LocaleUtils.toLocale("en") = new Locale("en", "")
* LocaleUtils.toLocale("en_GB") = new Locale("en", "GB")
* LocaleUtils.toLocale("en-GB") = new Locale("en", "GB")
* LocaleUtils.toLocale("en_001") = new Locale("en", "001")
* LocaleUtils.toLocale("en_GB_xxx") = new Locale("en", "GB", "xxx") (#)
* </pre>
Expand All @@ -300,7 +305,7 @@ public static Locale toLocale(final Locale locale) {
* <p>This method validates the input strictly.
* The language code must be lowercase.
* The country code must be uppercase.
* The separator must be an underscore.
* The separator must be an underscore or a dash.
* The length must be correct.
* </p>
*
Expand All @@ -325,7 +330,7 @@ public static Locale toLocale(final String str) {
throw new IllegalArgumentException("Invalid locale format: " + str);
}
final char ch0 = str.charAt(0);
if (ch0 == '_') {
if (ch0 == UNDERSCORE || ch0 == DASH) {
if (len < 3) {
throw new IllegalArgumentException("Invalid locale format: " + str);
}
Expand All @@ -340,7 +345,7 @@ public static Locale toLocale(final String str) {
if (len < 5) {
throw new IllegalArgumentException("Invalid locale format: " + str);
}
if (str.charAt(3) != '_') {
if (str.charAt(3) != ch0) {
throw new IllegalArgumentException("Invalid locale format: " + str);
}
return new Locale(StringUtils.EMPTY, str.substring(1, 3), str.substring(4));
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/apache/commons/lang3/LocaleUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,10 @@ public void testToLocale_1Part() {
@Test
public void testToLocale_2Part() {
assertValidToLocale("us_EN", "us", "EN");
assertValidToLocale("us-EN", "us", "EN");
//valid though doesn't exist
assertValidToLocale("us_ZH", "us", "ZH");

assertThrows(
IllegalArgumentException.class, () -> LocaleUtils.toLocale("us-EN"), "Should fail as not underscore");
assertThrows(
IllegalArgumentException.class,
() -> LocaleUtils.toLocale("us_En"),
Expand Down Expand Up @@ -203,6 +202,7 @@ public void testToLocale_2Part() {
@Test
public void testToLocale_3Part() {
assertValidToLocale("us_EN_A", "us", "EN", "A");
assertValidToLocale("us-EN-A", "us", "EN", "A");
// this isn't pretty, but was caused by a jdk bug it seems
// http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4210525
if (SystemUtils.isJavaVersionAtLeast(JAVA_1_4)) {
Expand All @@ -214,7 +214,7 @@ public void testToLocale_3Part() {
}

assertThrows(
IllegalArgumentException.class, () -> LocaleUtils.toLocale("us_EN-a"), "Should fail as not underscore");
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");
}
Expand Down