Skip to content

Commit

Permalink
Return a magic bit pattern instead of throwing an exception.
Browse files Browse the repository at this point in the history
  • Loading branch information
wrandelshofer committed Oct 20, 2024
1 parent 5e6ce73 commit b2fc12e
Show file tree
Hide file tree
Showing 16 changed files with 123 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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];

Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -402,7 +402,7 @@ private long parseNaNOrInfinity(byte[] str, int index, int endIndex, boolean isN
}
}
}
throw new NumberFormatException(SYNTAX_ERROR);
return SYNTAX_ERROR_BITS;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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];

Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -409,7 +409,7 @@ private long parseNaNOrInfinity(char[] str, int index, int endIndex, boolean isN
}
}
}
throw new NumberFormatException(SYNTAX_ERROR);
return SYNTAX_ERROR_BITS;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -407,7 +407,7 @@ private long parseNaNOrInfinity(CharSequence str, int index, int endIndex, boole
}
}
}
throw new NumberFormatException(SYNTAX_ERROR);
return SYNTAX_ERROR_BITS;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand All @@ -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;
}
}

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading

0 comments on commit b2fc12e

Please sign in to comment.