Skip to content

Commit

Permalink
0.13.2 - Add 'StringSplit.split(String, char, int)', additional unit …
Browse files Browse the repository at this point in the history
…tests (close to 90% coverage!), and fixed a StringCommonality bug.
  • Loading branch information
TeamworkGuy2 committed Oct 1, 2019
1 parent 6110d1f commit fecc691
Show file tree
Hide file tree
Showing 15 changed files with 208 additions and 77 deletions.
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,17 @@ This project does its best to adhere to [Semantic Versioning](http://semver.org/


--------
### [0.13.1](N/A) - 2019-09-29
### [0.13.2](N/A) - 2019-09-30
#### Added
* `StringSplit.split(String, char, int)`
* Additional unit tests

#### Fixed
* `StringCommonality` several methods were failing for `direction = false` combined with `minIndex = 0`


--------
### [0.13.1](https://github.com/TeamworkGuy2/JTextUtil/commit/6110d1fa16c5187b7102a62fc96086cb907b5b94) - 2019-09-29
#### Added
* `StringSplit.lastMatchParts(String, char)`

Expand Down
Binary file modified bin/jtext_util-with-tests.jar
Binary file not shown.
Binary file modified bin/jtext_util.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion package-lib.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version" : "0.13.1",
"version" : "0.13.2",
"name" : "jtext-util",
"description" : "String search, replace, and transform functions that provide more control, at the trade-off of verbosity, than those already in the Java API",
"homepage" : "https://github.com/TeamworkGuy2/JTextUtil",
Expand Down
4 changes: 2 additions & 2 deletions src/twg2/text/stringSearch/StringCommonality.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static int commonStringRange(int minIndex, int maxIndex, int startOffset,
if(str1Len <= str1Off - iiOff || str2Len <= str2Off - iiOff) {
return 0;
}
while((minOff - ii) > minIndex && ii < commonCount && str1.charAt(str1Off - (ii + iiOff)) == str2.charAt(str2Off - (ii + iiOff))) {
while((minOff - (ii + iiOff)) >= minIndex && ii < commonCount && str1.charAt(str1Off - (ii + iiOff)) == str2.charAt(str2Off - (ii + iiOff))) {
ii++;
}
}
Expand All @@ -143,7 +143,7 @@ public static int commonStringRange(int minIndex, int maxIndex, int startOffset,
}

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

return commonCount;
Expand Down
9 changes: 3 additions & 6 deletions src/twg2/text/stringUtils/StringReplace.java
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,7 @@ public static int replaceStrings(Map<String, String> searchReplaceStrs, StringBu
/**
* @see #replaceStrings(char[], int, Collection, Collection, StringBuilder)
*/
public static int replaceStrings(String content, Collection<String> searchStrs, Collection<String> replaceStrs,
StringBuilder dst) {
public static int replaceStrings(String content, Collection<String> searchStrs, Collection<String> replaceStrs, StringBuilder dst) {
dst.append(content);
return replaceStrings(searchStrs, replaceStrs, dst, dst.length());
}
Expand All @@ -315,8 +314,7 @@ public static int replaceStrings(String content, Collection<String> searchStrs,
/**
* @see #replaceStrings(char[], int, Collection, Collection, StringBuilder)
*/
public static int replaceStrings(String content, int contentOff, Collection<String> searchStrs, Collection<String> replaceStrs,
StringBuilder dst) {
public static int replaceStrings(String content, int contentOff, Collection<String> searchStrs, Collection<String> replaceStrs, StringBuilder dst) {
dst.append(content, contentOff, content.length() - contentOff);
return replaceStrings(searchStrs, replaceStrs, dst, dst.length());
}
Expand All @@ -337,8 +335,7 @@ public static int replaceStrings(String content, int contentOff, Collection<Stri
* replaced with corresponding {@code replaceStrs}
* @return the length change of {@code dst} after replacing the matching strings in {@code content}
*/
public static int replaceStrings(char[] content, int contentOffset, Collection<String> searchStrs, Collection<String> replaceStrs,
StringBuilder dst) {
public static int replaceStrings(char[] content, int contentOffset, Collection<String> searchStrs, Collection<String> replaceStrs, StringBuilder dst) {
dst.append(content, contentOffset, content.length - contentOffset);
return replaceStrings(searchStrs, replaceStrs, dst, dst.length());
}
Expand Down
36 changes: 26 additions & 10 deletions src/twg2/text/stringUtils/StringSplit.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public class StringSplit {


/** A slightly faster version of {@link String#split(String)} that does not
* used {@link Pattern}, instead the pattern is interpreted literally
* and the input string is split based on the literal split string.
* @param input the input char sequence to split
* use {@link Pattern}, instead the pattern is interpreted literally
* and the input string is split based on the literal {@code pattern} string.
* @param input the input string to split
* @param pattern the exact pattern to find and split around
* @param limit the maximum number of splits to make, zero indicates that
* an infinite number of splits can occur
Expand All @@ -51,7 +51,7 @@ public static List<String> split(String input, String pattern, List<String> dst)
/** A slightly faster version of {@link String#split(String)} that does not
* used {@link Pattern}, instead the pattern is interpreted literally
* and the input string is split based on the literal split string.
* @param input the input char sequence to split
* @param input the input string to split
* @param pattern the exact pattern to find and split around
* @param limit the maximum number of splits to make, zero indicates that
* an infinite number of splits can occur
Expand Down Expand Up @@ -115,7 +115,7 @@ public static List<String> split(String input, String pattern, int limit, List<S
* This method is more space efficient than the {@link StringSplit#split(String, String, int)} version
* since no internal structure is created to store the split strings, instead the array provided
* is filled with the split strings.
* @param input the input char sequence to split
* @param input the input string to split
* @param pattern the exact pattern to find and split around
* @param dst an array of strings equal in length to the number of strings to split the {@code input} string into
* @return the {@code results} array of strings passed into the method
Expand Down Expand Up @@ -176,6 +176,22 @@ public static List<String> split(String input, char splitAt) {
}


/** A slightly faster version of {@link String#split(String)} that does not
* use {@link Pattern}, instead the pattern is interpreted literally
* and the input string is split based on the literal {@code pattern} character.
* @param input the input string to split
* @param pattern the exact pattern to find and split around
* @param limit the maximum number of splits to make, zero indicates that
* an infinite number of splits can occur
* @return an array of strings containing the resulting stings after
* splitting the input string
*/
public static String[] split(String input, char pattern, int limit) {
List<String> strs = split(input, pattern, limit, null);
return strs.toArray(new String[strs.size()]);
}


/**
* @see #split(String, char, int, List)
*/
Expand All @@ -186,8 +202,8 @@ public static List<String> split(String input, char splitAt, List<String> dst) {

/** A slightly faster version of {@link String#split(String)} that does not
* used {@link Pattern}, instead the string is split based on a specific char.<br>
* @param input the input char sequence to split
* @param splitAt the exact string to find and split around
* @param input the input string to split
* @param splitAt the exact character to find and split around
* @param limit the maximum number of splits to make, zero indicates that
* an infinite number of splits can occur
* @param dst the destination list to add the split strings to
Expand Down Expand Up @@ -249,8 +265,8 @@ public static List<String> split(String input, char splitAt, int limit, List<Str
* This method is more space efficient than the {@link StringSplit#split(String, String, int)} version
* since no internal structure is created to store the split strings, instead the array provided
* is filled with the split strings.
* @param input the input char sequence to split
* @param splitAt the exact string to find and split around
* @param input the input string to split
* @param splitAt the exact character to find and split around
* @param dst an array of strings equal in length to the number of strings to split the {@code input} string into
* @return the {@code results} array of strings passed into the method
*/
Expand Down Expand Up @@ -305,7 +321,7 @@ public static String[] split(String input, char splitAt, String[] dst) {
/** A slightly faster version of {@link String#split(String)} that does not
* used {@link Pattern}, instead the pattern is interpreted literally
* and the input string is split based on the literal split string.
* @param input the input char sequence to split
* @param input the input string to split
* @param pattern the exact pattern to find and split around
* @param childI the index of the matching substring to retrieve
* @param expectedCount the expected number of string when the string is split using the pattern, 0 if the expected count is not know
Expand Down
12 changes: 8 additions & 4 deletions test/twg2/text/test/DataUnescapePartialQuoted.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public class DataUnescapePartialQuoted {
"2. abc, xyz",
"3. \"abc, xyz\"], ",
"4. \\\"\"",
"5. , "
"5. \"a, b\", c",
"6. , "
);

public static List<String> expected = Arrays.asList(
Expand All @@ -25,6 +26,7 @@ public class DataUnescapePartialQuoted {
" abc",
" \"abc, xyz\"",
"\\\"\"",
"a, b",
""
);

Expand All @@ -35,18 +37,20 @@ public class DataUnescapePartialQuoted {
inputs.get(2).lastIndexOf(','),
inputs.get(3).lastIndexOf("]"),
inputs.get(4).lastIndexOf("\"") + 1,
inputs.get(5).lastIndexOf(',')
inputs.get(5).lastIndexOf("\"") + 1,
inputs.get(6).lastIndexOf(',')
);


/** 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 */
public static List<Integer> expectedIndexesIncludingTrueEndingChar = Arrays.asList(
public static List<Integer> _expectedIndexesIncludingTrueEndingChar = Arrays.asList(
inputs.get(0).lastIndexOf("\"") + 1,
inputs.get(1).lastIndexOf(','),
inputs.get(2).lastIndexOf(','),
inputs.get(3).lastIndexOf("]") + 1,
inputs.get(4).lastIndexOf("\"") + 1,
inputs.get(5).lastIndexOf(',')
inputs.get(5).lastIndexOf("\"") + 1,
inputs.get(6).lastIndexOf(',')
);


Expand Down
93 changes: 49 additions & 44 deletions test/twg2/text/test/StringCommonalityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,70 @@ public class StringCommonalityTest {

@Test
public void findPrefixTest() {
new CommonalityData(true, Arrays.asList(of("alp", 0), of("p", 2), of("", 8)), Arrays.asList(
List<String> strs = Arrays.asList(
"alpha, beta, gamma",
"alphabet",
"alpine"
)).test();
);
Assert.assertEquals("alp", StringCommonality.findPrefix(0, strs));
Assert.assertEquals("p", StringCommonality.findPrefix(2, strs));
Assert.assertEquals("", StringCommonality.findPrefix(8, strs));

new CommonalityData(true, Arrays.asList(of("123", 0), of("", 3), of("", 6)), Arrays.asList(
strs = Arrays.asList(
"12345678",
"12345",
"123"
)).test();
);
Assert.assertEquals("123", StringCommonality.findPrefix(0, strs));
Assert.assertEquals("", StringCommonality.findPrefix(3, strs));
Assert.assertEquals("", StringCommonality.findPrefix(6, strs));
}


@Test
public void findSuffixTest() {
new CommonalityData(false, Arrays.asList(of("ing", 0), of("i", 2), of("", 4)), Arrays.asList(
List<String> strs = Arrays.asList(
"sing",
"alphabetizing",
"-ing"
)).test();
);
Assert.assertEquals("ing", StringCommonality.findSuffix(0, strs));
Assert.assertEquals("i", StringCommonality.findSuffix(2, strs));
Assert.assertEquals("", StringCommonality.findSuffix(4, strs));

new CommonalityData(false, Arrays.asList(of("abc", 0), of("a", 5), of("", 6)), Arrays.asList(
strs = Arrays.asList(
"zabcabc",
"alphabetan-abc",
"a--abc"
)).test();
);
Assert.assertEquals("abc", StringCommonality.findSuffix(0, strs));
Assert.assertEquals("a", StringCommonality.findSuffix(5, strs));
Assert.assertEquals("", StringCommonality.findSuffix(6, strs));
}


@Test
public void findStringPortionTest() {
List<Entry<String, Integer>> suffixesAndOffsets = Arrays.asList(of("wxy", 4), of("w", 2), of("", 5));
List<String> strs = Arrays.asList(
"-1wxyxy",
"-2wxy",
"-3wxyz"
);

for(Entry<String, Integer> suffixOffset : suffixesAndOffsets) {
Assert.assertEquals(suffixOffset.getKey(), StringCommonality.commonStringPortion(1, 6, suffixOffset.getValue(), strs, false, false));
}
// forward
Assert.assertEquals("y", StringCommonality.commonStringPortion(1, 6, 4, strs, true, false));
Assert.assertEquals("wxy", StringCommonality.commonStringPortion(1, 6, 2, strs, true, false));
Assert.assertEquals("", StringCommonality.commonStringPortion(1, 6, 5, strs, true, false));

// backward
Assert.assertEquals("wxy", StringCommonality.commonStringPortion(1, 6, 4, strs, false, false));
Assert.assertEquals("w", StringCommonality.commonStringPortion(1, 6, 2, strs, false, false));
Assert.assertEquals("", StringCommonality.commonStringPortion(1, 6, 5, strs, false, false));
Assert.assertEquals("-", StringCommonality.commonStringPortion(0, 6, 0, strs, false, false));

// backward from end of string (won't match anything)
Assert.assertEquals("", StringCommonality.commonStringPortion(1, 6, 1, strs, false, true));
Assert.assertEquals("", StringCommonality.commonStringPortion(1, 6, 4, strs, false, true));
}


Expand All @@ -82,9 +104,9 @@ public void commonSuffixTest() {
}


@SuppressWarnings("unchecked")
@Test
public void findPrefixEntriesTest() {
@SuppressWarnings("unchecked")
Entry<String, Integer>[] entries = new Entry[] {
of("String-Aa", 1),
of("String=Ab", 2),
Expand All @@ -96,44 +118,27 @@ public void findPrefixEntriesTest() {
Assert.assertEquals("String", StringCommonality.findPrefix(0, entries));
Assert.assertEquals("tring", StringCommonality.findPrefix(1, entries));
Assert.assertEquals("A", StringCommonality.findPrefix(7, entries));
}


private static <K, V> Entry<K, V> of(K key, V val) {
return new AbstractMap.SimpleImmutableEntry<>(key, val);
}

}


entries = new Entry[] {
of("-aaa", 1),
of("-aa", 2),
of("-a", 3),
};

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

/**
* @author TeamworkGuy2
* @since 2015-5-9
*/
class CommonalityData {
boolean prefix;
List<Entry<String, Integer>> suffixesAndOffsets;
List<String> strs;

entries = new Entry[] {
of("-a", 3),
of("-aa", 2),
of("-aaa", 1),
};

public CommonalityData(boolean prefix, List<Entry<String, Integer>> suffixesAndOffsets, List<String> strs) {
this.prefix = prefix;
this.suffixesAndOffsets = suffixesAndOffsets;
this.strs = strs;
Assert.assertEquals("-a", StringCommonality.findPrefix(0, entries));
}


void test() {
for(Entry<String, Integer> suffixOffset : suffixesAndOffsets) {
if(prefix) {
Assert.assertEquals(suffixOffset.getKey(), StringCommonality.findPrefix(suffixOffset.getValue(), strs));
}
else {
Assert.assertEquals(suffixOffset.getKey(), StringCommonality.findSuffix(suffixOffset.getValue(), strs));
}
}
private static <K, V> Entry<K, V> of(K key, V val) {
return new AbstractMap.SimpleImmutableEntry<>(key, val);
}

}
12 changes: 12 additions & 0 deletions test/twg2/text/test/StringEscapeJsonTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,24 @@ public void jsonStr() {
"arc \u2460",
};

StringBuilder sb = new StringBuilder();

for(int i = 0, size = raw.length; i < size; i++) {
// toJson
String jsonFromRaw = StringEscapeJson.toJsonString(raw[i]);
Assert.assertEquals(json[i], jsonFromRaw);

StringEscapeJson.toJsonString(raw[i], sb);
Assert.assertEquals(json[i], sb.toString());
sb.setLength(0);

// fromJson
String rawFromJson = StringEscapeJson.fromJsonString(jsonFromRaw);
Assert.assertEquals(raw[i], rawFromJson);

StringEscapeJson.fromJsonString(jsonFromRaw, sb);
Assert.assertEquals(raw[i], sb.toString());
sb.setLength(0);
}
}

Expand Down
Loading

0 comments on commit fecc691

Please sign in to comment.