This commit is contained in:
Looly 2021-07-14 12:09:34 +08:00
parent ba8033059c
commit 89a3acfa88
4 changed files with 70 additions and 34 deletions

View File

@ -10,12 +10,14 @@
* 【core 】 IterUtil增加firstMatch方法 * 【core 】 IterUtil增加firstMatch方法
* 【core 】 增加NanoId * 【core 】 增加NanoId
* 【core 】 MapBuilder增加put方法pr#367@Gitee * 【core 】 MapBuilder增加put方法pr#367@Gitee
* 【core 】 StrUtil.insert支持负数index
### 🐞Bug修复 ### 🐞Bug修复
* 【core 】 修复FileUtil.normalize处理上级路径的问题issue#I3YPEH@Gitee * 【core 】 修复FileUtil.normalize处理上级路径的问题issue#I3YPEH@Gitee
* 【core 】 修复ClassScanner扫描空包遗漏问题 * 【core 】 修复ClassScanner扫描空包遗漏问题
* 【core 】 修复FastDatePrinter歧义问题pr#366@Gitee * 【core 】 修复FastDatePrinter歧义问题pr#366@Gitee
* 【core 】 修复DateUtil.format格式化Instant报错问题issue#I40CY2@Gitee * 【core 】 修复DateUtil.format格式化Instant报错问题issue#I40CY2@Gitee
* 【core 】 修复StrUtil.toUnderlineCase大写问题issue#I40CGS@Gitee
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------

View File

@ -4033,40 +4033,46 @@ public class CharSequenceUtil {
char c; char c;
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
c = str.charAt(i); c = str.charAt(i);
final Character preChar = (i > 0) ? str.charAt(i - 1) : null;
if (Character.isUpperCase(c)) { 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; final Character nextChar = (i < str.length() - 1) ? str.charAt(i + 1) : null;
if (null != preChar && Character.isUpperCase(preChar)) {
// 前一个字符为大写则按照一个词对待例如AB if (null != preChar) {
sb.append(c); if (symbol == preChar) {
} else if (null != nextChar && (false == Character.isLowerCase(nextChar))) { // 前一个为分隔符
// 后一个为非小写字母按照一个词对待 if (null == nextChar || Character.isLowerCase(nextChar)) {
if (null != preChar && symbol != preChar) { //普通首字母大写如_Abb -> _abb
// 前一个是非大写时按照新词对待加连接符例如xAB c = Character.toLowerCase(c);
sb.append(symbol);
} }
sb.append(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 { } else {
// 前后都为非大写按照新词对待 //前一个为大写
if (null != preChar && symbol != preChar) { if (null == nextChar || Character.isLowerCase(nextChar)) {
// 前一个非连接符补充连接符 // 普通首字母大写如ABcc -> A_bcc
sb.append(symbol); sb.append(symbol);
c = Character.toLowerCase(c);
} }
sb.append(Character.toLowerCase(c)); // 后一个为大写按照专有名词对待如ABC -> ABC
} }
} else { } else {
if (symbol != c // 首字母需要根据后一个判断是否转为小写
&& sb.length() > 0 if (null == nextChar || Character.isLowerCase(nextChar)) {
&& Character.isUpperCase(sb.charAt(-1)) // 普通首字母大写如Abc -> abc
&& Character.isLowerCase(c)) { c = Character.toLowerCase(c);
// 当结果中前一个字母为大写当前为小写(非数字或字符)说明此字符为新词开始连接符也表示新词 }
sb.append(symbol); // 后一个为大写按照专有名词对待如ABC -> ABC
}
} }
// 小写或符号
sb.append(c); sb.append(c);
} }
}
return sb.toString(); return sb.toString();
} }

View File

@ -174,6 +174,13 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
* @return this * @return this
*/ */
public StrBuilder insert(int index, char c) { 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); moveDataAfterIndex(index, 1);
value[index] = c; value[index] = c;
this.position = Math.max(this.position, index) + 1; this.position = Math.max(this.position, index) + 1;
@ -212,8 +219,12 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
return this; return this;
} }
if(index < 0){ if(index < 0){
index = 0; index = this.position + index;
} }
if ((index < 0) || (index > this.position)) {
throw new StringIndexOutOfBoundsException(index);
}
if (srcPos < 0) { if (srcPos < 0) {
srcPos = 0; srcPos = 0;
} else if (srcPos + length > src.length) { } else if (srcPos + length > src.length) {
@ -238,6 +249,13 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
* @return this * @return this
*/ */
public StrBuilder insert(int index, CharSequence csq) { 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) { if (null == csq) {
csq = StrUtil.EMPTY; csq = StrUtil.EMPTY;
} }
@ -289,7 +307,10 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
return this; return this;
} }
if(index < 0){ if(index < 0){
index = 0; index = this.position + index;
}
if ((index < 0) || (index > this.position)) {
throw new StringIndexOutOfBoundsException(index);
} }
final int length = end - start; final int length = end - start;

View File

@ -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("_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("HelloWorld_test", "hello_world_test")
.set("H2", "H2") .set("H2", "H2")
.set("H#case", "H#case") .set("H#case", "H#case")
.forEach((key, value) -> Assert.assertEquals(value, StrUtil.toUnderlineCase(key))); .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 @Test
public void containsAnyTest() { public void containsAnyTest() {
//字符 //字符