LANG-1610: Fix StringUtils.unwrap throws StringIndexOutOfBoundsException (#636)

Co-authored-by: Tony Liang <shuchel@zillowgroup.com>
This commit is contained in:
Tony Liang 2020-11-17 12:11:19 -08:00 committed by GitHub
parent bd8dbbe0be
commit c6f577555b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 2 additions and 1 deletions

View File

@ -9342,7 +9342,7 @@ public class StringUtils {
* @since 3.6 * @since 3.6
*/ */
public static String unwrap(final String str, final String wrapToken) { public static String unwrap(final String str, final String wrapToken) {
if (isEmpty(str) || isEmpty(wrapToken) || str.length() == 1) { if (isEmpty(str) || isEmpty(wrapToken) || str.length() < 2 * wrapToken.length()) {
return str; return str;
} }

View File

@ -3174,6 +3174,7 @@ public class StringUtilsTest {
assertEquals("abc", StringUtils.unwrap("abc", null)); assertEquals("abc", StringUtils.unwrap("abc", null));
assertEquals("abc", StringUtils.unwrap("abc", "")); assertEquals("abc", StringUtils.unwrap("abc", ""));
assertEquals("a", StringUtils.unwrap("a", "a")); assertEquals("a", StringUtils.unwrap("a", "a"));
assertEquals("ababa", StringUtils.unwrap("ababa", "aba"));
assertEquals("", StringUtils.unwrap("aa", "a")); assertEquals("", StringUtils.unwrap("aa", "a"));
assertEquals("abc", StringUtils.unwrap("\'abc\'", "\'")); assertEquals("abc", StringUtils.unwrap("\'abc\'", "\'"));
assertEquals("abc", StringUtils.unwrap("\"abc\"", "\"")); assertEquals("abc", StringUtils.unwrap("\"abc\"", "\""));