Insight Spring中的算法-占位符查找

实用算法学习、分析

算法1

/**
 * 查找占位符匹配的后缀索引。
 * 因为Spring支持嵌套的占位符表示,所以配对的查找是这个方法核心要解决的
 * 逻辑:遍历字符串buf,匹配遇到的占位符前缀和后缀,如果是配套的后缀,则返回索引。
 * 因为有嵌套占位符的情况,需要一个临时的变量记录内嵌占位符的出现次数,通过成对匹配的计算(出现前缀加1,出现后缀减1),防止错误返回内嵌占位符的后缀索引。
 * @param buf 配置字符串,比如:${foo:${defaultFoo}}
 * @param startIndex 占位符在 buf中的index,初始值 = buf.indexOf("${");
 */
private int findPlaceholderEndIndex(CharSequence buf, int startIndex) {
	int index = startIndex + this.placeholderPrefix.length();
	// 嵌套的占位符出现次数标识变量,出现前缀加1,出现后缀减1。
	int withinNestedPlaceholder = 0;
	while (index < buf.length()) {
		// 检测后缀,两种情况
		if (StringUtils.substringMatch(buf, index, this.placeholderSuffix)) {
			// 1. 该后缀属于内嵌占位符,继续遍历
			if (withinNestedPlaceholder > 0) {
				withinNestedPlaceholder--;
				index = index + this.placeholderSuffix.length();
			}
			// 2. 目标占位符的后缀
			else {
				return index;
			}
		}
		// 检测前缀,证明遇到的了内嵌占位符
		else if (StringUtils.substringMatch(buf, index, this.simplePrefix)) {
			withinNestedPlaceholder++;
			index = index + this.simplePrefix.length();
		}
		else {
			index++;
		}
	}
	return -1;
}


算法2

/**
 * Test whether the given string matches the given substring
 * at the given index.
 * 思路:以被查询的字符串,循环匹配每个字符,遇到不符合直接返回不匹配。
 * 匹配查找和数据库的join 查询,小表驱动大表有异曲同工之处
 * @param str the original string (or StringBuilder)
 * @param index the index in the original string to start matching against
 * @param substring the substring to match at the given index
 */
public static boolean substringMatch(CharSequence str, int index, CharSequence substring) {
	// 遍历 toFind 字符串
	for (int j = 0; j < substring.length(); j++) {
		int i = index + j;
		// 如果原字符串不包含(长度不够)或者字符不匹配,立即失败
		if (i >= str.length() || str.charAt(i) != substring.charAt(j)) {
			return false;
		}
	}
	return true;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值