20 package net.sf.gridarta.preferences;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24 import org.jetbrains.annotations.NotNull;
39 private static final Pattern[]
PATTERNS_ENCODE = { Pattern.compile(
"\\\\"), Pattern.compile(
"\r"), Pattern.compile(
"\n"), };
45 private static final String[]
REPLACEMENTS_ENCODE = { Matcher.quoteReplacement(
"\\\\"), Matcher.quoteReplacement(
"\\r"), Matcher.quoteReplacement(
"\\n"), };
52 private static final Pattern[]
PATTERNS_DECODE = { Pattern.compile(
"\\\\n"), Pattern.compile(
"\\\\r"), Pattern.compile(
"\\\\\\\\"), };
58 private static final String[]
REPLACEMENTS_DECODE = { Matcher.quoteReplacement(
"\n"), Matcher.quoteReplacement(
"\r"), Matcher.quoteReplacement(
"\\"), };
73 public static String
encode(@NotNull
final String str) {
74 assert PATTERNS_ENCODE.length == REPLACEMENTS_ENCODE.length;
76 for (
int i = 0; i < PATTERNS_ENCODE.length; i++) {
77 tmp = PATTERNS_ENCODE[i].matcher(tmp).replaceAll(REPLACEMENTS_ENCODE[i]);
89 public static String
decode(@NotNull
final String str) {
90 assert PATTERNS_DECODE.length == REPLACEMENTS_DECODE.length;
92 for (
int i = 0; i < PATTERNS_DECODE.length; i++) {
93 tmp = PATTERNS_DECODE[i].matcher(tmp).replaceAll(REPLACEMENTS_DECODE[i]);
static String encode(@NotNull final String str)
Encode a string to make it fit into one line.
static final String [] REPLACEMENTS_DECODE
The replacement strings for PATTERNS_DECODE.
static String decode(@NotNull final String str)
Decode a string which was encoded by encode(String).
static final Pattern [] PATTERNS_DECODE
Patterns that must be decoded.
Utility class to encode arbitrary Strings to fit in a single text line.
static final Pattern [] PATTERNS_ENCODE
Patterns that must be encoded.
static final String [] REPLACEMENTS_ENCODE
The replacement strings for PATTERNS_ENCODE.
Codec()
Private constructor to prevent instantiation.