private SingleByteTransition deleteMatchStepForWildcard()

in src/main/software/amazon/event/ruler/ByteMachine.java [242:287]


    private SingleByteTransition deleteMatchStepForWildcard(ByteState byteState, int charIndex, Patterns pattern,
                                                            InputCharacter[] characters,
                                                            SingleByteTransition transition, ByteState nextByteState) {
        final InputCharacter currentChar = characters[charIndex];

        // There will be a match using second last character on second last state when last character is
        // a wildcard. This allows empty substring to satisfy wildcard.
        if (charIndex == characters.length - 2 && isWildcard(characters[characters.length - 1])) {
            // Delete the match.
            SingleByteTransition updatedTransition = deleteMatch(currentChar, byteState, pattern, transition);
            if (updatedTransition != null) {
                return updatedTransition;
            }

        // Undo the machine changes for a wildcard as described in the Javadoc of addTransitionNextStateForWildcard.
        } else if (nextByteState != null && isWildcard(currentChar)) {
            // Remove transition for all possible byte values if it leads to an only self-referencing state
            if (nextByteState.hasOnlySelfReferentialTransition()) {
                byteState.removeTransitionForAllBytes(transition);
            }

            // Remove match on last char that exists for second-last char wildcard on second last state to be satisfied
            // by empty substring.
            if (charIndex == characters.length - 2 && isWildcard(characters[characters.length - 2])) {
                deleteMatches(characters[charIndex + 1], byteState, pattern);
            // Remove match on second last char that exists for third last and last char wildcard combination on third
            // last state to be both satisfied by empty substrings. I.e. "ax" can match "a*x*".
            } else if (charIndex == characters.length - 3 && isWildcard(characters[characters.length - 1]) &&
                    isWildcard(characters[characters.length - 3])) {
                deleteMatches(characters[charIndex + 1], byteState, pattern);
            }

            // Remove transition that exists for wildcard to be satisfied by empty substring.
            ByteTransition skipWildcardTransition = getTransition(byteState, characters[charIndex + 1]);
            for (SingleByteTransition eachTrans : skipWildcardTransition.expand()) {
                ByteState skipWildcardState = eachTrans.getNextByteState();
                if (!eachTrans.getMatches().iterator().hasNext() && skipWildcardState != null &&
                        (skipWildcardState.hasNoTransitions() ||
                         skipWildcardState.hasOnlySelfReferentialTransition())) {
                    removeTransition(byteState, characters[charIndex + 1], eachTrans);
                }
            }
        }

        return transition;
    }