Applying the final part of Benjamin Bentmann's patch to LANG-432, improving our handling of case-insensitive Strings

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@828317 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2009-10-22 05:46:33 +00:00
parent 7d3fbbfd43
commit 711e204e73
2 changed files with 47 additions and 4 deletions

View File

@ -1020,8 +1020,8 @@ public class StringUtils {
/**
* <p>Checks if String contains a search String irrespective of case,
* handling <code>null</code>. This method uses
* {@link #contains(String, String)}.</p>
* handling <code>null</code>. Case-insensitivity is defined as by
* {@link String#equalsIgnoreCase(String)}.
*
* <p>A <code>null</code> String will return <code>false</code>.</p>
*
@ -1045,7 +1045,14 @@ public class StringUtils {
if (str == null || searchStr == null) {
return false;
}
return contains(str.toUpperCase(), searchStr.toUpperCase());
int len = searchStr.length();
int max = str.length() - len;
for (int i = 0; i <= max; i++) {
if (str.regionMatches(true, i, searchStr, 0, len)) {
return true;
}
}
return false;
}
// IndexOfAny chars

View File

@ -16,6 +16,8 @@
*/
package org.apache.commons.lang;
import java.util.Locale;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
@ -311,7 +313,41 @@ public class StringUtilsEqualsIndexOfTest extends TestCase {
assertTrue(StringUtils.containsIgnoreCase("xabcz", "ABC"));
}
//-----------------------------------------------------------------------
public void testContainsIgnoreCase_LocaleIndependence() {
Locale orig = Locale.getDefault();
Locale[] locales = { Locale.ENGLISH, new Locale("tr"), Locale.getDefault() };
String[][] tdata = {
{ "i", "I" },
{ "I", "i" },
{ "\u03C2", "\u03C3" },
{ "\u03A3", "\u03C2" },
{ "\u03A3", "\u03C3" },
};
String[][] fdata = {
{ "\u00DF", "SS" },
};
try {
for (int i = 0; i < locales.length; i++) {
Locale.setDefault(locales[i]);
for (int j = 0; j < tdata.length; j++) {
assertTrue(Locale.getDefault() + ": " + j + " " + tdata[j][0] + " " + tdata[j][1], StringUtils
.containsIgnoreCase(tdata[j][0], tdata[j][1]));
}
for (int j = 0; j < fdata.length; j++) {
assertFalse(Locale.getDefault() + ": " + j + " " + fdata[j][0] + " " + fdata[j][1], StringUtils
.containsIgnoreCase(fdata[j][0], fdata[j][1]));
}
}
} finally {
Locale.setDefault(orig);
}
}
// -----------------------------------------------------------------------
public void testIndexOfAny_StringStringarray() {
assertEquals(-1, StringUtils.indexOfAny(null, (String[]) null));
assertEquals(-1, StringUtils.indexOfAny(null, FOOBAR_SUB_ARRAY));