Skip to content

Commit 52d82d1

Browse files
committed
Applying Benjamin Bentmann's patch from CODEC-65, which changes the various encoders to use a locale when lowercasing. The encoders all assume the English locale, so when running in non English locales the default toLowerCase sometimes does not work as the encoder expects.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@651874 13f79535-47bb-0310-9956-ffa450edef68
1 parent 9c0cabe commit 52d82d1

5 files changed

Lines changed: 40 additions & 6 deletions

File tree

src/java/org/apache/commons/codec/language/Caverphone.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public String caverphone(String txt) {
5656
}
5757

5858
// 1. Convert to lowercase
59-
txt = txt.toLowerCase();
59+
txt = txt.toLowerCase(java.util.Locale.ENGLISH);
6060

6161
// 2. Remove anything not A-Z
6262
txt = txt.replaceAll("[^a-z]", "");

src/java/org/apache/commons/codec/language/DoubleMetaphone.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ private String cleanInput(String input) {
923923
if (input.length() == 0) {
924924
return null;
925925
}
926-
return input.toUpperCase();
926+
return input.toUpperCase(java.util.Locale.ENGLISH);
927927
}
928928

929929
/**

src/java/org/apache/commons/codec/language/Metaphone.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ public String metaphone(String txt) {
8585
}
8686
// single character is itself
8787
if (txt.length() == 1) {
88-
return txt.toUpperCase() ;
88+
return txt.toUpperCase(java.util.Locale.ENGLISH) ;
8989
}
9090

91-
char[] inwd = txt.toUpperCase().toCharArray() ;
91+
char[] inwd = txt.toUpperCase(java.util.Locale.ENGLISH).toCharArray() ;
9292

9393
StringBuffer local = new StringBuffer(40); // manipulate
9494
StringBuffer code = new StringBuffer(10) ; // output

src/java/org/apache/commons/codec/language/SoundexUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ static String clean(String str) {
5050
}
5151
}
5252
if (count == len) {
53-
return str.toUpperCase();
53+
return str.toUpperCase(java.util.Locale.ENGLISH);
5454
}
55-
return new String(chars, 0, count).toUpperCase();
55+
return new String(chars, 0, count).toUpperCase(java.util.Locale.ENGLISH);
5656
}
5757

5858
/**

src/test/org/apache/commons/codec/StringEncoderAbstractTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.apache.commons.codec;
1919

20+
import java.util.Locale;
21+
2022
import junit.framework.TestCase;
2123

2224
/**
@@ -63,4 +65,36 @@ public void testEncodeWithInvalidObject() throws Exception {
6365
assertTrue( "An exception was not thrown when we tried to encode " +
6466
"a Float object", exceptionThrown );
6567
}
68+
69+
public void testLocaleIndependence() throws Exception {
70+
StringEncoder encoder = makeEncoder();
71+
72+
String[] data = { "I", "i", };
73+
74+
Locale orig = Locale.getDefault();
75+
Locale[] locales = { Locale.ENGLISH, new Locale("tr"), Locale.getDefault() };
76+
77+
try {
78+
for (int i = 0; i < data.length; i++) {
79+
String ref = null;
80+
for (int j = 0; j < locales.length; j++) {
81+
Locale.setDefault(locales[j]);
82+
if (j <= 0) {
83+
ref = encoder.encode(data[i]);
84+
} else {
85+
String cur = null;
86+
try {
87+
cur = encoder.encode(data[i]);
88+
} catch (Exception e) {
89+
fail(Locale.getDefault().toString() + ": " + e.getMessage());
90+
}
91+
assertEquals(Locale.getDefault().toString() + ": ", ref, cur);
92+
}
93+
}
94+
}
95+
} finally {
96+
Locale.setDefault(orig);
97+
}
98+
}
99+
66100
}

0 commit comments

Comments
 (0)