@@ -91,7 +91,9 @@ public static List<String> split(String input, String pattern, int limit, List<S
9191 nextIndex = (nextIndex == -1 ) ? inputSize : nextIndex ;
9292 // If the matching string index is greater than or equal to the end
9393 // of the string, then break, there is no more string left to parse
94- if (indexPlusPatternSize > inputSize ) { break ; }
94+ if (indexPlusPatternSize > inputSize ) {
95+ break ;
96+ }
9597 // Add the new sub string between the end of the previous matching
9698 // pattern and the next matching pattern to the list of splits
9799 dst .add (input .substring (indexPlusPatternSize , nextIndex ));
@@ -147,7 +149,9 @@ public static String[] split(String input, String pattern, String[] dst) {
147149 nextIndex = (nextIndex == -1 ) ? inputSize : nextIndex ;
148150 // If the matching string index is greater than or equal to the end
149151 // of the string, then break, there is no more string left to parse
150- if (indexPlusPatternSize > inputSize ) { break ; }
152+ if (indexPlusPatternSize > inputSize ) {
153+ break ;
154+ }
151155 // Add the new sub string between the end of the previous matching
152156 // pattern and the next matching pattern to the list of splits
153157 dst [matchingCount ] = input .substring (indexPlusPatternSize , nextIndex );
@@ -222,7 +226,9 @@ public static List<String> split(String input, char splitAt, int limit, List<Str
222226 nextIndex = (nextIndex == -1 ) ? inputSize : nextIndex ;
223227 // If the matching string index is greater than or equal to the end
224228 // of the string, then break, there is no more string left to parse
225- if (indexPlusPatternSize > inputSize ) { break ; }
229+ if (indexPlusPatternSize > inputSize ) {
230+ break ;
231+ }
226232 // Add the new sub string between the end of the previous matching
227233 // pattern and the next matching pattern to the list of splits
228234 dst .add (input .substring (indexPlusPatternSize , nextIndex ));
@@ -277,7 +283,9 @@ public static String[] split(String input, char splitAt, String[] dst) {
277283 nextIndex = (nextIndex == -1 ) ? inputSize : nextIndex ;
278284 // If the matching string index is greater than or equal to the end
279285 // of the string, then break, there is no more string left to parse
280- if (indexPlusPatternSize > inputSize ) { break ; }
286+ if (indexPlusPatternSize > inputSize ) {
287+ break ;
288+ }
281289 // Add the new sub string between the end of the previous matching
282290 // pattern and the next matching pattern to the list of splits
283291 dst [matchingCount ] = input .substring (indexPlusPatternSize , nextIndex );
@@ -310,13 +318,10 @@ public static String findNthMatch(String input, String pattern, int childI, int
310318 if (expectedCount < 0 ) {
311319 throw new IllegalArgumentException ("expectedCount (" + expectedCount + ") must be greater than 0" );
312320 }
313- if (childI >= expectedCount ) {
321+ if (expectedCount > 0 && childI >= expectedCount ) {
314322 throw new IllegalStateException ("childI (" + childI + ") must be less than expected count (" + expectedCount + ")" );
315323 }
316324
317- // If the limit is zero (meaning an infinite number of splits) make the
318- // limit negative so that it never matches the comparison in the loop
319- expectedCount = (expectedCount == 0 ) ? -1 : expectedCount ;
320325 int inputSize = input .length ();
321326 int patternSize = pattern .length ();
322327 int index = 0 ;
@@ -333,15 +338,18 @@ public static String findNthMatch(String input, String pattern, int childI, int
333338 indexPlusPatternSize = index + patternSize ; // small optimization (me being picky)
334339 // Find the next matching string index after the current matching string index
335340 nextIndex = input .indexOf (pattern , indexPlusPatternSize );
336- // If the maximum number of strings have been match, set the next index to -1
337- // so that the next statement acts as if the end of the string has been reached
338- if (matchingCount == expectedCount - 1 && nextIndex > -1 ) { throw new IllegalStateException ("found more than " + expectedCount + " split strings" ); }
341+ // If the maximum number of strings have been matched and another is found, throw an exception
342+ if (matchingCount == expectedCount - 1 && nextIndex > -1 ) {
343+ throw new IllegalStateException ("found more than " + expectedCount + " split strings" );
344+ }
339345 // If no more matching strings can be found, include the remainder of
340346 // the string after the last matching string
341347 nextIndex = (nextIndex == -1 ) ? inputSize : nextIndex ;
342348 // If the matching string index is greater than or equal to the end
343349 // of the string, then break, there is no more string left to parse
344- if (indexPlusPatternSize > inputSize ) { break ; }
350+ if (indexPlusPatternSize > inputSize ) {
351+ break ;
352+ }
345353 // Add the new sub string between the end of the previous matching
346354 // pattern and the next matching pattern to the list of splits
347355 //dst.add(input.substring(indexPlusPatternSize, nextIndex));
@@ -359,7 +367,9 @@ public static String findNthMatch(String input, String pattern, int childI, int
359367 matchingCount ++;
360368 } while (index != -1 ); // While at end because (index = -patternSize) could equal -1 if the pattern size is -1
361369
362- if (expectedCount != 0 && matchingCount != expectedCount ) { throw new IllegalStateException ("found " + matchingCount + ", expected " + expectedCount + " split strings" ); }
370+ if (expectedCount != 0 && matchingCount != expectedCount ) {
371+ throw new IllegalStateException ("found " + matchingCount + ", expected " + expectedCount + " split strings" );
372+ }
363373 // so initialize everything first and run the first loop before evaluating the while statement
364374 // Return the string list
365375 return matchStr ;
@@ -389,7 +399,9 @@ public static int countMatches(String src, String pattern) {
389399 nextIndex = (nextIndex == -1 ) ? srcSize : nextIndex ;
390400 // If the matching string index is greater than or equal to the end
391401 // of the string, then break, there is no more string left to parse
392- if (indexPlusPatternSize > srcSize ) { break ; }
402+ if (indexPlusPatternSize > srcSize ) {
403+ break ;
404+ }
393405 // If the next found index is the end of the string, set the index
394406 // to -1 so that the outer while loop ends, else keep the current index
395407 index = (nextIndex == srcSize ) ? -1 : nextIndex ;
@@ -424,7 +436,9 @@ public static int countMatches(char[] src, int srcOff, int srcLen, char[] patter
424436 nextIndex = (nextIndex == -1 ) ? srcSize : nextIndex ;
425437 // If the matching string index is greater than or equal to the end
426438 // of the string, then break, there is no more string left to parse
427- if (indexPlusPatternSize > srcSize ) { break ; }
439+ if (indexPlusPatternSize > srcSize ) {
440+ break ;
441+ }
428442 // If the next found index is the end of the string, set the index
429443 // to -1 so that the outer while loop ends, else keep the current index
430444 index = (nextIndex == srcSize ) ? -1 : nextIndex ;
@@ -538,6 +552,23 @@ private static String _preLastMatch(String src, int idx) {
538552 }
539553
540554
555+ /** Returns the portions of a string before and after the last index of a character.
556+ * For example {@code firstMatchParts("abc-mid-123", '-')}, returns {@code Entry("abc-mid", "123")}
557+ * @param src the string to search
558+ * @param pattern the character to search for
559+ * @return an entry where the key is the portion of {@code src} before the last matching character
560+ * and the value is the portion of {@code src} after the last matching character
561+ */
562+ public static Map .Entry <String , String > lastMatchParts (String src , char patternChar ) {
563+ int idxPre = src .lastIndexOf (patternChar );
564+ String pre = src .substring (0 , (idxPre < 0 ? src .length () : idxPre ));
565+
566+ String post = src .substring (idxPre > -1 && idxPre + 1 >= src .length () ? src .length () : (idxPre < 0 ? src .length () : idxPre + 1 ));
567+
568+ return new AbstractMap .SimpleImmutableEntry <>(pre , post );
569+ }
570+
571+
541572 /** Returns the portions of a string before and after the last index of a sub-string.
542573 * For example {@code firstMatchParts("abc-mid-123", "-")}, returns {@code Entry("abc-mid", "123")}
543574 * @param src the string to search
@@ -549,8 +580,7 @@ public static Map.Entry<String, String> lastMatchParts(String src, String patter
549580 int idxPre = src .lastIndexOf (pattern );
550581 String pre = src .substring (0 , (idxPre < 0 ? src .length () : idxPre ));
551582
552- int idxPost = idxPre ;
553- String post = src .substring (idxPost > -1 && idxPost + pattern .length () >= src .length () ? src .length () : (idxPost < 0 ? src .length () : idxPost + pattern .length ()));
583+ String post = src .substring (idxPre > -1 && idxPre + pattern .length () >= src .length () ? src .length () : (idxPre < 0 ? src .length () : idxPre + pattern .length ()));
554584
555585 return new AbstractMap .SimpleImmutableEntry <>(pre , post );
556586 }
0 commit comments