Skip to content

Commit

Permalink
fix brief bug
Browse files Browse the repository at this point in the history
  • Loading branch information
looly committed Jul 18, 2021
1 parent d8f393f commit 56e107e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

-------------------------------------------------------------------------------------------------------------

# 5.7.5 (2021-07-18)
# 5.7.5 (2021-07-19)

### 🐣新特性
* 【core 】 DateUtil增加ceiling重载,可选是否归零毫秒
Expand All @@ -24,6 +24,7 @@
* 【core 】 修复DateUtil.format格式化Instant报错问题(issue#I40CY2@Gitee)
* 【core 】 修复StrUtil.toUnderlineCase大写问题(issue#I40CGS@Gitee)
* 【jwt 】 修复JWT.validate报错问题(issue#I40MR2@Gitee)
* 【core 】 修复StrUtil.brief越界问题

-------------------------------------------------------------------------------------------------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4245,22 +4245,42 @@ public static String concat(boolean isNullToEmpty, CharSequence... strs) {
/**
* 将给定字符串,变成 "xxx...xxx" 形式的字符串
*
* <ul>
* <li>abcdef 5 -》 a...f</li>
* <li>abcdef 4 -》 a..f</li>
* <li>abcdef 3 -》 a.f</li>
* <li>abcdef 2 -》 a.</li>
* <li>abcdef 1 -》 a</li>
* </ul>
*
* @param str 字符串
* @param maxLength 最大长度
* @param maxLength 结果的最大长度
* @return 截取后的字符串
*/
public static String brief(CharSequence str, int maxLength) {
if (null == str) {
return null;
}
if (maxLength <= 0 || str.length() <= maxLength) {
final int strLength = str.length();
if (maxLength <= 0 || strLength <= maxLength) {
return str.toString();
}
int w = maxLength / 2;
int l = str.length() + 3;

// since 5.7.5,特殊长度
switch (maxLength){
case 1:
return String.valueOf(str.charAt(0));
case 2:
return str.charAt(0) + ".";
case 3:
return str.charAt(0) + "." + str.charAt(str.length() - 1);
}

final int w = maxLength / 2;
final String str2 = str.toString();
return format("{}...{}", str2.substring(0, maxLength - w), str2.substring(l - w));
return format("{}...{}",
str2.substring(0, maxLength - w),
str2.substring(strLength - w + 3));
}

/**
Expand Down
32 changes: 32 additions & 0 deletions hutool-core/src/test/java/cn/hutool/core/util/StrUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,38 @@ public void briefTest() {
Assert.assertEquals(brief.length(), maxLength);
}

@Test
public void briefTest2() {
String str = "123";
int maxLength = 3;
String brief = StrUtil.brief(str, maxLength);
Assert.assertEquals("123", brief);

maxLength = 2;
brief = StrUtil.brief(str, maxLength);
Assert.assertEquals("1.", brief);

maxLength = 1;
brief = StrUtil.brief(str, maxLength);
Assert.assertEquals("1", brief);
}

@Test
public void briefTest3() {
String str = "123abc";
int maxLength = 3;
String brief = StrUtil.brief(str, maxLength);
Assert.assertEquals("1.c", brief);

maxLength = 2;
brief = StrUtil.brief(str, maxLength);
Assert.assertEquals("1.", brief);

maxLength = 1;
brief = StrUtil.brief(str, maxLength);
Assert.assertEquals("1", brief);
}

@Test
public void filterTest() {
final String filterNumber = StrUtil.filter("hutool678", CharUtil::isNumber);
Expand Down

0 comments on commit 56e107e

Please sign in to comment.