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

get rid of redundant length calculation on number generation #207

Open
wants to merge 1 commit into
base: master
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
18 changes: 13 additions & 5 deletions src/yajl_gen.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,13 @@ yajl_gen_status
yajl_gen_integer(yajl_gen g, long long int number)
{
char i[32];
int len;
ENSURE_VALID_STATE; ENSURE_NOT_KEY; INSERT_SEP; INSERT_WHITESPACE;
sprintf(i, "%lld", number);
g->print(g->ctx, i, (unsigned int)strlen(i));
len = sprintf(i, "%lld", number);
if (len < 0) {
return yajl_gen_invalid_number;
}
g->print(g->ctx, i, (unsigned int)len);
APPENDED_ATOM;
FINAL_NEWLINE;
return yajl_gen_status_ok;
Expand All @@ -227,14 +231,18 @@ yajl_gen_status
yajl_gen_double(yajl_gen g, double number)
{
char i[32];
int len;
ENSURE_VALID_STATE; ENSURE_NOT_KEY;
if (isnan(number) || isinf(number)) return yajl_gen_invalid_number;
INSERT_SEP; INSERT_WHITESPACE;
sprintf(i, "%.20g", number);
if (strspn(i, "0123456789-") == strlen(i)) {
len = sprintf(i, "%.20g", number);
if (len < 0) {
return yajl_gen_invalid_number;
}
if (strspn(i, "0123456789-") == (unsigned int)len) {
strcat(i, ".0");
}
g->print(g->ctx, i, (unsigned int)strlen(i));
g->print(g->ctx, i, (unsigned int)len);
APPENDED_ATOM;
FINAL_NEWLINE;
return yajl_gen_status_ok;
Expand Down