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 】 增加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
-------------------------------------------------------------------------------------------------------------

View File

@ -4033,40 +4033,46 @@ 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
sb.append(symbol);
if (null != preChar) {
if (symbol == preChar) {
// 前一个为分隔符
if (null == nextChar || Character.isLowerCase(nextChar)) {
//普通首字母大写如_Abb -> _abb
c = Character.toLowerCase(c);
}
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 {
// 前后都为非大写按照新词对待
if (null != preChar && symbol != preChar) {
// 前一个非连接符补充连接符
//前一个为大写
if (null == nextChar || Character.isLowerCase(nextChar)) {
// 普通首字母大写如ABcc -> A_bcc
sb.append(symbol);
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);
// 首字母需要根据后一个判断是否转为小写
if (null == nextChar || Character.isLowerCase(nextChar)) {
// 普通首字母大写如Abc -> abc
c = Character.toLowerCase(c);
}
// 后一个为大写按照专有名词对待如ABC -> ABC
}
}
// 小写或符号
sb.append(c);
}
}
return sb.toString();
}
@ -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;
}

View File

@ -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;

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("_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() {
//字符