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 * 【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
------------------------------------------------------------------------------------------------------------- -------------------------------------------------------------------------------------------------------------

View File

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

View File

@ -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);
} }
// 小写或符号 // 小写或符号

View File

@ -10,7 +10,6 @@ import java.util.List;
* 字符串工具类单元测试 * 字符串工具类单元测试
* *
* @author Looly * @author Looly
*
*/ */
public class StrUtilTest { public class StrUtilTest {
@ -39,7 +38,7 @@ public class StrUtilTest {
public void cutTest() { public void cutTest() {
String str = "aaabbbcccdddaadfdfsdfsdf0"; String str = "aaabbbcccdddaadfdfsdfsdf0";
String[] cut = StrUtil.cut(str, 4); String[] cut = StrUtil.cut(str, 4);
Assert.assertArrayEquals(new String[] { "aaab", "bbcc", "cddd", "aadf", "dfsd", "fsdf", "0" }, cut); Assert.assertArrayEquals(new String[]{"aaab", "bbcc", "cddd", "aadf", "dfsd", "fsdf", "0"}, cut);
} }
@Test @Test
@ -59,20 +58,20 @@ public class StrUtilTest {
public void splitToLongTest() { public void splitToLongTest() {
String str = "1,2,3,4, 5"; String str = "1,2,3,4, 5";
long[] longArray = StrUtil.splitToLong(str, ','); long[] longArray = StrUtil.splitToLong(str, ',');
Assert.assertArrayEquals(new long[] { 1, 2, 3, 4, 5 }, longArray); Assert.assertArrayEquals(new long[]{1, 2, 3, 4, 5}, longArray);
longArray = StrUtil.splitToLong(str, ","); longArray = StrUtil.splitToLong(str, ",");
Assert.assertArrayEquals(new long[] { 1, 2, 3, 4, 5 }, longArray); Assert.assertArrayEquals(new long[]{1, 2, 3, 4, 5}, longArray);
} }
@Test @Test
public void splitToIntTest() { public void splitToIntTest() {
String str = "1,2,3,4, 5"; String str = "1,2,3,4, 5";
int[] intArray = StrUtil.splitToInt(str, ','); int[] intArray = StrUtil.splitToInt(str, ',');
Assert.assertArrayEquals(new int[] { 1, 2, 3, 4, 5 }, intArray); Assert.assertArrayEquals(new int[]{1, 2, 3, 4, 5}, intArray);
intArray = StrUtil.splitToInt(str, ","); intArray = StrUtil.splitToInt(str, ",");
Assert.assertArrayEquals(new int[] { 1, 2, 3, 4, 5 }, intArray); Assert.assertArrayEquals(new int[]{1, 2, 3, 4, 5}, intArray);
} }
@Test @Test
@ -103,8 +102,8 @@ public class StrUtilTest {
strip = StrUtil.strip(str, null, "567"); strip = StrUtil.strip(str, null, "567");
Assert.assertEquals("abcd123", strip); Assert.assertEquals("abcd123", strip);
Assert.assertEquals("", StrUtil.strip("a","a")); Assert.assertEquals("", StrUtil.strip("a", "a"));
Assert.assertEquals("", StrUtil.strip("a","a", "b")); Assert.assertEquals("", StrUtil.strip("a", "a", "b"));
} }
@Test @Test
@ -190,7 +189,7 @@ public class StrUtilTest {
@Test @Test
public void replaceTest4() { public void replaceTest4() {
String a = "1039"; String a = "1039";
String result = StrUtil.padPre(a,8,"0"); //在字符串1039前补4个0 String result = StrUtil.padPre(a, 8, "0"); //在字符串1039前补4个0
Assert.assertEquals("00001039", result); Assert.assertEquals("00001039", result);
} }
@ -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
@ -421,13 +410,13 @@ public class StrUtilTest {
@Test @Test
public void subBetweenAllTest() { public void subBetweenAllTest() {
Assert.assertArrayEquals(new String[]{"yz","abc"},StrUtil.subBetweenAll("saho[yz]fdsadp[abc]a","[","]")); Assert.assertArrayEquals(new String[]{"yz", "abc"}, StrUtil.subBetweenAll("saho[yz]fdsadp[abc]a", "[", "]"));
Assert.assertArrayEquals(new String[]{"abc"}, StrUtil.subBetweenAll("saho[yzfdsadp[abc]a]","[","]")); Assert.assertArrayEquals(new String[]{"abc"}, StrUtil.subBetweenAll("saho[yzfdsadp[abc]a]", "[", "]"));
Assert.assertArrayEquals(new String[]{"abc", "abc"}, StrUtil.subBetweenAll("yabczyabcz","y","z")); Assert.assertArrayEquals(new String[]{"abc", "abc"}, StrUtil.subBetweenAll("yabczyabcz", "y", "z"));
Assert.assertArrayEquals(new String[0], StrUtil.subBetweenAll(null,"y","z")); Assert.assertArrayEquals(new String[0], StrUtil.subBetweenAll(null, "y", "z"));
Assert.assertArrayEquals(new String[0], StrUtil.subBetweenAll("","y","z")); Assert.assertArrayEquals(new String[0], StrUtil.subBetweenAll("", "y", "z"));
Assert.assertArrayEquals(new String[0], StrUtil.subBetweenAll("abc",null,"z")); Assert.assertArrayEquals(new String[0], StrUtil.subBetweenAll("abc", null, "z"));
Assert.assertArrayEquals(new String[0], StrUtil.subBetweenAll("abc","y",null)); Assert.assertArrayEquals(new String[0], StrUtil.subBetweenAll("abc", "y", null));
} }
@Test @Test
@ -436,15 +425,15 @@ public class StrUtilTest {
String src1 = "/* \n* hutool */ asdas /* \n* hutool */"; String src1 = "/* \n* hutool */ asdas /* \n* hutool */";
String src2 = "/ * hutool */ asdas / * hutool */"; String src2 = "/ * hutool */ asdas / * hutool */";
String[] results1 = StrUtil.subBetweenAll(src1,"/**","*/"); String[] results1 = StrUtil.subBetweenAll(src1, "/**", "*/");
Assert.assertEquals(0, results1.length); Assert.assertEquals(0, results1.length);
String[] results2 = StrUtil.subBetweenAll(src2,"/*","*/"); String[] results2 = StrUtil.subBetweenAll(src2, "/*", "*/");
Assert.assertEquals(0, results2.length); Assert.assertEquals(0, results2.length);
} }
@Test @Test
public void briefTest(){ public void briefTest() {
String str = RandomUtil.randomString(1000); String str = RandomUtil.randomString(1000);
int maxLength = RandomUtil.randomInt(1000); int maxLength = RandomUtil.randomInt(1000);
String brief = StrUtil.brief(str, maxLength); String brief = StrUtil.brief(str, maxLength);
@ -460,7 +449,7 @@ public class StrUtilTest {
} }
@Test @Test
public void wrapAllTest(){ public void wrapAllTest() {
String[] strings = StrUtil.wrapAll("`", "`", StrUtil.splitToArray("1,2,3,4", ',')); String[] strings = StrUtil.wrapAll("`", "`", StrUtil.splitToArray("1,2,3,4", ','));
Assert.assertEquals("[`1`, `2`, `3`, `4`]", StrUtil.utf8Str(strings)); Assert.assertEquals("[`1`, `2`, `3`, `4`]", StrUtil.utf8Str(strings));

View File

@ -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() {
// 在不忽略大小写的情况下f2fac都不匹配
ObjA o2 = JSONUtil.toBean(jsonStr, ObjA.class);
Assert.assertNull(o2.getFAC());
Assert.assertNull(o2.getF2());
}
@Test
public void testToBeanIgnoreCase() {
// 在忽略大小写的情况下f2fac都匹配
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