You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2009/10/22 07:46:33 UTC

svn commit: r828317 - in /commons/proper/lang/trunk/src: java/org/apache/commons/lang/StringUtils.java test/org/apache/commons/lang/StringUtilsEqualsIndexOfTest.java

Author: bayard
Date: Thu Oct 22 05:46:33 2009
New Revision: 828317

URL: http://svn.apache.org/viewvc?rev=828317&view=rev
Log:
Applying the final part of Benjamin Bentmann's patch to LANG-432, improving our handling of case-insensitive Strings

Modified:
    commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java
    commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsEqualsIndexOfTest.java

Modified: commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java?rev=828317&r1=828316&r2=828317&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java (original)
+++ commons/proper/lang/trunk/src/java/org/apache/commons/lang/StringUtils.java Thu Oct 22 05:46:33 2009
@@ -1020,8 +1020,8 @@
 
     /**
      * <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 @@
         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

Modified: commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsEqualsIndexOfTest.java
URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsEqualsIndexOfTest.java?rev=828317&r1=828316&r2=828317&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsEqualsIndexOfTest.java (original)
+++ commons/proper/lang/trunk/src/test/org/apache/commons/lang/StringUtilsEqualsIndexOfTest.java Thu Oct 22 05:46:33 2009
@@ -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 @@
         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));