Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/main/java/org/apache/commons/text/CaseUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.commons.text;

import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

import org.apache.commons.lang3.ArrayUtils;
Expand Down Expand Up @@ -70,7 +71,7 @@ public static String toCamelCase(String str, final boolean capitalizeFirstLetter
if (StringUtils.isEmpty(str)) {
return str;
}
str = str.toLowerCase();
str = str.toLowerCase(Locale.ROOT);
final int strLen = str.length();
final int[] newCodePoints = new int[strLen];
int outOffset = 0;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/apache/commons/text/WordUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.commons.text;

import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.function.Predicate;
import java.util.regex.Matcher;
Expand Down Expand Up @@ -250,7 +251,7 @@ public static String capitalizeFully(String str, final char... delimiters) {
if (StringUtils.isEmpty(str)) {
return str;
}
str = str.toLowerCase();
str = str.toLowerCase(Locale.ROOT);
return capitalize(str, delimiters);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ private static Map<String, StringLookup> parseStringLookups(final String str) {
try {
for (final String lookupName : str.split("[\\s,]+")) {
if (!lookupName.isEmpty()) {
addLookup(DefaultStringLookup.valueOf(lookupName.toUpperCase()), lookupMap);
addLookup(DefaultStringLookup.valueOf(lookupName.toUpperCase(Locale.ROOT)), lookupMap);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hello @dxbjavid
This change is untested, either remove it or create a test for it.
TY

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

added a test for it. testDefaultStringLookupsHolder_givenSingleLookup_localeIndependent feeds a lowercase "file" through the holder under a tr-TR default locale; without Locale.ROOT the upper-case folds the i to dotted İ and valueOf throws, so the test fails on the unpatched code and passes with the fix.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Great, ty.

}
}
} catch (final IllegalArgumentException exc) {
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/org/apache/commons/text/CaseUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.lang.reflect.Modifier;

import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.DefaultLocale;

/**
* Tests {@link CaseUtils} class.
Expand Down Expand Up @@ -77,4 +78,12 @@ void testToCamelCase() {
assertEquals("\uD800\uDF00\uD800\uDF01\uD800\uDF02\uD800\uDF03",
CaseUtils.toCamelCase("\uD800\uDF00\uD800\uDF01\uD800\uDF14\uD800\uDF02\uD800\uDF03", true, '\uD800', '\uDF14'));
}

@Test
@DefaultLocale(language = "tr", country = "TR")
void testToCamelCaseLocaleIndependent() {
// Turkish lower-cases 'I' (U+0049) to dotless 'i' (U+0131), which would otherwise leak into the result.
assertEquals("TipTop", CaseUtils.toCamelCase("TIP.TOP", true, '.'));
assertEquals("toCamelCase", CaseUtils.toCamelCase("TO CAMEL CASE", false, null));
}
}
9 changes: 9 additions & 0 deletions src/test/java/org/apache/commons/text/WordUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.DefaultLocale;

/**
* Tests {@link WordUtils}.
Expand Down Expand Up @@ -124,6 +125,14 @@ void testCapitalizeFully_String() {

}

@Test
@DefaultLocale(language = "tr", country = "TR")
void testCapitalizeFully_LocaleIndependent() {
// Turkish lower-cases 'I' (U+0049) to dotless 'i' (U+0131), which would otherwise leak into the result.
assertEquals("Heli World", WordUtils.capitalizeFully("HELI WORLD"));
assertEquals("I Am Here 123", WordUtils.capitalizeFully("I AM HERE 123"));
}

@Test
void testCapitalizeFully_Text88() {
assertEquals("I am fine now", WordUtils.capitalizeFully("i am fine now", new char[] {}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import javax.xml.XMLConstants;

import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.DefaultLocale;
import org.junitpioneer.jupiter.SetSystemProperty;

/**
Expand Down Expand Up @@ -173,6 +174,16 @@ void testDefaultStringLookupsHolder_givenSingleLookup() {
checkDefaultStringLookupsHolder(props, "base64", StringLookupFactory.KEY_BASE64_ENCODER);
}

@Test
@DefaultLocale(language = "tr", country = "TR")
void testDefaultStringLookupsHolder_givenSingleLookup_localeIndependent() {
// Turkish upper-cases 'i' (U+0069) to dotted 'İ' (U+0130), so "file" would fold to "FİLE"
// and fail to resolve against the DefaultStringLookup enum without Locale.ROOT.
final Properties props = new Properties();
props.setProperty(StringLookupFactory.DEFAULT_STRING_LOOKUPS_PROPERTY, "file");
checkDefaultStringLookupsHolder(props, StringLookupFactory.KEY_FILE);
}

@Test
void testDefaultStringLookupsHolder_givenSingleLookup_weirdString() {
final Properties props = new Properties();
Expand Down
Loading