From 9da756ee7c3d7caa49ca28a366ac76edbd3dcd1b Mon Sep 17 00:00:00 2001 From: xiaoxiamo <82970607@qq.com> Date: Fri, 12 Jul 2024 14:18:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20SpotBugs=E6=89=AB=E6=8F=8F=E5=88=86?= =?UTF-8?q?=E6=9E=90=EF=BC=88=E4=B8=83=EF=BC=89=EF=BC=9A=E6=9B=B4=E6=94=B9?= =?UTF-8?q?=E6=B8=85=E9=99=A4StringBuilder=E6=96=B9=E5=BC=8F(=E6=9B=B4?= =?UTF-8?q?=E6=94=B9getPlaceholderList()=E6=96=B9=E6=B3=95)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java3y/austin/web/utils/Convert4Amis.java | 66 +++++++++---------- 1 file changed, 30 insertions(+), 36 deletions(-) diff --git a/austin-web/src/main/java/com/java3y/austin/web/utils/Convert4Amis.java b/austin-web/src/main/java/com/java3y/austin/web/utils/Convert4Amis.java index 729ae87..9b7fe52 100644 --- a/austin-web/src/main/java/com/java3y/austin/web/utils/Convert4Amis.java +++ b/austin-web/src/main/java/com/java3y/austin/web/utils/Convert4Amis.java @@ -26,7 +26,6 @@ import me.chanjar.weixin.mp.bean.template.WxMpTemplate; import java.lang.reflect.Field; import java.util.*; -import java.util.stream.Collectors; /** * 【该类的逻辑不用看,没有什么意义】 @@ -231,62 +230,57 @@ public class Convert4Amis { * @return */ public static Set getPlaceholderList(String content) { - char[] textChars = content.toCharArray(); - StringBuilder textSofar = new StringBuilder(); + + // 内容为空,直接返回 + if (content == null || content.isEmpty()) { + return Collections.emptySet(); + } + StringBuilder sb = new StringBuilder(); - // 存储占位符 位置信息集合 - List placeholderList = new ArrayList<>(); - // 当前标识 + Set placeholderSet = new HashSet<>(); int modeTg = IGNORE_TG; - for (int m = 0; m < textChars.length; m++) { - char c = textChars[m]; - textSofar.append(c); + + for (char c : content.toCharArray()) { switch (c) { - case '{': { - modeTg = START_TG; - sb.append(c); - } - break; - case '$': { + case '{': + if (modeTg == IGNORE_TG) { + sb.append(c); + modeTg = START_TG; + } + break; + case '$': if (modeTg == START_TG) { sb.append(c); modeTg = READ_TG; } else { - if (modeTg == READ_TG) { - sb = new StringBuilder(); - modeTg = IGNORE_TG; - } + sb.setLength(0); + modeTg = IGNORE_TG; } - } - break; - case '}': { + break; + case '}': if (modeTg == READ_TG) { - modeTg = IGNORE_TG; sb.append(c); - String str = sb.toString(); - if (StrUtil.isNotEmpty(str)) { - placeholderList.add(str); - textSofar = new StringBuilder(); - } - sb = new StringBuilder(); + String placeholder = sb.toString(); + placeholderSet.add(placeholder.replaceAll("[\\{\\$\\}]", "")); + sb.setLength(0); + modeTg = IGNORE_TG; } else if (modeTg == START_TG) { + sb.setLength(0); modeTg = IGNORE_TG; - sb = new StringBuilder(); } break; - } - default: { + default: if (modeTg == READ_TG) { sb.append(c); } else if (modeTg == START_TG) { + sb.setLength(0); modeTg = IGNORE_TG; - sb = new StringBuilder(); } - } + break; } } - Set result = placeholderList.stream().map(s -> s.replaceAll("\\{", "").replaceAll("\\$", "").replaceAll("\\}", "")).collect(Collectors.toSet()); - return result; + + return placeholderSet; } /**