@@ -296,11 +296,11 @@ public String put(String key, String value) {
296296 if (key == null || value == null ) {
297297 throw new NullPointerException ();
298298 }
299- String rawValue = escape (value , false );
299+ String rawValue = rawValue (value );
300300 if (values .containsKey (key )) {
301301 replaceValue (key , rawValue , value );
302302 } else {
303- String rawKey = escape (key , true );
303+ String rawKey = rawKey (key );
304304 addNewKeyValue (rawKey , key , rawValue , value );
305305 }
306306 return values .put (key , value );
@@ -575,19 +575,50 @@ private Cursor indexOf(String key) {
575575 return index (
576576 tokens .indexOf (
577577 new PropertiesParser .Token (
578- PropertiesParser .Type .KEY , escape (key , true ), key )));
578+ PropertiesParser .Type .KEY , rawKey (key ), key )));
579579 }
580580
581- private String escape (String raw , boolean forKey ) {
582- raw = raw .replace ("\\ " , "\\ \\ " );
583- raw = raw .replace ("\n " , "\\ n" );
584- raw = raw .replace ("\r " , "\\ r" );
585- raw = raw .replace ("\t " , "\\ t" );
586- raw = raw .replace ("\f " , "\\ f" );
587- if (forKey ) {
588- raw = raw .replace (" " , "\\ " );
589- }
590- return raw ;
581+ /**
582+ * Returns a copy of the given string that can be used to
583+ * prepare values before passing them to methods like
584+ * code>putRaw()</code>.
585+ *
586+ * @param value Input value
587+ * @return Encoded value
588+ */
589+ public static String rawValue (String value ) {
590+ return value
591+ .replace ("\\ " , "\\ \\ " )
592+ .replace ("\n " , "\\ n" )
593+ .replace ("\r " , "\\ r" )
594+ .replace ("\t " , "\\ t" )
595+ .replace ("\f " , "\\ f" );
596+ }
597+
598+ /**
599+ * Returns a copy of the given string that can be used to
600+ * prepare keys before passing them to methods like
601+ * code>putRaw()</code>.
602+ *
603+ * @param key Input key
604+ * @return Encoded key
605+ */
606+ public static String rawKey (String key ) {
607+ return rawValue (key ).replace (" " , "\\ " );
608+ }
609+
610+ /**
611+ * Returns a copy of the given string where all non-ISO8859-1
612+ * characters have been encoded using \u0000 escape sequences.
613+ *
614+ * @param text Input string
615+ * @return Encoded string
616+ */
617+ public static String escape (String text ) {
618+ return replace (
619+ text ,
620+ "[^\\ x{0000}-\\ x{00FF}]" ,
621+ m -> "\\ \\ u" + String .format ("%04x" , (int )m .group (0 ).charAt (0 )));
591622 }
592623
593624 private static String replace (String input , String regex , Function <Matcher , String > callback ) {
0 commit comments