From 75fed09d816e806e4b1c8520a8c91ec4fbd66550 Mon Sep 17 00:00:00 2001 From: emptypoint <1215582715@qq.com> Date: Wed, 9 Nov 2022 02:49:15 +0000 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8D=95=E4=BC=98=E5=8C=96=E5=8D=A0?= =?UTF-8?q?=E4=BD=8D=E7=AC=A6=E8=A7=A3=E6=9E=90=E5=99=A8=201.=20=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E4=BA=86=E4=BF=9D=E5=AD=98=E7=BB=93=E6=9E=9C=E7=9A=84?= =?UTF-8?q?StringBuilder=E7=9A=84=E5=88=9D=E5=A7=8B=E5=8C=96=E9=95=BF?= =?UTF-8?q?=E5=BA=A6=202.=20=E4=BC=98=E5=8C=96=E4=BA=86expression=E7=9A=84?= =?UTF-8?q?=E9=87=8D=E7=BD=AE=E6=93=8D=E4=BD=9C;=203.=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E4=BA=86=E8=A7=A3=E6=9E=90=E5=BC=82=E5=B8=B8=E6=97=B6=E7=9A=84?= =?UTF-8?q?=E5=A4=84=E7=90=86;?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: emptypoint <1215582715@qq.com> --- .../hutool/core/text/PlaceholderParser.java | 87 +++++++++---------- 1 file changed, 41 insertions(+), 46 deletions(-) diff --git a/hutool-core/src/main/java/cn/hutool/core/text/PlaceholderParser.java b/hutool-core/src/main/java/cn/hutool/core/text/PlaceholderParser.java index c01cd6116..3bad83de3 100644 --- a/hutool-core/src/main/java/cn/hutool/core/text/PlaceholderParser.java +++ b/hutool-core/src/main/java/cn/hutool/core/text/PlaceholderParser.java @@ -102,52 +102,47 @@ public class PlaceholderParser implements UnaryOperator { // 开始匹配 char[] src = text.toCharArray(); - final StringBuilder result = new StringBuilder(); - StringBuilder expression = new StringBuilder(); - while (openCursor > -1) { - - // 开始符号是否被转义,若是则跳过并寻找下一个开始符号 - if (openCursor > 0 && src[openCursor - 1] == escape) { - result.append(src, closeCursor, openCursor - closeCursor - 1).append(open); - closeCursor = openCursor + openLength; - openCursor = text.indexOf(open, closeCursor); - continue; - } - - // 记录当前位符的开始符号与上一占位符的结束符号间的字符串 - result.append(src, closeCursor, openCursor - closeCursor); - - // 重置结束游标至当前占位符的开始处 - closeCursor = openCursor + openLength; - - // 寻找结束符号下标 - int end = text.indexOf(close, closeCursor); - while (end > -1) { - // 结束符号被转义,寻找下一个结束符号 - if (end > closeCursor && src[end - 1] == escape) { - expression.append(src, closeCursor, end - closeCursor - 1).append(close); - closeCursor = end + closeLength; - end = text.indexOf(close, closeCursor); - } - // 找到结束符号 - else { - expression.append(src, closeCursor, end - closeCursor); - break; - } - } - - // 未能找到结束符号,说明匹配结束 - if (end == -1) { - result.append(src, openCursor, src.length - openCursor); - closeCursor = src.length; - } - // 找到结束符号,将开始到结束符号之间的字符串替换为指定表达式 - else { - result.append(processor.apply(expression.toString())); - expression = new StringBuilder(); - // 完成当前占位符的处理匹配,寻找下一个 - closeCursor = end + close.length(); - } + final StringBuilder result = new StringBuilder(src.length); + StringBuilder expression = new StringBuilder(); + while (openCursor > -1) { + + // 开始符号是否被转义,若是则跳过并寻找下一个开始符号 + if (openCursor > 0 && src[openCursor - 1] == escape) { + result.append(src, closeCursor, openCursor - closeCursor - 1).append(open); + closeCursor = openCursor + openLength; + openCursor = text.indexOf(open, closeCursor); + continue; + } + + // 记录当前位符的开始符号与上一占位符的结束符号间的字符串 + result.append(src, closeCursor, openCursor - closeCursor); + + // 重置结束游标至当前占位符的开始处 + closeCursor = openCursor + openLength; + + // 寻找结束符号下标 + int end = text.indexOf(close, closeCursor); + while (end > -1) { + // 结束符号被转义,寻找下一个结束符号 + if (end > closeCursor && src[end - 1] == escape) { + expression.append(src, closeCursor, end - closeCursor - 1).append(close); + closeCursor = end + closeLength; + end = text.indexOf(close, closeCursor); + if (end == -1) { + throw new UtilException("\"{}\" 中字符下标 {} 处的开始符没有找到对应的结束符", text, openCursor); + } + } + // 找到结束符号 + else { + expression.append(src, closeCursor, end - closeCursor); + break; + } + } + // 找到结束符号,将开始到结束符号之间的字符串替换为指定表达式 + result.append(processor.apply(expression.toString())); + expression.setLength(0); + // 完成当前占位符的处理匹配,寻找下一个 + closeCursor = end + close.length(); // 寻找下一个开始符号 openCursor = text.indexOf(open, closeCursor); -- Gitee