fix: SpotBugs扫描分析(七):更改清除StringBuilder方式(更改getPlaceholderList()方法)

pull/66/head
xiaoxiamo 6 months ago
parent 5c2f39cd49
commit 9da756ee7c

@ -26,7 +26,6 @@ import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.*; import java.util.*;
import java.util.stream.Collectors;
/** /**
* *
@ -231,62 +230,57 @@ public class Convert4Amis {
* @return * @return
*/ */
public static Set<String> getPlaceholderList(String content) { public static Set<String> getPlaceholderList(String content) {
char[] textChars = content.toCharArray();
StringBuilder textSofar = new StringBuilder(); // 内容为空,直接返回
if (content == null || content.isEmpty()) {
return Collections.emptySet();
}
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
// 存储占位符 位置信息集合 Set<String> placeholderSet = new HashSet<>();
List<String> placeholderList = new ArrayList<>();
// 当前标识
int modeTg = IGNORE_TG; int modeTg = IGNORE_TG;
for (int m = 0; m < textChars.length; m++) {
char c = textChars[m]; for (char c : content.toCharArray()) {
textSofar.append(c);
switch (c) { switch (c) {
case '{': { case '{':
modeTg = START_TG; if (modeTg == IGNORE_TG) {
sb.append(c); sb.append(c);
} modeTg = START_TG;
break; }
case '$': { break;
case '$':
if (modeTg == START_TG) { if (modeTg == START_TG) {
sb.append(c); sb.append(c);
modeTg = READ_TG; modeTg = READ_TG;
} else { } else {
if (modeTg == READ_TG) { sb.setLength(0);
sb = new StringBuilder(); modeTg = IGNORE_TG;
modeTg = IGNORE_TG;
}
} }
} break;
break; case '}':
case '}': {
if (modeTg == READ_TG) { if (modeTg == READ_TG) {
modeTg = IGNORE_TG;
sb.append(c); sb.append(c);
String str = sb.toString(); String placeholder = sb.toString();
if (StrUtil.isNotEmpty(str)) { placeholderSet.add(placeholder.replaceAll("[\\{\\$\\}]", ""));
placeholderList.add(str); sb.setLength(0);
textSofar = new StringBuilder(); modeTg = IGNORE_TG;
}
sb = new StringBuilder();
} else if (modeTg == START_TG) { } else if (modeTg == START_TG) {
sb.setLength(0);
modeTg = IGNORE_TG; modeTg = IGNORE_TG;
sb = new StringBuilder();
} }
break; break;
} default:
default: {
if (modeTg == READ_TG) { if (modeTg == READ_TG) {
sb.append(c); sb.append(c);
} else if (modeTg == START_TG) { } else if (modeTg == START_TG) {
sb.setLength(0);
modeTg = IGNORE_TG; modeTg = IGNORE_TG;
sb = new StringBuilder();
} }
} break;
} }
} }
Set<String> result = placeholderList.stream().map(s -> s.replaceAll("\\{", "").replaceAll("\\$", "").replaceAll("\\}", "")).collect(Collectors.toSet());
return result; return placeholderSet;
} }
/** /**

Loading…
Cancel
Save