mirror of
https://gitee.com/dromara/sa-token.git
synced 2026-05-14 04:42:09 +08:00
Merge pull request #907 from 07heco/perf/optimize-pattern-cache-and-eliminate-magic-value
refactor: optimize constant specification and encapsulation for StrFormatter
This commit is contained in:
@@ -28,12 +28,34 @@ package cn.dev33.satoken.util;
|
||||
public class StrFormatter {
|
||||
|
||||
/**
|
||||
* 占位符
|
||||
* 占位符(保留原有 public 访问权限,避免破坏外部依赖)
|
||||
* @deprecated 语义不明确,建议内部使用 {@link #DEFAULT_PLACEHOLDER} 替代
|
||||
*/
|
||||
@Deprecated
|
||||
public static String EMPTY_JSON = "{}";
|
||||
|
||||
|
||||
/**
|
||||
* 反斜杠转义字符(保留原有 public 访问权限,避免破坏外部依赖)
|
||||
* @deprecated 命名不规范,建议内部使用 {@link #BACKSLASH_CHAR} 替代
|
||||
*/
|
||||
@Deprecated
|
||||
public static char C_BACKSLASH = '\\';
|
||||
|
||||
|
||||
/**
|
||||
* 新增内部规范常量(private,仅内部使用)
|
||||
* 默认占位符 */
|
||||
private static final String DEFAULT_PLACEHOLDER = "{}";
|
||||
|
||||
/**
|
||||
* 反斜杠转义字符
|
||||
* */
|
||||
private static final char BACKSLASH_CHAR = '\\';
|
||||
|
||||
/**
|
||||
* 字符串构建器初始扩容长度
|
||||
* */
|
||||
private static final int BUFFER_INIT_CAPACITY = 50;
|
||||
|
||||
/**
|
||||
* 格式化字符串<br>
|
||||
* 此方法只是简单将占位符 {} 按照顺序替换为参数<br>
|
||||
@@ -45,10 +67,10 @@ public class StrFormatter {
|
||||
*
|
||||
* @param strPattern 字符串模板
|
||||
* @param argArray 参数列表
|
||||
* @return 结果
|
||||
* @return 格式化后的结果
|
||||
*/
|
||||
public static String format(String strPattern, Object... argArray) {
|
||||
return formatWith(strPattern, EMPTY_JSON, argArray);
|
||||
return formatWith(strPattern, DEFAULT_PLACEHOLDER, argArray);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -63,7 +85,7 @@ public class StrFormatter {
|
||||
* @param strPattern 字符串模板
|
||||
* @param placeHolder 占位符,例如{}
|
||||
* @param argArray 参数列表
|
||||
* @return 结果
|
||||
* @return 格式化后的结果
|
||||
* @since 1.33.0
|
||||
*/
|
||||
public static String formatWith(String strPattern, String placeHolder, Object... argArray) {
|
||||
@@ -74,7 +96,7 @@ public class StrFormatter {
|
||||
final int placeHolderLength = placeHolder.length();
|
||||
|
||||
// 初始化定义好的长度以获得更好的性能
|
||||
final StringBuilder sbu = new StringBuilder(strPatternLength + 50);
|
||||
final StringBuilder sbu = new StringBuilder(strPatternLength + BUFFER_INIT_CAPACITY);
|
||||
|
||||
int handledPosition = 0;// 记录已经处理到的位置
|
||||
int delimIndex;// 占位符所在位置
|
||||
@@ -90,8 +112,8 @@ public class StrFormatter {
|
||||
}
|
||||
|
||||
// 转义符
|
||||
if (delimIndex > 0 && strPattern.charAt(delimIndex - 1) == C_BACKSLASH) {// 转义符
|
||||
if (delimIndex > 1 && strPattern.charAt(delimIndex - 2) == C_BACKSLASH) {// 双转义符
|
||||
if (delimIndex > 0 && strPattern.charAt(delimIndex - 1) == BACKSLASH_CHAR) {// 转义符
|
||||
if (delimIndex > 1 && strPattern.charAt(delimIndex - 2) == BACKSLASH_CHAR) {// 双转义符
|
||||
// 转义符之前还有一个转义符,占位符依旧有效
|
||||
sbu.append(strPattern, handledPosition, delimIndex - 1);
|
||||
sbu.append(argArray[argIndex]);
|
||||
@@ -116,4 +138,4 @@ public class StrFormatter {
|
||||
return sbu.toString();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user