mirror of
https://github.com/houbb/sensitive-word.git
synced 2026-03-22 08:27:36 +08:00
refactor: 查找第一个敏感词使用fail_fast,并修复逻辑上的错误
This commit is contained in:
@@ -26,6 +26,10 @@ public abstract class AbstractSensitiveWord implements ISensitiveWord {
|
||||
*/
|
||||
protected abstract List<IWordResult> doFindAll(String string, IWordContext context);
|
||||
|
||||
|
||||
protected abstract IWordResult doFindFirst(String string, IWordContext context);
|
||||
|
||||
|
||||
/**
|
||||
* 替换
|
||||
* @param target 目标字符串
|
||||
@@ -80,13 +84,10 @@ public abstract class AbstractSensitiveWord implements ISensitiveWord {
|
||||
|
||||
@Override
|
||||
public IWordResult findFirst(String string, IWordContext context) {
|
||||
//TODO: 这个是懒惰的实现,性能一般。也可以调整为 FAST_OVER 模式。
|
||||
List<IWordResult> allList = findAll(string, context);
|
||||
if(CollectionUtil.isNotEmpty(allList)) {
|
||||
return allList.get(0);
|
||||
if(StringUtil.isEmpty(string)){
|
||||
return null;
|
||||
}
|
||||
|
||||
return null;
|
||||
return doFindFirst(string,context);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.github.houbb.sensitive.word.core;
|
||||
|
||||
import com.github.houbb.heaven.util.guava.Guavas;
|
||||
import com.github.houbb.heaven.util.util.CollectionUtil;
|
||||
import com.github.houbb.sensitive.word.api.*;
|
||||
import com.github.houbb.sensitive.word.api.context.InnerSensitiveWordContext;
|
||||
import com.github.houbb.sensitive.word.constant.enums.WordTypeEnum;
|
||||
@@ -34,6 +35,16 @@ public class SensitiveWord extends AbstractSensitiveWord {
|
||||
return innerSensitiveWords(string, WordValidModeEnum.FAIL_OVER, context);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected IWordResult doFindFirst(String string, IWordContext context) {
|
||||
List<IWordResult> wordResults = innerSensitiveWords(string, WordValidModeEnum.FAIL_FAST, context);
|
||||
if(!CollectionUtil.isEmpty(wordResults)){
|
||||
return wordResults.get(0);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取敏感词列表
|
||||
*
|
||||
@@ -84,12 +95,13 @@ public class SensitiveWord extends AbstractSensitiveWord {
|
||||
//v0.13.0 添加判断
|
||||
if(wordResultCondition.match(wordResult, text, modeEnum, context)) {
|
||||
resultList.add(wordResult);
|
||||
// 快速返回
|
||||
if (WordValidModeEnum.FAIL_FAST.equals(modeEnum)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// 快速返回
|
||||
if (WordValidModeEnum.FAIL_FAST.equals(modeEnum)) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
// 增加 i 的步长
|
||||
// 为什么要-1,因为默认就会自增1
|
||||
|
||||
Reference in New Issue
Block a user