public static int getANSIControlSequenceLength ( String string , int index ) { int len = 0 , restlen = string . length ( ) - index ; if ( restlen >= 3 ) { // Control sequences require a minimum of three characters char esc = string . charAt ( index ) , bracket = string . charAt ( index + 1 ) ; if ( esc == 0x1B && bracket == ' ' ) { // escape & open bracket len = 3 ; // esc,bracket and (later)terminator. // digits or semicolons can still precede the terminator: for ( int i = 2 ; i < restlen ; i ++ ) { char ch = string . charAt ( i + index ) ; // only ascii-digits or semicolons allowed here: if ( ( ch >= ' ' && ch <= ' ' ) || ch == ' ' ) { len ++ ; } else { break ; } } // if string ends in digits/semicolons, then it's not a sequence. if ( len > restlen ) { len = 0 ; } } } return len ; }
Given a string and an index in that string returns the number of characters starting at index that make up a complete ANSI control sequence . If there is no control sequence starting there the method will return 0 .