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.util.*;
import java.util.stream.Collectors;
/**
*
@ -231,62 +230,57 @@ public class Convert4Amis {
* @return
*/
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();
// 存储占位符 位置信息集合
List<String> placeholderList = new ArrayList<>();
// 当前标识
Set<String> 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<String> result = placeholderList.stream().map(s -> s.replaceAll("\\{", "").replaceAll("\\$", "").replaceAll("\\}", "")).collect(Collectors.toSet());
return result;
return placeholderSet;
}
/**

Loading…
Cancel
Save