Gridarta Editor
Codec.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2023 The Gridarta Developers.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package net.sf.gridarta.preferences;
21 
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
24 import org.jetbrains.annotations.NotNull;
25 
32 public class Codec {
33 
38  @NotNull
39  private static final Pattern @NotNull [] PATTERNS_ENCODE = { Pattern.compile("\\\\"), Pattern.compile("\r"), Pattern.compile("\n"), };
40 
44  @NotNull
45  private static final String @NotNull [] REPLACEMENTS_ENCODE = { Matcher.quoteReplacement("\\\\"), Matcher.quoteReplacement("\\r"), Matcher.quoteReplacement("\\n"), };
46 
51  @NotNull
52  private static final Pattern @NotNull [] PATTERNS_DECODE = { Pattern.compile("\\\\n"), Pattern.compile("\\\\r"), Pattern.compile("\\\\\\\\"), };
53 
57  @NotNull
58  private static final String @NotNull [] REPLACEMENTS_DECODE = { Matcher.quoteReplacement("\n"), Matcher.quoteReplacement("\r"), Matcher.quoteReplacement("\\"), };
59 
63  private Codec() {
64  }
65 
72  @NotNull
73  public static String encode(@NotNull final String str) {
74  final StringBuilder sb = new StringBuilder();
75  for (int i = 0; i < str.length(); i++) {
76  final char ch = str.charAt(i);
77  switch (ch) {
78  case '\r':
79  sb.append("\\r");
80  break;
81 
82  case '\n':
83  sb.append("\\n");
84  break;
85 
86  case '\\':
87  sb.append("\\\\");
88  break;
89 
90  default:
91  sb.append(ch);
92  break;
93  }
94  }
95  return sb.toString();
96  }
97 
104  @NotNull
105  public static String decode(@NotNull final String str) {
106  final StringBuilder sb = new StringBuilder();
107  int i = 0;
108  while (i < str.length()) {
109  final char ch = str.charAt(i);
110  if (ch == '\\' && i + 1 < str.length()) {
111  i++;
112  final char ch2 = str.charAt(i);
113  switch (ch2) {
114  case 'r':
115  sb.append('\r');
116  break;
117 
118  case 'n':
119  sb.append('\n');
120  break;
121 
122  case '\\':
123  sb.append('\\');
124  break;
125 
126  default:
127  // invalid => do not replace
128  sb.append('\\');
129  sb.append(ch2);
130  break;
131  }
132  } else {
133  sb.append(ch);
134  }
135  i++;
136  }
137  return sb.toString();
138  }
139 
140 }
net.sf.gridarta.preferences.Codec.Codec
Codec()
Private constructor to prevent instantiation.
Definition: Codec.java:63
net.sf.gridarta.preferences.Codec.encode
static String encode(@NotNull final String str)
Encode a string to make it fit into one line.
Definition: Codec.java:73
net.sf.gridarta.preferences.Codec
Utility class to encode arbitrary Strings to fit in a single text line.
Definition: Codec.java:32
net.sf.gridarta.preferences.Codec.REPLACEMENTS_ENCODE
static final String[] REPLACEMENTS_ENCODE
The replacement strings for PATTERNS_ENCODE.
Definition: Codec.java:45
net.sf.gridarta.preferences.Codec.REPLACEMENTS_DECODE
static final String[] REPLACEMENTS_DECODE
The replacement strings for PATTERNS_DECODE.
Definition: Codec.java:58
net.sf.gridarta.preferences.Codec.PATTERNS_ENCODE
static final Pattern[] PATTERNS_ENCODE
Patterns that must be encoded.
Definition: Codec.java:39
net.sf.gridarta.preferences.Codec.PATTERNS_DECODE
static final Pattern[] PATTERNS_DECODE
Patterns that must be decoded.
Definition: Codec.java:52
net.sf.gridarta.preferences.Codec.decode
static String decode(@NotNull final String str)
Decode a string which was encoded by encode(String).
Definition: Codec.java:105