aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/me/shedaniel/rei/client/SearchArgument.java
blob: c71ed89133e3620a529203f6d4475a9c1a6c1fda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/*
 * Roughly Enough Items by Danielshe.
 * Licensed under the MIT License.
 */

package me.shedaniel.rei.client;

import com.google.common.base.CharMatcher;

import java.util.Locale;
import java.util.function.Function;

public class SearchArgument {
    
    public static final SearchArgument ALWAYS = new SearchArgument(ArgumentType.ALWAYS, "", true);
    private ArgumentType argumentType;
    private String text;
    public final Function<String, Boolean> INCLUDE = s -> search(text, s);
    public final Function<String, Boolean> NOT_INCLUDE = s -> !search(text, s);
    private boolean include;
    
    public SearchArgument(ArgumentType argumentType, String text, boolean include) {
        this(argumentType, text, include, true);
    }
    
    public SearchArgument(ArgumentType argumentType, String text, boolean include, boolean autoLowerCase) {
        this.argumentType = argumentType;
        this.text = autoLowerCase ? text.toLowerCase(Locale.ROOT) : text;
        this.include = include;
    }
    
    public static boolean search(CharSequence pattern, String text) {
        int patternLength = pattern.length();
        if (patternLength == 0)
            return true;
        if (patternLength > text.length())
            return false;
        if (!CharMatcher.ascii().matchesAllOf(text) || !CharMatcher.ascii().matchesAllOf(pattern))
            return text.contains(pattern);
        int shift[] = new int[256];
        for(int k = 0; k < 256; k++)
            shift[k] = patternLength;
        for(int k = 0; k < patternLength - 1; k++)
            shift[pattern.charAt(k)] = patternLength - 1 - k;
        int i = 0, j = 0;
        while ((i + patternLength) <= text.length()) {
            j = patternLength - 1;
            while (text.charAt(i + j) == pattern.charAt(j)) {
                j -= 1;
                if (j < 0)
                    return i >= 0;
            }
            i = i + shift[text.charAt(i + patternLength - 1)];
        }
        return false;
    }
    
    public Function<String, Boolean> getFunction(boolean include) {
        return include ? INCLUDE : NOT_INCLUDE;
    }
    
    public ArgumentType getArgumentType() {
        return argumentType;
    }
    
    public String getText() {
        return text;
    }
    
    public boolean isInclude() {
        return include;
    }
    
    @Override
    public String toString() {
        return String.format("Argument[%s]: name = %s, include = %b", argumentType.name(), text, include);
    }
    
    public enum ArgumentType {
        TEXT,
        MOD,
        TOOLTIP,
        ALWAYS
    }
    
}