summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2017-03-10 10:07:33 -0500
committerbculkin2442 <bjculkin@mix.wvu.edu>2017-03-10 10:07:33 -0500
commit3cc21e9d65ac146842f6a6331972d151b2a6dd64 (patch)
tree9235c1a341286e02beebfd7b827f9735952a97ef /BJC-Utils2/src/main/java
parent355b4d1dda5965ea9b58bd2c80e3703a55abce98 (diff)
StringUtils expanded
Diffstat (limited to 'BJC-Utils2/src/main/java')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcutils/DoubleMatcher.java65
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java10
2 files changed, 75 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/DoubleMatcher.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/DoubleMatcher.java
new file mode 100644
index 0000000..d6e77c7
--- /dev/null
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/DoubleMatcher.java
@@ -0,0 +1,65 @@
+package bjc.utils.funcutils;
+
+import java.util.regex.Pattern;
+
+/*
+ * Checks if a string would pass Double.parseDouble.
+ *
+ * Uses a regex from the javadoc for Double.valueOf()
+ */
+class DoubleMatcher {
+ private static final String Digits = "(\\p{Digit}+)";
+ private static final String HexDigits = "(\\p{XDigit}+)";
+
+ /*
+ * an exponent is 'e' or 'E' followed by an optionally signed decimal
+ * integer.
+ */
+ private static final String Exp = "[eE][+-]?" + Digits;
+
+ private static final String fpRegex = ("[\\x00-\\x20]*" + // Optional leading "whitespace"
+ "[+-]?(" + // Optional sign character
+ "NaN|" + // "NaN" string
+ "Infinity|" + // "Infinity" string
+
+ /* A decimal floating-point string representing a finite positive
+ * number without a leading sign has at most five basic pieces:
+ * Digits . Digits ExponentPart FloatTypeSuffix
+ *
+ * Since this method allows integer-only strings as input
+ * in addition to strings of floating-point literals, the
+ * two sub-patterns below are simplifications of the grammar
+ * productions from section 3.10.2 of
+ * The Java™ Language Specification.
+ */
+
+ /*
+ * Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
+ */
+ "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+
+
+ /*
+ * . Digits ExponentPart_opt FloatTypeSuffix_opt
+ */
+ "(\\.("+Digits+")("+Exp+")?)|"+
+
+ /*
+ * Hexadecimal strings
+ */
+ "((" +
+ /*
+ * 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
+ */
+ "(0[xX]" + HexDigits + "(\\.)?)|" +
+
+ /*
+ * 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt
+ */
+ "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +
+
+ ")[pP][+-]?" + Digits + "))" +
+ "[fFdD]?))" +
+ "[\\x00-\\x20]*"); // Optional trailing "whitespace"
+
+ public static final Pattern floatingLiteral = Pattern.compile("\\A" + fpRegex + "\\Z");
+}
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 718514c..2307f11 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java
@@ -204,4 +204,14 @@ public class StringUtils {
return new String(Character.toChars(codepoint));
}
+
+ public static boolean isDouble(String inp) {
+ return DoubleMatcher.floatingLiteral.matcher(inp).matches();
+ }
+
+ private static Pattern intLitPattern = Pattern.compile("\\A[+\\-]?\\d+\\Z");
+
+ public static boolean isInt(String inp) {
+ return intLitPattern.matcher(inp).matches();
+ }
}