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
.runtime
.cldc
.util
;
12 import java
.util
.Arrays
;
15 * This contains utilities which operate on character sequences.
19 public final class CharSequenceUtils
26 private CharSequenceUtils()
31 * Returns the position where the given string is found.
33 * @param __src The sequence to look within.
34 * @param __lookFor The sequence to find.
35 * @param __index The starting index.
36 * @return The index of the sequence or {@code -1} if it is not found.
39 public static int indexOf(CharSequence __src
, CharSequence __lookFor
,
42 if (__src
== null || __lookFor
== null)
43 throw new NullPointerException("NARG");
49 // If the sequence is empty, then it will always be a match
50 int srcLen
= __src
.length();
51 int lookLen
= __lookFor
.length();
55 // If the string is longer than ours, then it will never be a match
56 if (lookLen
> srcLen
- __index
)
59 // Do a long complicated loop matching, but we only need to check
60 // for as long as the sequence can actually fit
62 for (int srcAt
= __index
, lim
= (srcLen
- lookLen
) + 1;
65 // Check sequence characters
66 for (int x
= srcAt
, b
= 0; b
< lookLen
; x
++, b
++)
67 if (__src
.charAt(x
) != __lookFor
.charAt(b
))
70 // Since the inner loop continues to the outer, if this was reached
71 // then we know the full sequence was matched
75 // Otherwise, nothing was found because we tried every character
80 * Splits the specified character sequence using the given delimeter and
81 * returns all of the fields which are contained within. Extra whitespace
82 * within fields are not trimmed.
84 * @param __delim The delimeter to split fields by.
85 * @param __s The sequence to split.
86 * @return An array containing all of the fields.
87 * @throws NullPointerException On null arguments.
90 public static CharSequence
[] fieldSplit(char __delim
,
92 throws NullPointerException
95 throw new NullPointerException("NARG");
97 // Get all indexes of that given character
98 int[] ind
= CharSequenceUtils
.multipleIndexOf(__delim
, __s
);
99 int delCount
= ind
.length
;
101 int n
= delCount
+ 1;
102 CharSequence
[] rv
= new CharSequence
[n
];
103 for (int l
= -1, r
= 0, i
= 0; i
< n
; i
++, l
++, r
++)
104 rv
[i
] = __s
.subSequence((l
>= 0 ? ind
[l
] + 1 : 0),
105 (r
< delCount ? ind
[r
] : __s
.length()));
111 * Searches the given sequence for the first occurrence of the specified
114 * @param __c The character to locate.
115 * @param __s The sequence to look inside.
116 * @return The index of the first occurrence.
117 * @throws NullPointerException On null arguments.
120 public static int firstIndex(char __c
, CharSequence __s
)
121 throws NullPointerException
124 throw new NullPointerException("NARG");
126 for (int i
= 0, n
= __s
.length(); i
< n
; i
++)
127 if (__c
== __s
.charAt(i
))
133 * Searches the given sequence for the first occurrence of the specified
136 * @param __c The characters to locate.
137 * @param __s The sequence to look inside.
138 * @return The index of the first occurrence.
139 * @throws NullPointerException On null arguments.
142 public static int firstIndex(char[] __c
, CharSequence __s
)
143 throws NullPointerException
145 if (__c
== null || __s
== null)
146 throw new NullPointerException("NARG");
148 // For optimization sort the input array to find characters faster
152 // Forward to one which assumes sorted input
153 return CharSequenceUtils
.firstIndexSorted(__c
, __s
);
157 * Searches the given sequence for the first occurrence of the specified
160 * @param __c The characters to locate.
161 * @param __s The sequence to look inside.
162 * @return The index of the first occurrence.
163 * @throws NullPointerException On null arguments.
166 public static int firstIndex(String __c
, CharSequence __s
)
167 throws NullPointerException
169 if (__c
== null || __s
== null)
170 throw new NullPointerException("NARG");
172 return CharSequenceUtils
.firstIndex(__c
.toCharArray(), __s
);
176 * Searches the given sequence for the first occurrence of the specified
177 * characters. This assumes that the character set has already been
180 * @param __c The characters to locate, this is required to be sorted.
181 * @param __s The sequence to look inside.
182 * @return The index of the first occurrence.
183 * @throws NullPointerException On null arguments.
186 public static int firstIndexSorted(char[] __c
, CharSequence __s
)
187 throws NullPointerException
189 if (__c
== null || __s
== null)
190 throw new NullPointerException("NARG");
192 // Go through ever character
193 for (int i
= 0, n
= __s
.length(), y
= __c
.length
; i
< n
; i
++)
195 // Use binary search because it is faster than checking each
197 char c
= __s
.charAt(i
);
198 if (Arrays
.binarySearch(__c
, c
) >= 0)
207 * Returns an array containing all of the indexes that the specified
208 * character appears in the given sequence.
210 * @param __c The character to get the indexes for.
211 * @param __s The sequence to check in.
212 * @return An array containing the array indexes for the given character,
213 * if there are none then the array will be empty.
214 * @throws NullPointerException On null arguments.
217 public static int[] multipleIndexOf(char __c
, CharSequence __s
)
218 throws NullPointerException
221 throw new NullPointerException("NARG");
223 IntegerList list
= new IntegerList();
225 // Find every character index
226 for (int i
= 0, n
= __s
.length(); i
< n
; i
++)
228 char c
= __s
.charAt(i
);
230 // Add index to list if found
236 return list
.toIntegerArray();