1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
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
;
17 * This class contains static methods which can be used for manipulating
22 public final class StringUtils
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.
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");
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
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
67 // If reading delimeters, clear flag and mark
68 // to remember the current index
75 // Otherwise end of sequence, generate string
79 __out
.add(__s
.substring(mark
, i
));
81 // Switch to handling delimeters
86 // If reading delimeters, clear flag and mark
87 // to remember the current index, is not delimeters
96 // Return output always
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.
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.
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.
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.
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.
184 public static final String
[] fieldSplitAndTrim(char __delim
, String __s
)
185 throws NullPointerException
188 throw new NullPointerException("NARG");
191 String
[] rv
= StringUtils
.fieldSplit(__delim
, __s
);
192 for (int i
= 0, n
= rv
.length
; i
< n
; i
++)
193 rv
[i
] = rv
[i
].trim();
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.
208 public static final String
[] fieldSplit(char __delim
, String __s
)
209 throws NullPointerException
212 throw new NullPointerException("NARG");
214 CharSequence
[] xrv
= CharSequenceUtils
.fieldSplit(__delim
, __s
);
215 return Arrays
.<String
, CharSequence
>copyOf(xrv
, xrv
.length
,
220 * Searches the given sequence for the first occurrence of the specified
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.
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
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.
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
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.
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
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.
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.
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.
310 public static final String
toLowerCaseNoLocale(String __s
)
311 throws NullPointerException
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.
332 public static final String
toUpperCaseNoLocale(String __s
)
333 throws NullPointerException
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
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.
355 private static int __indexOf(char[] __a
, char __c
)
356 throws NullPointerException
359 throw new NullPointerException("NARG");
361 for (int i
= 0, n
= __a
.length
; i
< n
; i
++)