Author: markt Date: Tue Jan 16 12:18:30 2018 New Revision: 1821235 URL: http://svn.apache.org/viewvc?rev=1821235&view=rev Log: Fix bug searching for single character Strings (search would always fail) Bug found during comparison of indexOf() implementations between ByteChunk and CharChunk
Modified: tomcat/trunk/java/org/apache/tomcat/util/buf/CharChunk.java tomcat/trunk/test/org/apache/tomcat/util/buf/TestCharChunk.java Modified: tomcat/trunk/java/org/apache/tomcat/util/buf/CharChunk.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/buf/CharChunk.java?rev=1821235&r1=1821234&r2=1821235&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/util/buf/CharChunk.java (original) +++ tomcat/trunk/java/org/apache/tomcat/util/buf/CharChunk.java Tue Jan 16 12:18:30 2018 @@ -615,7 +615,11 @@ public final class CharChunk extends Abs if (buff[i] != first) { continue; } - // found first char, now look for a match + // Special case - search string is a single character + if (srcLen == 1) { + return i - start; + } + // Found first char, now look for a match int myPos = i + 1; for (int srcPos = srcOff + 1; srcPos < srcEnd;) { if (buff[myPos++] != src.charAt(srcPos++)) { Modified: tomcat/trunk/test/org/apache/tomcat/util/buf/TestCharChunk.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/tomcat/util/buf/TestCharChunk.java?rev=1821235&r1=1821234&r2=1821235&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/tomcat/util/buf/TestCharChunk.java (original) +++ tomcat/trunk/test/org/apache/tomcat/util/buf/TestCharChunk.java Tue Jan 16 12:18:30 2018 @@ -36,4 +36,30 @@ public class TestCharChunk { Assert.assertFalse(cc.endsWith("x")); Assert.assertFalse(cc.endsWith("xxtest")); } + + @Test + public void testIndexOf_String() { + char[] chars = "Hello\u00a0world".toCharArray(); + final int len = chars.length; + + CharChunk cc = new CharChunk(); + cc.setChars(chars, 0, len); + + Assert.assertEquals(0, cc.indexOf("Hello", 0, "Hello".length(), 0)); + Assert.assertEquals(2, cc.indexOf("ll", 0, 2, 0)); + Assert.assertEquals(2, cc.indexOf("Hello", 2, 2, 0)); + + Assert.assertEquals(7, cc.indexOf("o", 0, 1, 5)); + + // Does work outside of 0-127 (unlike ByteChunk) + Assert.assertEquals(5, cc.indexOf("\u00a0", 0, 1, 0)); + + cc.setChars(chars, 6, 5); + Assert.assertEquals(1, cc.indexOf("o", 0, 1, 0)); + + cc.setChars(chars, 6, 2); + Assert.assertEquals(0, cc.indexOf("wo", 0, 1, 0)); + Assert.assertEquals(-1, cc.indexOf("d", 0, 1, 0)); + } + } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org