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 List<IWordResult> doFindAll(String string, IWordContext context);
|
||||||
|
|
||||||
|
|
||||||
|
protected abstract IWordResult doFindFirst(String string, IWordContext context);
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 替换
|
* 替换
|
||||||
* @param target 目标字符串
|
* @param target 目标字符串
|
||||||
@@ -80,13 +84,10 @@ public abstract class AbstractSensitiveWord implements ISensitiveWord {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public IWordResult findFirst(String string, IWordContext context) {
|
public IWordResult findFirst(String string, IWordContext context) {
|
||||||
//TODO: 这个是懒惰的实现,性能一般。也可以调整为 FAST_OVER 模式。
|
if(StringUtil.isEmpty(string)){
|
||||||
List<IWordResult> allList = findAll(string, context);
|
return null;
|
||||||
if(CollectionUtil.isNotEmpty(allList)) {
|
|
||||||
return allList.get(0);
|
|
||||||
}
|
}
|
||||||
|
return doFindFirst(string,context);
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package com.github.houbb.sensitive.word.core;
|
package com.github.houbb.sensitive.word.core;
|
||||||
|
|
||||||
import com.github.houbb.heaven.util.guava.Guavas;
|
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.*;
|
||||||
import com.github.houbb.sensitive.word.api.context.InnerSensitiveWordContext;
|
import com.github.houbb.sensitive.word.api.context.InnerSensitiveWordContext;
|
||||||
import com.github.houbb.sensitive.word.constant.enums.WordTypeEnum;
|
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);
|
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 添加判断
|
//v0.13.0 添加判断
|
||||||
if(wordResultCondition.match(wordResult, text, modeEnum, context)) {
|
if(wordResultCondition.match(wordResult, text, modeEnum, context)) {
|
||||||
resultList.add(wordResult);
|
resultList.add(wordResult);
|
||||||
|
// 快速返回
|
||||||
|
if (WordValidModeEnum.FAIL_FAST.equals(modeEnum)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 快速返回
|
|
||||||
if (WordValidModeEnum.FAIL_FAST.equals(modeEnum)) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 增加 i 的步长
|
// 增加 i 的步长
|
||||||
// 为什么要-1,因为默认就会自增1
|
// 为什么要-1,因为默认就会自增1
|
||||||
|
|||||||
Reference in New Issue
Block a user