fix StrUtil bug

This commit is contained in:
Looly
2020-09-15 01:00:26 +08:00
parent ddd173a17c
commit d205225cef
5 changed files with 85 additions and 78 deletions

View File

@@ -3,7 +3,7 @@
-------------------------------------------------------------------------------------------------------------
# 5.4.3 (2020-09-13)
# 5.4.3 (2020-09-15)
### 新特性
* 【core 】 使用静态的of方法来new对象pr#177@Gitee
@@ -16,6 +16,7 @@
### Bug修复
* 【core 】 修复Dict.of错误issue#I1UUO5@Gitee
* 【core 】 修复UrlBuilder地址参数问题issue#I1UWCA@Gitee
* 【core 】 修复StrUtil.toSymbolCase转换问题issue#1075@Github
-------------------------------------------------------------------------------------------------------------

View File

@@ -462,6 +462,9 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
@Override
public char charAt(int index) {
if(index < 0){
index = this.position + index;
}
if ((index < 0) || (index > this.position)) {
throw new StringIndexOutOfBoundsException(index);
}

View File

@@ -2633,7 +2633,7 @@ public class StrUtil {
}
final int length = str.length();
final StringBuilder sb = new StringBuilder();
final StrBuilder sb = new StrBuilder();
char c;
for (int i = 0; i < length; i++) {
c = str.charAt(i);
@@ -2642,12 +2642,12 @@ public class StrUtil {
// 遇到大写字母处理
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 && Character.isUpperCase(nextChar)) {
// 后一个为写字母,按照一个词对待
} else if (null != nextChar && (false == Character.isLowerCase(nextChar))) {
// 后一个为非小写字母,按照一个词对待
if (null != preChar && symbol != preChar) {
// 前一个是非大写时按照新词对待,加连接符
// 前一个是非大写时按照新词对待,加连接符例如xAB
sb.append(symbol);
}
sb.append(c);
@@ -2660,8 +2660,11 @@ public class StrUtil {
sb.append(Character.toLowerCase(c));
}
} else {
if (sb.length() > 0 && Character.isUpperCase(sb.charAt(sb.length() - 1)) && symbol != c) {
// 当结果中前一个字母为大写,当前为小写,说明此字符为新词开始(连接符也表示新词)
if (symbol != c
&& sb.length() > 0
&& Character.isUpperCase(sb.charAt(-1))
&& Character.isLowerCase(c)) {
// 当结果中前一个字母为大写,当前为小写(非数字或字符),说明此字符为新词开始(连接符也表示新词)
sb.append(symbol);
}
// 小写或符号

View File

@@ -10,7 +10,6 @@ import java.util.List;
* 字符串工具类单元测试
*
* @author Looly
*
*/
public class StrUtilTest {
@@ -347,25 +346,15 @@ public class StrUtilTest {
@Test
public void toUnderLineCaseTest() {
String str = "Table_Test_Of_day";
String result = StrUtil.toUnderlineCase(str);
Assert.assertEquals("table_test_of_day", result);
String str1 = "_Table_Test_Of_day_";
String result1 = StrUtil.toUnderlineCase(str1);
Assert.assertEquals("_table_test_of_day_", result1);
String str2 = "_Table_Test_Of_DAY_";
String result2 = StrUtil.toUnderlineCase(str2);
Assert.assertEquals("_table_test_of_DAY_", result2);
String str3 = "_TableTestOfDAYtoday";
String result3 = StrUtil.toUnderlineCase(str3);
Assert.assertEquals("_table_test_of_DAY_today", result3);
String str4 = "HelloWorld_test";
String result4 = StrUtil.toUnderlineCase(str4);
Assert.assertEquals("hello_world_test", result4);
Dict.create()
.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("HelloWorld_test", "hello_world_test")
.set("H2", "H2")
.set("H#case", "H#case")
.forEach((key, value) -> Assert.assertEquals(value, StrUtil.toUnderlineCase(key)));
}
@Test

View File

@@ -5,12 +5,23 @@ import org.junit.Assert;
import org.junit.Test;
public class Issue1075Test {
@Test
public void test() {
String s = "{\"f1\":\"f1\",\"F2\":\"f2\",\"fac\":\"fac\"}";
ObjA o2 = JSONUtil.parseObj(s, JSONConfig.create().setIgnoreCase(true)).toBean(ObjA.class);
final String jsonStr = "{\"f1\":\"f1\",\"f2\":\"f2\",\"fac\":\"fac\"}";
@Test
public void testToBean() {
// 在不忽略大小写的情况下f2、fac都不匹配
ObjA o2 = JSONUtil.toBean(jsonStr, ObjA.class);
Assert.assertNull(o2.getFAC());
Assert.assertNull(o2.getF2());
}
@Test
public void testToBeanIgnoreCase() {
// 在忽略大小写的情况下f2、fac都匹配
ObjA o2 = JSONUtil.parseObj(jsonStr, JSONConfig.create().setIgnoreCase(true)).toBean(ObjA.class);
Assert.assertEquals("fac", o2.getFAC());
Assert.assertEquals("f2", o2.getF2());
}
@Data