public class FastDoubleParser
extends java.lang.Object
Licensing: MIT License
Copyright (c) 2024 Werner Randelshofer, Switzerland.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Some portions of the code have been derived from other projects. All these projects require that we include a copyright notice, and some require that we also include some text of their license file.
fast_double_parser, Copyright (c) 2022 Daniel Lemire. BSL License. https://github.com/lemire/fast_double_parser https://github.com/lemire/fast_double_parser/blob/07d9189a8fb815fe800cb15ca022e7a07093236e/LICENSE.BSL (The file 'thirdparty-LICENSE' is included in the sources and classes Jar files that are released by this project - as is required by that license.)
fast_float, Copyright (c) 2021 The fast_float authors. MIT License. https://github.com/fastfloat/fast_float https://github.com/fastfloat/fast_float/blob/cc1e01e9eee74128e48d51488a6b1df4a767a810/LICENSE-MIT (The file 'thirdparty-LICENSE' is included in the sources and classes Jar files that are released by this project - as is required by that license.)
Modifier and Type | Field and Description |
---|---|
static int |
DOUBLE_EXPONENT_BIAS
Bias used in the exponent of a double.
|
static int |
DOUBLE_SIGNIFICAND_WIDTH
The number of bits in the significand, including the implicit bit.
|
static java.lang.String |
ILLEGAL_OFFSET_OR_ILLEGAL_LENGTH
Message text for the
IllegalArgumentException that is thrown
when offset or length are illegal |
static int |
MAX_REQUIRED_DIGITS |
static long |
MINIMAL_NINETEEN_DIGIT_INTEGER
This is the smallest non-negative number that has 19 decimal digits.
|
static java.lang.String |
SYNTAX_ERROR
Message text for the
NumberFormatException that is thrown
when the syntax is illegal. |
static long |
SYNTAX_ERROR_BITS
Uses the unused mantissa of a NaN value to encode a syntax error.
|
Constructor and Description |
---|
FastDoubleParser() |
Modifier and Type | Method and Description |
---|---|
static double |
parseDouble(java.lang.CharSequence str)
Convenience method for calling
parseDouble(CharSequence, int,
int) . |
static double |
parseDouble(java.lang.CharSequence str,
int offset,
int length)
Parses a
FloatingPointLiteral from a CharSequence and
converts it into a double value. |
long |
parseFloatingPointLiteral(java.lang.CharSequence str,
int offset,
int length)
Parses a
FloatingPointLiteral production with optional leading
and trailing white space. |
static double |
tryDecFloatToDoubleTruncated(boolean isNegative,
long significand,
int exponent,
boolean isSignificandTruncated,
int exponentOfTruncatedSignificand)
Tries to compute
significand * 10^exponent exactly using a fast
algorithm; and if isNegative is true, negate the result;
the significand can be truncated. |
static long |
tryToParseEightHexDigits(java.lang.CharSequence str,
int offset)
Tries to parse eight digits at once using the
'SIMD within a register technique' (SWAR).
|
static long |
tryToParseEightHexDigitsUtf16(long first,
long second)
Tries to parse eight hex digits from two longs using the
'SIMD within a register technique' (SWAR).
|
static long |
tryToParseEightHexDigitsUtf8(long chunk)
Tries to parse eight digits from a long using the
'SIMD within a register technique' (SWAR).
|
public static final int DOUBLE_EXPONENT_BIAS
public static final int DOUBLE_SIGNIFICAND_WIDTH
public static final int MAX_REQUIRED_DIGITS
public static final long MINIMAL_NINETEEN_DIGIT_INTEGER
public static final java.lang.String SYNTAX_ERROR
NumberFormatException
that is thrown
when the syntax is illegal.public static final long SYNTAX_ERROR_BITS
public static final java.lang.String ILLEGAL_OFFSET_OR_ILLEGAL_LENGTH
IllegalArgumentException
that is thrown
when offset or length are illegalpublic static double parseDouble(java.lang.CharSequence str) throws java.lang.NumberFormatException
parseDouble(CharSequence, int,
int)
.str
- the string to be parsedjava.lang.NullPointerException
- if the string is nulljava.lang.NumberFormatException
- if the string can not be parsed successfullypublic static double parseDouble(java.lang.CharSequence str, int offset, int length) throws java.lang.NumberFormatException
FloatingPointLiteral
from a CharSequence
and
converts it into a double
value.str
- the string to be parsedoffset
- the start offset of the FloatingPointLiteral
in
str
length
- the length of FloatingPointLiteral
in str
java.lang.NullPointerException
- if the string is nulljava.lang.IllegalArgumentException
- if offset or length are illegaljava.lang.NumberFormatException
- if the string can not be parsed
successfullypublic final long parseFloatingPointLiteral(java.lang.CharSequence str, int offset, int length)
FloatingPointLiteral
production with optional leading
and trailing white space.
- FloatingPointLiteralWithWhiteSpace:
- [WhiteSpace] FloatingPointLiteral [WhiteSpace]
str
- a string containing a FloatingPointLiteralWithWhiteSpace
offset
- start offset of FloatingPointLiteralWithWhiteSpace
in str
length
- length of FloatingPointLiteralWithWhiteSpace
in
str
-1L
.public static double tryDecFloatToDoubleTruncated(boolean isNegative, long significand, int exponent, boolean isSignificandTruncated, int exponentOfTruncatedSignificand)
significand * 10^exponent
exactly using a fast
algorithm; and if isNegative
is true, negate the result;
the significand can be truncated.isNegative
- true if the sign is negativesignificand
- the significandexponent
- the exponent number (the power)isSignificandTruncated
- true if significand has been truncatedexponentOfTruncatedSignificand
- the exponent number of the
truncated significandDouble.NaN
if the fast path failed.public static long tryToParseEightHexDigits(java.lang.CharSequence str, int offset)
str
- a character sequenceoffset
- the index of the first character in the character sequencepublic static long tryToParseEightHexDigitsUtf16(long first, long second)
char[] chars = ...;
long first = (long) chars[0] << 48
| (long) chars[1] << 32
| (long) chars[2] << 16
| (long) chars[3];
long second = (long) chars[4] << 48
| (long) chars[5] << 32
| (long) chars[6] << 16
| (long) chars[7];
first
- contains 4 utf-16 characters in big endian ordersecond
- contains 4 utf-16 characters in big endian orderpublic static long tryToParseEightHexDigitsUtf8(long chunk)
chunk
- contains 8 ascii characters in big endian ordervalue
does not contain 8 digits