修复 CharSequenceUtil.brief 方法字符串越界,以及maxLength部分值时,结果与预期不符的BUG修复

This commit is contained in:
王良 2022-01-05 15:11:09 +08:00
parent cdf105589d
commit 2449716921
2 changed files with 38 additions and 21 deletions

View File

@ -2015,9 +2015,9 @@ public class CharSequenceUtil {
} }
if (counterOfDoubleByte % 2 != 0) { if (counterOfDoubleByte % 2 != 0) {
if(halfUp){ if (halfUp) {
len += 1; len += 1;
}else{ } else {
len -= 1; len -= 1;
} }
} }
@ -3538,7 +3538,7 @@ public class CharSequenceUtil {
final int strLength = str.length(); final int strLength = str.length();
final int searchStrLength = searchStr.length(); final int searchStrLength = searchStr.length();
if(strLength < searchStrLength){ if (strLength < searchStrLength) {
// issue#I4M16G@Gitee // issue#I4M16G@Gitee
return str(str); return str(str);
} }
@ -4201,11 +4201,17 @@ public class CharSequenceUtil {
* 将给定字符串变成 "xxx...xxx" 形式的字符串 * 将给定字符串变成 "xxx...xxx" 形式的字符串
* *
* <ul> * <ul>
* <li>abcdef 5 - a...f</li> * <li>abcdefgh 9 - abcdefgh</li>
* <li>abcdef 4 - a..f</li> * <li>abcdefgh 8 - abcdefgh</li>
* <li>abcdef 3 - a.f</li> * <li>abcdefgh 7 - ab...gh</li>
* <li>abcdef 2 - a.</li> * <li>abcdefgh 6 - ab...h</li>
* <li>abcdef 1 - a</li> * <li>abcdefgh 5 - a...h</li>
* <li>abcdefgh 4 - a..h</li>
* <li>abcdefgh 3 - a.h</li>
* <li>abcdefgh 2 - a.</li>
* <li>abcdefgh 1 - a</li>
* <li>abcdefgh 0 - abcdefgh</li>
* <li>abcdefgh -1 - abcdefgh</li>
* </ul> * </ul>
* *
* @param str 字符串 * @param str 字符串
@ -4228,14 +4234,17 @@ public class CharSequenceUtil {
case 2: case 2:
return str.charAt(0) + "."; return str.charAt(0) + ".";
case 3: case 3:
return str.charAt(0) + "." + str.charAt(str.length() - 1); return str.charAt(0) + "." + str.charAt(strLength - 1);
case 4:
return str.charAt(0) + ".." + str.charAt(strLength - 1);
} }
final int w = maxLength / 2; final int w2 = (maxLength - 3) / 2;
final int w1 = w2 + (maxLength - 3) % 2; // w2 w2 + 1
final String str2 = str.toString(); final String str2 = str.toString();
return format("{}...{}", return format("{}...{}",
str2.substring(0, maxLength - w), str2.substring(0, w1),
str2.substring(strLength - w + 3)); str2.substring(strLength - w2));
} }
/** /**

View File

@ -529,12 +529,20 @@ public class StrUtilTest {
@Test @Test
public void briefTest() { public void briefTest() {
String str = RandomUtil.randomString(1000); // case: 1 str.length - 1
int maxLength = RandomUtil.randomInt(1000); String str = RandomUtil.randomString(RandomUtil.randomInt(1, 100));
for (int maxLength = 1; maxLength < str.length(); maxLength++) {
String brief = StrUtil.brief(str, maxLength); String brief = StrUtil.brief(str, maxLength);
Assert.assertEquals(brief.length(), maxLength); Assert.assertEquals(brief.length(), maxLength);
} }
// case: 不会格式化的值
Assert.assertEquals(str, StrUtil.brief(str, 0));
Assert.assertEquals(str, StrUtil.brief(str, -1));
Assert.assertEquals(str, StrUtil.brief(str, str.length()));
Assert.assertEquals(str, StrUtil.brief(str, str.length() + 1));
}
@Test @Test
public void briefTest2() { public void briefTest2() {
String str = "123"; String str = "123";