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

@ -624,8 +624,8 @@ public class CharSequenceUtil {
/** /**
* 按照断言除去字符串头尾部的断言为真的字符如果字符串是{@code null}依然返回{@code null} * 按照断言除去字符串头尾部的断言为真的字符如果字符串是{@code null}依然返回{@code null}
* *
* @param str 要处理的字符串 * @param str 要处理的字符串
* @param mode {@code -1}表示trimStart{@code 0}表示trim全部 {@code 1}表示trimEnd * @param mode {@code -1}表示trimStart{@code 0}表示trim全部 {@code 1}表示trimEnd
* @param predicate 断言是否过掉字符返回{@code true}表述过滤掉{@code false}表示不过滤 * @param predicate 断言是否过掉字符返回{@code true}表述过滤掉{@code false}表示不过滤
* @return 除去指定字符后的的字符串如果原字串为{@code null}则返回{@code null} * @return 除去指定字符后的的字符串如果原字串为{@code null}则返回{@code null}
* @since 5.7.4 * @since 5.7.4
@ -4033,39 +4033,45 @@ 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);
}
//后一个为大写按照专有名词对待如_AB -> _AB
} else if (Character.isLowerCase(preChar)) {
// 前一个为小写
sb.append(symbol); 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 { } else {
// 前后都为非大写按照新词对待 // 首字母需要根据后一个判断是否转为小写
if (null != preChar && symbol != preChar) { if (null == nextChar || Character.isLowerCase(nextChar)) {
// 前一个非连接符补充连接符 // 普通首字母大写如Abc -> abc
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);
}
// 小写或符号
sb.append(c);
} }
sb.append(c);
} }
return sb.toString(); return sb.toString();
} }
@ -4272,7 +4278,7 @@ public class CharSequenceUtil {
/** /**
* conjunction 为分隔符将多个对象转换为字符串 * conjunction 为分隔符将多个对象转换为字符串
* *
* @param <T> 元素类型 * @param <T> 元素类型
* @param conjunction 分隔符 * @param conjunction 分隔符
* @param iterable 集合 * @param iterable 集合
* @return 连接后的字符串 * @return 连接后的字符串
@ -4295,7 +4301,7 @@ public class CharSequenceUtil {
if (StrUtil.isBlank(value)) { if (StrUtil.isBlank(value)) {
return false; return false;
} }
for (int i = value.length(); --i >= 0;) { for (int i = value.length(); --i >= 0; ) {
if (false == matcher.match(value.charAt(i))) { if (false == matcher.match(value.charAt(i))) {
return false; return false;
} }

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;
@ -211,9 +218,13 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
if (ArrayUtil.isEmpty(src) || srcPos > src.length || length <= 0) { if (ArrayUtil.isEmpty(src) || srcPos > src.length || length <= 0) {
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;
} }
@ -288,8 +306,11 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
if (start >= end) { if (start >= end) {
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() {
//字符 //字符