public int spanAndCount ( CharSequence s , int start , SpanCondition spanCondition , OutputInt outCount ) { if ( spanCondition == SpanCondition . NOT_CONTAINED ) { return spanNot ( s , start , outCount ) ; } // Consider strings; they may overlap with the span, // and they may result in a smaller count that with just code points. if ( spanCondition == SpanCondition . CONTAINED ) { return spanContainedAndCount ( s , start , outCount ) ; } // SIMPLE (not synchronized, does not use offsets) int stringsLength = strings . size ( ) ; int length = s . length ( ) ; int pos = start ; int rest = length - start ; int count = 0 ; while ( rest != 0 ) { // Try to match the next code point. int cpLength = spanOne ( spanSet , s , pos , rest ) ; int maxInc = ( cpLength > 0 ) ? cpLength : 0 ; // Try to match all of the strings. for ( int i = 0 ; i < stringsLength ; ++ i ) { String string = strings . get ( i ) ; int length16 = string . length ( ) ; if ( maxInc < length16 && length16 <= rest && matches16CPB ( s , pos , length , string , length16 ) ) { maxInc = length16 ; } } // We are done if there is no match beyond pos. if ( maxInc == 0 ) { outCount . value = count ; return pos ; } // Continue from the longest match. ++ count ; pos += maxInc ; rest -= maxInc ; } outCount . value = count ; return pos ; }
Spans a string and counts the smallest number of set elements on any path across the span .