Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Simplify JSON_INT_MAX #102

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

Closed
wants to merge 1 commit into from

Conversation

per-ok
Copy link
Contributor

@per-ok per-ok commented Aug 3, 2021

Simplifies the JSON_INT_MAX expression.

Thanks to @LB-- for suggesting to use CHAR_INT instead of 8 (char type is defined as at least 8 bits).

@LB--
Copy link
Member

LB-- commented Aug 7, 2021

Seems alright to me, though I wonder if the #include <stdint.h> stuff at the top of the file can be safely removed now? I don't see any other obvious places in the source where that header's content was needed.

@DimitriPapadopoulos
Copy link
Contributor

DimitriPapadopoulos commented Aug 7, 2021

I agree <stdint.h> should be removed from json.c, but it should be included by json.h instead of <inttypes.h>:

#ifndef json_int_t
   #ifndef _MSC_VER
      #include <inttypes.h>
      #define json_int_t int64_t

Indeed, the int64_t type is defined in <stdint.h>. Actually, <inttypes.h> includes <stdint.h> and simply defines additional printf-related macros for the types defined in <stdint.h>, which are not needed here as far as I can see.

Fixed by pull request #109.

@DimitriPapadopoulos
Copy link
Contributor

DimitriPapadopoulos commented Aug 10, 2021

Header <limits.h> is ANSI C, so this patch looks good.

Indeed, <stdint.h> can be removed. Note that it will be added back by #109, but for #ifndef _MSC_VER, not for #ifdef _MSC_VER.

@LB-- LB-- added this to the v1.1.1 milestone Aug 14, 2021
LB-- added a commit to LB--/json-parser that referenced this pull request Aug 14, 2021
Also added contributor's name to author file.
Copy link
Member

@LB-- LB-- left a comment

Choose a reason for hiding this comment

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

Hm, I tried merging this but it breaks the build due to mismatched sign comparisons, the original definition of the macro was signed whereas this new macro is unsigned. Is it safe to just add a cast back to json_int_t?

Also, if you have a preference about your name being in the AUTHORS file, now would be the time to say so.

@LB-- LB-- linked an issue Aug 14, 2021 that may be closed by this pull request
: (sizeof(json_int_t) == 4
? INT32_MAX
: INT64_MAX));
#define JSON_INT_MAX ((1UL << ((sizeof(json_int_t) * CHAR_BIT) - 1)) - 1)
Copy link
Contributor

Choose a reason for hiding this comment

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

For a patch release such as 1.1.1, I would change as little code as possible:

static const json_int_t JSON_INT_MAX = (json_int_t)((1UL << ((sizeof(json_int_t) * CHAR_BIT) - 1)) - 1);

Copy link
Contributor

@DimitriPapadopoulos DimitriPapadopoulos Aug 14, 2021

Choose a reason for hiding this comment

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

Also, 1UL is an unsigned long. What if json_int_t is larger than long?

Copy link
Member

Choose a reason for hiding this comment

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

That's a very good point, perhaps the alternative you proposed in #84 would be better then

DimitriPapadopoulos added a commit to DimitriPapadopoulos/json-parser that referenced this pull request Aug 16, 2021
Either it has been defined explictly, or we attempt to calculate it
using the method reverted by fcfa748.

See discussion in json-parser#84 and json-parser#102.
DimitriPapadopoulos added a commit to DimitriPapadopoulos/json-parser that referenced this pull request Aug 16, 2021
Either it has been defined explictly, or we attempt to calculate it
using the method reverted by fcfa748.

See discussion in json-parser#84 and json-parser#102.
DimitriPapadopoulos added a commit to DimitriPapadopoulos/json-parser that referenced this pull request Aug 18, 2021
Either it has been defined explictly, or we attempt to calculate it
using the method reverted by fcfa748.

See discussion in json-parser#84 and json-parser#102.
DimitriPapadopoulos added a commit to DimitriPapadopoulos/json-parser that referenced this pull request Aug 18, 2021
Either it has been defined explictly, or we attempt to calculate it
using the method reverted by fcfa748.

See discussion in json-parser#84 and json-parser#102.
DimitriPapadopoulos added a commit to DimitriPapadopoulos/json-parser that referenced this pull request Aug 19, 2021
Either it has been defined explictly, or we attempt to calculate it
using the method reverted by fcfa748.

See discussion in json-parser#84 and json-parser#102.
DimitriPapadopoulos added a commit to DimitriPapadopoulos/json-parser that referenced this pull request Aug 29, 2021
Either it has been defined explictly, or we attempt to calculate it
using the method reverted by fcfa748.

See discussion in json-parser#84 and json-parser#102.
DimitriPapadopoulos added a commit to DimitriPapadopoulos/json-parser that referenced this pull request Aug 29, 2021
Either it has been defined explictly, or we attempt to calculate it
using the method reverted by fcfa748.

See discussion in json-parser#84 and json-parser#102.
DimitriPapadopoulos added a commit to DimitriPapadopoulos/json-parser that referenced this pull request Oct 26, 2021
Either it has been defined explictly, or we attempt to calculate it
using the method reverted by fcfa748.

See discussion in json-parser#84 and json-parser#102.
@LB-- LB-- self-assigned this Oct 30, 2021
LB-- added a commit to LB--/json-parser that referenced this pull request Oct 31, 2021
Fixes json-parser#84
Closes json-parser#102
Probably affects json-parser#115

In theory this should be guaranteed to work regardless of the underlying representation: https://stackoverflow.com/a/2711560/1959975

Demos for C++17 and C89:
https://gcc.godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1DIApACYAQuYukl9ZATwDKjdAGFUtAK4sGe1wAyeAyYAHI%2BAEaYxBIAHKQADqgKhE4MHt6%2BekkpjgJBIeEsUTFc8XaYDmlCBEzEBBk%2Bfly2mPZ5DDV1BAVhkdFxtrX1jVktCsM9wX3FA2UAlLaoXsTI7BzmAMxYNCEA1ABq2ABKAJIAYgCaAPpuAIIBbhDaCgLXwQTXBKR7DPN7LzeHy%2Bvy2W2uAFk7gANCD/EwAdgse2ImAIKwYe2erwY70EX3mUC8DBSwBC6ABOLxnwI8wAtFw9gB6LHE0nkylA/G0szzEybZGIgAiJg0d1F4rMmzwVB2e2u12QCW8CmVXgUEq2wWQ3iwe35bloeBYhA1m2wmqlO2mhxOF0u2K5NJ%2Bf31AFY3BK9t7ObjgQRQVLwSwmKo4fqkSi0Ri9hN0CAQAwfNE8MhrkaTQQzW5AX7ufzsAmQ2G%2BQKI0L3Z6xT7bWcrrcHk9c9Svi6%2BWKtm0lJbtphdpha/bB/X7o8e64ZZqxUc6w7kAg6j953VS1Zp3arhA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.1.1 milestone Oct 31, 2021
@LB--
Copy link
Member

LB-- commented Oct 31, 2021

Thank you for this, however after further research and consideration I believe #115 offers a better solution for this, so I'm closing this for now.

@LB-- LB-- closed this Oct 31, 2021
DimitriPapadopoulos added a commit to DimitriPapadopoulos/json-parser that referenced this pull request Oct 31, 2021
Either it has been defined explictly, or we attempt to calculate it
using the method reverted by fcfa748.

See discussion in json-parser#84 and json-parser#102.
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.

3 participants