private NameState insertMatch()

in src/main/software/amazon/event/ruler/ByteMachine.java [1588:1633]


    private NameState insertMatch(InputCharacter[] characters, int currentIndex, ByteState state,
                                  NameState nextNameState, Patterns pattern, ByteState prevState) {
        InputCharacter character = characters[currentIndex];

        ByteMatch match = findMatch(getTransition(state, character).getMatches(), pattern);
        if (match != null) {
            // There is a match linked to the transition that's the same type, so we just re-use its nextNameState
            return match.getNextNameState();
        }

        // we make a new NameState and hook it up
        NameState nameState = nextNameState == null ? new NameState() : nextNameState;

        match = new ByteMatch(pattern, nameState);
        addMatchReferences(match);

        if (isWildcard(character)) {
            // Rule ends with '*'. Allow no characters up to many characters to match, using a composite match state
            // that references a self-referencing composite match state. Two composite states are used so the count of
            // matching rule prefixes is accurate in MachineComplexityEvaluator. If there is a previous state, then the
            // first composite already exists and we will find it. Otherwise, create a brand new composite.
            SingleByteTransition composite;
            if (prevState != null) {
                composite = getTransitionHavingNextByteState(prevState, state, characters[currentIndex - 1]);
                assert composite != null : "Composite must exist";
            } else {
                composite = new ByteState().setMatch(match);
            }

            ByteState nextState = composite.getNextByteState();
            CompositeByteTransition compositeClone = (CompositeByteTransition) composite.clone();
            nextState.addTransitionForAllBytes(compositeClone);
            nextState.setIndeterminatePrefix(true);
        } else {
            addTransition(state, character, match);

            // If second last char was wildcard, the previous state will also need to transition to the match so that
            // empty substring matches wildcard.
            if (prevState != null && currentIndex == characters.length - 1 &&
                    isWildcard(characters[characters.length - 2])) {
                addTransition(prevState, character, match);
            }
        }

        return nameState;
    }