Updated Branches: refs/heads/1.2.x 4c7799722 -> ba271a70b
Make sure doubles parse as doubles Simple check that doubles are serialized in such a way that they'll by parsed as doubles by most implementations. Project: http://git-wip-us.apache.org/repos/asf/couchdb/repo Commit: http://git-wip-us.apache.org/repos/asf/couchdb/commit/ba271a70 Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/ba271a70 Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/ba271a70 Branch: refs/heads/1.2.x Commit: ba271a70b83c6df16af43204c2ba9f4d5ca89711 Parents: 4c77997 Author: Paul Joseph Davis <[email protected]> Authored: Wed Feb 22 13:39:23 2012 -0600 Committer: Paul Joseph Davis <[email protected]> Committed: Wed Feb 22 13:39:23 2012 -0600 ---------------------------------------------------------------------- src/ejson/encode.c | 23 ++++++++++++++++++++--- 1 files changed, 20 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/couchdb/blob/ba271a70/src/ejson/encode.c ---------------------------------------------------------------------- diff --git a/src/ejson/encode.c b/src/ejson/encode.c index 916a0b7..1dbd1df 100644 --- a/src/ejson/encode.c +++ b/src/ejson/encode.c @@ -97,6 +97,9 @@ final_encode(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) ERL_NIF_TERM term; double number; encode_ctx ctx; + char* start; + size_t len; + size_t i; ctx.env = env; ctx.fill_offset = 0; @@ -145,10 +148,24 @@ final_encode(ErlNifEnv* env, int argc, const ERL_NIF_TERM argv[]) goto done; } // write the string into the buffer - snprintf((char*)ctx.bin.data+ctx.fill_offset, 32, - "%.16g", number); + start = (char*) (ctx.bin.data + ctx.fill_offset); + snprintf(start, 32, "%0.20g", number); + len = strlen(start); + for(i = 0; i < len; i++) { + if(start[i] == '.' || start[i] == 'e' || start[i] == 'E') { + break; + } + } + if(i == len) { + if(i > 29) { + ctx.error = BADARG; + goto done; + } + start[len++] = '.'; + start[len++] = '0'; + } // increment the length - ctx.fill_offset += strlen((char*)ctx.bin.data+ctx.fill_offset); + ctx.fill_offset += len; } } else if (enif_inspect_binary(env, term, &termbin)) { // this is a regular binary, copy the contents into the buffer
