public static void escape ( String str , StringBuilder rval ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) { final char hi = str . charAt ( i ) ; if ( hi <= 0x8 || hi == 0xB || hi == 0xC || ( hi >= 0xE && hi <= 0x1F ) || ( hi >= 0x7F && hi <= 0xA0 ) || // 0xA0 is end of // non-printable latin-1 // supplement // characters ( ( hi >= 0x24F // 0x24F is the end of latin extensions && ! Character . isHighSurrogate ( hi ) ) // TODO: there's probably a lot of other characters that // shouldn't be escaped that // fall outside these ranges, this is one example from the // json-ld tests ) ) { rval . append ( String . format ( "\\u%04x" , ( int ) hi ) ) ; } else if ( Character . isHighSurrogate ( hi ) ) { final char lo = str . charAt ( ++ i ) ; final int c = ( hi << 10 ) + lo + ( 0x10000 - ( 0xD800 << 10 ) - 0xDC00 ) ; rval . append ( String . format ( "\\U%08x" , c ) ) ; } else { switch ( hi ) { case ' ' : rval . append ( "\\b" ) ; break ; case ' ' : rval . append ( "\\n" ) ; break ; case ' ' : rval . append ( "\\t" ) ; break ; case ' ' : rval . append ( "\\f" ) ; break ; case ' ' : rval . append ( "\\r" ) ; break ; // case '\'': // rval += "\\'"; // break; case ' ' : rval . append ( "\\\"" ) ; // rval += "\\u0022"; break ; case ' ' : rval . append ( "\\\\" ) ; break ; default : // just put the char as is rval . append ( hi ) ; break ; } } } // return rval; }
Escapes the given string according to the N - Quads escape rules