This commit is contained in:
Looly 2022-04-25 16:04:49 +08:00
parent cce26f9bb0
commit 940d1cbe31
3 changed files with 10 additions and 6 deletions

View File

@ -82,7 +82,7 @@ public class StrMatcher {
char c = 0;
char pre;
boolean inVar = false;
StrBuilder part = StrUtil.strBuilder();
StringBuilder part = StrUtil.builder();
for (int i = 0; i < length; i++) {
pre = c;
c = pattern.charAt(i);
@ -92,16 +92,17 @@ public class StrMatcher {
// 变量结束
inVar = false;
patterns.add(part.toString());
part.clear();
part.setLength(0);
}
} else if ('{' == c && '$' == pre) {
// 变量开始
inVar = true;
final String preText = part.subString(0, part.length() - 1);
final String preText = part.substring(0, part.length() - 1);
if (StrUtil.isNotEmpty(preText)) {
patterns.add(preText);
}
part.reset().append(pre).append(c);
part.setLength(0);
part.append(pre).append(c);
} else {
// 普通字符
part.append(c);

View File

@ -0,0 +1,4 @@
package cn.hutool.core.text;
public class StrTemplate {
}

View File

@ -1,6 +1,5 @@
package cn.hutool.core.text;
import cn.hutool.core.lang.Console;
import org.junit.Assert;
import org.junit.Test;
@ -34,7 +33,7 @@ public class StrMatcherTest {
// 当有无匹配项的时候按照全不匹配对待
final StrMatcher strMatcher = new StrMatcher("${name}经过${year}年");
final Map<String, String> match = strMatcher.match("小明经过20年成长为一个大人。");
Console.log(match);
//Console.log(match);
Assert.assertEquals("小明", match.get("name"));
Assert.assertEquals("20", match.get("year"));
}