diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2019-06-24 20:35:41 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2019-06-24 20:35:41 -0400 |
| commit | eb703a69c83a0bdb3d2172b0bd26d38188ecbe86 (patch) | |
| tree | 85b39bbc362e07e01c5e010a4af75084aa78c27a | |
| parent | 1ab7c6b0e02cdbc2dd911bb9df8a5bc6467d0434 (diff) | |
Fix some bugs in escapeSplit
The main bug was that if you started the string with a delimiter, you
would only get an empty string as a result. Turns out endsWith("") is
usually true :-|
Also, added some more tests, and some more early outs to escapeSplit.
| -rw-r--r-- | src/main/java/bjc/everge/StringUtils.java | 51 | ||||
| -rw-r--r-- | src/test/java/bjc/everge/StringUtilsTest.java | 7 |
2 files changed, 50 insertions, 8 deletions
diff --git a/src/main/java/bjc/everge/StringUtils.java b/src/main/java/bjc/everge/StringUtils.java index 3f19b8f..3b40832 100644 --- a/src/main/java/bjc/everge/StringUtils.java +++ b/src/main/java/bjc/everge/StringUtils.java @@ -4,12 +4,16 @@ import java.util.Arrays; import java.util.ArrayList; import java.util.List; +import java.util.regex.Pattern; + /** * Utility methods for strings. * * @author Ben Culkin. */ public class StringUtils { + public static boolean isDebug = false; + /** * Split a string on every occurance of a string not preceeded by an escape. * @@ -22,8 +26,22 @@ public class StringUtils { * @return The string split as specified above. */ public static String[] escapeSplit(String escape, String splat, String inp) { + /* + * Special case some stuffs. + */ + if (inp == null || inp.equals("")) { + // No input + return new String[] {inp}; + } + + if (!inp.contains(splat)) { + // Input does not contain any delimiters + return new String[] {inp}; + } + if (escape == null || escape.equals("")) { - return inp.split(splat); + // No escape, so we can just split normally + return inp.split(Pattern.quote(splat)); } List<String> ret = new ArrayList<>(); @@ -31,25 +49,38 @@ public class StringUtils { String wrk = inp; int idx = wrk.indexOf(splat); - // System.err.printf("[DEBUG] 'hard' escapeSplit: (%s) (%s) (%s) init: %d\n", - // escape, splat, inp, idx); + if (isDebug) { + System.err.printf("[DEBUG] 'hard' escapeSplit: (%s) (%s) (%s) init: %d\n", + escape, splat, inp, idx); + } while (idx != -1) { - while (idx != -1 && wrk.regionMatches(idx - 1, escape, 0, escape.length())) { + boolean hasEscape = wrk.regionMatches(idx - 1, escape, 0, escape.length()); + + while (idx != -1 && hasEscape) { int oidx = wrk.indexOf(splat, idx + 1); - // System.err.printf("[TRACE] idx: %d, oidx: %d\n", idx, oidx); + + if (isDebug) { + System.err.printf("[TRACE] idx: %d, oidx: %d\n", idx, oidx); + } + idx = oidx; + + hasEscape = wrk.regionMatches(idx - 1, escape, 0, escape.length()); } if (idx == -1) { break; } - // System.err.printf("[TRACE] sliced string into (%s) and (%s) at %d\n", - // wrk.substring(0, idx), wrk.substring(idx), idx); + if (isDebug) { + System.err.printf("[TRACE] sliced string into (%s) and (%s) at %d\n", + wrk.substring(0, idx), wrk.substring(idx), idx); + } + String tmp = wrk.substring(0, idx); ret.add(tmp); - if (wrk.endsWith(tmp)) { + if (!tmp.equals("") && wrk.endsWith(tmp)) { wrk = ""; } else { wrk = wrk.substring(idx + splat.length()); @@ -58,6 +89,10 @@ public class StringUtils { idx = wrk.indexOf(splat); } + if (isDebug) { + System.err.printf("\t[TRACE] Remnant is (%s) for string (%s)\n", wrk, inp); + } + if (!wrk.equals("")) ret.add(wrk); return ret.toArray(new String[0]); diff --git a/src/test/java/bjc/everge/StringUtilsTest.java b/src/test/java/bjc/everge/StringUtilsTest.java index 9edc028..9eac017 100644 --- a/src/test/java/bjc/everge/StringUtilsTest.java +++ b/src/test/java/bjc/everge/StringUtilsTest.java @@ -28,6 +28,13 @@ public class StringUtilsTest { assertSplitsTo("a / b/c", "/", " ", "a", "/ ", "b/c"); } + @Test + public void testEdgeSplit() { + // Starting with the delimiter doesn't create a blank string + assertSplitsTo("/a", "|", "/", "", "a"); + assertSplitsTo("a/", "|", "/", "a"); + } + private void assertSplitsTo(String inp, String esc, String splat, String... right) { try { String[] lst = StringUtils.escapeSplit(esc, splat, inp); |
