Skip to content

Commit fecc691

Browse files
committed
0.13.2 - Add 'StringSplit.split(String, char, int)', additional unit tests (close to 90% coverage!), and fixed a StringCommonality bug.
1 parent 6110d1f commit fecc691

15 files changed

+208
-77
lines changed

CHANGELOG.md

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

55

66
--------
7-
### [0.13.1](N/A) - 2019-09-29
7+
### [0.13.2](N/A) - 2019-09-30
8+
#### Added
9+
* `StringSplit.split(String, char, int)`
10+
* Additional unit tests
11+
12+
#### Fixed
13+
* `StringCommonality` several methods were failing for `direction = false` combined with `minIndex = 0`
14+
15+
16+
--------
17+
### [0.13.1](https://github.com/TeamworkGuy2/JTextUtil/commit/6110d1fa16c5187b7102a62fc96086cb907b5b94) - 2019-09-29
818
#### Added
919
* `StringSplit.lastMatchParts(String, char)`
1020

bin/jtext_util-with-tests.jar

201 Bytes
Binary file not shown.

bin/jtext_util.jar

53 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.1",
2+
"version" : "0.13.2",
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/stringSearch/StringCommonality.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public static int commonStringRange(int minIndex, int maxIndex, int startOffset,
128128
if(str1Len <= str1Off - iiOff || str2Len <= str2Off - iiOff) {
129129
return 0;
130130
}
131-
while((minOff - ii) > minIndex && ii < commonCount && str1.charAt(str1Off - (ii + iiOff)) == str2.charAt(str2Off - (ii + iiOff))) {
131+
while((minOff - (ii + iiOff)) >= minIndex && ii < commonCount && str1.charAt(str1Off - (ii + iiOff)) == str2.charAt(str2Off - (ii + iiOff))) {
132132
ii++;
133133
}
134134
}
@@ -143,7 +143,7 @@ public static int commonStringRange(int minIndex, int maxIndex, int startOffset,
143143
}
144144

145145
if(i != size - 1) {
146-
throw new IllegalStateException("collection had " + (i < size - 1 ? "fewer" : "more") + " values than expected based on .size()");
146+
throw new IllegalStateException("collection had " + (i < size - 1 ? "fewer" : "more") + " values than expected based on size()");
147147
}
148148

149149
return commonCount;

src/twg2/text/stringUtils/StringReplace.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,7 @@ public static int replaceStrings(Map<String, String> searchReplaceStrs, StringBu
305305
/**
306306
* @see #replaceStrings(char[], int, Collection, Collection, StringBuilder)
307307
*/
308-
public static int replaceStrings(String content, Collection<String> searchStrs, Collection<String> replaceStrs,
309-
StringBuilder dst) {
308+
public static int replaceStrings(String content, Collection<String> searchStrs, Collection<String> replaceStrs, StringBuilder dst) {
310309
dst.append(content);
311310
return replaceStrings(searchStrs, replaceStrs, dst, dst.length());
312311
}
@@ -315,8 +314,7 @@ public static int replaceStrings(String content, Collection<String> searchStrs,
315314
/**
316315
* @see #replaceStrings(char[], int, Collection, Collection, StringBuilder)
317316
*/
318-
public static int replaceStrings(String content, int contentOff, Collection<String> searchStrs, Collection<String> replaceStrs,
319-
StringBuilder dst) {
317+
public static int replaceStrings(String content, int contentOff, Collection<String> searchStrs, Collection<String> replaceStrs, StringBuilder dst) {
320318
dst.append(content, contentOff, content.length() - contentOff);
321319
return replaceStrings(searchStrs, replaceStrs, dst, dst.length());
322320
}
@@ -337,8 +335,7 @@ public static int replaceStrings(String content, int contentOff, Collection<Stri
337335
* replaced with corresponding {@code replaceStrs}
338336
* @return the length change of {@code dst} after replacing the matching strings in {@code content}
339337
*/
340-
public static int replaceStrings(char[] content, int contentOffset, Collection<String> searchStrs, Collection<String> replaceStrs,
341-
StringBuilder dst) {
338+
public static int replaceStrings(char[] content, int contentOffset, Collection<String> searchStrs, Collection<String> replaceStrs, StringBuilder dst) {
342339
dst.append(content, contentOffset, content.length - contentOffset);
343340
return replaceStrings(searchStrs, replaceStrs, dst, dst.length());
344341
}

src/twg2/text/stringUtils/StringSplit.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public class StringSplit {
2525

2626

2727
/** A slightly faster version of {@link String#split(String)} that does not
28-
* used {@link Pattern}, instead the pattern is interpreted literally
29-
* and the input string is split based on the literal split string.
30-
* @param input the input char sequence to split
28+
* use {@link Pattern}, instead the pattern is interpreted literally
29+
* and the input string is split based on the literal {@code pattern} string.
30+
* @param input the input string to split
3131
* @param pattern the exact pattern to find and split around
3232
* @param limit the maximum number of splits to make, zero indicates that
3333
* an infinite number of splits can occur
@@ -51,7 +51,7 @@ public static List<String> split(String input, String pattern, List<String> dst)
5151
/** A slightly faster version of {@link String#split(String)} that does not
5252
* used {@link Pattern}, instead the pattern is interpreted literally
5353
* and the input string is split based on the literal split string.
54-
* @param input the input char sequence to split
54+
* @param input the input string to split
5555
* @param pattern the exact pattern to find and split around
5656
* @param limit the maximum number of splits to make, zero indicates that
5757
* an infinite number of splits can occur
@@ -115,7 +115,7 @@ public static List<String> split(String input, String pattern, int limit, List<S
115115
* This method is more space efficient than the {@link StringSplit#split(String, String, int)} version
116116
* since no internal structure is created to store the split strings, instead the array provided
117117
* is filled with the split strings.
118-
* @param input the input char sequence to split
118+
* @param input the input string to split
119119
* @param pattern the exact pattern to find and split around
120120
* @param dst an array of strings equal in length to the number of strings to split the {@code input} string into
121121
* @return the {@code results} array of strings passed into the method
@@ -176,6 +176,22 @@ public static List<String> split(String input, char splitAt) {
176176
}
177177

178178

179+
/** A slightly faster version of {@link String#split(String)} that does not
180+
* use {@link Pattern}, instead the pattern is interpreted literally
181+
* and the input string is split based on the literal {@code pattern} character.
182+
* @param input the input string to split
183+
* @param pattern the exact pattern to find and split around
184+
* @param limit the maximum number of splits to make, zero indicates that
185+
* an infinite number of splits can occur
186+
* @return an array of strings containing the resulting stings after
187+
* splitting the input string
188+
*/
189+
public static String[] split(String input, char pattern, int limit) {
190+
List<String> strs = split(input, pattern, limit, null);
191+
return strs.toArray(new String[strs.size()]);
192+
}
193+
194+
179195
/**
180196
* @see #split(String, char, int, List)
181197
*/
@@ -186,8 +202,8 @@ public static List<String> split(String input, char splitAt, List<String> dst) {
186202

187203
/** A slightly faster version of {@link String#split(String)} that does not
188204
* used {@link Pattern}, instead the string is split based on a specific char.<br>
189-
* @param input the input char sequence to split
190-
* @param splitAt the exact string to find and split around
205+
* @param input the input string to split
206+
* @param splitAt the exact character to find and split around
191207
* @param limit the maximum number of splits to make, zero indicates that
192208
* an infinite number of splits can occur
193209
* @param dst the destination list to add the split strings to
@@ -249,8 +265,8 @@ public static List<String> split(String input, char splitAt, int limit, List<Str
249265
* This method is more space efficient than the {@link StringSplit#split(String, String, int)} version
250266
* since no internal structure is created to store the split strings, instead the array provided
251267
* is filled with the split strings.
252-
* @param input the input char sequence to split
253-
* @param splitAt the exact string to find and split around
268+
* @param input the input string to split
269+
* @param splitAt the exact character to find and split around
254270
* @param dst an array of strings equal in length to the number of strings to split the {@code input} string into
255271
* @return the {@code results} array of strings passed into the method
256272
*/
@@ -305,7 +321,7 @@ public static String[] split(String input, char splitAt, String[] dst) {
305321
/** A slightly faster version of {@link String#split(String)} that does not
306322
* used {@link Pattern}, instead the pattern is interpreted literally
307323
* and the input string is split based on the literal split string.
308-
* @param input the input char sequence to split
324+
* @param input the input string to split
309325
* @param pattern the exact pattern to find and split around
310326
* @param childI the index of the matching substring to retrieve
311327
* @param expectedCount the expected number of string when the string is split using the pattern, 0 if the expected count is not know

test/twg2/text/test/DataUnescapePartialQuoted.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ public class DataUnescapePartialQuoted {
1616
"2. abc, xyz",
1717
"3. \"abc, xyz\"], ",
1818
"4. \\\"\"",
19-
"5. , "
19+
"5. \"a, b\", c",
20+
"6. , "
2021
);
2122

2223
public static List<String> expected = Arrays.asList(
@@ -25,6 +26,7 @@ public class DataUnescapePartialQuoted {
2526
" abc",
2627
" \"abc, xyz\"",
2728
"\\\"\"",
29+
"a, b",
2830
""
2931
);
3032

@@ -35,18 +37,20 @@ public class DataUnescapePartialQuoted {
3537
inputs.get(2).lastIndexOf(','),
3638
inputs.get(3).lastIndexOf("]"),
3739
inputs.get(4).lastIndexOf("\"") + 1,
38-
inputs.get(5).lastIndexOf(',')
40+
inputs.get(5).lastIndexOf("\"") + 1,
41+
inputs.get(6).lastIndexOf(',')
3942
);
4043

4144

4245
/** The index of the last ending character (i.e. if the string is a quoted string followed by an ending char, return the index of the ending char, not the closing quote */
43-
public static List<Integer> expectedIndexesIncludingTrueEndingChar = Arrays.asList(
46+
public static List<Integer> _expectedIndexesIncludingTrueEndingChar = Arrays.asList(
4447
inputs.get(0).lastIndexOf("\"") + 1,
4548
inputs.get(1).lastIndexOf(','),
4649
inputs.get(2).lastIndexOf(','),
4750
inputs.get(3).lastIndexOf("]") + 1,
4851
inputs.get(4).lastIndexOf("\"") + 1,
49-
inputs.get(5).lastIndexOf(',')
52+
inputs.get(5).lastIndexOf("\"") + 1,
53+
inputs.get(6).lastIndexOf(',')
5054
);
5155

5256

test/twg2/text/test/StringCommonalityTest.java

Lines changed: 49 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -19,48 +19,70 @@ public class StringCommonalityTest {
1919

2020
@Test
2121
public void findPrefixTest() {
22-
new CommonalityData(true, Arrays.asList(of("alp", 0), of("p", 2), of("", 8)), Arrays.asList(
22+
List<String> strs = Arrays.asList(
2323
"alpha, beta, gamma",
2424
"alphabet",
2525
"alpine"
26-
)).test();
26+
);
27+
Assert.assertEquals("alp", StringCommonality.findPrefix(0, strs));
28+
Assert.assertEquals("p", StringCommonality.findPrefix(2, strs));
29+
Assert.assertEquals("", StringCommonality.findPrefix(8, strs));
2730

28-
new CommonalityData(true, Arrays.asList(of("123", 0), of("", 3), of("", 6)), Arrays.asList(
31+
strs = Arrays.asList(
2932
"12345678",
3033
"12345",
3134
"123"
32-
)).test();
35+
);
36+
Assert.assertEquals("123", StringCommonality.findPrefix(0, strs));
37+
Assert.assertEquals("", StringCommonality.findPrefix(3, strs));
38+
Assert.assertEquals("", StringCommonality.findPrefix(6, strs));
3339
}
3440

3541

3642
@Test
3743
public void findSuffixTest() {
38-
new CommonalityData(false, Arrays.asList(of("ing", 0), of("i", 2), of("", 4)), Arrays.asList(
44+
List<String> strs = Arrays.asList(
3945
"sing",
4046
"alphabetizing",
4147
"-ing"
42-
)).test();
48+
);
49+
Assert.assertEquals("ing", StringCommonality.findSuffix(0, strs));
50+
Assert.assertEquals("i", StringCommonality.findSuffix(2, strs));
51+
Assert.assertEquals("", StringCommonality.findSuffix(4, strs));
4352

44-
new CommonalityData(false, Arrays.asList(of("abc", 0), of("a", 5), of("", 6)), Arrays.asList(
53+
strs = Arrays.asList(
4554
"zabcabc",
4655
"alphabetan-abc",
4756
"a--abc"
48-
)).test();
57+
);
58+
Assert.assertEquals("abc", StringCommonality.findSuffix(0, strs));
59+
Assert.assertEquals("a", StringCommonality.findSuffix(5, strs));
60+
Assert.assertEquals("", StringCommonality.findSuffix(6, strs));
4961
}
5062

5163

5264
@Test
5365
public void findStringPortionTest() {
54-
List<Entry<String, Integer>> suffixesAndOffsets = Arrays.asList(of("wxy", 4), of("w", 2), of("", 5));
5566
List<String> strs = Arrays.asList(
5667
"-1wxyxy",
5768
"-2wxy",
5869
"-3wxyz"
5970
);
6071

61-
for(Entry<String, Integer> suffixOffset : suffixesAndOffsets) {
62-
Assert.assertEquals(suffixOffset.getKey(), StringCommonality.commonStringPortion(1, 6, suffixOffset.getValue(), strs, false, false));
63-
}
72+
// forward
73+
Assert.assertEquals("y", StringCommonality.commonStringPortion(1, 6, 4, strs, true, false));
74+
Assert.assertEquals("wxy", StringCommonality.commonStringPortion(1, 6, 2, strs, true, false));
75+
Assert.assertEquals("", StringCommonality.commonStringPortion(1, 6, 5, strs, true, false));
76+
77+
// backward
78+
Assert.assertEquals("wxy", StringCommonality.commonStringPortion(1, 6, 4, strs, false, false));
79+
Assert.assertEquals("w", StringCommonality.commonStringPortion(1, 6, 2, strs, false, false));
80+
Assert.assertEquals("", StringCommonality.commonStringPortion(1, 6, 5, strs, false, false));
81+
Assert.assertEquals("-", StringCommonality.commonStringPortion(0, 6, 0, strs, false, false));
82+
83+
// backward from end of string (won't match anything)
84+
Assert.assertEquals("", StringCommonality.commonStringPortion(1, 6, 1, strs, false, true));
85+
Assert.assertEquals("", StringCommonality.commonStringPortion(1, 6, 4, strs, false, true));
6486
}
6587

6688

@@ -82,9 +104,9 @@ public void commonSuffixTest() {
82104
}
83105

84106

107+
@SuppressWarnings("unchecked")
85108
@Test
86109
public void findPrefixEntriesTest() {
87-
@SuppressWarnings("unchecked")
88110
Entry<String, Integer>[] entries = new Entry[] {
89111
of("String-Aa", 1),
90112
of("String=Ab", 2),
@@ -96,44 +118,27 @@ public void findPrefixEntriesTest() {
96118
Assert.assertEquals("String", StringCommonality.findPrefix(0, entries));
97119
Assert.assertEquals("tring", StringCommonality.findPrefix(1, entries));
98120
Assert.assertEquals("A", StringCommonality.findPrefix(7, entries));
99-
}
100-
101-
102-
private static <K, V> Entry<K, V> of(K key, V val) {
103-
return new AbstractMap.SimpleImmutableEntry<>(key, val);
104-
}
105-
106-
}
107-
108121

122+
entries = new Entry[] {
123+
of("-aaa", 1),
124+
of("-aa", 2),
125+
of("-a", 3),
126+
};
109127

128+
Assert.assertEquals("-a", StringCommonality.findPrefix(0, entries));
110129

111-
/**
112-
* @author TeamworkGuy2
113-
* @since 2015-5-9
114-
*/
115-
class CommonalityData {
116-
boolean prefix;
117-
List<Entry<String, Integer>> suffixesAndOffsets;
118-
List<String> strs;
119-
130+
entries = new Entry[] {
131+
of("-a", 3),
132+
of("-aa", 2),
133+
of("-aaa", 1),
134+
};
120135

121-
public CommonalityData(boolean prefix, List<Entry<String, Integer>> suffixesAndOffsets, List<String> strs) {
122-
this.prefix = prefix;
123-
this.suffixesAndOffsets = suffixesAndOffsets;
124-
this.strs = strs;
136+
Assert.assertEquals("-a", StringCommonality.findPrefix(0, entries));
125137
}
126138

127139

128-
void test() {
129-
for(Entry<String, Integer> suffixOffset : suffixesAndOffsets) {
130-
if(prefix) {
131-
Assert.assertEquals(suffixOffset.getKey(), StringCommonality.findPrefix(suffixOffset.getValue(), strs));
132-
}
133-
else {
134-
Assert.assertEquals(suffixOffset.getKey(), StringCommonality.findSuffix(suffixOffset.getValue(), strs));
135-
}
136-
}
140+
private static <K, V> Entry<K, V> of(K key, V val) {
141+
return new AbstractMap.SimpleImmutableEntry<>(key, val);
137142
}
138143

139144
}

test/twg2/text/test/StringEscapeJsonTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,24 @@ public void jsonStr() {
2626
"arc \u2460",
2727
};
2828

29+
StringBuilder sb = new StringBuilder();
30+
2931
for(int i = 0, size = raw.length; i < size; i++) {
32+
// toJson
3033
String jsonFromRaw = StringEscapeJson.toJsonString(raw[i]);
3134
Assert.assertEquals(json[i], jsonFromRaw);
3235

36+
StringEscapeJson.toJsonString(raw[i], sb);
37+
Assert.assertEquals(json[i], sb.toString());
38+
sb.setLength(0);
39+
40+
// fromJson
3341
String rawFromJson = StringEscapeJson.fromJsonString(jsonFromRaw);
3442
Assert.assertEquals(raw[i], rawFromJson);
43+
44+
StringEscapeJson.fromJsonString(jsonFromRaw, sb);
45+
Assert.assertEquals(raw[i], sb.toString());
46+
sb.setLength(0);
3547
}
3648
}
3749

0 commit comments

Comments
 (0)