diff options
| author | bjculkin <bjculkin@mix.wvu.edu> | 2017-04-06 17:23:34 -0400 |
|---|---|---|
| committer | bjculkin <bjculkin@mix.wvu.edu> | 2017-04-06 17:23:34 -0400 |
| commit | b6c4a9a2a0479edf285e501b97c54e4f4575186b (patch) | |
| tree | 64561f81e4e1be954e6f6c1508866a722986bbd6 /BJC-Utils2 | |
| parent | b912d84b1afe05e5f079bd6edef2f0d74020f9aa (diff) | |
Convert doubles to properties
Diffstat (limited to 'BJC-Utils2')
| -rw-r--r-- | BJC-Utils2/data/formats.sprop | 94 | ||||
| -rw-r--r-- | BJC-Utils2/data/regexes.sprop | 22 | ||||
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/parserutils/DoubleMatcher.java | 82 |
3 files changed, 139 insertions, 59 deletions
diff --git a/BJC-Utils2/data/formats.sprop b/BJC-Utils2/data/formats.sprop index ba54208..d58dba7 100644 --- a/BJC-Utils2/data/formats.sprop +++ b/BJC-Utils2/data/formats.sprop @@ -15,4 +15,96 @@ stringEscape \\(%1$s|%2$s|%3$s) ## The parts are: ## 1) Anything that's not a possible escape sequence or quote. ## 2) A possible escape sequence. -doubleQuotes ("(%1$s|%2$s)*")
\ No newline at end of file +doubleQuotes ("(%1$s|%2$s)*") + +##################################### +# Format strings for handling doubles +##################################### + +## Format a floating point exponent regex. +## The parts are: +## 1) Exponent indicator, +## 2) One or more digits. +fpExponent %1$s%2$s + +## Format a decimal number with an integer part. +## The parts are: +## 1) A series of decimal digits +## 2) An exponent. +## +## The number format is: +## 1) An integer part +## 2) An optional dot +## 3) An optional decimal part +## 4) An optional exponent +fpDecimalInteger (?:%1$s(?:\.?)(?:%1$s?)(?:%2$s)?) + +## Format a decimal number with no integer part. +## The parts are: +## 1) A series of decimal digits +## 2) An exponent. +## +## The number format is: +## 1) A dot +## 2) A decimal part +## 3) An optional exponent +fpDecimalDecimal (?:\.(?:%1$s)(?:%2$s)?) + +## Format a hexadecimal number with no decimal part. +## The parts are: +## 1) A series of hex digits +## +## The number format is: +## 1) A hex leader. +## 2) A series of hex digits. +## 3) An optional dot. +fpHexInteger (?:0[xX]%1$s(?:\.)?) + +## Format a hexadecimal number with a decimal part +## The parts are: +## 1) A series of hex digits. +## +## The number format is: +## 1) A hex leader. +## 2) A optional series of hex digits. +## 3) A dot. +## 4) A series of hex digits. +fpHexDecimal (?:0[xX]%1$s?(?:\.)%1$s) + +## Format a hexadecimal leader before a prefix. +## The parts are: +## 1) A hex number with no decimal part +## 2) A hex number with a decimal part +fpHexLeader (?:%1$s|%2$s) + +## Format a hexadecimal floating point number. +## The parts are: +## 1) A hexadecimal leader. +## 2) A series of decimal digits. +## +## The number format is: +## 1) A hexadecimal leader. +## 2) A exponent indicator. +## 3) An optional sign. +## 4) A series of decimal digits. +fpHexString (?:%1$s[pP][+-]?%2$s) + +## Format the number part of a double. +## The parts are: +## 1) A decimal double with an integer part. +## 2) A decimal double without an integer part. +## 3) A hexadecimal double. +fpNumber (?:%1$s|%2$s|%3$s) + +## Format a floating point leader. +## +## NOTE: The other parts are completed by where we're inserted. + +## Format a double +## The parts are: +## 1) A leader +## 2) A number +## +## NOTE: The parens are not mismatched. +## The other one is contributed by the leader. +fpDouble %1$1(?:%2$s[fFdD]?))[\x00-\x20]*
\ No newline at end of file diff --git a/BJC-Utils2/data/regexes.sprop b/BJC-Utils2/data/regexes.sprop index 6c40e67..cdacea0 100644 --- a/BJC-Utils2/data/regexes.sprop +++ b/BJC-Utils2/data/regexes.sprop @@ -18,7 +18,23 @@ unescapedQuote (?<!\\)\" ## Match one or more characters that aren't part of an escape or a quote. nonStringEscape [^\\\"]+ -################################## -# Miscellaneous validation regexes -################################## +######################################## +# Double validation regular expressions. +######################################## + +## Unit pieces for doubles +fpDigits (?:\p{Digit}+) +fpHexDigits (?:\p{XDigit}+) + +## An exponent is e or E followed by a (optionally signed) decimal integer. +fpExponent [eE][+-]? + +## A double leader +## +## NOTE: The incomplete parts are finished by where it is inserted. +fpLeader [\x00-\x20]*[+-]?(?:NaN|Infinity| + +############################################## +# Miscellaneous validation regular expressions +############################################## intLiteral \A[+\-]\d+\Z
\ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/DoubleMatcher.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/DoubleMatcher.java index 63eabca..83d2d2a 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/DoubleMatcher.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/DoubleMatcher.java @@ -2,70 +2,42 @@ package bjc.utils.parserutils; import java.util.regex.Pattern; +import static bjc.utils.PropertyDB.*; + /* * 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. + * Unit pieces. */ - 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 + ")?)|" + + private static final String DecDigits = getRegex("fpDigits"); + private static final String HexDigits = getRegex("fpHexDigits"); + private static final String Exponent = applyFormat("fpExponent", getRegex("fpExponent"), DecDigits); - /* - * Hexadecimal strings - */ - "((" + - /* - * 0[xX] HexDigits ._opt BinaryExponent - * FloatTypeSuffix_opt - */ - "(0[xX]" + HexDigits + "(\\.)?)|" + + /* + * Decimal floating point numbers. + */ + private static final String SIMPLE_DEC = applyFormat("fpDecimalDecimal", DecDigits, Exponent); + private static final String SIMPLE_INTDEC = applyFormat("fpDecimalInteger", DecDigits, Exponent); - /* - * 0[xX] HexDigits_opt . HexDigits BinaryExponent - * FloatTypeSuffix_opt - */ - "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" + + /* + * Hex floating point numbers. + */ + private static final String HEX_INT = applyFormat("fpHexInteger", HexDigits); + private static final String HEX_DEC = applyFormat("fpHexDecimal", HexDigits); + private static final String HEX_LEAD = applyFormat("fpHexLeader", HEX_INT, HEX_DEC); + private static final String HEX_STRING = applyFormat("fpHexString", HEX_LEAD, DecDigits); - ")[pP][+-]?" + Digits + "))" + "[fFdD]?))" + "[\\x00-\\x20]*"; // Optional - // trailing - // "whitespace" + /* + * Floating point components. + */ + private static final String FP_LEADER = getRegex("fpLeader"); + private static final String FP_NUM = applyFormat("fpNumber", SIMPLE_INTDEC, SIMPLE_DEC, + HEX_STRING); - public static final Pattern floatingLiteral = Pattern.compile("\\A" + fpRegex + "\\Z"); -} + private static final String fpRegex = applyFormat("fpDouble", FP_LEADER, FP_NUM); + public static final Pattern floatingLiteral = Pattern.compile("\\A" + fpRegex + "\\Z"); +}
\ No newline at end of file |
