Skip to content
New issue

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

By clicking “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? Sign in to your account

Add support for parsing numbers according to JSON format #220

Merged
merged 6 commits into from
Sep 15, 2023

Conversation

mayawarrier
Copy link
Contributor

@mayawarrier mayawarrier commented Sep 14, 2023

Details on the format are available here (RFC 8259): https://datatracker.ietf.org/doc/html/rfc8259#section-6
This is compatible with previous JSON RFCs as well as the number format hasn't changed.

@lemire
Copy link
Member

lemire commented Sep 14, 2023

Looks like a good idea. I will review and I expect to merge.

@@ -16,6 +16,7 @@ enum chars_format {
scientific = 1 << 0,
fixed = 1 << 2,
hex = 1 << 3,
json = 1 << 4 | fixed | scientific,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obviously, this will cause a conflict with #219

but that's not a big issue.


int main()
{
const std::vector<double> expected{ -0.2, 0.02, 0.002, 1., 0. };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both 1. and 0. are fine, but my expectation is that you could use 1 and 0. (This is not a request for you to change the code, just a comment.)

@lemire
Copy link
Member

lemire commented Sep 14, 2023

@mayawarrier I think that the JSON format does not allow a leading +... only in the exponent, so 1e+1 is ok, but +1e1 is not.

Copy link
Member

@lemire lemire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not clear that this code disallows leading pluses, and I think it should.

@mayawarrier
Copy link
Contributor Author

mayawarrier commented Sep 14, 2023

@mayawarrier I think that the JSON format does not allow a leading +... only in the exponent, so 1e+1 is ok, but +1e1 is not.

Got it, I'll just ignore the FASTFLOAT_ALLOWS_LEADING_PLUS macro when the runtime format is json.

@mayawarrier
Copy link
Contributor Author

I disallowed leading pluses as requested. I also added an option to allow inf/nan in JSON mode. This is a common option in JSON parsers despite not being part of the standard.

@lemire
Copy link
Member

lemire commented Sep 15, 2023

I am sure that this could be perfected, but it looks solid to me, merging.

@lemire lemire merged commit e6b370d into fastfloat:main Sep 15, 2023
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants