Adding Dmitry Katsubo's patch from LANG-846, providing CharSequenceUtils.regionMatches with a proper green implementation instead of inefficiently converting to Strings

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1469220 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2013-04-18 08:15:47 +00:00
parent cc7fa1ea05
commit f9acb40b99
2 changed files with 38 additions and 3 deletions

View File

@ -189,9 +189,30 @@ static boolean regionMatches(final CharSequence cs, final boolean ignoreCase, fi
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;
}
}
}

View File

@ -120,6 +120,13 @@ public void testEndsWith() {
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 void testEndsWithIgnoreCase() {
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