From 89a3acfa88dd6e83768bd56c7e02f713c2bcabe6 Mon Sep 17 00:00:00 2001 From: Looly Date: Wed, 14 Jul 2021 12:09:34 +0800 Subject: [PATCH] fix bug --- CHANGELOG.md | 2 + .../cn/hutool/core/text/CharSequenceUtil.java | 64 ++++++++++--------- .../java/cn/hutool/core/text/StrBuilder.java | 29 +++++++-- .../java/cn/hutool/core/util/StrUtilTest.java | 9 ++- 4 files changed, 70 insertions(+), 34 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bdea9025b..8d669594f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,12 +10,14 @@ * 【core 】 IterUtil增加firstMatch方法 * 【core 】 增加NanoId * 【core 】 MapBuilder增加put方法(pr#367@Gitee) +* 【core 】 StrUtil.insert支持负数index ### 🐞Bug修复 * 【core 】 修复FileUtil.normalize处理上级路径的问题(issue#I3YPEH@Gitee) * 【core 】 修复ClassScanner扫描空包遗漏问题 * 【core 】 修复FastDatePrinter歧义问题(pr#366@Gitee) * 【core 】 修复DateUtil.format格式化Instant报错问题(issue#I40CY2@Gitee) +* 【core 】 修复StrUtil.toUnderlineCase大写问题(issue#I40CGS@Gitee) ------------------------------------------------------------------------------------------------------------- diff --git a/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java b/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java index a86d1ef6e..67cb78395 100644 --- a/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java @@ -624,8 +624,8 @@ public class CharSequenceUtil { /** * 按照断言,除去字符串头尾部的断言为真的字符,如果字符串是{@code null},依然返回{@code null}。 * - * @param str 要处理的字符串 - * @param mode {@code -1}表示trimStart,{@code 0}表示trim全部, {@code 1}表示trimEnd + * @param str 要处理的字符串 + * @param mode {@code -1}表示trimStart,{@code 0}表示trim全部, {@code 1}表示trimEnd * @param predicate 断言是否过掉字符,返回{@code true}表述过滤掉,{@code false}表示不过滤 * @return 除去指定字符后的的字符串,如果原字串为{@code null},则返回{@code null} * @since 5.7.4 @@ -4033,39 +4033,45 @@ public class CharSequenceUtil { char c; for (int i = 0; i < length; i++) { c = str.charAt(i); - final Character preChar = (i > 0) ? str.charAt(i - 1) : null; if (Character.isUpperCase(c)) { - // 遇到大写字母处理 + final Character preChar = (i > 0) ? str.charAt(i - 1) : null; final Character nextChar = (i < str.length() - 1) ? str.charAt(i + 1) : null; - if (null != preChar && Character.isUpperCase(preChar)) { - // 前一个字符为大写,则按照一个词对待,例如AB - sb.append(c); - } else if (null != nextChar && (false == Character.isLowerCase(nextChar))) { - // 后一个为非小写字母,按照一个词对待 - if (null != preChar && symbol != preChar) { - // 前一个是非大写时按照新词对待,加连接符,例如xAB + + if (null != preChar) { + if (symbol == preChar) { + // 前一个为分隔符 + if (null == nextChar || Character.isLowerCase(nextChar)) { + //普通首字母大写,如_Abb -> _abb + c = Character.toLowerCase(c); + } + //后一个为大写,按照专有名词对待,如_AB -> _AB + } else if (Character.isLowerCase(preChar)) { + // 前一个为小写 sb.append(symbol); + if (null == nextChar || Character.isLowerCase(nextChar)) { + //普通首字母大写,如aBcc -> a_bcc + c = Character.toLowerCase(c); + } + // 后一个为大写,按照专有名词对待,如aBC -> a_BC + } else { + //前一个为大写 + if (null == nextChar || Character.isLowerCase(nextChar)) { + // 普通首字母大写,如ABcc -> A_bcc + sb.append(symbol); + c = Character.toLowerCase(c); + } + // 后一个为大写,按照专有名词对待,如ABC -> ABC } - sb.append(c); } else { - // 前后都为非大写按照新词对待 - if (null != preChar && symbol != preChar) { - // 前一个非连接符,补充连接符 - sb.append(symbol); + // 首字母,需要根据后一个判断是否转为小写 + if (null == nextChar || Character.isLowerCase(nextChar)) { + // 普通首字母大写,如Abc -> abc + c = Character.toLowerCase(c); } - sb.append(Character.toLowerCase(c)); + // 后一个为大写,按照专有名词对待,如ABC -> ABC } - } else { - if (symbol != c - && sb.length() > 0 - && Character.isUpperCase(sb.charAt(-1)) - && Character.isLowerCase(c)) { - // 当结果中前一个字母为大写,当前为小写(非数字或字符),说明此字符为新词开始(连接符也表示新词) - sb.append(symbol); - } - // 小写或符号 - sb.append(c); } + sb.append(c); } return sb.toString(); } @@ -4272,7 +4278,7 @@ public class CharSequenceUtil { /** * 以 conjunction 为分隔符将多个对象转换为字符串 * - * @param 元素类型 + * @param 元素类型 * @param conjunction 分隔符 * @param iterable 集合 * @return 连接后的字符串 @@ -4295,7 +4301,7 @@ public class CharSequenceUtil { if (StrUtil.isBlank(value)) { return false; } - for (int i = value.length(); --i >= 0;) { + for (int i = value.length(); --i >= 0; ) { if (false == matcher.match(value.charAt(i))) { return false; } diff --git a/hutool-core/src/main/java/cn/hutool/core/text/StrBuilder.java b/hutool-core/src/main/java/cn/hutool/core/text/StrBuilder.java index 94aa03ce5..719f914f6 100644 --- a/hutool-core/src/main/java/cn/hutool/core/text/StrBuilder.java +++ b/hutool-core/src/main/java/cn/hutool/core/text/StrBuilder.java @@ -174,6 +174,13 @@ public class StrBuilder implements CharSequence, Appendable, Serializable { * @return this */ public StrBuilder insert(int index, char c) { + if(index < 0){ + index = this.position + index; + } + if ((index < 0) || (index > this.position)) { + throw new StringIndexOutOfBoundsException(index); + } + moveDataAfterIndex(index, 1); value[index] = c; this.position = Math.max(this.position, index) + 1; @@ -211,9 +218,13 @@ public class StrBuilder implements CharSequence, Appendable, Serializable { if (ArrayUtil.isEmpty(src) || srcPos > src.length || length <= 0) { return this; } - if (index < 0) { - index = 0; + if(index < 0){ + index = this.position + index; } + if ((index < 0) || (index > this.position)) { + throw new StringIndexOutOfBoundsException(index); + } + if (srcPos < 0) { srcPos = 0; } else if (srcPos + length > src.length) { @@ -238,6 +249,13 @@ public class StrBuilder implements CharSequence, Appendable, Serializable { * @return this */ public StrBuilder insert(int index, CharSequence csq) { + if(index < 0){ + index = this.position + index; + } + if ((index < 0) || (index > this.position)) { + throw new StringIndexOutOfBoundsException(index); + } + if (null == csq) { csq = StrUtil.EMPTY; } @@ -288,8 +306,11 @@ public class StrBuilder implements CharSequence, Appendable, Serializable { if (start >= end) { return this; } - if (index < 0) { - index = 0; + if(index < 0){ + index = this.position + index; + } + if ((index < 0) || (index > this.position)) { + throw new StringIndexOutOfBoundsException(index); } final int length = end - start; diff --git a/hutool-core/src/test/java/cn/hutool/core/util/StrUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/StrUtilTest.java index 38571ea49..4e4f7971c 100644 --- a/hutool-core/src/test/java/cn/hutool/core/util/StrUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/StrUtilTest.java @@ -364,13 +364,20 @@ public class StrUtilTest { .set("Table_Test_Of_day", "table_test_of_day") .set("_Table_Test_Of_day_", "_table_test_of_day_") .set("_Table_Test_Of_DAY_", "_table_test_of_DAY_") - .set("_TableTestOfDAYtoday", "_table_test_of_DAY_today") + .set("_TableTestOfDAYToday", "_table_test_of_DAY_today") .set("HelloWorld_test", "hello_world_test") .set("H2", "H2") .set("H#case", "H#case") .forEach((key, value) -> Assert.assertEquals(value, StrUtil.toUnderlineCase(key))); } + @Test + public void toUnderLineCaseTest2() { + Dict.create() + .set("PNLabel", "PN_label") + .forEach((key, value) -> Assert.assertEquals(value, StrUtil.toUnderlineCase(key))); + } + @Test public void containsAnyTest() { //字符