From 4d0b2d3f50bf56b83498a3b787ac08afaeb85eff Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 18 Jun 2019 16:38:49 +0800 Subject: up to 4x faster search --- .../me/shedaniel/rei/client/SearchArgument.java | 32 ++++++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) (limited to 'src/main/java/me/shedaniel/rei/client/SearchArgument.java') diff --git a/src/main/java/me/shedaniel/rei/client/SearchArgument.java b/src/main/java/me/shedaniel/rei/client/SearchArgument.java index 4c834ff1b..59ba0e0be 100644 --- a/src/main/java/me/shedaniel/rei/client/SearchArgument.java +++ b/src/main/java/me/shedaniel/rei/client/SearchArgument.java @@ -5,18 +5,19 @@ package me.shedaniel.rei.client; +import java.util.Locale; import java.util.function.Function; import java.util.regex.Pattern; public class SearchArgument { - public static final Function INCLUDE = integer -> integer > -1; - public static final Function NOT_INCLUDE = integer -> integer <= -1; + public static final SearchArgument ALWAYS = new SearchArgument(ArgumentType.ALWAYS, "", true); private ArgumentType argumentType; private String text; + public final Function INCLUDE = s -> boyerMooreHorspoolSearch(text, s) > -1; + public final Function NOT_INCLUDE = s -> boyerMooreHorspoolSearch(text, s) <= -1; private boolean include; private Pattern pattern; - public static final SearchArgument ALWAYS = new SearchArgument(ArgumentType.ALWAYS, "", true); public SearchArgument(ArgumentType argumentType, String text, boolean include) { this(argumentType, text, include, true); @@ -24,12 +25,31 @@ public class SearchArgument { public SearchArgument(ArgumentType argumentType, String text, boolean include, boolean autoLowerCase) { this.argumentType = argumentType; - this.text = autoLowerCase ? text.toLowerCase() : text; + this.text = autoLowerCase ? text.toLowerCase(Locale.ROOT) : text; this.include = include; } - public static Function getFunction(boolean include) { - return include ? SearchArgument.INCLUDE : SearchArgument.NOT_INCLUDE; + public static int boyerMooreHorspoolSearch(CharSequence pattern, CharSequence text) { + int shift[] = new int[256]; + for(int k = 0; k < 256; k++) + shift[k] = pattern.length(); + for(int k = 0; k < pattern.length() - 1; k++) + shift[pattern.charAt(k)] = pattern.length() - 1 - k; + int i = 0, j = 0; + while ((i + pattern.length()) <= text.length()) { + j = pattern.length() - 1; + while (text.charAt(i + j) == pattern.charAt(j)) { + j -= 1; + if (j < 0) + return i; + } + i = i + shift[text.charAt(i + pattern.length() - 1)]; + } + return -1; + } + + public Function getFunction(boolean include) { + return include ? INCLUDE : NOT_INCLUDE; } public ArgumentType getArgumentType() { -- cgit