From c1d4716b079d4ef32650144709294525fed6badf Mon Sep 17 00:00:00 2001 From: Benjamin Culkin Date: Thu, 9 Nov 2023 01:59:21 +0000 Subject: Add support for suffixed double --- .../java/bjc/utils/parserutils/DoubleMatcher.java | 6 +++--- .../java/bjc/utils/parserutils/TokenUtils.java | 22 +++++++++++++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-) (limited to 'base') diff --git a/base/src/main/java/bjc/utils/parserutils/DoubleMatcher.java b/base/src/main/java/bjc/utils/parserutils/DoubleMatcher.java index 1bf9b24..7480020 100644 --- a/base/src/main/java/bjc/utils/parserutils/DoubleMatcher.java +++ b/base/src/main/java/bjc/utils/parserutils/DoubleMatcher.java @@ -46,6 +46,6 @@ class DoubleMatcher { /* * Full double. */ - private static final String rDouble = applyFormat("fpDouble", rFPLeader, rFPNum); - public static final Pattern doubleLiteral = Pattern.compile("\\A" + rDouble + "\\Z"); -} + public static final String rawDouble = applyFormat("fpDouble", rFPLeader, rFPNum); + public static final Pattern doubleLiteral = Pattern.compile("\\A" + rawDouble + "\\Z"); +} \ No newline at end of file diff --git a/base/src/main/java/bjc/utils/parserutils/TokenUtils.java b/base/src/main/java/bjc/utils/parserutils/TokenUtils.java index 860bbdf..20c8ed2 100644 --- a/base/src/main/java/bjc/utils/parserutils/TokenUtils.java +++ b/base/src/main/java/bjc/utils/parserutils/TokenUtils.java @@ -5,6 +5,7 @@ import static bjc.utils.misc.PropertyDB.getCompiledRegex; import static bjc.utils.misc.PropertyDB.getRegex; import java.util.*; +import java.util.function.Function; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -58,6 +59,8 @@ public class TokenUtils { private static Pattern doubleQuotePatt = Pattern.compile(rDoubleQuoteString); private static Pattern quotePatt = getCompiledRegex("unescapedQuote"); + + private static Map SUFFIX_MAP = new HashMap<>(); /* This may do something. */ // private static Pattern intLitPattern = getCompiledRegex("intLiteral"); @@ -296,7 +299,24 @@ public class TokenUtils { public static boolean isDouble(final String inp) { return DoubleMatcher.doubleLiteral.matcher(inp).matches(); } - + + /** + * Check if a given string would be successfully converted to a double by + * {@link Double#parseDouble(String)} if a given suffix was removed. + * + * @param inp + * The string to check. + * @param suffix + * The suffix to remove + * @return Whether the string is a valid double or not. + */ + public static boolean isSuffixedDouble(final String inp, final String suffix) { + Function patInit; + patInit = (String sfx) -> Pattern.compile("\\A" + DoubleMatcher.rawDouble + suffix + "\\Z"); + + return SUFFIX_MAP.computeIfAbsent(suffix, patInit).matcher(inp).matches(); + } + /** * Check if a given string would be successfully converted to a integer by * {@link Integer#parseInt(String)}. -- cgit v1.2.3