summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java
diff options
context:
space:
mode:
authorbjculkin <bjculkin@mix.wvu.edu>2017-03-16 14:38:01 -0400
committerbjculkin <bjculkin@mix.wvu.edu>2017-03-16 14:42:10 -0400
commitb570540dd42c375c6b6fe2ad5e85c160e95c58cd (patch)
treee97c5e904817fe6710f714f6b4fdbaa1e9818715 /BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java
parent8185b03f18d3b32e84ad9aefbd85c9ad4a3f82df (diff)
Add sequence delimitation.
Essentially, this allows you to convert flat strings to trees that match the delimiter structure of that flat string.
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java130
1 files changed, 114 insertions, 16 deletions
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");
+ }
+ }
}