Package ch.randelshofer.fastdoubleparser
Class JavaDoubleParser
- java.lang.Object
-
- ch.randelshofer.fastdoubleparser.JavaDoubleParser
-
public final class JavaDoubleParser extends java.lang.ObjectParses adoublevalue; the supported syntax is compatible withDouble.valueOf(String).Syntax
Leading and trailing whitespace characters in the string are ignored. Whitespace is removed as if by the
String.trim()method; that is, characters in the range [U+0000,U+0020].The rest of string should constitute a Java
FloatingPointLiteralas described by the lexical syntax rules shown below:- FloatingPointLiteral:
- [Sign]
NaN - [Sign]
Infinity - [Sign] DecimalFloatingPointLiteral
- [Sign] HexFloatingPointLiteral
- HexFloatingPointLiteral:
- HexSignificand BinaryExponent [FloatTypeSuffix]
- HexSignificand:
- HexNumeral
- HexNumeral
.0x[HexDigits].HexDigits0X[HexDigits].HexDigits - HexNumeral
- BinaryExponent:
- BinaryExponentIndicator SignedInteger
- BinaryExponentIndicator:
pP
- DecimalFloatingPointLiteral:
- DecSignificand [DecExponent] [FloatTypeSuffix]
- DecSignificand:
- IntegerPart
.[FractionPart].FractionPart- IntegerPart
- IntegerPart:
- Digits
- FractionPart:
- Digits
- DecExponent:
- ExponentIndicator [Sign] Digits
- ExponentIndicator:
eE
- Sign:
++-
- Digits:
- Digit {Digit}
- Digit:
- (one of)
0 1 2 3 4 5 6 7 8 9
- HexNumeral:
0xHexDigits0XHexDigits
- HexDigits:
- HexDigit {HexDigit}
- HexDigit:
- (one of)
0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F
- FloatTypeSuffix:
- (one of)
f F d D
Expected character lengths for values produced by
Double.toString(double):DecSignificand(IntegerPart+FractionPart): 1 to 17 digitsIntegerPart: 1 to 7 digitsFractionPart: 1 to 16 digitsSignedIntegerin exponent: 1 to 3 digitsFloatingPointLiteral: 1 to 24 characters, e.g. "-1.2345678901234568E-300"
FloatingPointLiteralwith or without white space around it:Integer.MAX_VALUE- 4 = 2,147,483,643 characters.
References:
- The Java® Language Specification, Java SE 18 Edition, Chapter 3. Lexical Structure, 3.10.2. Floating-Point Literals
- docs.oracle.com
-
-
Field Summary
Fields Modifier and Type Field Description private static JavaDoubleBitsFromByteArrayBYTE_ARRAY_PARSERprivate static JavaDoubleBitsFromCharArrayCHAR_ARRAY_PARSERprivate static JavaDoubleBitsFromCharSequenceCHAR_SEQUENCE_PARSER
-
Constructor Summary
Constructors Modifier Constructor Description privateJavaDoubleParser()Don't let anyone instantiate this class.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static doubleparseDouble(byte[] str)Convenience method for callingparseDouble(byte[], int, int).static doubleparseDouble(byte[] str, int offset, int length)Parses aFloatingPointLiteralfrom abyte-Array and converts it into adoublevalue.static doubleparseDouble(char[] str)Convenience method for callingparseDouble(char[], int, int).static doubleparseDouble(char[] str, int offset, int length)Parses aFloatingPointLiteralfrom abyte-Array and converts it into adoublevalue.static doubleparseDouble(java.lang.CharSequence str)Convenience method for callingparseDouble(CharSequence, int, int).static doubleparseDouble(java.lang.CharSequence str, int offset, int length)Parses aFloatingPointLiteralfrom aCharSequenceand converts it into adoublevalue.
-
-
-
Field Detail
-
BYTE_ARRAY_PARSER
private static final JavaDoubleBitsFromByteArray BYTE_ARRAY_PARSER
-
CHAR_ARRAY_PARSER
private static final JavaDoubleBitsFromCharArray CHAR_ARRAY_PARSER
-
CHAR_SEQUENCE_PARSER
private static final JavaDoubleBitsFromCharSequence CHAR_SEQUENCE_PARSER
-
-
Method Detail
-
parseDouble
public static double parseDouble(java.lang.CharSequence str) throws java.lang.NumberFormatExceptionConvenience method for callingparseDouble(CharSequence, int, int).- Parameters:
str- the string to be parsed- Returns:
- the parsed value
- Throws:
java.lang.NullPointerException- if the string is nulljava.lang.NumberFormatException- if the string can not be parsed successfully
-
parseDouble
public static double parseDouble(java.lang.CharSequence str, int offset, int length) throws java.lang.NumberFormatExceptionParses aFloatingPointLiteralfrom aCharSequenceand converts it into adoublevalue.- Parameters:
str- the string to be parsedoffset- the start offset of theFloatingPointLiteralinstrlength- the length ofFloatingPointLiteralinstr- Returns:
- the parsed value
- Throws:
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 successfully
-
parseDouble
public static double parseDouble(byte[] str) throws java.lang.NumberFormatExceptionConvenience method for callingparseDouble(byte[], int, int).- Parameters:
str- the string to be parsed, a byte array with characters in ISO-8859-1, ASCII or UTF-8 encoding- Returns:
- the parsed value
- Throws:
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 successfully
-
parseDouble
public static double parseDouble(byte[] str, int offset, int length) throws java.lang.NumberFormatExceptionParses aFloatingPointLiteralfrom abyte-Array and converts it into adoublevalue.- Parameters:
str- the string to be parsed, a byte array with characters in ISO-8859-1, ASCII or UTF-8 encodingoffset- The index of the first byte to parselength- The number of bytes to parse- Returns:
- the parsed value
- Throws:
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 successfully
-
parseDouble
public static double parseDouble(char[] str) throws java.lang.NumberFormatExceptionConvenience method for callingparseDouble(char[], int, int).- Parameters:
str- the string to be parsed- Returns:
- the parsed value
- Throws:
java.lang.NullPointerException- if the string is nulljava.lang.NumberFormatException- if the string can not be parsed successfully
-
parseDouble
public static double parseDouble(char[] str, int offset, int length) throws java.lang.NumberFormatExceptionParses aFloatingPointLiteralfrom abyte-Array and converts it into adoublevalue.See
JavaDoubleParserfor the syntax ofFloatingPointLiteral.- Parameters:
str- the string to be parsed, a byte array with characters in ISO-8859-1, ASCII or UTF-8 encodingoffset- The index of the first character to parselength- The number of characters to parse- Returns:
- the parsed value
- Throws:
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 successfully
-
-