Make Exported just be SquirrelJMEVendorApi.
[SquirrelJME.git] / modules / cldc-compact / src / main / java / cc / squirreljme / runtime / cldc / util / CharSequenceUtils.java
blob10fcb045dc6ab1656fe2b409aae2086cf0e28a39
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.runtime.cldc.util;
12 import java.util.Arrays;
14 /**
15 * This contains utilities which operate on character sequences.
17 * @since 2017/11/30
19 public final class CharSequenceUtils
21 /**
22 * Not used.
24 * @since 2017/11/30
26 private CharSequenceUtils()
30 /**
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.
37 * @since 2019/05/14
39 public static int indexOf(CharSequence __src, CharSequence __lookFor,
40 int __index)
42 if (__src == null || __lookFor == null)
43 throw new NullPointerException("NARG");
45 // Normalize position
46 if (__index < 0)
47 __index = 0;
49 // If the sequence is empty, then it will always be a match
50 int srcLen = __src.length();
51 int lookLen = __lookFor.length();
52 if (lookLen <= 0)
53 return __index;
55 // If the string is longer than ours, then it will never be a match
56 if (lookLen > srcLen - __index)
57 return -1;
59 // Do a long complicated loop matching, but we only need to check
60 // for as long as the sequence can actually fit
61 __outer:
62 for (int srcAt = __index, lim = (srcLen - lookLen) + 1;
63 srcAt < lim; srcAt++)
65 // Check sequence characters
66 for (int x = srcAt, b = 0; b < lookLen; x++, b++)
67 if (__src.charAt(x) != __lookFor.charAt(b))
68 continue __outer;
70 // Since the inner loop continues to the outer, if this was reached
71 // then we know the full sequence was matched
72 return srcAt;
75 // Otherwise, nothing was found because we tried every character
76 return -1;
79 /**
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.
88 * @since 2017/11/30
90 public static CharSequence[] fieldSplit(char __delim,
91 CharSequence __s)
92 throws NullPointerException
94 if (__s == null)
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()));
107 return rv;
111 * Searches the given sequence for the first occurrence of the specified
112 * character.
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.
118 * @since 2017/11/30
120 public static int firstIndex(char __c, CharSequence __s)
121 throws NullPointerException
123 if (__s == null)
124 throw new NullPointerException("NARG");
126 for (int i = 0, n = __s.length(); i < n; i++)
127 if (__c == __s.charAt(i))
128 return i;
129 return -1;
133 * Searches the given sequence for the first occurrence of the specified
134 * characters.
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.
140 * @since 2017/11/30
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
149 __c = __c.clone();
150 Arrays.sort(__c);
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
158 * characters.
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.
164 * @since 2017/11/30
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
178 * sorted.
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.
184 * @since 2017/11/30
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
196 // and every element
197 char c = __s.charAt(i);
198 if (Arrays.binarySearch(__c, c) >= 0)
199 return i;
202 // Not found
203 return -1;
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.
215 * @since 2017/11/26
217 public static int[] multipleIndexOf(char __c, CharSequence __s)
218 throws NullPointerException
220 if (__s == null)
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
231 if (c == __c)
232 list.addInteger(i);
235 // Finish
236 return list.toIntegerArray();