diff options
Diffstat (limited to 'base/src/main/java/bjc/utils/funcutils/StringUtils.java')
| -rw-r--r-- | base/src/main/java/bjc/utils/funcutils/StringUtils.java | 93 |
1 files changed, 56 insertions, 37 deletions
diff --git a/base/src/main/java/bjc/utils/funcutils/StringUtils.java b/base/src/main/java/bjc/utils/funcutils/StringUtils.java index 7a4ee20..5d191bc 100644 --- a/base/src/main/java/bjc/utils/funcutils/StringUtils.java +++ b/base/src/main/java/bjc/utils/funcutils/StringUtils.java @@ -22,18 +22,19 @@ public class StringUtils { * expression. * * @param input - * The string to check. + * The string to check. * * @param rRegex - * The regex to see if the string only contains matches of. + * 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) + if (input == null) throw new NullPointerException("Input must not be null"); - else if(rRegex == null) throw new NullPointerException("Regex must not be null"); + else if (rRegex == null) throw new NullPointerException("Regex must not be null"); /* * This regular expression is fairly simple. @@ -50,13 +51,13 @@ public class StringUtils { * Indent the string being built in a StringBuilder n levels. * * @param builder - * The builder to indent in. + * The builder to indent in. * * @param levels - * The number of levels to indent. + * The number of levels to indent. */ public static void indentNLevels(final StringBuilder builder, final int levels) { - for(int i = 0; i < levels; i++) { + for (int i = 0; i < levels; i++) { builder.append("\t"); } } @@ -66,10 +67,10 @@ public class StringUtils { * empty. * * @param <ContainedType> - * The type in the deque. + * The type in the deque. * * @param queue - * The deque to print. + * The deque to print. * * @return A string version of the deque, with allowance for an empty * deque. @@ -82,26 +83,26 @@ public class StringUtils { * Converts a sequence to an English list. * * @param objects - * The sequence to convert to an English list. + * 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. * * @param comma - * The string to use as a comma + * The string to use as a comma * * @return The sequence as an English list. */ public static String toEnglishList(final Object[] objects, final String join, final String comma) { - if(objects == null) throw new NullPointerException("Sequence must not be null"); + if (objects == null) throw new NullPointerException("Sequence must not be null"); final StringBuilder sb = new StringBuilder(); final String joiner = join; final String coma = comma; - switch(objects.length) { + switch (objects.length) { case 0: /* Empty list. */ break; @@ -117,7 +118,7 @@ public class StringUtils { break; default: /* Three or more items. */ - for(int i = 0; i < objects.length - 1; i++) { + for (int i = 0; i < objects.length - 1; i++) { sb.append(objects[i].toString()); sb.append(coma + " "); } @@ -140,11 +141,11 @@ public class StringUtils { * Converts a sequence to an English list. * * @param objects - * The sequence to convert to an English list. + * 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,17 +157,15 @@ public class StringUtils { * Converts a sequence to an English list. * * @param objects - * The sequence to convert to an English list. + * The sequence to convert to an English list. * * @param and - * Whether to use 'and' or 'or'. + * 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"); - } + if (and) { return toEnglishList(objects, "and"); } return toEnglishList(objects, "or"); } @@ -175,7 +174,7 @@ public class StringUtils { * Count the number of graphemes in a string. * * @param value - * The string to check. + * The string to check. * * @return The number of graphemes in the string. */ @@ -184,7 +183,7 @@ public class StringUtils { it.setText(value); int count = 0; - while(it.next() != BreakIterator.DONE) { + while (it.next() != BreakIterator.DONE) { count++; } @@ -195,17 +194,17 @@ public class StringUtils { * Count the number of times a pattern matches in a given string. * * @param value - * The string to count occurances in. + * The string to count occurances in. * * @param pattern - * The pattern to count occurances of. + * The pattern to count occurances 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()) + while (mat.find()) num += 1; return num; @@ -215,16 +214,36 @@ public class StringUtils { * Get a substring until a specified string. * * @param strang - * The string to substring. + * The string to substring. * @param vx - * The place to substring until. + * The place to substring until. + * * @return The specified substring. */ public static String substringTo(String strang, String vx) { + return substringTo(strang, vx, 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. + * + * @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); - if(idx == -1) { - return strang; + if (idx == -1) { + if (allowFail) return strang; + + return null; } return strang.substring(0, strang.indexOf(vx)); @@ -235,7 +254,7 @@ public class StringUtils { * string literals. * * @param com - * The command to split from + * The command to split from * @return The split arguments. */ public static List<String> processArguments(String com) { @@ -243,11 +262,11 @@ public class StringUtils { BooleanToggle togg = new BooleanToggle(); - for(String strang : TokenUtils.removeDQuotedStrings(com)) { - if(togg.get()) { + for (String strang : TokenUtils.removeDQuotedStrings(com)) { + if (togg.get()) { strings.add(TokenUtils.descapeString(strang)); } else { - for(String strung : strang.split("\\s+")) { + for (String strung : strang.split("\\s+")) { strings.add(strung); } } |
