Gridarta Editor
StringUtils.java
Go to the documentation of this file.
1 /*
2  * Gridarta MMORPG map editor for Crossfire, Daimonin and similar games.
3  * Copyright (C) 2000-2015 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.utils;
21 
22 import java.util.Arrays;
23 import java.util.regex.Pattern;
24 import org.jetbrains.annotations.NotNull;
25 import org.jetbrains.annotations.Nullable;
26 
31 public class StringUtils {
32 
36  @NotNull
37  public static final Pattern PATTERN_WHITESPACE = Pattern.compile("[\\x00-\\x09\\x0b\\x20]+");
38 
42  @NotNull
43  public static final Pattern PATTERN_WHITESPACE_NEWLINE = Pattern.compile("[\\x00-\\x0b\\x0d\\x20]+");
44 
48  @NotNull
49  private static final Pattern PATTERN_TRAILING_WHITESPACE = Pattern.compile("[\\x00-\\x09\\x0b\\x20]+$");
50 
54  @NotNull
55  private static final Pattern PATTERN_MULTI_LINE_TRAILING_WHITESPACE = Pattern.compile("(?m)[\\x00-\\x09\\x0b\\x20]+$");
56 
60  @NotNull
61  public static final Pattern PATTERN_END_OF_LINE = Pattern.compile("\\s*\n");
62 
66  @NotNull
67  public static final Pattern PATTERN_SPACE = Pattern.compile(" ");
68 
72  @NotNull
73  public static final Pattern PATTERN_SPACES = Pattern.compile(" +");
74 
78  @NotNull
79  public static final Pattern PATTERN_SLASH = Pattern.compile("/");
80 
84  @NotNull
85  public static final Pattern PATTERN_BACKSLASH = Pattern.compile("\\\\");
86 
90  @NotNull
91  public static final Pattern PATTERN_COLON = Pattern.compile(":");
92 
96  @NotNull
97  public static final Pattern PATTERN_COMMA = Pattern.compile(",");
98 
102  @NotNull
103  public static final Pattern PATTERN_EQUAL = Pattern.compile("=");
104 
108  @NotNull
109  public static final Pattern PATTERN_NEWLINE = Pattern.compile("\n");
110 
114  private StringUtils() {
115  }
116 
122  public static String removeTrailingWhitespace(@NotNull final CharSequence str) {
123  return PATTERN_TRAILING_WHITESPACE.matcher(str).replaceFirst("");
124  }
125 
131  public static String removeTrailingWhitespaceFromLines(@NotNull final CharSequence str) {
132  return PATTERN_MULTI_LINE_TRAILING_WHITESPACE.matcher(str).replaceAll("");
133  }
134 
143  public static CharSequence ensureTrailingNewline(@NotNull final String str) {
144  return str.length() <= 0 ? "" : str.endsWith("\n") ? str : str + "\n";
145  }
146 
158  @Nullable
159  public static CharSequence diffTextString(@NotNull final CharSequence base, @NotNull final String str, final boolean ignoreValues) {
160  for (final String line : PATTERN_END_OF_LINE.split(base, 0)) {
161  if (ignoreValues ? line.startsWith(str) : line.equals(str)) {
162  return line;
163  }
164  }
165  return null;
166  }
167 
177  @Nullable
178  public static String getAttribute(@NotNull final CharSequence attributes, @NotNull final String attributeName) {
179  final String str = attributeName + " ";
180  for (final String line : PATTERN_END_OF_LINE.split(attributes, 0)) {
181  if (line.startsWith(str)) {
182  return line.substring(str.length());
183  }
184  }
185  return null;
186  }
187 
193  @NotNull
194  public static String sortLines(@NotNull final CharSequence string) {
195  final String[] lines = PATTERN_NEWLINE.split(string, -1);
196  Arrays.sort(lines);
197  final StringBuilder sb = new StringBuilder();
198  for (final String line : lines) {
199  sb.append(line).append('\n');
200  }
201  return sb.toString();
202  }
203 
204 }
Utility class for string manipulation.
static final Pattern PATTERN_EQUAL
The pattern that matches a single equal sign ("=").
static String removeTrailingWhitespace(@NotNull final CharSequence str)
Removes trailing whitespace from a string.
static final Pattern PATTERN_SPACE
The pattern that matches a single space.
static final Pattern PATTERN_NEWLINE
The pattern that matches a single newline ("\n").
static CharSequence ensureTrailingNewline(@NotNull final String str)
Returns a given string which ends with a trailing newline character; empty strings remain empty...
static final Pattern PATTERN_SPACES
The pattern that matches a non-empty sequence of spaces.
static String getAttribute(@NotNull final CharSequence attributes, @NotNull final String attributeName)
Returns an attribute line from a set of attribute definitions.
static String removeTrailingWhitespaceFromLines(@NotNull final CharSequence str)
Removes trailing whitespace from all lines of a string.
static CharSequence diffTextString(@NotNull final CharSequence base, @NotNull final String str, final boolean ignoreValues)
Helper function for &#39;diffArchText()&#39;: Looks for occurrence of the attribute &#39;str&#39; in &#39;base&#39; and if fo...
StringUtils()
Private constructor to prevent instantiation.
static final Pattern PATTERN_COMMA
The pattern that matches a single comma (",").
static final Pattern PATTERN_WHITESPACE
Pattern to match whitespace excluding NL and CR.
static final Pattern PATTERN_BACKSLASH
The pattern that matches a single backslash ("\").
static final Pattern PATTERN_END_OF_LINE
The pattern to match end of line characters separating lines.
static final Pattern PATTERN_MULTI_LINE_TRAILING_WHITESPACE
Pattern to match trailing whitespace in a multi line string.
static final Pattern PATTERN_TRAILING_WHITESPACE
Pattern to match trailing whitespace.
static final Pattern PATTERN_COLON
The pattern that matches a single colon (":").
static final Pattern PATTERN_WHITESPACE_NEWLINE
Pattern to match whitespace including NL and CR.
static String sortLines(@NotNull final CharSequence string)
Sorts newline separated lines in a string.
static final Pattern PATTERN_SLASH
The pattern that matches a single slash ("/").