Author: bayard Date: Thu Apr 18 08:15:47 2013 New Revision: 1469220 URL: http://svn.apache.org/r1469220 Log: Adding Dmitry Katsubo's patch from LANG-846, providing CharSequenceUtils.regionMatches with a proper green implementation instead of inefficiently converting to Strings
Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/CharSequenceUtils.java commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsStartsEndsWithTest.java Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/CharSequenceUtils.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/CharSequenceUtils.java?rev=1469220&r1=1469219&r2=1469220&view=diff ============================================================================== --- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/CharSequenceUtils.java (original) +++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/CharSequenceUtils.java Thu Apr 18 08:15:47 2013 @@ -189,9 +189,30 @@ public class CharSequenceUtils { if (cs instanceof String && substring instanceof String) { return ((String) cs).regionMatches(ignoreCase, thisStart, (String) substring, start, length); } else { - // TODO: Implement rather than convert to String - return cs.toString().regionMatches(ignoreCase, thisStart, substring.toString(), start, length); + int index1 = thisStart; + int index2 = start; + int tmpLen = length; + + while (tmpLen-- > 0) { + char c1 = cs.charAt(index1++); + char c2 = substring.charAt(index2++); + + if (c1 == c2) { + continue; + } + + if (!ignoreCase) { + return false; + } + + // The same check as in String.regionMatches(): + if (Character.toUpperCase(c1) != Character.toUpperCase(c2) + && Character.toLowerCase(c1) != Character.toLowerCase(c2)) { + return false; + } + } + + return true; } } - } Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsStartsEndsWithTest.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsStartsEndsWithTest.java?rev=1469220&r1=1469219&r2=1469220&view=diff ============================================================================== --- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsStartsEndsWithTest.java (original) +++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/StringUtilsStartsEndsWithTest.java Thu Apr 18 08:15:47 2013 @@ -120,6 +120,13 @@ public class StringUtilsStartsEndsWithTe assertTrue("endsWith(FOOBAR, BAR)", StringUtils.endsWith(FOOBAR, BAR)); assertFalse("endsWith(foobar, BAR)", StringUtils.endsWith(foobar, BAR)); assertFalse("endsWith(FOOBAR, bar)", StringUtils.endsWith(FOOBAR, bar)); + + // "alpha,beta,gamma,delta".endsWith("delta") + assertTrue("endsWith(\u03B1\u03B2\u03B3\u03B4, \u03B4)", + StringUtils.endsWith("\u03B1\u03B2\u03B3\u03B4", "\u03B4")); + // "alpha,beta,gamma,delta".endsWith("gamma,DELTA") + assertFalse("endsWith(\u03B1\u03B2\u03B3\u03B4, \u03B3\u0394)", + StringUtils.endsWith("\u03B1\u03B2\u03B3\u03B4", "\u03B3\u0394")); } /** @@ -149,6 +156,13 @@ public class StringUtilsStartsEndsWithTe assertTrue(StringUtils.endsWithIgnoreCase("abcdef", "def")); assertTrue(StringUtils.endsWithIgnoreCase("ABCDEF", "def")); assertFalse(StringUtils.endsWithIgnoreCase("ABCDEF", "cde")); + + // "alpha,beta,gamma,delta".endsWith("DELTA") + assertTrue("endsWith(\u03B1\u03B2\u03B3\u03B4, \u0394)", + StringUtils.endsWithIgnoreCase("\u03B1\u03B2\u03B3\u03B4", "\u0394")); + // "alpha,beta,gamma,delta".endsWith("GAMMA") + assertFalse("endsWith(\u03B1\u03B2\u03B3\u03B4, \u0393)", + StringUtils.endsWithIgnoreCase("\u03B1\u03B2\u03B3\u03B4", "\u0393")); } @Test