private boolean match()

in commons-digester3-core/src/main/java/org/apache/commons/digester3/SimpleRegexMatcher.java [81:161]


    private boolean match( final String basePattern, final String regexPattern, int baseAt, int regexAt )
    {
        if ( log.isTraceEnabled() )
        {
            log.trace( "Base: " + basePattern );
            log.trace( "Regex: " + regexPattern );
            log.trace( "Base@" + baseAt );
            log.trace( "Regex@" + regexAt );
        }

        // check bounds
        if ( regexAt >= regexPattern.length() )
        {
            // maybe we've got a match
            if ( baseAt >= basePattern.length() )
            {
                // ok!
                return true;
            }
            // run out early
            return false;

        }
        if ( baseAt >= basePattern.length() )
        {
            // run out early
            return false;
        }

        // ok both within bounds
        final char regexCurrent = regexPattern.charAt( regexAt );
        switch ( regexCurrent )
        {
            case '*':
                // this is the tricky case
                // check for terminal
                if ( ++regexAt >= regexPattern.length() )
                {
                    // this matches anything let - so return true
                    return true;
                }
                // go through every subsequent appearance of the next character
                // and so if the rest of the regex matches
                final char nextRegex = regexPattern.charAt( regexAt );
                if ( log.isTraceEnabled() )
                {
                    log.trace( "Searching for next '" + nextRegex + "' char" );
                }
                int nextMatch = basePattern.indexOf( nextRegex, baseAt );
                while ( nextMatch != -1 )
                {
                    if ( log.isTraceEnabled() )
                    {
                        log.trace( "Trying '*' match@" + nextMatch );
                    }
                    if ( match( basePattern, regexPattern, nextMatch, regexAt ) )
                    {
                        return true;
                    }
                    nextMatch = basePattern.indexOf( nextRegex, nextMatch + 1 );
                }
                log.trace( "No matches found." );
                return false;

            case '?':
                // this matches anything
                return match( basePattern, regexPattern, ++baseAt, ++regexAt );

            default:
                if ( log.isTraceEnabled() )
                {
                    log.trace( "Comparing " + regexCurrent + " to " + basePattern.charAt( baseAt ) );
                }
                if ( regexCurrent == basePattern.charAt( baseAt ) )
                {
                    // still got more to go
                    return match( basePattern, regexPattern, ++baseAt, ++regexAt );
                }
                return false;
        }
    }