Skip to content

Handle null values while parsing double and float #57

New issue

Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “No Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? No Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/main/java/org/simdjson/NumberParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ float parseFloat(byte[] buffer, int len, int offset) {
currentIdx = digitsParsingResult.currentIdx();
int digitCount = currentIdx - digitsStartIdx;
if (digitCount == 0) {
if (checkIfNull(buffer, len, offset)) return Float.NaN;
throw new JsonParsingException("Invalid number. Minus has to be followed by a digit.");
}
if ('0' == buffer[digitsStartIdx] && digitCount > 1) {
Expand Down Expand Up @@ -273,6 +274,7 @@ float parseFloat(byte[] buffer, int len, int offset) {
currentIdx = digitsParsingResult.currentIdx();
int digitCount = currentIdx - digitsStartIdx;
if (digitCount == 0) {
if (checkIfNull(buffer, len, offset)) return Double.NaN;
throw new JsonParsingException("Invalid number. Minus has to be followed by a digit.");
}
if ('0' == buffer[digitsStartIdx] && digitCount > 1) {
Expand Down Expand Up @@ -360,4 +362,11 @@ int currentIdx() {
return currentIdx;
}
}

private boolean checkIfNull(byte[] buffer, int len, int offset) {
boolean statsWithN = buffer[offset] == 'n' || buffer[offset] == 'N';
boolean endsWithL = buffer[offset + 3] == 'l' || buffer[offset + 3] == 'L';
return statsWithN && endsWithL;
}

}