Mercurial > vim
comparison src/json.c @ 18800:f41b55f9357c v8.1.2388
patch 8.1.2388: using old C style comments
Commit: https://github.com/vim/vim/commit/4ba37b5833de99db9e9afe8928b31c864182405c
Author: Bram Moolenaar <Bram@vim.org>
Date: Wed Dec 4 21:57:43 2019 +0100
patch 8.1.2388: using old C style comments
Problem: Using old C style comments.
Solution: Use // comments where appropriate.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Wed, 04 Dec 2019 22:00:04 +0100 |
parents | 0f7ae8010787 |
children | ba9f50bfda83 |
comparison
equal
deleted
inserted
replaced
18799:d7d0942e231b | 18800:f41b55f9357c |
---|---|
46 char_u * | 46 char_u * |
47 json_encode(typval_T *val, int options) | 47 json_encode(typval_T *val, int options) |
48 { | 48 { |
49 garray_T ga; | 49 garray_T ga; |
50 | 50 |
51 /* Store bytes in the growarray. */ | 51 // Store bytes in the growarray. |
52 ga_init2(&ga, 1, 4000); | 52 ga_init2(&ga, 1, 4000); |
53 json_encode_gap(&ga, val, options); | 53 json_encode_gap(&ga, val, options); |
54 ga_append(&ga, NUL); | 54 ga_append(&ga, NUL); |
55 return ga.ga_data; | 55 return ga.ga_data; |
56 } | 56 } |
102 vimconv_T conv; | 102 vimconv_T conv; |
103 char_u *converted = NULL; | 103 char_u *converted = NULL; |
104 | 104 |
105 if (!enc_utf8) | 105 if (!enc_utf8) |
106 { | 106 { |
107 /* Convert the text from 'encoding' to utf-8, the JSON string is | 107 // Convert the text from 'encoding' to utf-8, the JSON string is |
108 * always utf-8. */ | 108 // always utf-8. |
109 conv.vc_type = CONV_NONE; | 109 conv.vc_type = CONV_NONE; |
110 convert_setup(&conv, p_enc, (char_u*)"utf-8"); | 110 convert_setup(&conv, p_enc, (char_u*)"utf-8"); |
111 if (conv.vc_type != CONV_NONE) | 111 if (conv.vc_type != CONV_NONE) |
112 converted = res = string_convert(&conv, res, NULL); | 112 converted = res = string_convert(&conv, res, NULL); |
113 convert_setup(&conv, NULL, NULL); | 113 convert_setup(&conv, NULL, NULL); |
115 #endif | 115 #endif |
116 ga_append(gap, '"'); | 116 ga_append(gap, '"'); |
117 while (*res != NUL) | 117 while (*res != NUL) |
118 { | 118 { |
119 int c; | 119 int c; |
120 /* always use utf-8 encoding, ignore 'encoding' */ | 120 // always use utf-8 encoding, ignore 'encoding' |
121 c = utf_ptr2char(res); | 121 c = utf_ptr2char(res); |
122 | 122 |
123 switch (c) | 123 switch (c) |
124 { | 124 { |
125 case 0x08: | 125 case 0x08: |
130 ga_append(gap, '\\'); ga_append(gap, 'n'); break; | 130 ga_append(gap, '\\'); ga_append(gap, 'n'); break; |
131 case 0x0c: | 131 case 0x0c: |
132 ga_append(gap, '\\'); ga_append(gap, 'f'); break; | 132 ga_append(gap, '\\'); ga_append(gap, 'f'); break; |
133 case 0x0d: | 133 case 0x0d: |
134 ga_append(gap, '\\'); ga_append(gap, 'r'); break; | 134 ga_append(gap, '\\'); ga_append(gap, 'r'); break; |
135 case 0x22: /* " */ | 135 case 0x22: // " |
136 case 0x5c: /* \ */ | 136 case 0x5c: // backslash |
137 ga_append(gap, '\\'); | 137 ga_append(gap, '\\'); |
138 ga_append(gap, c); | 138 ga_append(gap, c); |
139 break; | 139 break; |
140 default: | 140 default: |
141 if (c >= 0x20) | 141 if (c >= 0x20) |
198 { | 198 { |
199 case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break; | 199 case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break; |
200 case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break; | 200 case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break; |
201 case VVAL_NONE: if ((options & JSON_JS) != 0 | 201 case VVAL_NONE: if ((options & JSON_JS) != 0 |
202 && (options & JSON_NO_NONE) == 0) | 202 && (options & JSON_NO_NONE) == 0) |
203 /* empty item */ | 203 // empty item |
204 break; | 204 break; |
205 /* FALLTHROUGH */ | 205 // FALLTHROUGH |
206 case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break; | 206 case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break; |
207 } | 207 } |
208 break; | 208 break; |
209 | 209 |
210 case VAR_NUMBER: | 210 case VAR_NUMBER: |
220 | 220 |
221 case VAR_FUNC: | 221 case VAR_FUNC: |
222 case VAR_PARTIAL: | 222 case VAR_PARTIAL: |
223 case VAR_JOB: | 223 case VAR_JOB: |
224 case VAR_CHANNEL: | 224 case VAR_CHANNEL: |
225 /* no JSON equivalent TODO: better error */ | 225 // no JSON equivalent TODO: better error |
226 emsg(_(e_invarg)); | 226 emsg(_(e_invarg)); |
227 return FAIL; | 227 return FAIL; |
228 | 228 |
229 case VAR_BLOB: | 229 case VAR_BLOB: |
230 b = val->vval.v_blob; | 230 b = val->vval.v_blob; |
266 return FAIL; | 266 return FAIL; |
267 if ((options & JSON_JS) | 267 if ((options & JSON_JS) |
268 && li->li_next == NULL | 268 && li->li_next == NULL |
269 && li->li_tv.v_type == VAR_SPECIAL | 269 && li->li_tv.v_type == VAR_SPECIAL |
270 && li->li_tv.vval.v_number == VVAL_NONE) | 270 && li->li_tv.vval.v_number == VVAL_NONE) |
271 /* add an extra comma if the last item is v:none */ | 271 // add an extra comma if the last item is v:none |
272 ga_append(gap, ','); | 272 ga_append(gap, ','); |
273 li = li->li_next; | 273 li = li->li_next; |
274 if (li != NULL) | 274 if (li != NULL) |
275 ga_append(gap, ','); | 275 ga_append(gap, ','); |
276 } | 276 } |
403 varnumber_T nr; | 403 varnumber_T nr; |
404 | 404 |
405 if (res != NULL) | 405 if (res != NULL) |
406 ga_init2(&ga, 1, 200); | 406 ga_init2(&ga, 1, 200); |
407 | 407 |
408 p = reader->js_buf + reader->js_used + 1; /* skip over " or ' */ | 408 p = reader->js_buf + reader->js_used + 1; // skip over " or ' |
409 while (*p != quote) | 409 while (*p != quote) |
410 { | 410 { |
411 /* The JSON is always expected to be utf-8, thus use utf functions | 411 // The JSON is always expected to be utf-8, thus use utf functions |
412 * here. The string is converted below if needed. */ | 412 // here. The string is converted below if needed. |
413 if (*p == NUL || p[1] == NUL || utf_ptr2len(p) < utf_byte2len(*p)) | 413 if (*p == NUL || p[1] == NUL || utf_ptr2len(p) < utf_byte2len(*p)) |
414 { | 414 { |
415 /* Not enough bytes to make a character or end of the string. Get | 415 // Not enough bytes to make a character or end of the string. Get |
416 * more if possible. */ | 416 // more if possible. |
417 if (reader->js_fill == NULL) | 417 if (reader->js_fill == NULL) |
418 break; | 418 break; |
419 len = (int)(reader->js_end - p); | 419 len = (int)(reader->js_end - p); |
420 reader->js_used = (int)(p - reader->js_buf); | 420 reader->js_used = (int)(p - reader->js_buf); |
421 if (!reader->js_fill(reader)) | 421 if (!reader->js_fill(reader)) |
422 break; /* didn't get more */ | 422 break; // didn't get more |
423 p = reader->js_buf + reader->js_used; | 423 p = reader->js_buf + reader->js_used; |
424 reader->js_end = reader->js_buf + STRLEN(reader->js_buf); | 424 reader->js_end = reader->js_buf + STRLEN(reader->js_buf); |
425 continue; | 425 continue; |
426 } | 426 } |
427 | 427 |
464 && (int)(reader->js_end - p) >= 6 | 464 && (int)(reader->js_end - p) >= 6 |
465 && *p == '\\' && *(p+1) == 'u') | 465 && *p == '\\' && *(p+1) == 'u') |
466 { | 466 { |
467 varnumber_T nr2 = 0; | 467 varnumber_T nr2 = 0; |
468 | 468 |
469 /* decode surrogate pair: \ud812\u3456 */ | 469 // decode surrogate pair: \ud812\u3456 |
470 len = 0; | 470 len = 0; |
471 vim_str2nr(p + 2, NULL, &len, | 471 vim_str2nr(p + 2, NULL, &len, |
472 STR2NR_HEX + STR2NR_FORCE, &nr2, NULL, 4, TRUE); | 472 STR2NR_HEX + STR2NR_FORCE, &nr2, NULL, 4, TRUE); |
473 if (len == 0) | 473 if (len == 0) |
474 { | 474 { |
490 buf[utf_char2bytes((int)nr, buf)] = NUL; | 490 buf[utf_char2bytes((int)nr, buf)] = NUL; |
491 ga_concat(&ga, buf); | 491 ga_concat(&ga, buf); |
492 } | 492 } |
493 break; | 493 break; |
494 default: | 494 default: |
495 /* not a special char, skip over \ */ | 495 // not a special char, skip over backslash |
496 ++p; | 496 ++p; |
497 continue; | 497 continue; |
498 } | 498 } |
499 if (c > 0) | 499 if (c > 0) |
500 { | 500 { |
531 #if defined(USE_ICONV) | 531 #if defined(USE_ICONV) |
532 if (!enc_utf8) | 532 if (!enc_utf8) |
533 { | 533 { |
534 vimconv_T conv; | 534 vimconv_T conv; |
535 | 535 |
536 /* Convert the utf-8 string to 'encoding'. */ | 536 // Convert the utf-8 string to 'encoding'. |
537 conv.vc_type = CONV_NONE; | 537 conv.vc_type = CONV_NONE; |
538 convert_setup(&conv, (char_u*)"utf-8", p_enc); | 538 convert_setup(&conv, (char_u*)"utf-8", p_enc); |
539 if (conv.vc_type != CONV_NONE) | 539 if (conv.vc_type != CONV_NONE) |
540 { | 540 { |
541 res->vval.v_string = | 541 res->vval.v_string = |
558 } | 558 } |
559 return MAYBE; | 559 return MAYBE; |
560 } | 560 } |
561 | 561 |
562 typedef enum { | 562 typedef enum { |
563 JSON_ARRAY, /* parsing items in an array */ | 563 JSON_ARRAY, // parsing items in an array |
564 JSON_OBJECT_KEY, /* parsing key of an object */ | 564 JSON_OBJECT_KEY, // parsing key of an object |
565 JSON_OBJECT /* parsing item in an object, after the key */ | 565 JSON_OBJECT // parsing item in an object, after the key |
566 } json_decode_T; | 566 } json_decode_T; |
567 | 567 |
568 typedef struct { | 568 typedef struct { |
569 json_decode_T jd_type; | 569 json_decode_T jd_type; |
570 typval_T jd_tv; /* the list or dict */ | 570 typval_T jd_tv; // the list or dict |
571 typval_T jd_key_tv; | 571 typval_T jd_key_tv; |
572 char_u *jd_key; | 572 char_u *jd_key; |
573 } json_dec_item_T; | 573 } json_dec_item_T; |
574 | 574 |
575 /* | 575 /* |
609 p = reader->js_buf + reader->js_used; | 609 p = reader->js_buf + reader->js_used; |
610 if (*p == NUL) | 610 if (*p == NUL) |
611 { | 611 { |
612 retval = MAYBE; | 612 retval = MAYBE; |
613 if (top_item->jd_type == JSON_OBJECT) | 613 if (top_item->jd_type == JSON_OBJECT) |
614 /* did get the key, clear it */ | 614 // did get the key, clear it |
615 clear_tv(&top_item->jd_key_tv); | 615 clear_tv(&top_item->jd_key_tv); |
616 goto theend; | 616 goto theend; |
617 } | 617 } |
618 if (top_item->jd_type == JSON_OBJECT_KEY | 618 if (top_item->jd_type == JSON_OBJECT_KEY |
619 || top_item->jd_type == JSON_ARRAY) | 619 || top_item->jd_type == JSON_ARRAY) |
620 { | 620 { |
621 /* Check for end of object or array. */ | 621 // Check for end of object or array. |
622 if (*p == (top_item->jd_type == JSON_ARRAY ? ']' : '}')) | 622 if (*p == (top_item->jd_type == JSON_ARRAY ? ']' : '}')) |
623 { | 623 { |
624 ++reader->js_used; /* consume the ']' or '}' */ | 624 ++reader->js_used; // consume the ']' or '}' |
625 --stack.ga_len; | 625 --stack.ga_len; |
626 if (stack.ga_len == 0) | 626 if (stack.ga_len == 0) |
627 { | 627 { |
628 retval = OK; | 628 retval = OK; |
629 goto theend; | 629 goto theend; |
642 && reader->js_buf[reader->js_used] != '[' | 642 && reader->js_buf[reader->js_used] != '[' |
643 && reader->js_buf[reader->js_used] != '{') | 643 && reader->js_buf[reader->js_used] != '{') |
644 { | 644 { |
645 char_u *key; | 645 char_u *key; |
646 | 646 |
647 /* accept an object key that is not in quotes */ | 647 // accept an object key that is not in quotes |
648 key = p = reader->js_buf + reader->js_used; | 648 key = p = reader->js_buf + reader->js_used; |
649 while (*p != NUL && *p != ':' && *p > ' ') | 649 while (*p != NUL && *p != ':' && *p > ' ') |
650 ++p; | 650 ++p; |
651 if (cur_item != NULL) | 651 if (cur_item != NULL) |
652 { | 652 { |
658 } | 658 } |
659 else | 659 else |
660 { | 660 { |
661 switch (*p) | 661 switch (*p) |
662 { | 662 { |
663 case '[': /* start of array */ | 663 case '[': // start of array |
664 if (top_item && top_item->jd_type == JSON_OBJECT_KEY) | 664 if (top_item && top_item->jd_type == JSON_OBJECT_KEY) |
665 { | 665 { |
666 retval = FAIL; | 666 retval = FAIL; |
667 break; | 667 break; |
668 } | 668 } |
677 cur_item->vval.v_number = VVAL_NONE; | 677 cur_item->vval.v_number = VVAL_NONE; |
678 retval = FAIL; | 678 retval = FAIL; |
679 break; | 679 break; |
680 } | 680 } |
681 | 681 |
682 ++reader->js_used; /* consume the '[' */ | 682 ++reader->js_used; // consume the '[' |
683 top_item = ((json_dec_item_T *)stack.ga_data) | 683 top_item = ((json_dec_item_T *)stack.ga_data) |
684 + stack.ga_len; | 684 + stack.ga_len; |
685 top_item->jd_type = JSON_ARRAY; | 685 top_item->jd_type = JSON_ARRAY; |
686 ++stack.ga_len; | 686 ++stack.ga_len; |
687 if (cur_item != NULL) | 687 if (cur_item != NULL) |
689 top_item->jd_tv = *cur_item; | 689 top_item->jd_tv = *cur_item; |
690 cur_item = &item; | 690 cur_item = &item; |
691 } | 691 } |
692 continue; | 692 continue; |
693 | 693 |
694 case '{': /* start of object */ | 694 case '{': // start of object |
695 if (top_item && top_item->jd_type == JSON_OBJECT_KEY) | 695 if (top_item && top_item->jd_type == JSON_OBJECT_KEY) |
696 { | 696 { |
697 retval = FAIL; | 697 retval = FAIL; |
698 break; | 698 break; |
699 } | 699 } |
708 cur_item->vval.v_number = VVAL_NONE; | 708 cur_item->vval.v_number = VVAL_NONE; |
709 retval = FAIL; | 709 retval = FAIL; |
710 break; | 710 break; |
711 } | 711 } |
712 | 712 |
713 ++reader->js_used; /* consume the '{' */ | 713 ++reader->js_used; // consume the '{' |
714 top_item = ((json_dec_item_T *)stack.ga_data) | 714 top_item = ((json_dec_item_T *)stack.ga_data) |
715 + stack.ga_len; | 715 + stack.ga_len; |
716 top_item->jd_type = JSON_OBJECT_KEY; | 716 top_item->jd_type = JSON_OBJECT_KEY; |
717 ++stack.ga_len; | 717 ++stack.ga_len; |
718 if (cur_item != NULL) | 718 if (cur_item != NULL) |
720 top_item->jd_tv = *cur_item; | 720 top_item->jd_tv = *cur_item; |
721 cur_item = &top_item->jd_key_tv; | 721 cur_item = &top_item->jd_key_tv; |
722 } | 722 } |
723 continue; | 723 continue; |
724 | 724 |
725 case '"': /* string */ | 725 case '"': // string |
726 retval = json_decode_string(reader, cur_item, *p); | 726 retval = json_decode_string(reader, cur_item, *p); |
727 break; | 727 break; |
728 | 728 |
729 case '\'': | 729 case '\'': |
730 if (options & JSON_JS) | 730 if (options & JSON_JS) |
734 emsg(_(e_invarg)); | 734 emsg(_(e_invarg)); |
735 retval = FAIL; | 735 retval = FAIL; |
736 } | 736 } |
737 break; | 737 break; |
738 | 738 |
739 case ',': /* comma: empty item */ | 739 case ',': // comma: empty item |
740 if ((options & JSON_JS) == 0) | 740 if ((options & JSON_JS) == 0) |
741 { | 741 { |
742 emsg(_(e_invarg)); | 742 emsg(_(e_invarg)); |
743 retval = FAIL; | 743 retval = FAIL; |
744 break; | 744 break; |
745 } | 745 } |
746 /* FALLTHROUGH */ | 746 // FALLTHROUGH |
747 case NUL: /* empty */ | 747 case NUL: // empty |
748 if (cur_item != NULL) | 748 if (cur_item != NULL) |
749 { | 749 { |
750 cur_item->v_type = VAR_SPECIAL; | 750 cur_item->v_type = VAR_SPECIAL; |
751 cur_item->vval.v_number = VVAL_NONE; | 751 cur_item->vval.v_number = VVAL_NONE; |
752 } | 752 } |
793 #endif | 793 #endif |
794 { | 794 { |
795 varnumber_T nr; | 795 varnumber_T nr; |
796 | 796 |
797 vim_str2nr(reader->js_buf + reader->js_used, | 797 vim_str2nr(reader->js_buf + reader->js_used, |
798 NULL, &len, 0, /* what */ | 798 NULL, &len, 0, // what |
799 &nr, NULL, 0, TRUE); | 799 &nr, NULL, 0, TRUE); |
800 if (len == 0) | 800 if (len == 0) |
801 { | 801 { |
802 emsg(_(e_invarg)); | 802 emsg(_(e_invarg)); |
803 retval = FAIL; | 803 retval = FAIL; |
879 } | 879 } |
880 retval = OK; | 880 retval = OK; |
881 break; | 881 break; |
882 } | 882 } |
883 #endif | 883 #endif |
884 /* check for truncated name */ | 884 // check for truncated name |
885 len = (int)(reader->js_end - (reader->js_buf + reader->js_used)); | 885 len = (int)(reader->js_end - (reader->js_buf + reader->js_used)); |
886 if ( | 886 if ( |
887 (len < 5 && STRNICMP((char *)p, "false", len) == 0) | 887 (len < 5 && STRNICMP((char *)p, "false", len) == 0) |
888 #ifdef FEAT_FLOAT | 888 #ifdef FEAT_FLOAT |
889 || (len < 9 && STRNICMP((char *)p, "-Infinity", len) == 0) | 889 || (len < 9 && STRNICMP((char *)p, "-Infinity", len) == 0) |
897 else | 897 else |
898 retval = FAIL; | 898 retval = FAIL; |
899 break; | 899 break; |
900 } | 900 } |
901 | 901 |
902 /* We are finished when retval is FAIL or MAYBE and when at the | 902 // We are finished when retval is FAIL or MAYBE and when at the |
903 * toplevel. */ | 903 // toplevel. |
904 if (retval == FAIL) | 904 if (retval == FAIL) |
905 break; | 905 break; |
906 if (retval == MAYBE || stack.ga_len == 0) | 906 if (retval == MAYBE || stack.ga_len == 0) |
907 goto theend; | 907 goto theend; |
908 | 908 |
1035 cur_item = &top_item->jd_key_tv; | 1035 cur_item = &top_item->jd_key_tv; |
1036 break; | 1036 break; |
1037 } | 1037 } |
1038 } | 1038 } |
1039 | 1039 |
1040 /* Get here when parsing failed. */ | 1040 // Get here when parsing failed. |
1041 if (res != NULL) | 1041 if (res != NULL) |
1042 { | 1042 { |
1043 clear_tv(res); | 1043 clear_tv(res); |
1044 res->v_type = VAR_SPECIAL; | 1044 res->v_type = VAR_SPECIAL; |
1045 res->vval.v_number = VVAL_NONE; | 1045 res->vval.v_number = VVAL_NONE; |
1059 static int | 1059 static int |
1060 json_decode_all(js_read_T *reader, typval_T *res, int options) | 1060 json_decode_all(js_read_T *reader, typval_T *res, int options) |
1061 { | 1061 { |
1062 int ret; | 1062 int ret; |
1063 | 1063 |
1064 /* We find the end once, to avoid calling strlen() many times. */ | 1064 // We find the end once, to avoid calling strlen() many times. |
1065 reader->js_end = reader->js_buf + STRLEN(reader->js_buf); | 1065 reader->js_end = reader->js_buf + STRLEN(reader->js_buf); |
1066 json_skip_white(reader); | 1066 json_skip_white(reader); |
1067 ret = json_decode_item(reader, res, options); | 1067 ret = json_decode_item(reader, res, options); |
1068 if (ret != OK) | 1068 if (ret != OK) |
1069 { | 1069 { |
1091 int | 1091 int |
1092 json_decode(js_read_T *reader, typval_T *res, int options) | 1092 json_decode(js_read_T *reader, typval_T *res, int options) |
1093 { | 1093 { |
1094 int ret; | 1094 int ret; |
1095 | 1095 |
1096 /* We find the end once, to avoid calling strlen() many times. */ | 1096 // We find the end once, to avoid calling strlen() many times. |
1097 reader->js_end = reader->js_buf + STRLEN(reader->js_buf); | 1097 reader->js_end = reader->js_buf + STRLEN(reader->js_buf); |
1098 json_skip_white(reader); | 1098 json_skip_white(reader); |
1099 ret = json_decode_item(reader, res, options); | 1099 ret = json_decode_item(reader, res, options); |
1100 json_skip_white(reader); | 1100 json_skip_white(reader); |
1101 | 1101 |
1117 json_find_end(js_read_T *reader, int options) | 1117 json_find_end(js_read_T *reader, int options) |
1118 { | 1118 { |
1119 int used_save = reader->js_used; | 1119 int used_save = reader->js_used; |
1120 int ret; | 1120 int ret; |
1121 | 1121 |
1122 /* We find the end once, to avoid calling strlen() many times. */ | 1122 // We find the end once, to avoid calling strlen() many times. |
1123 reader->js_end = reader->js_buf + STRLEN(reader->js_buf); | 1123 reader->js_end = reader->js_buf + STRLEN(reader->js_buf); |
1124 json_skip_white(reader); | 1124 json_skip_white(reader); |
1125 ret = json_decode_item(reader, NULL, options); | 1125 ret = json_decode_item(reader, NULL, options); |
1126 reader->js_used = used_save; | 1126 reader->js_used = used_save; |
1127 return ret; | 1127 return ret; |