From b570540dd42c375c6b6fe2ad5e85c160e95c58cd Mon Sep 17 00:00:00 2001 From: bjculkin Date: Thu, 16 Mar 2017 14:38:01 -0400 Subject: Add sequence delimitation. Essentially, this allows you to convert flat strings to trees that match the delimiter structure of that flat string. --- .../main/java/bjc/utils/funcutils/StringUtils.java | 130 ++++++++++++++++++--- 1 file changed, 114 insertions(+), 16 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java index 82e2a79..0229304 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java @@ -92,22 +92,22 @@ public class StringUtils { /* * This regex matches java-style string escapes */ - private static String escapeString = "\\\\([btnfr\"'\\\\]" // Match - // shortform - // escape - // sequences - // like - // \t - // or - // \" - + "|[0-3]?[0-7]{1,2}" // Match - // octal - // escape - // sequences - + "|u[0-9a-fA-F]{4})"; // Match - // unicode - // escape - // sequences + private static String escapeString = "\\\\([btnfr\"'\\\\]" // Match + // shortform + // escape + // sequences + // like + // \t + // or + // \" + + "|[0-3]?[0-7]{1,2}" // Match + // octal + // escape + // sequences + + "|u[0-9a-fA-F]{4})"; // Match + // unicode + // escape + // sequences private static Pattern escapePatt = Pattern.compile(escapeString); /* @@ -236,4 +236,102 @@ public class StringUtils { public static boolean isInt(String inp) { return intLitPattern.matcher(inp).matches(); } + + /** + * 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. + * @param comma + * The string to use as a comma + * + * @return + */ + public static String toEnglishList(Object[] objects, String join, String comma) { + if(objects == null) { + throw new NullPointerException("Sequence must not be null"); + } + + StringBuilder sb = new StringBuilder(); + + String joiner = " " + join + " "; + String coma = comma + " "; + + switch(objects.length) { + case 0: + /* + * Empty list. + */ + break; + case 1: + /* + * One item. + */ + sb.append(objects[0].toString()); + break; + case 2: + /* + * Two items. + */ + sb.append(objects[0].toString()); + sb.append(joiner); + sb.append(objects[1].toString()); + break; + default: + /* + * Three or more items. + */ + for(int i = 0; i < objects.length - 1; i++) { + sb.append(objects[i].toString()); + sb.append(coma); + } + /* + * Uncomment this to remove serial commas. + * + * int lc = sb.length() - 1; + * + * sb.delete(lc - coma.length(), lc); + */ + sb.append(joiner); + sb.append(objects[objects.length - 1].toString()); + } + + return sb.toString(); + } + + /** + * 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. + * + * @return + */ + public static String toEnglishList(Object[] objects, String join) { + return toEnglishList(objects, join, ","); + } + + /** + * Converts a sequence to an English list. + * + * @param objects + * The sequence to convert to an English list. + * @param and + * Whether to use 'and' or 'or'. + * + * @return + */ + public static String toEnglishList(Object[] objects, boolean and) { + if(and) { + return toEnglishList(objects, "and"); + } else { + return toEnglishList(objects, "or"); + } + } } -- cgit v1.2.3