private boolean shouldEscape ( StringBuilder content , int charPosition ) { int counter = 0 ; int pos = charPosition - 1 ; while ( pos > - 1 && content . charAt ( pos ) == ESCAPE_CHAR ) { counter ++ ; pos -- ; } return counter % 2 != 0 ; }
Count the number of escape chars before a given character and if that number is odd then that character should be escaped .