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

Parse NaN from object like {"n": NaN} #25

Merged
merged 1 commit into from
Apr 6, 2017

Conversation

ajdavis
Copy link
Contributor

@ajdavis ajdavis commented Apr 5, 2017

Parsing is case-insensitive. Closes #24.

@ajdavis
Copy link
Contributor Author

ajdavis commented Apr 5, 2017

This is ready for an initial review. "make check" passes, but there's not yet any test for the new NaN parsing. The next steps are to control this NaN parsing with a compile-time or runtime option, and to add a test for it that expects success or failure depending on whether NaN parsing is enabled.

Would you like to do those steps, or tell me how I should do them?

state->special_flags &= ~JSONSL_SPECIALf_NULL;
}

if (tolower(CUR_CHAR) != 'a') {
Copy link
Owner

Choose a reason for hiding this comment

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

else if

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually it's two "!=" statements, so CUR_CHAR might be neither 'a', 'N', nor 'n', I think we have to check both if statements.

jsonsl.c Outdated
@@ -1425,11 +1440,13 @@ static unsigned short Special_Table[0x100] = {
/* 0x37 */ JSONSL_SPECIALf_UNSIGNED /* <7> */, /* 0x37 */
/* 0x38 */ JSONSL_SPECIALf_UNSIGNED /* <8> */, /* 0x38 */
/* 0x39 */ JSONSL_SPECIALf_UNSIGNED /* <9> */, /* 0x39 */
/* 0x3a */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x59 */
/* 0x3a */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
/* 0x4e */ JSONSL_SPECIALf_NAN /* <N> */, /* 0x4e */
Copy link
Owner

Choose a reason for hiding this comment

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

FYI this table is generated by a Perl script. Better to modify it there.

jsonsl.c Outdated
/* 0x5a */ 0,0,0,0,0,0,0,0,0,0,0,0, /* 0x65 */
/* 0x66 */ JSONSL_SPECIALf_FALSE /* <f> */, /* 0x66 */
/* 0x67 */ 0,0,0,0,0,0,0, /* 0x6d */
/* 0x6e */ JSONSL_SPECIALf_NULL /* <n> */, /* 0x6e */
/* 0x6e */ JSONSL_SPECIALf_NULL | JSONSL_SPECIALf_NAN /* <n> */, /* 0x6e */
Copy link
Owner

Choose a reason for hiding this comment

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

likewise

@mnunberg
Copy link
Owner

mnunberg commented Apr 5, 2017

JSONSL_PARSE_NAN would be the compiler directive.

Regarding tests - all existing parse tests load from a file. You'd make a new file and simply feed a [nan] or similar, verifying it parses successfully. This reminds me that I should add better tests for type-specific valdiation (right now all our tests do is ensure that things parse successfully (or not)).

@mnunberg mnunberg closed this Apr 5, 2017
@mnunberg mnunberg reopened this Apr 5, 2017
@mnunberg
Copy link
Owner

mnunberg commented Apr 5, 2017

oops. didn't mean to close

@ajdavis
Copy link
Contributor Author

ajdavis commented Apr 5, 2017

I see the tests didn't work on Travis as they did on my machine, investigating....

@ajdavis
Copy link
Contributor Author

ajdavis commented Apr 5, 2017

OK, this pull request is ready for review.

@mnunberg
Copy link
Owner

mnunberg commented Apr 6, 2017

Can you squash these into a single commit?

Also, I'm still a bit uneasy about having the NaN parsing around there as dead code even if it's not being compiled that way. I have another idea. How about making a proxy variable.. e.g.

(in the character table for special_begin):

JSONSL_SPECIALf_NULL|JSONSL__NAN_PROXY

Then..

#ifdef JSONSL_PARSE_NAN
#define JSONSL__NAN_PROXY JSONSL_SPECIALf_NAN
#else
#define JSONSL__NAN_PROXY 0
#endif

You can even go one step further and only define the enum value if PARSE_NAN is defined (there's no reason for the NAN value to be there otherwise).. though I define the macros using the xmacro pattern.. so I'm not sure tht's easily modifiable.

This way, the current (pre-nan) code can remain the same, and newer code/branches can be added on only if PARSE_NAN is defined. The way you have it currently, it will run through some cycles determining NAN stuff even if nan parsing is disabled. My desire to have this be a compile time option is less about one of configuration and more about not impacting performance (even theoretically) for less common situations (granted, null isn't super common either..).

So basically:

#ifdef JSONSL_PARSE_NAN
else if (state->special_flags & JSONSL_SPECIALf_NAN) {
    // ...
}
#endif

Because _NAN_PROXY is 0 if PARSE_NAN is not defined, the character table will only yield JSONSL_SPECIALf_NULL on n and nothing on N.

Parsing is case-insensitive. Closes mnunberg#24.
@ajdavis
Copy link
Contributor Author

ajdavis commented Apr 6, 2017

Done, let me know if I followed your instructions correctly.

@mnunberg mnunberg merged commit 85d048e into mnunberg:master Apr 6, 2017
@mnunberg
Copy link
Owner

mnunberg commented Apr 6, 2017

yep! exactly what I had in mind. thanks for the contribution!

@ajdavis ajdavis deleted the parse-nan branch April 6, 2017 19:43
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