Skip to content

Commit

Permalink
Move parsing of NaN and infinity to the end of the parsing algorithm.
Browse files Browse the repository at this point in the history
  • Loading branch information
wrandelshofer committed Oct 18, 2024
1 parent e3f06a5 commit 7a3ae60
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,6 @@ public final long parseFloatingPointLiteral(char[] str, int offset, int length)
}
}

// Parse NaN or Infinity (this occurs rarely)
// ---------------------
if (nanOrInfinityChar.containsKey(ch)) {
return parseNaNOrInfinity(str, index, endIndex, isNegative);
}

// Parse significand
// -----------------
// Note: a multiplication by a constant is cheaper than an
Expand Down Expand Up @@ -175,11 +169,11 @@ public final long parseFloatingPointLiteral(char[] str, int offset, int length)
exponent += expNumber;
}

// Check if FloatingPointLiteral is complete
// ------------------------
// Parse NaN or Infinity (this occurs rarely)
// ---------------------
if (illegal || index < endIndex
|| digitCount == 0) {
throw new NumberFormatException(SYNTAX_ERROR);
return parseNaNOrInfinity(str, index, endIndex, isNegative);
}

// Re-parse significand in case of a potential overflow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,6 @@ public final long parseFloatingPointLiteral(CharSequence str, int offset, int le
}
}

// Parse NaN or Infinity (this occurs rarely)
// ---------------------
if (nanOrInfinityChar.containsKey(ch)) {
return parseNaNOrInfinity(str, index, endIndex, isNegative);
}


// Parse significand
// -----------------
Expand Down Expand Up @@ -177,11 +171,11 @@ public final long parseFloatingPointLiteral(CharSequence str, int offset, int le
exponent += expNumber;
}

// Check if FloatingPointLiteral is complete
// ------------------------
// Parse NaN or Infinity (this occurs rarely)
// ---------------------
if (illegal || index < endIndex
|| digitCount == 0) {
throw new NumberFormatException(SYNTAX_ERROR);
return parseNaNOrInfinity(str, index, endIndex, isNegative);
}

// Re-parse significand in case of a potential overflow
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ private long parseDecFloatLiteral(byte[] str, int index, int startIndex, int end
index = skipWhitespace(str, index, endIndex);
if (illegal || index < endIndex
|| !hasLeadingZero && digitCount == 0) {
throw new NumberFormatException(SYNTAX_ERROR);
// Parse NaN or Infinity (this occurs rarely)
// ---------------------
return parseNaNOrInfinity(str, index, endIndex, isNegative);
}

// Re-parse significand in case of a potential overflow
Expand Down Expand Up @@ -218,12 +220,6 @@ public long parseFloatingPointLiteral(byte[] str, int offset, int length) {
}
}

// Parse NaN or Infinity
// ---------------------
if (ch >= 'I') {
return parseNaNOrInfinity(str, index, endIndex, isNegative);
}

// Parse optional leading zero
// ---------------------------
final boolean hasLeadingZero = ch == '0';
Expand Down Expand Up @@ -378,6 +374,7 @@ private long parseHexFloatingPointLiteral(


private long parseNaNOrInfinity(byte[] str, int index, int endIndex, boolean isNegative) {
if (index < endIndex) {
if (str[index] == 'N') {
if (index + 2 < endIndex
// && str[index] == 'N'
Expand All @@ -399,6 +396,7 @@ private long parseNaNOrInfinity(byte[] str, int index, int endIndex, boolean isN
}
}
}
}
throw new NumberFormatException(SYNTAX_ERROR);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,9 @@ private long parseDecFloatLiteral(char[] str, int index, int startIndex, int end
index = skipWhitespace(str, index, endIndex);
if (illegal || index < endIndex
|| !hasLeadingZero && digitCount == 0) {
throw new NumberFormatException(SYNTAX_ERROR);
// Parse NaN or Infinity (this occurs rarely)
// ---------------------
return parseNaNOrInfinity(str, index, endIndex, isNegative);
}

// Re-parse significand in case of a potential overflow
Expand Down Expand Up @@ -220,12 +222,6 @@ public long parseFloatingPointLiteral(char[] str, int offset, int length) {
}
}

// Parse NaN or Infinity
// ---------------------
if (ch >= 'I') {
return parseNaNOrInfinity(str, index, endIndex, isNegative);
}

// Parse optional leading zero
// ---------------------------
final boolean hasLeadingZero = ch == '0';
Expand Down Expand Up @@ -379,6 +375,7 @@ private long parseHexFloatLiteral(
}

private long parseNaNOrInfinity(char[] str, int index, int endIndex, boolean isNegative) {
if (index < endIndex) {
if (str[index] == 'N') {
if (index + 2 < endIndex
// && str[index] == 'N'
Expand Down Expand Up @@ -407,6 +404,7 @@ private long parseNaNOrInfinity(char[] str, int index, int endIndex, boolean isN
}
}
}
}
throw new NumberFormatException(SYNTAX_ERROR);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ private long parseDecFloatLiteral(CharSequence str, int index, int startIndex, i
index = skipWhitespace(str, index, endIndex);
if (illegal || index < endIndex
|| !hasLeadingZero && digitCount == 0) {
throw new NumberFormatException(SYNTAX_ERROR);
// Parse NaN or Infinity (this occurs rarely)
// ---------------------
return parseNaNOrInfinity(str, index, endIndex, isNegative);
}

// Re-parse significand in case of a potential overflow
Expand Down Expand Up @@ -217,12 +219,6 @@ public final long parseFloatingPointLiteral(CharSequence str, int offset, int le
}
}

// Parse NaN or Infinity (this occurs rarely)
// ---------------------
if (ch >= 'I') {
return parseNaNOrInfinity(str, index, endIndex, isNegative);
}

// Parse optional leading zero
// ---------------------------
final boolean hasLeadingZero = ch == '0';
Expand Down Expand Up @@ -375,6 +371,7 @@ private long parseHexFloatLiteral(


private long parseNaNOrInfinity(CharSequence str, int index, int endIndex, boolean isNegative) {
if (index < endIndex) {
if (str.charAt(index) == 'N') {
if (index + 2 < endIndex
// && str.charAt(index) == 'N'
Expand Down Expand Up @@ -403,6 +400,7 @@ private long parseNaNOrInfinity(CharSequence str, int index, int endIndex, boole
}
}
}
}
throw new NumberFormatException(SYNTAX_ERROR);
}

Expand Down

0 comments on commit 7a3ae60

Please sign in to comment.