Gridarta Editor
FilterCodecUtils.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.model.filter;
21 
22 import org.jetbrains.annotations.NotNull;
23 
27 public class FilterCodecUtils {
28 
32  private FilterCodecUtils() {
33  }
34 
40  public static void encodeString(@NotNull final StringBuilder sb, @NotNull final String string) {
41  for (final char ch : string.toCharArray()) {
42  if (ch == '%' || ch == ',' || ch == ')' || ch == '=' || Character.isISOControl(ch)) {
43  sb.append(String.format("%%%04X", (int) ch));
44  } else {
45  sb.append(ch);
46  }
47  }
48  }
49 
59  @NotNull
60  public static String decodeString(@NotNull final StringBuilder sb) {
61  final StringBuilder result = new StringBuilder();
62  while (sb.length() > 0) {
63  final char ch = sb.charAt(0);
64  if (ch == ',' || ch == ')' || ch == '=') {
65  break;
66  }
67  if (ch == '%') {
68  result.append((char) Integer.parseInt(sb.substring(1, 5), 16));
69  sb.delete(0, 5);
70  } else {
71  result.append(ch);
72  sb.delete(0, 1);
73  }
74  }
75  return result.toString();
76  }
77 
78 }
net.sf.gridarta.model.filter.FilterCodecUtils.encodeString
static void encodeString(@NotNull final StringBuilder sb, @NotNull final String string)
Encodes a string configuration to a string builder.
Definition: FilterCodecUtils.java:40
net.sf.gridarta.model.filter.FilterCodecUtils.decodeString
static String decodeString(@NotNull final StringBuilder sb)
Decodes a string from a string builder.
Definition: FilterCodecUtils.java:60
net.sf.gridarta.model.filter.FilterCodecUtils.FilterCodecUtils
FilterCodecUtils()
Private constructor to prevent instantiation.
Definition: FilterCodecUtils.java:32
net.sf.gridarta.model.filter.FilterCodecUtils
Utility class for codec related functions.
Definition: FilterCodecUtils.java:27