diff options
Diffstat (limited to 'src/3rdParty/colib/ljson.c')
| -rw-r--r-- | src/3rdParty/colib/ljson.c | 36 |
1 files changed, 18 insertions, 18 deletions
diff --git a/src/3rdParty/colib/ljson.c b/src/3rdParty/colib/ljson.c index e6fabeb..4daba07 100644 --- a/src/3rdParty/colib/ljson.c +++ b/src/3rdParty/colib/ljson.c | |||
| @@ -200,11 +200,11 @@ typedef struct { | |||
| 200 | int curdepth; // 当前层次 | 200 | int curdepth; // 当前层次 |
| 201 | int maxdepth; // 最大层次 | 201 | int maxdepth; // 最大层次 |
| 202 | int allowcomment; // 是否允许注释 | 202 | int allowcomment; // 是否允许注释 |
| 203 | char errmsg[ERRMSG_SIZE]; // 保存错误消息 | 203 | char errmsg[ERRMSG_SIZE]; // 保存错误消息 |
| 204 | //>>>jmp_buf jb; // 用于实现从解析中出错直接跳出 | 204 | //>>>jmp_buf jb; // 用于实现从解析中出错直接跳出 |
| 205 | } json_parser_t; | 205 | } json_parser_t; |
| 206 | 206 | ||
| 207 | static inline void parser_init(json_parser_t *parser, const char *str, size_t size, void *ud, | 207 | static inline void parser_init(json_parser_t *parser, const char *str, size_t size, void *ud, |
| 208 | int maxdepth, int allowcomment) { | 208 | int maxdepth, int allowcomment) { |
| 209 | membuffer_init(&parser->buff); | 209 | membuffer_init(&parser->buff); |
| 210 | membuffer_ensure_space(&parser->buff, size); | 210 | membuffer_ensure_space(&parser->buff, size); |
| @@ -254,7 +254,7 @@ static const char* parser_error_content(json_parser_t *p) { | |||
| 254 | static inline void parser_add_depth(json_parser_t *p) { | 254 | static inline void parser_add_depth(json_parser_t *p) { |
| 255 | p->curdepth++; | 255 | p->curdepth++; |
| 256 | if (p->curdepth >= p->maxdepth) | 256 | if (p->curdepth >= p->maxdepth) |
| 257 | parser_throw_error(p, "Too many nested data, max depth is %d, at: %s[:%lu]", p->maxdepth, | 257 | parser_throw_error(p, "Too many nested data, max depth is %d, at: %s[:%lu]", p->maxdepth, |
| 258 | parser_error_content(p), currpos(p)); | 258 | parser_error_content(p), currpos(p)); |
| 259 | } | 259 | } |
| 260 | 260 | ||
| @@ -402,7 +402,7 @@ static inline void parser_process_string(json_parser_t *p) { | |||
| 402 | } else { | 402 | } else { |
| 403 | parser_throw_error(p, "Invalid escape sequence, at: %s[:%lu]", parser_error_content(p), currpos(p)); | 403 | parser_throw_error(p, "Invalid escape sequence, at: %s[:%lu]", parser_error_content(p), currpos(p)); |
| 404 | } | 404 | } |
| 405 | } else if (ch == '"') { | 405 | } else if (ch == '"') { |
| 406 | break; | 406 | break; |
| 407 | } else if ((unsigned char)ch < 0x20) { | 407 | } else if ((unsigned char)ch < 0x20) { |
| 408 | parser_throw_error(p, "Invalid string, at: %s[:%lu]", parser_error_content(p), currpos(p)); | 408 | parser_throw_error(p, "Invalid string, at: %s[:%lu]", parser_error_content(p), currpos(p)); |
| @@ -490,7 +490,7 @@ static inline void parser_process_number(json_parser_t *p, char ch) { | |||
| 490 | 490 | ||
| 491 | if (isdouble) { | 491 | if (isdouble) { |
| 492 | int n = exponent < 0 ? -exponent : exponent; | 492 | int n = exponent < 0 ? -exponent : exponent; |
| 493 | if (unlikely(n>511)) | 493 | if (unlikely(n>511)) |
| 494 | n = 511; // inf | 494 | n = 511; // inf |
| 495 | double p10 = 1.0; | 495 | double p10 = 1.0; |
| 496 | double *d; | 496 | double *d; |
| @@ -576,13 +576,13 @@ static void parser_process_value(json_parser_t *p) { | |||
| 576 | parser_skip_whitespaces(p); | 576 | parser_skip_whitespaces(p); |
| 577 | char ch = get_and_next(p); | 577 | char ch = get_and_next(p); |
| 578 | switch (ch) { | 578 | switch (ch) { |
| 579 | case 'f': | 579 | case 'f': |
| 580 | parser_process_false(p); | 580 | parser_process_false(p); |
| 581 | break; | 581 | break; |
| 582 | case 't': | 582 | case 't': |
| 583 | parser_process_true(p); | 583 | parser_process_true(p); |
| 584 | break; | 584 | break; |
| 585 | case 'n': | 585 | case 'n': |
| 586 | parser_process_null(p); | 586 | parser_process_null(p); |
| 587 | break; | 587 | break; |
| 588 | case '"': | 588 | case '"': |
| @@ -609,7 +609,7 @@ static void parser_do_parse(const char *str, size_t size, void *ud, int maxdepth | |||
| 609 | parser_process_value(&p); | 609 | parser_process_value(&p); |
| 610 | parser_skip_whitespaces(&p); | 610 | parser_skip_whitespaces(&p); |
| 611 | if (peekchar(&p) != '\0') { | 611 | if (peekchar(&p) != '\0') { |
| 612 | parser_throw_error(&p, "Expect '<eof>' but got '%c', at: %s[:%lu]", peekchar(&p), | 612 | parser_throw_error(&p, "Expect '<eof>' but got '%c', at: %s[:%lu]", peekchar(&p), |
| 613 | parser_error_content(&p), currpos(&p)); | 613 | parser_error_content(&p), currpos(&p)); |
| 614 | } | 614 | } |
| 615 | parser_free(&p); | 615 | parser_free(&p); |
| @@ -625,7 +625,7 @@ typedef struct { | |||
| 625 | int format; // 是否格式化 | 625 | int format; // 是否格式化 |
| 626 | int empty_as_array; // 空表是否当成数组 | 626 | int empty_as_array; // 空表是否当成数组 |
| 627 | int num_as_str; // 数字Key转为字符串 | 627 | int num_as_str; // 数字Key转为字符串 |
| 628 | char errmsg[ERRMSG_SIZE]; // 保存错误消息 | 628 | char errmsg[ERRMSG_SIZE]; // 保存错误消息 |
| 629 | } json_dumpper_t; | 629 | } json_dumpper_t; |
| 630 | 630 | ||
| 631 | // 足够转换数字的缓存大小 | 631 | // 足够转换数字的缓存大小 |
| @@ -699,7 +699,7 @@ static void dumpper_process_string(json_dumpper_t *d, lua_State *L, int idx) { | |||
| 699 | for (i = 0; i < len; ++i) { | 699 | for (i = 0; i < len; ++i) { |
| 700 | ch = (unsigned char)str[i]; | 700 | ch = (unsigned char)str[i]; |
| 701 | esc = char2escape[ch]; | 701 | esc = char2escape[ch]; |
| 702 | if (likely(!esc)) | 702 | if (likely(!esc)) |
| 703 | membuffer_putc_unsafe(buff, (char)ch); | 703 | membuffer_putc_unsafe(buff, (char)ch); |
| 704 | else { | 704 | else { |
| 705 | membuffer_putc_unsafe(buff, '\\'); | 705 | membuffer_putc_unsafe(buff, '\\'); |
| @@ -781,7 +781,7 @@ static void dumpper_process_object(json_dumpper_t *d, lua_State *L, int depth) { | |||
| 781 | } else { | 781 | } else { |
| 782 | comma = 1; | 782 | comma = 1; |
| 783 | if (unlikely(d->format)) membuffer_putc(buff, '\n'); | 783 | if (unlikely(d->format)) membuffer_putc(buff, '\n'); |
| 784 | } | 784 | } |
| 785 | // key | 785 | // key |
| 786 | ktp = lua_type(L, -2); | 786 | ktp = lua_type(L, -2); |
| 787 | if (ktp == LUA_TSTRING) { | 787 | if (ktp == LUA_TSTRING) { |
| @@ -814,7 +814,7 @@ static void dumpper_process_object(json_dumpper_t *d, lua_State *L, int depth) { | |||
| 814 | if (unlikely(d->format && comma)) { | 814 | if (unlikely(d->format && comma)) { |
| 815 | membuffer_putc(buff, '\n'); | 815 | membuffer_putc(buff, '\n'); |
| 816 | dumpper_add_indent(d, depth-1); | 816 | dumpper_add_indent(d, depth-1); |
| 817 | } | 817 | } |
| 818 | membuffer_putc(buff, '}'); | 818 | membuffer_putc(buff, '}'); |
| 819 | } | 819 | } |
| 820 | 820 | ||
| @@ -873,9 +873,9 @@ static void dumpper_process_value(json_dumpper_t *d, lua_State *L, int depth) { | |||
| 873 | // 接口 | 873 | // 接口 |
| 874 | #define DEF_MAX_DEPTH 128 | 874 | #define DEF_MAX_DEPTH 128 |
| 875 | 875 | ||
| 876 | // 从字符串加载:json.load(str, maxdepth) -> obj | 876 | // 从字符串加载:json.decode(str, maxdepth) -> obj |
| 877 | // 要求字符串必须以0结尾 | 877 | // 要求字符串必须以0结尾 |
| 878 | int colibc_json_load(lua_State *L) { | 878 | int colibc_json_decode(lua_State *L) { |
| 879 | size_t size; | 879 | size_t size; |
| 880 | const char *str = luaL_checklstring(L, 1, &size); | 880 | const char *str = luaL_checklstring(L, 1, &size); |
| 881 | int maxdepth = (int)luaL_optinteger(L, 2, DEF_MAX_DEPTH); | 881 | int maxdepth = (int)luaL_optinteger(L, 2, DEF_MAX_DEPTH); |
| @@ -884,8 +884,8 @@ int colibc_json_load(lua_State *L) { | |||
| 884 | return 1; | 884 | return 1; |
| 885 | } | 885 | } |
| 886 | 886 | ||
| 887 | // 保存到字符串: json.dump(obj) -> str | 887 | // 保存到字符串: json.encode(obj) -> str |
| 888 | int colibc_json_dump(lua_State *L) { | 888 | int colibc_json_encode(lua_State *L) { |
| 889 | luaL_checkany(L, 1); | 889 | luaL_checkany(L, 1); |
| 890 | json_dumpper_t dumpper; | 890 | json_dumpper_t dumpper; |
| 891 | membuffer_init(&dumpper.buff); | 891 | membuffer_init(&dumpper.buff); |
| @@ -902,8 +902,8 @@ int colibc_json_dump(lua_State *L) { | |||
| 902 | } | 902 | } |
| 903 | 903 | ||
| 904 | static const luaL_Reg lib[] = { | 904 | static const luaL_Reg lib[] = { |
| 905 | {"load", colibc_json_load}, | 905 | {"decode", colibc_json_decode}, |
| 906 | {"dump", colibc_json_dump}, | 906 | {"encode", colibc_json_encode}, |
| 907 | {NULL, NULL}, | 907 | {NULL, NULL}, |
| 908 | }; | 908 | }; |
| 909 | 909 | ||
