static Matcher startsWithCharSeq ( Matcher matcher ) { if ( matcher instanceof SeqMatcher ) { List < Matcher > matchers = matcher . < SeqMatcher > as ( ) . matchers ( ) ; if ( matchers . size ( ) >= 2 && matchers . get ( 0 ) instanceof StartMatcher && matchers . get ( 1 ) instanceof CharSeqMatcher ) { List < Matcher > ms = new ArrayList <> ( ) ; ms . add ( new StartsWithMatcher ( matchers . get ( 1 ) . < CharSeqMatcher > as ( ) . pattern ( ) ) ) ; ms . addAll ( matchers . subList ( 2 , matchers . size ( ) ) ) ; return SeqMatcher . create ( ms ) ; } } return matcher ; }
If the matcher has a start anchored character sequence then replace it with a StartsWithMatcher . In a tight loop this is much faster than a running with a sequence of two matchers .