From 7c4d9b7d6dba55c77fd1fd24beedcb6147d46cae Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Wed, 30 Mar 2016 09:01:35 -0400 Subject: Changed some of the token manipulators to not affect solo operators The main change is that they won't handle tokens that consist only of the operator. --- .../main/java/bjc/utils/funcutils/ListUtils.java | 52 +++++++++++++--------- .../main/java/bjc/utils/funcutils/StringUtils.java | 34 ++++++++++++++ 2 files changed, 64 insertions(+), 22 deletions(-) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java (limited to 'BJC-Utils2/src') diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java index 7630069..6e8c63a 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java @@ -95,8 +95,8 @@ public class ListUtils { /* * List that holds current partition */ - GenHolder> currPart = new GenHolder<>( - new FunctionalList<>()); + GenHolder> currPart = + new GenHolder<>(new FunctionalList<>()); /* * List that holds elements rejected during current pass */ @@ -142,6 +142,7 @@ public class ListUtils { * @param ops * The operators to split on. * @return A list of tokens split on all the operators + * */ public static FunctionalList splitTokens( FunctionalList input, @@ -152,25 +153,28 @@ public class ListUtils { (op) -> ret.transform((oldRet) -> oldRet.flatMap((tok) -> { return op.merge((opName, opRegex) -> { if (tok.contains(opName)) { - FunctionalList splitTokens = new FunctionalList<>( - tok.split(opRegex)); - - FunctionalList rt = new FunctionalList<>(); - - int tkSize = splitTokens.getSize(); - splitTokens.forEachIndexed((idx, tk) -> { - - if (idx != tkSize && idx != 0) { - rt.add(opName); - rt.add(tk); - - } else { - rt.add(tk); - - } - }); - - return rt; + if (StringUtils.containsOnly(tok, opRegex)) { + return new FunctionalList<>(tok); + } else { + FunctionalList splitTokens = + new FunctionalList<>( + tok.split(opRegex)); + FunctionalList rt = + new FunctionalList<>(); + int tkSize = splitTokens.getSize(); + splitTokens.forEachIndexed((idx, tk) -> { + + if (idx != tkSize && idx != 0) { + rt.add(opName); + rt.add(tk); + + } else { + rt.add(tk); + + } + }); + return rt; + } } else { return new FunctionalList<>(tok); } @@ -188,6 +192,7 @@ public class ListUtils { * @param ops * The affixes to remove * @return The tokens that have been deaffixed + * */ @SuppressWarnings("unchecked") public static FunctionalList deAffixTokens( @@ -199,7 +204,10 @@ public class ListUtils { (op) -> ret.transform((oldRet) -> oldRet.flatMap((tok) -> { return (FunctionalList) op .merge((opName, opRegex) -> { - if (tok.startsWith(opName)) { + if (StringUtils.containsOnly(tok, + opRegex)) { + return new FunctionalList<>(tok); + } else if (tok.startsWith(opName)) { return new FunctionalList<>(op, tok.split(opRegex)[1]); } else if (tok.endsWith(opName)) { diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java new file mode 100644 index 0000000..b7d20aa --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java @@ -0,0 +1,34 @@ +package bjc.utils.funcutils; + +/** + * Utility methods for operations on strings + * + * @author ben + * + */ +public class StringUtils { + + /** + * Check if a string consists only of one or more matches of a regular + * expression + * + * @param input + * The string to check + * @param regex + * The regex to see if the string only contains matches of + * @return Whether or not the string consists only of multiple matches + * of the provided regex + */ + public static boolean containsOnly(String input, String regex) { + /* + * This regular expression is fairly simple. + * + * First, we match the beginning of the string. Then, we start a + * non-capturing group whose contents are the passed in regex. That + * group is then matched one or more times and the pattern matches + * to the end of the string + */ + return input.matches("\\A(?:" + regex + ")+\\Z"); + } + +} -- cgit v1.2.3