blob: 0080841a5a0147b1d6f88804397e71670ffbb428 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
package org.zwobble.mammoth.internal.styles.parsing;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class EscapeSequences {
private static final Pattern PATTERN = Pattern.compile("\\\\(.)");
public static String decode(String value) {
Matcher matcher = PATTERN.matcher(value);
StringBuilder decoded = new StringBuilder();
int lastIndex = 0;
while (matcher.find()) {
decoded.append(value.substring(lastIndex, matcher.start()));
decoded.append(escapeSequence(matcher.group(1)));
lastIndex = matcher.end();
}
decoded.append(value.substring(lastIndex, value.length()));
return decoded.toString();
}
private static char escapeSequence(String code) {
switch (code) {
case "n":
return '\n';
case "r":
return '\r';
case "t":
return '\t';
default:
return code.charAt(0);
}
}
}
|