Class SliceMatcher

java.lang.Object
software.amazon.smithy.utils.SliceMatcher

public final class SliceMatcher extends Object
Matches a slice of a 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

    Constructors
    Constructor
    Description
    SliceMatcher(String... keywords)
    Creates a matcher over the given candidate keywords.
  • Method Summary

    Modifier and Type
    Method
    Description
    int
    match(char[] chars, int offset, int length)
    Matches the slice chars[offset, offset + length) against the candidates.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • SliceMatcher

      public SliceMatcher(String... keywords)
      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 slice chars[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 -1 if none match.