private static String [ ] splitOnTokens ( String text , String oneAnySymbolStr , String noneOrMoreSymbolsStr ) { if ( ! hasWildcards ( text , ONE_ANY_SYMBOL , NONE_OR_MORE_SYMBOLS ) ) { return new String [ ] { text } ; } List < String > list = new ArrayList <> ( ) ; StringBuilder buffer = new StringBuilder ( ) ; boolean escapeMode = false ; final int length = text . length ( ) ; for ( int i = 0 ; i < length ; i ++ ) { final char current = text . charAt ( i ) ; switch ( current ) { case ESCAPE_CHAR : if ( escapeMode ) { buffer . append ( current ) ; } else { escapeMode = true ; } break ; case ONE_ANY_SYMBOL : if ( escapeMode ) { buffer . append ( current ) ; escapeMode = false ; } else { flushBuffer ( buffer , list ) ; if ( ! list . isEmpty ( ) && noneOrMoreSymbolsStr . equals ( list . get ( list . size ( ) - 1 ) ) ) { list . set ( list . size ( ) - 1 , oneAnySymbolStr ) ; list . add ( noneOrMoreSymbolsStr ) ; } else { list . add ( oneAnySymbolStr ) ; } } break ; case NONE_OR_MORE_SYMBOLS : if ( escapeMode ) { buffer . append ( current ) ; escapeMode = false ; } else { flushBuffer ( buffer , list ) ; if ( list . isEmpty ( ) || ! noneOrMoreSymbolsStr . equals ( list . get ( list . size ( ) - 1 ) ) ) { list . add ( noneOrMoreSymbolsStr ) ; } } break ; default : if ( escapeMode ) { buffer . append ( ESCAPE_CHAR ) ; escapeMode = false ; } buffer . append ( current ) ; } } if ( escapeMode ) { buffer . append ( ESCAPE_CHAR ) ; } if ( buffer . length ( ) != 0 ) { list . add ( buffer . toString ( ) ) ; } return list . toArray ( ArrayUtils . EMPTY_STRING_ARRAY ) ; }
Splits a string into a number of tokens . The text is split by _ and % . Multiple % are collapsed into a single % patterns like %_ will be transferred to _%