From 6b5e9cd02345fb70e1354e079cffdd7739c4c50b Mon Sep 17 00:00:00 2001 From: xiaocairush Date: Sun, 18 Jun 2023 16:37:35 +0800 Subject: [PATCH] =?UTF-8?q?feat;=E4=BC=98=E5=8C=96=E6=9B=BF=E4=BB=A3?= =?UTF-8?q?=E6=95=8F=E6=84=9F=E8=AF=8D=E7=9A=84=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mallchat/common/common/utils/SensitiveWordUtils.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/mallchat-common/src/main/java/com/abin/mallchat/common/common/utils/SensitiveWordUtils.java b/mallchat-common/src/main/java/com/abin/mallchat/common/common/utils/SensitiveWordUtils.java index 1213034..bd09f76 100644 --- a/mallchat-common/src/main/java/com/abin/mallchat/common/common/utils/SensitiveWordUtils.java +++ b/mallchat-common/src/main/java/com/abin/mallchat/common/common/utils/SensitiveWordUtils.java @@ -46,8 +46,11 @@ public final class SensitiveWordUtils { if (StringUtils.isBlank(text)) return text; List matchResults = ac_trie.matches(text); StringBuffer result = new StringBuffer(text); + // matchResults是按照startIndex排序的,因此可以通过不断更新endIndex最大值的方式算出尚未被替代部分 + int endIndex = 0; for (MatchResult matchResult : matchResults) { - replaceBetween(result, matchResult.getStartIndex(), matchResult.getEndIndex()); + endIndex = Math.max(endIndex, matchResult.getEndIndex()); + replaceBetween(result, matchResult.getStartIndex(), endIndex); } return result.toString(); }