From b2fc12e725e66a1f5da38da86fc5b1d8a930bb62 Mon Sep 17 00:00:00 2001 From: wrandelshofer Date: Sun, 20 Oct 2024 18:42:56 +0200 Subject: [PATCH] Return a magic bit pattern instead of throwing an exception. --- ...leFloatingPointBitsFromByteArrayAscii.java | 8 ++-- ...bleFloatingPointBitsFromByteArrayUtf8.java | 8 ++-- ...gurableFloatingPointBitsFromCharArray.java | 8 ++-- ...ableFloatingPointBitsFromCharSequence.java | 8 ++-- ...actJavaFloatingPointBitsFromByteArray.java | 10 ++--- ...actJavaFloatingPointBitsFromCharArray.java | 10 ++--- ...JavaFloatingPointBitsFromCharSequence.java | 10 ++--- ...actJsonFloatingPointBitsFromByteArray.java | 6 +-- ...actJsonFloatingPointBitsFromCharArray.java | 6 +-- ...JsonFloatingPointBitsFromCharSequence.java | 6 +-- .../AbstractNumberParser.java | 6 +++ ...igurableDoubleMatcherFromCharSequence.java | 40 +++++++++++++++++++ .../ConfigurableDoubleParser.java | 30 +++++++++----- .../fastdoubleparser/JavaDoubleParser.java | 6 +++ .../fastdoubleparser/JavaFloatParser.java | 6 +++ .../fastdoubleparser/JsonDoubleParser.java | 6 +++ 16 files changed, 123 insertions(+), 51 deletions(-) create mode 100644 fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/ConfigurableDoubleMatcherFromCharSequence.java diff --git a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractConfigurableFloatingPointBitsFromByteArrayAscii.java b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractConfigurableFloatingPointBitsFromByteArrayAscii.java index e2c49b0c..51189b5f 100644 --- a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractConfigurableFloatingPointBitsFromByteArrayAscii.java +++ b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractConfigurableFloatingPointBitsFromByteArrayAscii.java @@ -76,7 +76,7 @@ public final long parseFloatingPointLiteral(byte[] str, int offset, int length) // ------------------- int index = skipFormatCharacters(str, offset, endIndex); if (index == endIndex) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } byte ch = str[index]; @@ -86,7 +86,7 @@ public final long parseFloatingPointLiteral(byte[] str, int offset, int length) if (isNegative || isPlusSign(ch)) { ++index; if (index == endIndex) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } } @@ -167,7 +167,7 @@ public final long parseFloatingPointLiteral(byte[] str, int offset, int length) // Check if FloatingPointLiteral is complete // ------------------------ if (illegal || index < endIndex) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } // Re-parse significand in case of a potential overflow @@ -237,7 +237,7 @@ private long parseNaNOrInfinity(byte[] str, int index, int endIndex, boolean isN return isNegative ? negativeInfinity() : positiveInfinity(); } } - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } /** diff --git a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractConfigurableFloatingPointBitsFromByteArrayUtf8.java b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractConfigurableFloatingPointBitsFromByteArrayUtf8.java index a8604ed4..715ef03c 100644 --- a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractConfigurableFloatingPointBitsFromByteArrayUtf8.java +++ b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractConfigurableFloatingPointBitsFromByteArrayUtf8.java @@ -67,7 +67,7 @@ public final long parseFloatingPointLiteral(byte[] str, int offset, int length) // ------------------- int index = skipFormatCharacters(str, offset, endIndex); if (index == endIndex) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } byte ch = str[index]; @@ -81,7 +81,7 @@ public final long parseFloatingPointLiteral(byte[] str, int offset, int length) index += plusSignChar.match(str, index, endIndex); } if (index == endIndex) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } // Parse significand @@ -167,7 +167,7 @@ public final long parseFloatingPointLiteral(byte[] str, int offset, int length) // Check if FloatingPointLiteral is complete // ------------------------ if (illegal || index < endIndex) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } // Re-parse significand in case of a potential overflow @@ -229,7 +229,7 @@ private long parseNaNOrInfinity(byte[] str, int index, int endIndex, boolean isN return isNegative ? negativeInfinity() : positiveInfinity(); } } - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } /** diff --git a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractConfigurableFloatingPointBitsFromCharArray.java b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractConfigurableFloatingPointBitsFromCharArray.java index 65e71341..f4850f20 100644 --- a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractConfigurableFloatingPointBitsFromCharArray.java +++ b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractConfigurableFloatingPointBitsFromCharArray.java @@ -76,7 +76,7 @@ public final long parseFloatingPointLiteral(char[] str, int offset, int length) // ------------------- int index = skipFormatCharacters(str, offset, endIndex); if (index == endIndex) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } char ch = str[index]; @@ -86,7 +86,7 @@ public final long parseFloatingPointLiteral(char[] str, int offset, int length) if (isNegative || isPlusSign(ch)) { ++index; if (index == endIndex) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } } @@ -168,7 +168,7 @@ public final long parseFloatingPointLiteral(char[] str, int offset, int length) // Check if FloatingPointLiteral is complete // ------------------------ if (illegal || index < endIndex) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } // Re-parse significand in case of a potential overflow @@ -238,7 +238,7 @@ private long parseNaNOrInfinity(char[] str, int index, int endIndex, boolean isN return isNegative ? negativeInfinity() : positiveInfinity(); } } - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } /** diff --git a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractConfigurableFloatingPointBitsFromCharSequence.java b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractConfigurableFloatingPointBitsFromCharSequence.java index 99fa94ac..a49cf61e 100644 --- a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractConfigurableFloatingPointBitsFromCharSequence.java +++ b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractConfigurableFloatingPointBitsFromCharSequence.java @@ -77,7 +77,7 @@ public final long parseFloatingPointLiteral(CharSequence str, int offset, int le // ------------------- int index = skipFormatCharacters(str, offset, endIndex); if (index == endIndex) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } char ch = str.charAt(index); @@ -87,7 +87,7 @@ public final long parseFloatingPointLiteral(CharSequence str, int offset, int le if (isNegative || isPlusSign(ch)) { ++index; if (index == endIndex) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } } @@ -170,7 +170,7 @@ public final long parseFloatingPointLiteral(CharSequence str, int offset, int le // Check if FloatingPointLiteral is complete // ------------------------ if (illegal || index < endIndex) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } // Re-parse significand in case of a potential overflow @@ -240,7 +240,7 @@ private long parseNaNOrInfinity(CharSequence str, int index, int endIndex, boole return isNegative ? negativeInfinity() : positiveInfinity(); } } - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } /** diff --git a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractJavaFloatingPointBitsFromByteArray.java b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractJavaFloatingPointBitsFromByteArray.java index 37cb5966..ca94f953 100644 --- a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractJavaFloatingPointBitsFromByteArray.java +++ b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractJavaFloatingPointBitsFromByteArray.java @@ -152,7 +152,7 @@ private long parseDecFloatLiteral(byte[] str, int index, int startIndex, int end // ------------------------ index = skipWhitespace(str, index, endIndex); if (illegal || index < endIndex) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } // Re-parse significand in case of a potential overflow @@ -210,7 +210,7 @@ public long parseFloatingPointLiteral(byte[] str, int offset, int length) { // ------------------- int index = skipWhitespace(str, offset, endIndex); if (index == endIndex) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } byte ch = str[index]; @@ -220,7 +220,7 @@ public long parseFloatingPointLiteral(byte[] str, int offset, int length) { if (isNegative || ch == '+') { ch = charAt(str, ++index, endIndex); if (ch == 0) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } } @@ -345,7 +345,7 @@ private long parseHexFloatingPointLiteral( if (illegal || index < endIndex || digitCount == 0 || !hasExponent) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } // Re-parse significand in case of a potential overflow @@ -402,7 +402,7 @@ private long parseNaNOrInfinity(byte[] str, int index, int endIndex, boolean isN } } } - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } /** diff --git a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractJavaFloatingPointBitsFromCharArray.java b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractJavaFloatingPointBitsFromCharArray.java index 96ca710e..776ea040 100644 --- a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractJavaFloatingPointBitsFromCharArray.java +++ b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractJavaFloatingPointBitsFromCharArray.java @@ -154,7 +154,7 @@ private long parseDecFloatLiteral(char[] str, int index, int startIndex, int end // ------------------------ index = skipWhitespace(str, index, endIndex); if (illegal || index < endIndex) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } // Re-parse significand in case of a potential overflow @@ -211,7 +211,7 @@ public long parseFloatingPointLiteral(char[] str, int offset, int length) { // ------------------- int index = skipWhitespace(str, offset, endIndex); if (index == endIndex) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } char ch = str[index]; @@ -221,7 +221,7 @@ public long parseFloatingPointLiteral(char[] str, int offset, int length) { if (isNegative || ch == '+') { ch = charAt(str, ++index, endIndex); if (ch == 0) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } } @@ -346,7 +346,7 @@ private long parseHexFloatLiteral( if (illegal || index < endIndex || digitCount == 0 || !hasExponent) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } // Re-parse significand in case of a potential overflow @@ -409,7 +409,7 @@ private long parseNaNOrInfinity(char[] str, int index, int endIndex, boolean isN } } } - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } /** diff --git a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractJavaFloatingPointBitsFromCharSequence.java b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractJavaFloatingPointBitsFromCharSequence.java index 136f658d..69476049 100644 --- a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractJavaFloatingPointBitsFromCharSequence.java +++ b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractJavaFloatingPointBitsFromCharSequence.java @@ -153,7 +153,7 @@ private long parseDecFloatLiteral(CharSequence str, int index, int startIndex, i // ------------------------ index = skipWhitespace(str, index, endIndex); if (illegal || index < endIndex) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } // Re-parse significand in case of a potential overflow @@ -210,7 +210,7 @@ public final long parseFloatingPointLiteral(CharSequence str, int offset, int le // ------------------- int index = skipWhitespace(str, offset, endIndex); if (index == endIndex) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } char ch = str.charAt(index); @@ -220,7 +220,7 @@ public final long parseFloatingPointLiteral(CharSequence str, int offset, int le if (isNegative || ch == '+') { ch = charAt(str, ++index, endIndex); if (ch == 0) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } } @@ -343,7 +343,7 @@ private long parseHexFloatLiteral( if (illegal || index < endIndex || digitCount == 0 || !hasExponent) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } // Re-parse significand in case of a potential overflow @@ -407,7 +407,7 @@ private long parseNaNOrInfinity(CharSequence str, int index, int endIndex, boole } } } - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } /** diff --git a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractJsonFloatingPointBitsFromByteArray.java b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractJsonFloatingPointBitsFromByteArray.java index 6a9db7be..f36dc846 100644 --- a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractJsonFloatingPointBitsFromByteArray.java +++ b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractJsonFloatingPointBitsFromByteArray.java @@ -38,7 +38,7 @@ public final long parseNumber(byte[] str, int offset, int length) { if (isNegative) { ch = charAt(str, ++index, endIndex); if (ch == 0) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } } @@ -48,7 +48,7 @@ public final long parseNumber(byte[] str, int offset, int length) { if (hasLeadingZero) { ch = charAt(str, ++index, endIndex); if (ch == '0') { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } } @@ -123,7 +123,7 @@ public final long parseNumber(byte[] str, int offset, int length) { // ------------------------ if (illegal || index < endIndex || !hasLeadingZero && digitCount == 0) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } // Re-parse significand in case of a potential overflow diff --git a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractJsonFloatingPointBitsFromCharArray.java b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractJsonFloatingPointBitsFromCharArray.java index 0ce50216..b05729a4 100644 --- a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractJsonFloatingPointBitsFromCharArray.java +++ b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractJsonFloatingPointBitsFromCharArray.java @@ -38,7 +38,7 @@ public final long parseNumber(char[] str, int offset, int length) { if (isNegative) { ch = charAt(str, ++index, endIndex); if (ch == 0) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } } @@ -48,7 +48,7 @@ public final long parseNumber(char[] str, int offset, int length) { if (hasLeadingZero) { ch = charAt(str, ++index, endIndex); if (ch == '0') { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } } @@ -123,7 +123,7 @@ public final long parseNumber(char[] str, int offset, int length) { // ------------------------ if (illegal || index < endIndex || !hasLeadingZero && digitCount == 0) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } // Re-parse significand in case of a potential overflow diff --git a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractJsonFloatingPointBitsFromCharSequence.java b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractJsonFloatingPointBitsFromCharSequence.java index eaf485e3..58d1a5d8 100644 --- a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractJsonFloatingPointBitsFromCharSequence.java +++ b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractJsonFloatingPointBitsFromCharSequence.java @@ -38,7 +38,7 @@ public final long parseNumber(CharSequence str, int offset, int length) { if (isNegative) { ch = charAt(str, ++index, endIndex); if (ch == 0) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } } @@ -48,7 +48,7 @@ public final long parseNumber(CharSequence str, int offset, int length) { if (hasLeadingZero) { ch = charAt(str, ++index, endIndex); if (ch == '0') { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } } @@ -125,7 +125,7 @@ public final long parseNumber(CharSequence str, int offset, int length) { // ------------------------ if (illegal || index < endIndex || !hasLeadingZero && digitCount == 0) { - throw new NumberFormatException(SYNTAX_ERROR); + return SYNTAX_ERROR_BITS; } // Re-parse significand in case of a potential overflow diff --git a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractNumberParser.java b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractNumberParser.java index f4baf3e9..03aba782 100644 --- a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractNumberParser.java +++ b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/AbstractNumberParser.java @@ -17,6 +17,12 @@ abstract class AbstractNumberParser { * when the syntax is illegal. */ public static final String SYNTAX_ERROR = "illegal syntax"; + + /** + * Uses the unused mantissa of a NaN value to encode a syntax error. + */ + public static final long SYNTAX_ERROR_BITS = 0x7ff8000000000000L + 1L; + /** * Message text for the {@link NumberFormatException} that is thrown * when there are too many input digits. diff --git a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/ConfigurableDoubleMatcherFromCharSequence.java b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/ConfigurableDoubleMatcherFromCharSequence.java new file mode 100644 index 00000000..9a17b5df --- /dev/null +++ b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/ConfigurableDoubleMatcherFromCharSequence.java @@ -0,0 +1,40 @@ +/* + * @(#)ConfigurableDoubleBitsFromCharSequence.java + * Copyright © 2024 Werner Randelshofer, Switzerland. MIT License. + */ +package ch.randelshofer.fastdoubleparser; + +/** + * Parses a {@code double} from a {@link CharSequence} with configurable {@link NumberFormatSymbols}. + */ +final class ConfigurableDoubleMatcherFromCharSequence extends AbstractConfigurableFloatingPointBitsFromCharSequence { + /** + * Creates a new instance. + */ + public ConfigurableDoubleMatcherFromCharSequence(NumberFormatSymbols symbols, boolean ignoreCase) { + super(symbols, ignoreCase); + } + + @Override + long nan() { + return Double.doubleToRawLongBits(Double.NaN); + } + + @Override + long negativeInfinity() { + return Double.doubleToRawLongBits(Double.NEGATIVE_INFINITY); + } + + @Override + long positiveInfinity() { + return Double.doubleToRawLongBits(Double.POSITIVE_INFINITY); + } + + @Override + long valueOfFloatLiteral(CharSequence str, int integerStartIndex, int integerEndIndex, int fractionStartIndex, int fractionEndIndex, boolean isSignificandNegative, + long significand, int exponent, boolean isSignificandTruncated, + int exponentOfTruncatedSignificand, int exponentValue, int startIndex, int endIndex) { + return Double.doubleToRawLongBits(1.0); + } + +} \ No newline at end of file diff --git a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/ConfigurableDoubleParser.java b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/ConfigurableDoubleParser.java index 6360bf0e..bab21dfe 100644 --- a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/ConfigurableDoubleParser.java +++ b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/ConfigurableDoubleParser.java @@ -8,6 +8,9 @@ import java.util.Collection; import java.util.Objects; +import static ch.randelshofer.fastdoubleparser.AbstractNumberParser.SYNTAX_ERROR; +import static ch.randelshofer.fastdoubleparser.AbstractNumberParser.SYNTAX_ERROR_BITS; + /** * Parses a floating point value with configurable {@link NumberFormatSymbols}. *

@@ -159,6 +162,7 @@ public ConfigurableDoubleParser(NumberFormatSymbols symbols, boolean ignoreCase) /** * Gets the number format symbols of this parser. + * * @return the number format symbols */ public NumberFormatSymbols getNumberFormatSymbols() { @@ -232,6 +236,7 @@ private boolean isAsciiCharCollection(Collection collection) { } return true; } + /** * Creates a new instance with decimal format symbols and case sensitivity. *

@@ -293,7 +298,7 @@ private ConfigurableDoubleBitsFromCharSequence getCharSequenceParser() { * @throws NumberFormatException if the provided char sequence could not be parsed */ public double parseDouble(CharSequence str) { - return Double.longBitsToDouble(getCharSequenceParser().parseFloatingPointLiteral(str, 0, str.length())); + return parseDouble(str, 0, str.length()); } /** @@ -305,7 +310,9 @@ public double parseDouble(CharSequence str) { * @throws NumberFormatException if the provided char sequence could not be parsed */ public double parseDouble(CharSequence str, int offset, int length) { - return Double.longBitsToDouble(getCharSequenceParser().parseFloatingPointLiteral(str, offset, length)); + long bitPattern = getCharSequenceParser().parseFloatingPointLiteral(str, offset, length); + if (bitPattern == SYNTAX_ERROR_BITS) throw new NumberFormatException(SYNTAX_ERROR); + return Double.longBitsToDouble(bitPattern); } /** @@ -316,7 +323,7 @@ public double parseDouble(CharSequence str, int offset, int length) { * @throws NumberFormatException if the provided char array could not be parsed */ public double parseDouble(char[] str) { - return Double.longBitsToDouble(getCharArrayParser().parseFloatingPointLiteral(str, 0, str.length)); + return parseDouble(str, 0, str.length); } /** @@ -328,7 +335,9 @@ public double parseDouble(char[] str) { * @throws NumberFormatException if the provided char array could not be parsed */ public double parseDouble(char[] str, int offset, int length) { - return Double.longBitsToDouble(getCharArrayParser().parseFloatingPointLiteral(str, offset, length)); + long bitPattern = getCharArrayParser().parseFloatingPointLiteral(str, offset, length); + if (bitPattern == SYNTAX_ERROR_BITS) throw new NumberFormatException(SYNTAX_ERROR); + return Double.longBitsToDouble(bitPattern); } /** @@ -351,17 +360,16 @@ public double parseDouble(byte[] str) { * @throws NumberFormatException if the provided char array could not be parsed */ public double parseDouble(byte[] str, int offset, int length) { + long bitPattern; if (isAscii || !ignoreCase && isAllSingleCharSymbolsAscii) { - return Double.longBitsToDouble(getByteArrayAsciiParser().parseFloatingPointLiteral(str, offset, length)); + bitPattern = getByteArrayAsciiParser().parseFloatingPointLiteral(str, offset, length); } else if (isDigitsAscii) { - return Double.longBitsToDouble(getByteArrayUtf8Parser().parseFloatingPointLiteral(str, offset, length)); + bitPattern = getByteArrayUtf8Parser().parseFloatingPointLiteral(str, offset, length); } else { - /* - String string = new String(str, offset, length, StandardCharsets.UTF_8); - return Double.longBitsToDouble(getCharSequenceParser().parseFloatingPointLiteral(string,0,string.length())); - */ Utf8Decoder.Result result = Utf8Decoder.decode(str, offset, length); - return Double.longBitsToDouble(getCharArrayParser().parseFloatingPointLiteral(result.chars(), 0, result.length())); + bitPattern = getCharArrayParser().parseFloatingPointLiteral(result.chars(), 0, result.length()); } + if (bitPattern == SYNTAX_ERROR_BITS) throw new NumberFormatException(SYNTAX_ERROR); + return Double.longBitsToDouble(bitPattern); } } diff --git a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/JavaDoubleParser.java b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/JavaDoubleParser.java index 7c713170..86c11793 100644 --- a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/JavaDoubleParser.java +++ b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/JavaDoubleParser.java @@ -4,6 +4,9 @@ */ package ch.randelshofer.fastdoubleparser; +import static ch.randelshofer.fastdoubleparser.AbstractNumberParser.SYNTAX_ERROR; +import static ch.randelshofer.fastdoubleparser.AbstractNumberParser.SYNTAX_ERROR_BITS; + /** * Parses a {@code double} value; the supported syntax is compatible with * {@link Double#valueOf(String)}. @@ -185,6 +188,7 @@ public static double parseDouble(CharSequence str) throws NumberFormatException */ public static double parseDouble(CharSequence str, int offset, int length) throws NumberFormatException { long bitPattern = CHAR_SEQUENCE_PARSER.parseFloatingPointLiteral(str, offset, length); + if (bitPattern == SYNTAX_ERROR_BITS) throw new NumberFormatException(SYNTAX_ERROR); return Double.longBitsToDouble(bitPattern); } @@ -219,6 +223,7 @@ public static double parseDouble(byte[] str) throws NumberFormatException { */ public static double parseDouble(byte[] str, int offset, int length) throws NumberFormatException { long bitPattern = BYTE_ARRAY_PARSER.parseFloatingPointLiteral(str, offset, length); + if (bitPattern == SYNTAX_ERROR_BITS) throw new NumberFormatException(SYNTAX_ERROR); return Double.longBitsToDouble(bitPattern); } @@ -251,6 +256,7 @@ public static double parseDouble(char[] str) throws NumberFormatException { */ public static double parseDouble(char[] str, int offset, int length) throws NumberFormatException { long bitPattern = CHAR_ARRAY_PARSER.parseFloatingPointLiteral(str, offset, length); + if (bitPattern == SYNTAX_ERROR_BITS) throw new NumberFormatException(SYNTAX_ERROR); return Double.longBitsToDouble(bitPattern); } } \ No newline at end of file diff --git a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/JavaFloatParser.java b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/JavaFloatParser.java index 5ebee874..b9291108 100644 --- a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/JavaFloatParser.java +++ b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/JavaFloatParser.java @@ -4,6 +4,9 @@ */ package ch.randelshofer.fastdoubleparser; +import static ch.randelshofer.fastdoubleparser.AbstractNumberParser.SYNTAX_ERROR; +import static ch.randelshofer.fastdoubleparser.AbstractNumberParser.SYNTAX_ERROR_BITS; + /** * Parses a {@code float} value; the supported syntax is compatible with * {@link Float#valueOf(String)}. @@ -67,6 +70,7 @@ public static float parseFloat(CharSequence str) throws NumberFormatException { */ public static float parseFloat(CharSequence str, int offset, int length) throws NumberFormatException { long bitPattern = CHAR_SEQUENCE_PARSER.parseFloatingPointLiteral(str, offset, length); + if (bitPattern == SYNTAX_ERROR_BITS) throw new NumberFormatException(SYNTAX_ERROR); return Float.intBitsToFloat((int) bitPattern); } @@ -99,6 +103,7 @@ public static float parseFloat(byte[] str) throws NumberFormatException { */ public static float parseFloat(byte[] str, int offset, int length) throws NumberFormatException { long bitPattern = BYTE_ARRAY_PARSER.parseFloatingPointLiteral(str, offset, length); + if (bitPattern == SYNTAX_ERROR_BITS) throw new NumberFormatException(SYNTAX_ERROR); return Float.intBitsToFloat((int) bitPattern); } @@ -130,6 +135,7 @@ public static float parseFloat(char[] str) throws NumberFormatException { */ public static float parseFloat(char[] str, int offset, int length) throws NumberFormatException { long bitPattern = CHAR_ARRAY_PARSER.parseFloatingPointLiteral(str, offset, length); + if (bitPattern == SYNTAX_ERROR_BITS) throw new NumberFormatException(SYNTAX_ERROR); return Float.intBitsToFloat((int) bitPattern); } } \ No newline at end of file diff --git a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/JsonDoubleParser.java b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/JsonDoubleParser.java index e0b5135e..342eaecb 100644 --- a/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/JsonDoubleParser.java +++ b/fastdoubleparser-dev/src/main/java/ch.randelshofer.fastdoubleparser/ch/randelshofer/fastdoubleparser/JsonDoubleParser.java @@ -4,6 +4,9 @@ */ package ch.randelshofer.fastdoubleparser; +import static ch.randelshofer.fastdoubleparser.AbstractNumberParser.SYNTAX_ERROR; +import static ch.randelshofer.fastdoubleparser.AbstractNumberParser.SYNTAX_ERROR_BITS; + /** * Parses a {@code double} value; the supported syntax is compatible with * {@code number} in the JSON format specification. @@ -81,6 +84,7 @@ public static double parseDouble(CharSequence str) throws NumberFormatException */ public static double parseDouble(CharSequence str, int offset, int length) throws NumberFormatException { long bitPattern = CHAR_SEQUENCE_PARSER.parseNumber(str, offset, length); + if (bitPattern == SYNTAX_ERROR_BITS) throw new NumberFormatException(SYNTAX_ERROR); return Double.longBitsToDouble(bitPattern); } @@ -114,6 +118,7 @@ public static double parseDouble(byte[] str) throws NumberFormatException { */ public static double parseDouble(byte[] str, int offset, int length) throws NumberFormatException { long bitPattern = BYTE_ARRAY_PARSER.parseNumber(str, offset, length); + if (bitPattern == SYNTAX_ERROR_BITS) throw new NumberFormatException(SYNTAX_ERROR); return Double.longBitsToDouble(bitPattern); } @@ -145,6 +150,7 @@ public static double parseDouble(char[] str) throws NumberFormatException { */ public static double parseDouble(char[] str, int offset, int length) throws NumberFormatException { long bitPattern = CHAR_ARRAY_PARSER.parseNumber(str, offset, length); + if (bitPattern == SYNTAX_ERROR_BITS) throw new NumberFormatException(SYNTAX_ERROR); return Double.longBitsToDouble(bitPattern); } } \ No newline at end of file