mirror of
https://gitee.com/dromara/hutool.git
synced 2026-02-09 09:16:26 +08:00
修复 StrMatcher连续变量解析导致的歧义问题(pr#1419@Gitee)
This commit is contained in:
@@ -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());
|
||||
|
||||
@@ -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}")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user