mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
fix SIOOBE in StringUtil#startsWith
This commit is contained in:
@@ -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"));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user