Class SliceMatcher
char[] against a fixed set of candidate keywords without allocating a string.
The candidates are supplied once at construction in a caller-defined order, and match(char[], int, int) returns the index
of the matching candidate (or -1). Callers typically switch on the returned index. This lets a
parser dispatch on a keyword by comparing directly against its character buffer instead of allocating a substring
and comparing strings.
Matching works like a shallow trie: candidates are first bucketed by length (the cheapest discriminator, precomputed once), and only candidates whose length equals the slice length are then compared character by character. Keyword sets are typically small and largely length-distinct, so this eliminates most candidates before any character comparison.
-
Constructor Summary
ConstructorsConstructorDescriptionSliceMatcher(String... keywords) Creates a matcher over the given candidate keywords. -
Method Summary
Modifier and TypeMethodDescriptionintmatch(char[] chars, int offset, int length) Matches the slicechars[offset, offset + length)against the candidates.
-
Constructor Details
-
SliceMatcher
Creates a matcher over the given candidate keywords.The index of each keyword in this array is what
match(char[], int, int)returns when that keyword matches. If a keyword appears more than once, the lowest index wins.- Parameters:
keywords- Candidate keywords in the order callers will switch on.
-
-
Method Details
-
match
public int match(char[] chars, int offset, int length) Matches the slicechars[offset, offset + length)against the candidates.- Parameters:
chars- Character buffer containing the slice.offset- Start of the slice (inclusive).length- Length of the slice.- Returns:
- The index of the matching candidate, or
-1if none match.
-