summaryrefslogtreecommitdiff
path: root/base/src/main/java/bjc/utils/funcutils/StringUtils.java
diff options
context:
space:
mode:
Diffstat (limited to 'base/src/main/java/bjc/utils/funcutils/StringUtils.java')
-rw-r--r--base/src/main/java/bjc/utils/funcutils/StringUtils.java249
1 files changed, 73 insertions, 176 deletions
diff --git a/base/src/main/java/bjc/utils/funcutils/StringUtils.java b/base/src/main/java/bjc/utils/funcutils/StringUtils.java
index b7a6835..0b57e18 100644
--- a/base/src/main/java/bjc/utils/funcutils/StringUtils.java
+++ b/base/src/main/java/bjc/utils/funcutils/StringUtils.java
@@ -1,79 +1,53 @@
package bjc.utils.funcutils;
-import java.util.ArrayList;
-import java.util.Deque;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Scanner;
-
+import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.ibm.icu.text.BreakIterator;
-import bjc.data.BooleanToggle;
import bjc.utils.ioutils.LevelSplitter;
-import bjc.utils.parserutils.TokenUtils;
-/**
- * Utility methods for operations on strings.
+/** Utility methods for operations on strings.
*
- * @author ben
- */
+ * @author ben */
public class StringUtils {
- /**
- * Check if a string consists only of one or more matches of a regular
+ /**Check if a string consists only of one or more matches of a regular
* expression.
*
- * @param input
- * The string to check.
- *
- * @param rRegex
- * The regex to see if the string only contains matches of.
+ * @param input The string to check.
+ * @param rRegex 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(final String input, final String rRegex) {
- if (input == null)
- throw new NullPointerException("Input must not be null");
- else if (rRegex == null)
- throw new NullPointerException("Regex must not be null");
+ if (input == null) throw new NullPointerException("Input must not be null");
+ else if (rRegex == null) throw new NullPointerException("Regex must not be null");
- /*
- * This regular expression is fairly simple.
+ /* 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.
- */
+ * or more times and the pattern matches to the end of the string. */
return input.matches("\\A(?:" + rRegex + ")+\\Z");
}
- /**
- * Indent the string being built in a StringBuilder n levels.
- *
- * @param builder
- * The builder to indent in.
+ /** Indent the string being built in a StringBuilder n levels.
*
- * @param levels
- * The number of levels to indent.
+ * @param builder The builder to indent in.
+ * @param levels The number of levels to indent.
*/
public static void indentNLevels(final StringBuilder builder, final int levels) {
- for (int i = 0; i < levels; i++) {
- builder.append("\t");
- }
+ for (int i = 0; i < levels; i++) builder.append("\t");
}
- /**
- * Print out a deque with a special case for easily showing a deque is empty.
- *
- * @param <ContainedType>
- * The type in the deque.
+ /** Print out a deque with a special case for easily showing a deque is empty.
*
- * @param queue
- * The deque to print.
+ * @param <ContainedType> The type in the deque.
*
+ * @param queue The deque to print.
+ *
* @return A string version of the deque, with allowance for an empty deque.
*/
public static <ContainedType> String printDeque(final Deque<ContainedType> queue) {
@@ -83,15 +57,10 @@ public class StringUtils {
/**
* Converts a sequence to an English list.
*
- * @param objects
- * The sequence to convert to an English list.
- *
+ * @param objects The sequence to convert to an English list.
* @param join
- * The string to use for separating the last element from the
- * rest.
- *
- * @param comma
- * The string to use as a comma
+ * The string to use for separating the last element from the rest.
+ * @param comma The string to use as a comma
*
* @return The sequence as an English list.
*/
@@ -140,15 +109,11 @@ public class StringUtils {
return sb.toString();
}
- /**
- * Converts a sequence to an English list.
- *
- * @param objects
- * The sequence to convert to an English list.
+ /** Converts a sequence to an English list.
*
+ * @param objects The sequence to convert to an English list.
* @param join
- * The string to use for separating the last element from the
- * rest.
+ * The string to use for separating the last element from the rest.
*
* @return The sequence as an English list.
*/
@@ -156,30 +121,21 @@ public class StringUtils {
return toEnglishList(objects, join, ",");
}
- /**
- * Converts a sequence to an English list.
- *
- * @param objects
- * The sequence to convert to an English list.
+ /** Converts a sequence to an English list.
*
- * @param and
- * Whether to use 'and' or 'or'.
+ * @param objects The sequence to convert to an English list.
+ * @param and Whether to use 'and' or 'or'.
*
* @return The sequence as an English list.
*/
public static String toEnglishList(final Object[] objects, final boolean and) {
- if (and) {
- return toEnglishList(objects, "and");
- }
-
- return toEnglishList(objects, "or");
+ if (and) return toEnglishList(objects, "and");
+ else return toEnglishList(objects, "or");
}
- /**
- * Count the number of graphemes in a string.
+ /** Count the number of graphemes in a string.
*
- * @param value
- * The string to check.
+ * @param value The string to check.
*
* @return The number of graphemes in the string.
*/
@@ -188,97 +144,57 @@ public class StringUtils {
it.setText(value);
int count = 0;
- while (it.next() != BreakIterator.DONE) {
- count++;
- }
+ while (it.next() != BreakIterator.DONE) count++;
return count;
}
- /**
- * Count the number of times a pattern matches in a given string.
- *
- * @param value
- * The string to count occurances in.
+ /** Count the number of times a pattern matches in a given string.
*
- * @param pattern
- * The pattern to count occurances of.
+ * @param value The string to count occurrences in.
+ * @param pattern The pattern to count occurrences of.
+ *
* @return The number of times the pattern matches.
*/
public static int countMatches(final String value, final String pattern) {
Matcher mat = Pattern.compile(pattern).matcher(value);
int num = 0;
- while (mat.find())
- num += 1;
+ while (mat.find()) num += 1;
return num;
}
- /**
- * Get a substring until a specified string.
+ /** Get a substring until a specified string.
*
- * @param strang
- * The string to substring.
- * @param vx
- * The place to substring until.
+ * @param strang The string to substring.
+ * @param until The place to substring until.
*
* @return The specified substring.
*/
- public static String substringTo(String strang, String vx) {
- return substringTo(strang, vx, true);
+ public static String substringTo(String strang, String until) {
+ return substringTo(strang, until, true);
}
/**
* Get a substring until a specified string.
*
- * @param strang
- * The string to substring.
- * @param vx
- * The place to substring until.
- * @param allowFail
- * Whether or not to allow failure.
+ * @param strang The string to substring.
+ * @param until The place to substring until.
+ * @param allowFail Whether or not to allow failure.
*
* @return The specified substring, or null if the specified place to substring
* to was not found, and we were not allowed to fail.
*/
- public static String substringTo(String strang, String vx, boolean allowFail) {
- int idx = strang.indexOf(vx);
+ public static String substringTo(String strang, String until, boolean allowFail) {
+ int idx = strang.indexOf(until);
if (idx == -1) {
- if (allowFail)
- return strang;
-
- return null;
- }
-
- return strang.substring(0, strang.indexOf(vx));
- }
-
- /**
- * Split a line into a series of space-separated arguments, including string
- * literals.
- *
- * @param com
- * The command to split from
- * @return The split arguments.
- */
- public static List<String> processArguments(String com) {
- List<String> strings = new ArrayList<>();
-
- BooleanToggle togg = new BooleanToggle();
-
- for (String strang : TokenUtils.removeDQuotedStrings(com)) {
- if (togg.get()) {
- strings.add(TokenUtils.descapeString(strang));
- } else {
- for (String strung : strang.split("\\s+")) {
- strings.add(strung);
- }
- }
+ if (allowFail) return strang;
+ else return null;
}
- return strings;
+ return strang.substring(0, strang.indexOf(until));
}
private static class LineIterator implements Iterator<String> {
@@ -306,16 +222,13 @@ public class StringUtils {
boolean doLoop = true;
do {
- if (!scn.hasNextLine())
- break;
+ if (!scn.hasNextLine()) break;
tmp = scn.nextLine().trim();
// Skip blank lines
- if (skipBlanks && tmp.equals(""))
- continue;
- if (processComments && tmp.startsWith(commentInd))
- continue;
+ if (skipBlanks && tmp.equals("")) continue;
+ if (processComments && tmp.startsWith(commentInd)) continue;
doLoop = tmp.endsWith("\\") && !tmp.endsWith("\\\\");
@@ -330,11 +243,9 @@ public class StringUtils {
}
}
- /**
- * Read a series of lines from an input source.
+ /** Read a series of lines from an input source.
*
- * @param scn
- * The source to read the lines from.
+ * @param scn The source to read the lines from.
*
* @return An iterator over the lines from the input source.
*/
@@ -345,17 +256,10 @@ public class StringUtils {
/**
* Read a series of lines from an input source.
*
- * @param scn
- * The source to read the lines from.
- *
- * @param processComments
- * Whether or not to skip comment lines.
- *
- * @param commentInd
- * Indicator for starting comment lines.
- *
- * @param skipBlanks
- * Whether or not to skip blank lines.
+ * @param scn The source to read the lines from.
+ * @param processComments Whether or not to skip comment lines.
+ * @param commentInd Indicator for starting comment lines.
+ * @param skipBlanks Whether or not to skip blank lines.
*
* @return An iterator over the lines from the input source.
*/
@@ -371,27 +275,23 @@ public class StringUtils {
return itr;
}
- /**
- * Check if a string contains any one of a specified number of things,
+ /** Check if a string contains any one of a specified number of things,
* respecting groups.
*
- * @param haystack
- * The string to look in.
- * @param needles
- * The strings to look for.
+ * @param haystack The string to look in.
+ * @param needles The strings to look for.
+ *
* @return Whether or not any of the strings were contained outside of groups.
*/
public static boolean levelContains(String haystack, String... needles) {
return LevelSplitter.def.levelContains(haystack, needles);
}
- /**
- * Split a string, respecting groups.
+ /** Split a string, respecting groups.
*
- * @param phrase
- * The string to split.
- * @param splits
- * The strings to split on.
+ * @param phrase The string to split.
+ * @param splits The strings to split on.
+ *
* @return A list of split strings. If keepDelims is true, it also includes the
* delimiters in between the split strings.
*/
@@ -399,15 +299,12 @@ public class StringUtils {
return LevelSplitter.def.levelSplit(phrase, false, splits);
}
- /**
- * Split a string, respecting groups.
+ /** Split a string, respecting groups.
*
- * @param phrase
- * The string to split.
- * @param keepDelims
- * Whether or not to include the delimiters in the results.
- * @param splits
- * The strings to split on.
+ * @param phrase The string to split.
+ * @param keepDelims Whether or not to include the delimiters in the results.
+ * @param splits The strings to split on.
+ *
* @return A list of split strings. If keepDelims is true, it also includes the
* delimiters in between the split strings.
*/
@@ -415,4 +312,4 @@ public class StringUtils {
String... splits) {
return LevelSplitter.def.levelSplit(phrase, keepDelims, splits);
}
-}
+} \ No newline at end of file