修复 StrMatcher连续变量解析导致的歧义问题(pr#1419@Gitee)

This commit is contained in:
Looly
2025-12-27 11:51:31 +08:00
parent c988de9bc8
commit a1d0921ff8
2 changed files with 19 additions and 1 deletions

View File

@@ -93,8 +93,9 @@ public class StrMatcher {
*
* @param pattern 表达式,使用${XXXX}作为变量占位符
* @return 表达式
* @throws IllegalArgumentException 连续变量不支持,例如:${a}${b}
*/
private static List<String> parse(final String pattern) {
private static List<String> parse(final String pattern) throws IllegalArgumentException {
final List<String> patterns = new ArrayList<>();
final int length = pattern.length();
char c = 0;
@@ -107,6 +108,13 @@ public class StrMatcher {
if (inVar) {
part.append(c);
if ('}' == c) {
if (!patterns.isEmpty()) {
final String lastPart = patterns.get(patterns.size() - 1);
if (StrUtil.isWrap(lastPart, "${", "}")) {
// issue#IDFNF7 连续变量会导致歧义,例如:${a}${b} 不支持
throw new IllegalArgumentException(StrUtil.format("Consecutive variables '{}{}' are not supported", lastPart, part.toString()));
}
}
// 变量结束
inVar = false;
patterns.add(part.toString());

View File

@@ -22,6 +22,8 @@ import org.junit.jupiter.api.Test;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class StrMatcherTest {
@Test
@@ -54,4 +56,12 @@ public class StrMatcherTest {
Assertions.assertEquals("小明", match.get("name"));
Assertions.assertEquals("20", match.get("year"));
}
@Test
public void issueIDFNF7Test() {
assertThrows(
IllegalArgumentException.class,
() -> new StrMatcher("${a}${b}")
);
}
}