Commonize into PathUtils; On Linux/BSD try to use xdg-open/x-www-browser if Java...
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / util / StringUtils.java
blob382f77e5a2f15b3a6719b8b7d021d4a581758fae
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.plugin.util;
12 import java.util.ArrayList;
13 import java.util.Arrays;
14 import java.util.Collection;
16 /**
17 * This class contains static methods which can be used for manipulating
18 * strings.
20 * @since 2017/11/23
22 public final class StringUtils
24 /**
25 * Not used.
27 * @since 2017/11/23
29 private StringUtils()
33 /**
34 * Splits the given string using the specified delimeters and outputs it
35 * to the given collection.
37 * @param __delim The delimeters to use.
38 * @param __s The string to split.
39 * @param __out The collection to place split strings into.
40 * @return {@code __out}
41 * @throws NullPointerException On null arguments.
42 * @since 2017/11/23
44 public static final Collection<String> basicSplit(char[] __delim,
45 String __s, Collection<String> __out)
46 throws NullPointerException
48 if (__delim == null || __s == null || __out == null)
49 throw new NullPointerException("NARG");
51 // Parse string
52 boolean dows = true;
53 int lastdelim = -2;
54 for (int i = 0, n = __s.length(), mark = 0; i <= n; i++)
56 // -1 is a special delimeter for the end of string because
57 // otherwise if the string does not end in a delimeter it will not
58 // be found
59 int c = (i == n ? -1 : __s.charAt(i));
61 // Is this a delimeter
62 if (c == lastdelim || c == -1 || StringUtils.__indexOf(__delim, (char)c) >= 0)
64 // Remember last delimeter for potential speed
65 lastdelim = c;
67 // If reading delimeters, clear flag and mark
68 // to remember the current index
69 if (dows)
71 dows = false;
72 mark = i;
75 // Otherwise end of sequence, generate string
76 else
78 // Split out
79 __out.add(__s.substring(mark, i));
81 // Switch to handling delimeters
82 dows = true;
86 // If reading delimeters, clear flag and mark
87 // to remember the current index, is not delimeters
88 // here
89 else if (dows)
91 dows = false;
92 mark = i;
96 // Return output always
97 return __out;
101 * Splits the given string using the specified delimeters and outputs it
102 * to the given collection.
104 * @param __delim The delimeters to use.
105 * @param __s The string to split.
106 * @param __out The collection to place split strings into.
107 * @return {@code __out}
108 * @throws NullPointerException On null arguments.
109 * @since 2017/11/23
111 public static final Collection<String> basicSplit(String __delim,
112 String __s, Collection<String> __out)
113 throws NullPointerException
115 if (__delim == null || __s == null || __out == null)
116 throw new NullPointerException("NARG");
118 return StringUtils.basicSplit(__delim.toCharArray(), __s, __out);
122 * Splits the given string using the specified delimeter.
124 * @param __delim The delimeter to use.
125 * @param __s The string to split.
126 * @return The split string.
127 * @throws NullPointerException On null arguments.
128 * @since 2018/12/23
130 public static final String[] basicSplit(char __delim, String __s)
132 return StringUtils.basicSplit(new char[]{__delim}, __s);
136 * Splits the given string using the specified delimeters.
138 * @param __delim The delimeters to use.
139 * @param __s The string to split.
140 * @return The split sequence of strings.
141 * @throws NullPointerException On null arguments.
142 * @since 2017/11/23
144 public static final String[] basicSplit(char[] __delim, String __s)
145 throws NullPointerException
147 if (__delim == null || __s == null)
148 throw new NullPointerException("NARG");
150 Collection<String> rv = StringUtils.basicSplit(__delim, __s,
151 new ArrayList<String>());
152 return rv.<String>toArray(new String[rv.size()]);
156 * Splits the given string using the specified delimeters.
158 * @param __delim The delimeters to use.
159 * @param __s The string to split.
160 * @return The split sequence of strings.
161 * @throws NullPointerException On null arguments.
162 * @since 2017/11/23
164 public static final String[] basicSplit(String __delim, String __s)
165 throws NullPointerException
167 if (__delim == null || __s == null)
168 throw new NullPointerException("NARG");
170 return StringUtils.basicSplit(__delim.toCharArray(), __s);
174 * Splits the specified string using the given delimeter and returns all
175 * of the fields which are contained within, any leading and trailing
176 * whitespace is trimmed.
178 * @param __delim The delimeter to split fields by.
179 * @param __s The string to split.
180 * @return An array containing all of the fields.
181 * @throws NullPointerException On null arguments.
182 * @since 2017/11/30
184 public static final String[] fieldSplitAndTrim(char __delim, String __s)
185 throws NullPointerException
187 if (__s == null)
188 throw new NullPointerException("NARG");
190 // Split and trim
191 String[] rv = StringUtils.fieldSplit(__delim, __s);
192 for (int i = 0, n = rv.length; i < n; i++)
193 rv[i] = rv[i].trim();
194 return rv;
198 * Splits the specified string using the given delimeter and returns all
199 * of the fields which are contained within. Extra whitespace within
200 * fields are not trimmed.
202 * @param __delim The delimeter to split fields by.
203 * @param __s The string to split.
204 * @return An array containing all of the fields.
205 * @throws NullPointerException On null arguments.
206 * @since 2017/11/30
208 public static final String[] fieldSplit(char __delim, String __s)
209 throws NullPointerException
211 if (__s == null)
212 throw new NullPointerException("NARG");
214 CharSequence[] xrv = CharSequenceUtils.fieldSplit(__delim, __s);
215 return Arrays.<String, CharSequence>copyOf(xrv, xrv.length,
216 String[].class);
220 * Searches the given sequence for the first occurrence of the specified
221 * character.
223 * @param __c The character to locate.
224 * @param __s The sequence to look inside.
225 * @return The index of the first occurrence.
226 * @throws NullPointerException On null arguments.
227 * @since 2017/11/30
229 public static final int firstIndex(char __c, String __s)
230 throws NullPointerException
232 return CharSequenceUtils.firstIndex(__c, __s);
236 * Searches the given string for the first occurrence of the specified
237 * characters.
239 * @param __c The characters to locate.
240 * @param __s The string to look inside.
241 * @return The index of the first occurrence.
242 * @throws NullPointerException On null arguments.
243 * @since 2017/11/30
245 public static final int firstIndex(char[] __c, String __s)
246 throws NullPointerException
248 return CharSequenceUtils.firstIndex(__c, __s);
252 * Searches the given string for the first occurrence of the specified
253 * characters.
255 * @param __c The characters to locate.
256 * @param __s The string to look inside.
257 * @return The index of the first occurrence.
258 * @throws NullPointerException On null arguments.
259 * @since 2017/11/30
261 public static final int firstIndex(String __c, String __s)
262 throws NullPointerException
264 return CharSequenceUtils.firstIndex(__c, __s);
268 * Searches the given string for the first occurrence of the specified
269 * characters. This assumes that the character set has already been
270 * sorted.
272 * @param __c The characters to locate, this is required to be sorted.
273 * @param __s The string to look inside.
274 * @return The index of the first occurrence.
275 * @throws NullPointerException On null arguments.
276 * @since 2017/11/30
278 public static final int firstIndexSorted(char[] __c, String __s)
279 throws NullPointerException
281 return CharSequenceUtils.firstIndexSorted(__c, __s);
285 * Returns an array containing all of the indexes that the specified
286 * character appears in the given string.
288 * @param __c The character to get the indexes for.
289 * @param __s The string to check in.
290 * @return An array containing the array indexes for the given character,
291 * if there are none then the array will be empty.
292 * @throws NullPointerException On null arguments.
293 * @since 2017/11/26
295 public static final int[] multipleIndexOf(char __c, String __s)
296 throws NullPointerException
298 return CharSequenceUtils.multipleIndexOf(__c, __s);
302 * Converts the specified string to lowercase ignoring locale, this uses
303 * {@link Character#toLowerCase(char)}.
305 * @param __s The string to convert.
306 * @return The lowercased string.
307 * @throws NullPointerException On null arguments.
308 * @since 2017/11/30
310 public static final String toLowerCaseNoLocale(String __s)
311 throws NullPointerException
313 if (__s == null)
314 throw new NullPointerException("NARG");
316 int n = __s.length();
317 StringBuilder sb = new StringBuilder(n);
318 for (int i = 0; i < n; i++)
319 sb.append(Character.toLowerCase(__s.charAt(i)));
320 return sb.toString();
324 * Converts the specified string to uppercase ignoring locale, this uses
325 * {@link Character#toUpperCase(char)}.
327 * @param __s The string to convert.
328 * @return The uppercased string.
329 * @throws NullPointerException On null arguments.
330 * @since 2017/11/30
332 public static final String toUpperCaseNoLocale(String __s)
333 throws NullPointerException
335 if (__s == null)
336 throw new NullPointerException("NARG");
338 int n = __s.length();
339 StringBuilder sb = new StringBuilder(n);
340 for (int i = 0; i < n; i++)
341 sb.append(Character.toUpperCase(__s.charAt(i)));
342 return sb.toString();
346 * Searches the input array to see if the given character is within the
347 * array.
349 * @param __a The array to check.
350 * @param __c The character to find in the array.
351 * @return The index of the character or {@code -1} if it was not found.
352 * @throws NullPointerException On null arguments.
353 * @since 2017/11/23
355 private static int __indexOf(char[] __a, char __c)
356 throws NullPointerException
358 if (__a == null)
359 throw new NullPointerException("NARG");
361 for (int i = 0, n = __a.length; i < n; i++)
362 if (__c == __a[i])
363 return i;
364 return -1;