mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
fix StrUtil bug
This commit is contained in:
parent
ddd173a17c
commit
d205225cef
@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
# 5.4.3 (2020-09-13)
|
# 5.4.3 (2020-09-15)
|
||||||
|
|
||||||
### 新特性
|
### 新特性
|
||||||
* 【core 】 使用静态的of方法来new对象(pr#177@Gitee)
|
* 【core 】 使用静态的of方法来new对象(pr#177@Gitee)
|
||||||
@ -16,6 +16,7 @@
|
|||||||
### Bug修复
|
### Bug修复
|
||||||
* 【core 】 修复Dict.of错误(issue#I1UUO5@Gitee)
|
* 【core 】 修复Dict.of错误(issue#I1UUO5@Gitee)
|
||||||
* 【core 】 修复UrlBuilder地址参数问题(issue#I1UWCA@Gitee)
|
* 【core 】 修复UrlBuilder地址参数问题(issue#I1UWCA@Gitee)
|
||||||
|
* 【core 】 修复StrUtil.toSymbolCase转换问题(issue#1075@Github)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -462,6 +462,9 @@ public class StrBuilder implements CharSequence, Appendable, Serializable {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public char charAt(int index) {
|
public char charAt(int index) {
|
||||||
|
if(index < 0){
|
||||||
|
index = this.position + index;
|
||||||
|
}
|
||||||
if ((index < 0) || (index > this.position)) {
|
if ((index < 0) || (index > this.position)) {
|
||||||
throw new StringIndexOutOfBoundsException(index);
|
throw new StringIndexOutOfBoundsException(index);
|
||||||
}
|
}
|
||||||
|
@ -2633,7 +2633,7 @@ public class StrUtil {
|
|||||||
}
|
}
|
||||||
|
|
||||||
final int length = str.length();
|
final int length = str.length();
|
||||||
final StringBuilder sb = new StringBuilder();
|
final StrBuilder sb = new StrBuilder();
|
||||||
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);
|
||||||
@ -2642,12 +2642,12 @@ public class StrUtil {
|
|||||||
// 遇到大写字母处理
|
// 遇到大写字母处理
|
||||||
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)) {
|
if (null != preChar && Character.isUpperCase(preChar)) {
|
||||||
// 前一个字符为大写,则按照一个词对待
|
// 前一个字符为大写,则按照一个词对待,例如AB
|
||||||
sb.append(c);
|
sb.append(c);
|
||||||
} else if (null != nextChar && Character.isUpperCase(nextChar)) {
|
} else if (null != nextChar && (false == Character.isLowerCase(nextChar))) {
|
||||||
// 后一个为大写字母,按照一个词对待
|
// 后一个为非小写字母,按照一个词对待
|
||||||
if (null != preChar && symbol != preChar) {
|
if (null != preChar && symbol != preChar) {
|
||||||
// 前一个是非大写时按照新词对待,加连接符
|
// 前一个是非大写时按照新词对待,加连接符,例如xAB
|
||||||
sb.append(symbol);
|
sb.append(symbol);
|
||||||
}
|
}
|
||||||
sb.append(c);
|
sb.append(c);
|
||||||
@ -2660,8 +2660,11 @@ public class StrUtil {
|
|||||||
sb.append(Character.toLowerCase(c));
|
sb.append(Character.toLowerCase(c));
|
||||||
}
|
}
|
||||||
} else {
|
} 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);
|
sb.append(symbol);
|
||||||
}
|
}
|
||||||
// 小写或符号
|
// 小写或符号
|
||||||
|
@ -10,7 +10,6 @@ import java.util.List;
|
|||||||
* 字符串工具类单元测试
|
* 字符串工具类单元测试
|
||||||
*
|
*
|
||||||
* @author Looly
|
* @author Looly
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class StrUtilTest {
|
public class StrUtilTest {
|
||||||
|
|
||||||
@ -347,25 +346,15 @@ public class StrUtilTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void toUnderLineCaseTest() {
|
public void toUnderLineCaseTest() {
|
||||||
String str = "Table_Test_Of_day";
|
Dict.create()
|
||||||
String result = StrUtil.toUnderlineCase(str);
|
.set("Table_Test_Of_day", "table_test_of_day")
|
||||||
Assert.assertEquals("table_test_of_day", result);
|
.set("_Table_Test_Of_day_", "_table_test_of_day_")
|
||||||
|
.set("_Table_Test_Of_DAY_", "_table_test_of_DAY_")
|
||||||
String str1 = "_Table_Test_Of_day_";
|
.set("_TableTestOfDAYtoday", "_table_test_of_DAY_today")
|
||||||
String result1 = StrUtil.toUnderlineCase(str1);
|
.set("HelloWorld_test", "hello_world_test")
|
||||||
Assert.assertEquals("_table_test_of_day_", result1);
|
.set("H2", "H2")
|
||||||
|
.set("H#case", "H#case")
|
||||||
String str2 = "_Table_Test_Of_DAY_";
|
.forEach((key, value) -> Assert.assertEquals(value, StrUtil.toUnderlineCase(key)));
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -5,12 +5,23 @@ import org.junit.Assert;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
public class Issue1075Test {
|
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("fac", o2.getFAC());
|
||||||
|
Assert.assertEquals("f2", o2.getF2());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
|
Loading…
x
Reference in New Issue
Block a user