Skip to content

Commit 84ff794

Browse files
committed
chore: Added escape() function
Added `escape()` function that escapes non-ISO8859-1 chars. Also made `unescape()`, `rawKey()` and `rawValue()` public.
1 parent 0c115c0 commit 84ff794

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
lines changed

src/main/java/org/codejive/properties/Properties.java

Lines changed: 44 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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) {

src/main/java/org/codejive/properties/PropertiesParser.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,14 @@ private String string() {
293293
return result;
294294
}
295295

296-
static String unescape(String escape) {
296+
/**
297+
* Returns a copy of the given string where all escape sequences
298+
* have been turned into their representative values.
299+
*
300+
* @param escape Input string
301+
* @return Decoded string
302+
*/
303+
public static String unescape(String escape) {
297304
StringBuilder txt = new StringBuilder();
298305
for (int i = 0; i < escape.length(); i++) {
299306
char ch = escape.charAt(i);

0 commit comments

Comments
 (0)