private static boolean match ( final CharSequence string , final CharSequence pattern , int sNdx , int pNdx ) { int pLen = pattern . length ( ) ; if ( pLen == 1 ) { if ( pattern . charAt ( 0 ) == ' ' ) { // speed-up return true ; } } int sLen = string . length ( ) ; boolean nextIsNotWildcard = false ; while ( true ) { // check if end of string and/or pattern occurred if ( ( sNdx >= sLen ) ) { // end of string still may have pending '*' in pattern while ( ( pNdx < pLen ) && ( pattern . charAt ( pNdx ) == ' ' ) ) { pNdx ++ ; } return pNdx >= pLen ; } if ( pNdx >= pLen ) { // end of pattern, but not end of the string return false ; } char p = pattern . charAt ( pNdx ) ; // pattern char // perform logic if ( ! nextIsNotWildcard ) { if ( p == ' ' ) { pNdx ++ ; nextIsNotWildcard = true ; continue ; } if ( p == ' ' ) { sNdx ++ ; pNdx ++ ; continue ; } if ( p == ' ' ) { char pNext = 0 ; // next pattern char if ( pNdx + 1 < pLen ) { pNext = pattern . charAt ( pNdx + 1 ) ; } if ( pNext == ' ' ) { // double '*' have the same effect as one '*' pNdx ++ ; continue ; } int i ; pNdx ++ ; // find recursively if there is any substring from the end of the // line that matches the rest of the pattern !!! for ( i = string . length ( ) ; i >= sNdx ; i -- ) { if ( match ( string , pattern , i , pNdx ) ) { return true ; } } return false ; } } else { nextIsNotWildcard = false ; } // check if pattern char and string char are equals if ( p != string . charAt ( sNdx ) ) { return false ; } // everything matches for now, continue sNdx ++ ; pNdx ++ ; } }
Internal matching recursive function .