From 2449716921c1be81f456c845ed138164dd198156 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E8=89=AF?= <841369634@qq.com> Date: Wed, 5 Jan 2022 15:11:09 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20`CharSequenceUtil.brief`?= =?UTF-8?q?=20=E6=96=B9=E6=B3=95=E5=AD=97=E7=AC=A6=E4=B8=B2=E8=B6=8A?= =?UTF-8?q?=E7=95=8C=EF=BC=8C=E4=BB=A5=E5=8F=8AmaxLength=E9=83=A8=E5=88=86?= =?UTF-8?q?=E5=80=BC=E6=97=B6=EF=BC=8C=E7=BB=93=E6=9E=9C=E4=B8=8E=E9=A2=84?= =?UTF-8?q?=E6=9C=9F=E4=B8=8D=E7=AC=A6=E7=9A=84BUG=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/hutool/core/text/CharSequenceUtil.java | 43 +++++++++++-------- .../java/cn/hutool/core/util/StrUtilTest.java | 16 +++++-- 2 files changed, 38 insertions(+), 21 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java b/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java index e836c98da..38f56fbd3 100644 --- a/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java +++ b/hutool-core/src/main/java/cn/hutool/core/text/CharSequenceUtil.java @@ -1090,7 +1090,7 @@ public class CharSequenceUtil { /** * 指定范围内查找指定字符 * - * @param text 字符串 + * @param text 字符串 * @param searchChar 被查找的字符 * @param start 起始位置,如果小于0,从0开始查找 * @param end 终止位置,如果超过str.length()则默认查找到字符串末尾 @@ -1207,9 +1207,9 @@ public class CharSequenceUtil { * 指定范围内查找字符串
* fromIndex 为搜索起始位置,从后往前计数 * - * @param text 字符串 + * @param text 字符串 * @param searchStr 需要查找位置的字符串 - * @param from 起始位置,从后往前计数 + * @param from 起始位置,从后往前计数 * @param ignoreCase 是否忽略大小写 * @return 位置 * @since 3.2.1 @@ -2015,9 +2015,9 @@ public class CharSequenceUtil { } if (counterOfDoubleByte % 2 != 0) { - if(halfUp){ + if (halfUp) { len += 1; - }else{ + } else { len -= 1; } } @@ -2486,8 +2486,8 @@ public class CharSequenceUtil { * StrUtil.repeatAndJoin("?", 5, null) = "?????" * * - * @param str 被重复的字符串 - * @param count 数量 + * @param str 被重复的字符串 + * @param count 数量 * @param delimiter 分界符 * @return 连接后的字符串 * @since 4.0.1 @@ -3538,7 +3538,7 @@ public class CharSequenceUtil { final int strLength = str.length(); final int searchStrLength = searchStr.length(); - if(strLength < searchStrLength){ + if (strLength < searchStrLength) { // issue#I4M16G@Gitee return str(str); } @@ -4201,11 +4201,17 @@ public class CharSequenceUtil { * 将给定字符串,变成 "xxx...xxx" 形式的字符串 * * * * @param str 字符串 @@ -4228,14 +4234,17 @@ public class CharSequenceUtil { case 2: return str.charAt(0) + "."; 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(); return format("{}...{}", - str2.substring(0, maxLength - w), - str2.substring(strLength - w + 3)); + str2.substring(0, w1), + str2.substring(strLength - w2)); } /** diff --git a/hutool-core/src/test/java/cn/hutool/core/util/StrUtilTest.java b/hutool-core/src/test/java/cn/hutool/core/util/StrUtilTest.java index e4ee3f2ca..54d37836a 100644 --- a/hutool-core/src/test/java/cn/hutool/core/util/StrUtilTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/util/StrUtilTest.java @@ -529,10 +529,18 @@ public class StrUtilTest { @Test public void briefTest() { - String str = RandomUtil.randomString(1000); - int maxLength = RandomUtil.randomInt(1000); - String brief = StrUtil.brief(str, maxLength); - Assert.assertEquals(brief.length(), maxLength); + // case: 1 至 str.length - 1 + String str = RandomUtil.randomString(RandomUtil.randomInt(1, 100)); + for (int maxLength = 1; maxLength < str.length(); maxLength++) { + String brief = StrUtil.brief(str, 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