Skip to content

Commit 6110d1f

Browse files
committed
0.13.1 - last commit documentation was incorrect, it added 'firstMatchParts(String, char)'. This commit adds 'StringSplit.lastMatchParts(String, char) and a bug fix for 'StringSplit.findNthMatch()'.
1 parent 57ffe72 commit 6110d1f

File tree

6 files changed

+69
-21
lines changed

6 files changed

+69
-21
lines changed

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,19 @@ This project does its best to adhere to [Semantic Versioning](http://semver.org/
44

55

66
--------
7-
### [0.13.0](N/A) - 2019-09-29
7+
### [0.13.1](N/A) - 2019-09-29
88
#### Added
99
* `StringSplit.lastMatchParts(String, char)`
10+
11+
#### Fixed
12+
* `StringSplit.findNthMatch()` incorrectly threw exception when zero was passed for the `expectedCount` argument
13+
* Fixed previous release documentation from `lastMatchParts()` to `firstMatchParts()`
14+
15+
16+
--------
17+
### [0.13.0](https://github.com/TeamworkGuy2/JTextUtil/commit/57ffe72e636212dc8853f238b57ff2a227541350) - 2019-09-29
18+
#### Added
19+
* `StringSplit.firstMatchParts(String, char)`
1020
* Additional unit tests
1121

1222
#### Changed

bin/jtext_util-with-tests.jar

227 Bytes
Binary file not shown.

bin/jtext_util.jar

119 Bytes
Binary file not shown.

package-lib.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version" : "0.13.0",
2+
"version" : "0.13.1",
33
"name" : "jtext-util",
44
"description" : "String search, replace, and transform functions that provide more control, at the trade-off of verbosity, than those already in the Java API",
55
"homepage" : "https://github.com/TeamworkGuy2/JTextUtil",

src/twg2/text/stringUtils/StringSplit.java

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

test/twg2/text/test/StringSplitTest.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,12 @@ public void stringCharSplitDst() {
154154
@Test
155155
public void stringSplitNthChildTest() {
156156
Assert.assertEquals(" def", StringSplit.findNthMatch("abc, def, ghi", ",", 1, 3));
157+
Assert.assertEquals(" def", StringSplit.findNthMatch("abc, def, ghi", ",", 1, 0));
157158

158-
CheckTask.assertException(() -> StringSplit.findNthMatch("abc, def, ghi", ",", 1, 2));
159-
CheckTask.assertException(() -> StringSplit.findNthMatch("abc, def, ghi", ",", 1, 4));
159+
CheckTask.assertException(() -> StringSplit.findNthMatch("abc, def, ghi", ",", 1, 2)); // expected count too small
160+
CheckTask.assertException(() -> StringSplit.findNthMatch("abc, def, ghi", ",", 1, 4)); // expected count too big
161+
CheckTask.assertException(() -> StringSplit.findNthMatch("abc, def, ghi", ",", 1, -1)); // negative expected count
162+
CheckTask.assertException(() -> StringSplit.findNthMatch("abc, def, ghi", ",", 3, 2)); // childI greater than expected count
160163
}
161164

162165

@@ -274,6 +277,11 @@ public void lastMatchPartsTest() {
274277
Assert.assertEquals(entry("", ""), StringSplit.lastMatchParts("", "-"));
275278
Assert.assertEquals(entry("a", ""), StringSplit.lastMatchParts("a-", "-"));
276279
Assert.assertEquals(entry("abc", ""), StringSplit.lastMatchParts("abc", "-"));
280+
281+
Assert.assertEquals(entry("abc-def", "ghi"), StringSplit.lastMatchParts("abc-def-ghi", '-'));
282+
Assert.assertEquals(entry("", ""), StringSplit.lastMatchParts("", '-'));
283+
Assert.assertEquals(entry("a", ""), StringSplit.lastMatchParts("a-", '-'));
284+
Assert.assertEquals(entry("abc", ""), StringSplit.lastMatchParts("abc", '-'));
277285
}
278286

279287

0 commit comments

Comments
 (0)