fix SIOOBE in StringUtil#startsWith

This commit is contained in:
Daniil Ovchinnikov
2018-06-28 17:23:19 +03:00
parent a730a20121
commit c4155aa622
2 changed files with 34 additions and 1 deletions
@@ -1835,6 +1835,7 @@ public class StringUtil extends StringUtilRt {
@Contract(pure = true)
public static boolean startsWith(@NotNull CharSequence text, int startIndex, @NotNull CharSequence prefix) {
if (startIndex < 0) return false;
int l1 = text.length() - startIndex;
int l2 = prefix.length();
if (l1 < l2) return false;
@@ -1,4 +1,4 @@
// Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.util.text;
import com.intellij.openapi.util.Comparing;
@@ -684,4 +684,36 @@ public class StringUtilTest {
assertFalse(StringUtil.isShortNameOf("x.y.zzz", "zz"));
assertFalse(StringUtil.isShortNameOf("x", "a.b.x"));
}
@Test
public void testStartsWith() {
assertTrue(StringUtil.startsWith("abcdefgh", 5, "fgh"));
assertTrue(StringUtil.startsWith("abcdefgh", 2, "cde"));
assertTrue(StringUtil.startsWith("abcdefgh", 0, "abc"));
assertTrue(StringUtil.startsWith("abcdefgh", 0, "abcdefgh"));
assertFalse(StringUtil.startsWith("abcdefgh", 5, "cde"));
assertFalse(StringUtil.startsWith("abcdefgh", -100, ""));
assertFalse(StringUtil.startsWith("abcdefgh", -1, ""));
assertTrue(StringUtil.startsWith("abcdefgh", 0, ""));
assertTrue(StringUtil.startsWith("abcdefgh", 4, ""));
assertTrue(StringUtil.startsWith("abcdefgh", 7, ""));
assertTrue(StringUtil.startsWith("abcdefgh", 8, ""));
assertFalse(StringUtil.startsWith("abcdefgh", 9, ""));
assertFalse(StringUtil.startsWith("abcdefgh", 100, ""));
assertFalse(StringUtil.startsWith("", -100, ""));
assertFalse(StringUtil.startsWith("", -1, ""));
assertTrue(StringUtil.startsWith("", 0, ""));
assertFalse(StringUtil.startsWith("", 1, ""));
assertFalse(StringUtil.startsWith("", 100, ""));
assertFalse(StringUtil.startsWith("ab", -100, "abcdefgh"));
assertFalse(StringUtil.startsWith("ab", -1, "abcdefgh"));
assertFalse(StringUtil.startsWith("ab", 0, "abcdefgh"));
assertFalse(StringUtil.startsWith("ab", 1, "abcdefgh"));
assertFalse(StringUtil.startsWith("ab", 2, "abcdefgh"));
assertFalse(StringUtil.startsWith("ab", 3, "abcdefgh"));
assertFalse(StringUtil.startsWith("ab", 100, "abcdefgh"));
}
}