diff options
author | Mark Pulford <mark@kyne.com.au> | 2011-12-31 17:43:57 +1030 |
---|---|---|
committer | Mark Pulford <mark@kyne.com.au> | 2011-12-31 17:43:57 +1030 |
commit | c203c255d8ac1375c066bc2cd2eb2fe9b0c35ed9 (patch) | |
tree | 6757cc9f043b224590beef44c731f394a1947feb | |
parent | 5dd3207e5fda2f8f502a8a2f32f5484ea3f758b5 (diff) | |
download | lua-cjson-c203c255d8ac1375c066bc2cd2eb2fe9b0c35ed9.tar.gz lua-cjson-c203c255d8ac1375c066bc2cd2eb2fe9b0c35ed9.tar.bz2 lua-cjson-c203c255d8ac1375c066bc2cd2eb2fe9b0c35ed9.zip |
Minor code cleanup
- Sanitised arg ordering to encode functions
- Commented json->tmp to explain why decode string processing uses
strbuf_*_unsafe() functions.
-rw-r--r-- | lua_cjson.c | 22 |
1 files changed, 12 insertions, 10 deletions
diff --git a/lua_cjson.c b/lua_cjson.c index e458fc3..fe6d41a 100644 --- a/lua_cjson.c +++ b/lua_cjson.c | |||
@@ -519,7 +519,7 @@ static int lua_array_length(lua_State *l, json_config_t *cfg, strbuf_t *json) | |||
519 | } | 519 | } |
520 | 520 | ||
521 | static void json_check_encode_depth(lua_State *l, json_config_t *cfg, | 521 | static void json_check_encode_depth(lua_State *l, json_config_t *cfg, |
522 | strbuf_t *json, int current_depth) | 522 | int current_depth, strbuf_t *json) |
523 | { | 523 | { |
524 | if (current_depth > cfg->encode_max_depth) { | 524 | if (current_depth > cfg->encode_max_depth) { |
525 | if (!cfg->encode_keep_buffer) | 525 | if (!cfg->encode_keep_buffer) |
@@ -558,14 +558,14 @@ static void json_append_array(lua_State *l, json_config_t *cfg, int current_dept | |||
558 | strbuf_append_char(json, ']'); | 558 | strbuf_append_char(json, ']'); |
559 | } | 559 | } |
560 | 560 | ||
561 | static void json_append_number(lua_State *l, strbuf_t *json, int index, | 561 | static void json_append_number(lua_State *l, json_config_t *cfg, |
562 | json_config_t *cfg) | 562 | strbuf_t *json, int lindex) |
563 | { | 563 | { |
564 | double num = lua_tonumber(l, index); | 564 | double num = lua_tonumber(l, lindex); |
565 | int len; | 565 | int len; |
566 | 566 | ||
567 | if (cfg->encode_refuse_badnum && (isinf(num) || isnan(num))) | 567 | if (cfg->encode_refuse_badnum && (isinf(num) || isnan(num))) |
568 | json_encode_exception(l, cfg, json, index, "must not be NaN or Inf"); | 568 | json_encode_exception(l, cfg, json, lindex, "must not be NaN or Inf"); |
569 | 569 | ||
570 | if (isnan(num)) { | 570 | if (isnan(num)) { |
571 | /* Some platforms may print -nan, just hard code it */ | 571 | /* Some platforms may print -nan, just hard code it */ |
@@ -600,7 +600,7 @@ static void json_append_object(lua_State *l, json_config_t *cfg, | |||
600 | keytype = lua_type(l, -2); | 600 | keytype = lua_type(l, -2); |
601 | if (keytype == LUA_TNUMBER) { | 601 | if (keytype == LUA_TNUMBER) { |
602 | strbuf_append_char(json, '"'); | 602 | strbuf_append_char(json, '"'); |
603 | json_append_number(l, json, -2, cfg); | 603 | json_append_number(l, cfg, json, -2); |
604 | strbuf_append_mem(json, "\":", 2); | 604 | strbuf_append_mem(json, "\":", 2); |
605 | } else if (keytype == LUA_TSTRING) { | 605 | } else if (keytype == LUA_TSTRING) { |
606 | json_append_string(l, json, -2); | 606 | json_append_string(l, json, -2); |
@@ -631,7 +631,7 @@ static void json_append_data(lua_State *l, json_config_t *cfg, | |||
631 | json_append_string(l, json, -1); | 631 | json_append_string(l, json, -1); |
632 | break; | 632 | break; |
633 | case LUA_TNUMBER: | 633 | case LUA_TNUMBER: |
634 | json_append_number(l, json, -1, cfg); | 634 | json_append_number(l, cfg, json, -1); |
635 | break; | 635 | break; |
636 | case LUA_TBOOLEAN: | 636 | case LUA_TBOOLEAN: |
637 | if (lua_toboolean(l, -1)) | 637 | if (lua_toboolean(l, -1)) |
@@ -642,7 +642,7 @@ static void json_append_data(lua_State *l, json_config_t *cfg, | |||
642 | case LUA_TTABLE: | 642 | case LUA_TTABLE: |
643 | len = lua_array_length(l, cfg, json); | 643 | len = lua_array_length(l, cfg, json); |
644 | current_depth++; | 644 | current_depth++; |
645 | json_check_encode_depth(l, cfg, json, current_depth); | 645 | json_check_encode_depth(l, cfg, current_depth, json); |
646 | if (len > 0) | 646 | if (len > 0) |
647 | json_append_array(l, cfg, current_depth, json, len); | 647 | json_append_array(l, cfg, current_depth, json, len); |
648 | else | 648 | else |
@@ -862,8 +862,11 @@ static void json_next_string_token(json_parse_t *json, json_token_t *token) | |||
862 | json->index++; | 862 | json->index++; |
863 | 863 | ||
864 | /* json->tmp is the temporary strbuf used to accumulate the | 864 | /* json->tmp is the temporary strbuf used to accumulate the |
865 | * decoded string value. */ | 865 | * decoded string value. |
866 | * json->tmp is sized to handle JSON containing only a string value. | ||
867 | */ | ||
866 | strbuf_reset(json->tmp); | 868 | strbuf_reset(json->tmp); |
869 | |||
867 | while ((ch = json->data[json->index]) != '"') { | 870 | while ((ch = json->data[json->index]) != '"') { |
868 | if (!ch) { | 871 | if (!ch) { |
869 | /* Premature end of the string */ | 872 | /* Premature end of the string */ |
@@ -947,7 +950,6 @@ static int json_is_invalid_number(json_parse_t *json) | |||
947 | return 0; /* Ordinary number */ | 950 | return 0; /* Ordinary number */ |
948 | } | 951 | } |
949 | 952 | ||
950 | |||
951 | /* Reject inf/nan */ | 953 | /* Reject inf/nan */ |
952 | if (!strncasecmp(&json->data[i], "inf", 3)) | 954 | if (!strncasecmp(&json->data[i], "inf", 3)) |
953 | return 1; | 955 | return 1; |