From 3db013fc7ec8b242f1a7456910484063fba7e938 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Thu, 18 Oct 2018 20:43:44 -0400 Subject: Fix levelSplitterRX, and add tests to ensure that it stays fixed It would also be fairly easy to convert this to a general splitRX, or add support for additional expressions. This might be a better interface than the one in ConfigurableTokenSplitter. (It'd almost certainly be easier to debug/introspect than the bodged together regex that CTS uses,) --- .../main/java/bjc/utils/ioutils/LevelSplitter.java | 28 ++++++++++++---------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'base/src/main/java/bjc/utils/ioutils/LevelSplitter.java') diff --git a/base/src/main/java/bjc/utils/ioutils/LevelSplitter.java b/base/src/main/java/bjc/utils/ioutils/LevelSplitter.java index 5798ad0..5d24661 100644 --- a/base/src/main/java/bjc/utils/ioutils/LevelSplitter.java +++ b/base/src/main/java/bjc/utils/ioutils/LevelSplitter.java @@ -1,6 +1,8 @@ package bjc.utils.ioutils; import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -43,9 +45,7 @@ public class LevelSplitter { while (i < haystack.length()) { if (inString == false && nestLevel == 0) { for (String needle : needles) { - if (haystack.regionMatches(i, needle, 0, needle.length())) { - return true; - } + if (haystack.regionMatches(i, needle, 0, needle.length())) { return true; } } } @@ -99,7 +99,7 @@ public class LevelSplitter { public List levelSplit(String phrase, String... splits) { return levelSplit(phrase, false, splits); } - + /** * Split a string, respecting groups. * @@ -199,15 +199,14 @@ public class LevelSplitter { // This doesn't seem like its working @SuppressWarnings("javadoc") public List levelSplitRX(String phrase, String patt) { - return levelSplit(phrase, false, patt); + return levelSplitRX(phrase, false, patt); } @SuppressWarnings("javadoc") public List levelSplitRX(String phrase, boolean keepDelims, String patt) { Pattern pat = Pattern.compile(patt); - String work = phrase; - Matcher mat = pat.matcher(work); + Matcher mat = pat.matcher(phrase); List strangs = new ArrayList<>(); @@ -220,12 +219,15 @@ public class LevelSplitter { char stringEnder = ' '; - while (i < work.length()) { - mat.region(lastMatch, phrase.length()); + while ((lastMatch + i) < phrase.length()) { + int ai = lastMatch + i; + + mat.region(lastMatch + i, phrase.length()); if (inString == false && nestLevel == 0) { if (mat.lookingAt()) { - strangs.add(work.substring(lastMatch, mat.start())); + + strangs.add(phrase.substring(lastMatch, mat.start())); if (keepDelims) strangs.add(mat.group()); lastMatch = mat.end(); //work = work.substring(mat.end()); @@ -239,11 +241,11 @@ public class LevelSplitter { if (inString) { if (prevCharWasSlash == true) { prevCharWasSlash = false; - } else if (work.charAt(i) == stringEnder) { + } else if (phrase.charAt(ai) == stringEnder) { inString = false; } } else { - switch (work.charAt(i)) { + switch (phrase.charAt(ai)) { case '\'': inString = true; stringEnder = '\''; @@ -270,7 +272,7 @@ public class LevelSplitter { i += 1; } - strangs.add(work); + strangs.add(phrase.substring(lastMatch)); return strangs; } -- cgit v1.2.3