private static EscapeStringResult escapeString ( String s , char quote ) { final String [ ] octal_strings = { "\\0" , "\\001" , "\\002" , "\\003" , "\\004" , "\\005" , "\\006" , "\\a" , "\\b" , "\\t" , "\\n" , "\\v" , "\\f" , "\\r" , "\\016" , "\\017" , "\\020" , "\\021" , "\\022" , "\\023" , "\\024" , "\\025" , "\\026" , "\\027" , "\\030" , "\\031" , "\\032" , "\\033" , "\\034" , "\\035" , "\\036" , "\\037" , } ; StringBuilder result = new StringBuilder ( s ) ; boolean needQuotes = s . isEmpty ( ) ; for ( int i = 0 , next_i ; i < result . length ( ) ; i = next_i ) { final int code_point = result . codePointAt ( i ) ; next_i = result . offsetByCodePoints ( i , 1 ) ; if ( code_point == 0 ) { result . replace ( i , next_i , "\\0" ) ; next_i = i + 2 ; needQuotes = true ; } else if ( code_point == ( int ) ' ' ) { result . replace ( i , next_i , "\\\\" ) ; next_i = i + 2 ; needQuotes = true ; } else if ( code_point == ( int ) quote ) { result . replace ( i , next_i , "\\" + quote ) ; next_i = i + 2 ; needQuotes = true ; } else if ( code_point < 32 ) { String octal = octal_strings [ code_point ] ; result . replace ( i , next_i , octal ) ; next_i = i + octal . length ( ) ; needQuotes = true ; } else if ( code_point >= 65536 ) { String repl = String . format ( "\\U%08x" , code_point ) ; result . replace ( i , next_i , repl ) ; next_i = i + repl . length ( ) ; needQuotes = true ; } else if ( code_point >= 128 ) { String repl = String . format ( "\\u%04x" , code_point ) ; result . replace ( i , next_i , repl ) ; next_i = i + repl . length ( ) ; needQuotes = true ; } } return new EscapeStringResult ( needQuotes , result ) ; }
Escape a string configuration element .