aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/text/Ascii7BitMatcher.java
blob: 85ecbaf140ed94e8f6fa91627aea1c2e129bb03f (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.text;

import java.util.BitSet;

/**
 * Fast replacement for regex based validators of simple expressions.
 * It can take a list of legal characters for the the first character,
 * and another list for the following. Limited to 7bit ascii.
 *
 * @author baldersheim
 */
public class Ascii7BitMatcher {

    private final BitSet legalFirst;
    private final BitSet legalRest;
    private static BitSet createBitSet(String legal) {
        BitSet legalChars = new BitSet(128);
        for (int i=0; i < legal.length(); i++) {
            char c = legal.charAt(i);
            if (c < 128) {
                legalChars.set(c);
            } else {
                throw new IllegalArgumentException("Char '" + c + "' at position " + i + " is not valid ascii 7 bit char");
            }
        }
        return legalChars;
    }
    public Ascii7BitMatcher(String legal) {
        this(legal, legal);
    }
    public Ascii7BitMatcher(String legalFirstChar, String legalChars) {
        legalFirst = createBitSet(legalFirstChar);
        legalRest = createBitSet(legalChars);
    }
    private static boolean isAscii7Bit(char c) { return c < 128;}
    private boolean isLegalFirst(char c) {
        return isAscii7Bit(c) && legalFirst.get(c);
    }
    private boolean isLegalRest(char c) {
        return isAscii7Bit(c) && legalRest.get(c);
    }
    public boolean matches(String s) {
        if (s == null || s.isEmpty() || ! isLegalFirst(s.charAt(0))) return false;
        for (int i = 1; i < s.length(); i++) {
            if ( ! isLegalRest(s.charAt(i))) {
                return false;
            }
        }
        return true;
    }
    static public String charsAndNumbers() {
        char[] chars = new char[26*2+10];
        int i = 0;
        for (char c = 'A'; c <= 'Z'; c++) {
            chars[i++] = c;
        }
        for (char c = 'a'; c <= 'z'; c++) {
            chars[i++] = c;
        }
        for (char c = '0'; c <= '9'; c++) {
            chars[i++] = c;
        }
        return new String(chars);
    }

}