You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by da...@apache.org on 2012/02/22 20:40:46 UTC
git commit: Make sure doubles parse as doubles
Updated Branches:
refs/heads/master 1e6a1b526 -> edbbe4319
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/edbbe431
Tree: http://git-wip-us.apache.org/repos/asf/couchdb/tree/edbbe431
Diff: http://git-wip-us.apache.org/repos/asf/couchdb/diff/edbbe431
Branch: refs/heads/master
Commit: edbbe43193b129f2a215aa3447daef7b93fafec0
Parents: 1e6a1b5
Author: Paul Joseph Davis <da...@apache.org>
Authored: Wed Feb 22 13:39:23 2012 -0600
Committer: Paul Joseph Davis <da...@apache.org>
Committed: Wed Feb 22 13:40:30 2012 -0600
----------------------------------------------------------------------
src/ejson/encode.c | 23 ++++++++++++++++++++---
1 files changed, 20 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/couchdb/blob/edbbe431/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