Mercurial > vim
comparison src/list.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 | 684a15da9929 |
children | 09f28c17ac58 |
comparison
equal
deleted
inserted
replaced
18799:d7d0942e231b | 18800:f41b55f9357c |
---|---|
15 | 15 |
16 #if defined(FEAT_EVAL) || defined(PROTO) | 16 #if defined(FEAT_EVAL) || defined(PROTO) |
17 | 17 |
18 static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob"); | 18 static char *e_listblobarg = N_("E899: Argument of %s must be a List or Blob"); |
19 | 19 |
20 /* List heads for garbage collection. */ | 20 // List heads for garbage collection. |
21 static list_T *first_list = NULL; /* list of all lists */ | 21 static list_T *first_list = NULL; // list of all lists |
22 | 22 |
23 /* | 23 /* |
24 * Add a watcher to a list. | 24 * Add a watcher to a list. |
25 */ | 25 */ |
26 void | 26 void |
75 list_T *l; | 75 list_T *l; |
76 | 76 |
77 l = ALLOC_CLEAR_ONE(list_T); | 77 l = ALLOC_CLEAR_ONE(list_T); |
78 if (l != NULL) | 78 if (l != NULL) |
79 { | 79 { |
80 /* Prepend the list to the list of lists for garbage collection. */ | 80 // Prepend the list to the list of lists for garbage collection. |
81 if (first_list != NULL) | 81 if (first_list != NULL) |
82 first_list->lv_used_prev = l; | 82 first_list->lv_used_prev = l; |
83 l->lv_used_prev = NULL; | 83 l->lv_used_prev = NULL; |
84 l->lv_used_next = first_list; | 84 l->lv_used_next = first_list; |
85 first_list = l; | 85 first_list = l; |
163 { | 163 { |
164 listitem_T *item; | 164 listitem_T *item; |
165 | 165 |
166 for (item = l->lv_first; item != NULL; item = l->lv_first) | 166 for (item = l->lv_first; item != NULL; item = l->lv_first) |
167 { | 167 { |
168 /* Remove the item before deleting it. */ | 168 // Remove the item before deleting it. |
169 l->lv_first = item->li_next; | 169 l->lv_first = item->li_next; |
170 clear_tv(&item->li_tv); | 170 clear_tv(&item->li_tv); |
171 vim_free(item); | 171 vim_free(item); |
172 } | 172 } |
173 } | 173 } |
185 | 185 |
186 for (ll = first_list; ll != NULL; ll = ll->lv_used_next) | 186 for (ll = first_list; ll != NULL; ll = ll->lv_used_next) |
187 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK) | 187 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK) |
188 && ll->lv_watch == NULL) | 188 && ll->lv_watch == NULL) |
189 { | 189 { |
190 /* Free the List and ordinary items it contains, but don't recurse | 190 // Free the List and ordinary items it contains, but don't recurse |
191 * into Lists and Dictionaries, they will be in the list of dicts | 191 // into Lists and Dictionaries, they will be in the list of dicts |
192 * or list of lists. */ | 192 // or list of lists. |
193 list_free_contents(ll); | 193 list_free_contents(ll); |
194 did_free = TRUE; | 194 did_free = TRUE; |
195 } | 195 } |
196 return did_free; | 196 return did_free; |
197 } | 197 } |
198 | 198 |
199 static void | 199 static void |
200 list_free_list(list_T *l) | 200 list_free_list(list_T *l) |
201 { | 201 { |
202 /* Remove the list from the list of lists for garbage collection. */ | 202 // Remove the list from the list of lists for garbage collection. |
203 if (l->lv_used_prev == NULL) | 203 if (l->lv_used_prev == NULL) |
204 first_list = l->lv_used_next; | 204 first_list = l->lv_used_next; |
205 else | 205 else |
206 l->lv_used_prev->lv_used_next = l->lv_used_next; | 206 l->lv_used_prev->lv_used_next = l->lv_used_next; |
207 if (l->lv_used_next != NULL) | 207 if (l->lv_used_next != NULL) |
219 { | 219 { |
220 ll_next = ll->lv_used_next; | 220 ll_next = ll->lv_used_next; |
221 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK) | 221 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK) |
222 && ll->lv_watch == NULL) | 222 && ll->lv_watch == NULL) |
223 { | 223 { |
224 /* Free the List and ordinary items it contains, but don't recurse | 224 // Free the List and ordinary items it contains, but don't recurse |
225 * into Lists and Dictionaries, they will be in the list of dicts | 225 // into Lists and Dictionaries, they will be in the list of dicts |
226 * or list of lists. */ | 226 // or list of lists. |
227 list_free_list(ll); | 227 list_free_list(ll); |
228 } | 228 } |
229 } | 229 } |
230 } | 230 } |
231 | 231 |
285 */ | 285 */ |
286 int | 286 int |
287 list_equal( | 287 list_equal( |
288 list_T *l1, | 288 list_T *l1, |
289 list_T *l2, | 289 list_T *l2, |
290 int ic, /* ignore case for strings */ | 290 int ic, // ignore case for strings |
291 int recursive) /* TRUE when used recursively */ | 291 int recursive) // TRUE when used recursively |
292 { | 292 { |
293 listitem_T *item1, *item2; | 293 listitem_T *item1, *item2; |
294 | 294 |
295 if (l1 == NULL || l2 == NULL) | 295 if (l1 == NULL || l2 == NULL) |
296 return FALSE; | 296 return FALSE; |
319 long idx; | 319 long idx; |
320 | 320 |
321 if (l == NULL) | 321 if (l == NULL) |
322 return NULL; | 322 return NULL; |
323 | 323 |
324 /* Negative index is relative to the end. */ | 324 // Negative index is relative to the end. |
325 if (n < 0) | 325 if (n < 0) |
326 n = l->lv_len + n; | 326 n = l->lv_len + n; |
327 | 327 |
328 /* Check for index out of range. */ | 328 // Check for index out of range. |
329 if (n < 0 || n >= l->lv_len) | 329 if (n < 0 || n >= l->lv_len) |
330 return NULL; | 330 return NULL; |
331 | 331 |
332 /* When there is a cached index may start search from there. */ | 332 // When there is a cached index may start search from there. |
333 if (l->lv_idx_item != NULL) | 333 if (l->lv_idx_item != NULL) |
334 { | 334 { |
335 if (n < l->lv_idx / 2) | 335 if (n < l->lv_idx / 2) |
336 { | 336 { |
337 /* closest to the start of the list */ | 337 // closest to the start of the list |
338 item = l->lv_first; | 338 item = l->lv_first; |
339 idx = 0; | 339 idx = 0; |
340 } | 340 } |
341 else if (n > (l->lv_idx + l->lv_len) / 2) | 341 else if (n > (l->lv_idx + l->lv_len) / 2) |
342 { | 342 { |
343 /* closest to the end of the list */ | 343 // closest to the end of the list |
344 item = l->lv_last; | 344 item = l->lv_last; |
345 idx = l->lv_len - 1; | 345 idx = l->lv_len - 1; |
346 } | 346 } |
347 else | 347 else |
348 { | 348 { |
349 /* closest to the cached index */ | 349 // closest to the cached index |
350 item = l->lv_idx_item; | 350 item = l->lv_idx_item; |
351 idx = l->lv_idx; | 351 idx = l->lv_idx; |
352 } | 352 } |
353 } | 353 } |
354 else | 354 else |
355 { | 355 { |
356 if (n < l->lv_len / 2) | 356 if (n < l->lv_len / 2) |
357 { | 357 { |
358 /* closest to the start of the list */ | 358 // closest to the start of the list |
359 item = l->lv_first; | 359 item = l->lv_first; |
360 idx = 0; | 360 idx = 0; |
361 } | 361 } |
362 else | 362 else |
363 { | 363 { |
364 /* closest to the end of the list */ | 364 // closest to the end of the list |
365 item = l->lv_last; | 365 item = l->lv_last; |
366 idx = l->lv_len - 1; | 366 idx = l->lv_len - 1; |
367 } | 367 } |
368 } | 368 } |
369 | 369 |
370 while (n > idx) | 370 while (n > idx) |
371 { | 371 { |
372 /* search forward */ | 372 // search forward |
373 item = item->li_next; | 373 item = item->li_next; |
374 ++idx; | 374 ++idx; |
375 } | 375 } |
376 while (n < idx) | 376 while (n < idx) |
377 { | 377 { |
378 /* search backward */ | 378 // search backward |
379 item = item->li_prev; | 379 item = item->li_prev; |
380 --idx; | 380 --idx; |
381 } | 381 } |
382 | 382 |
383 /* cache the used index */ | 383 // cache the used index |
384 l->lv_idx = idx; | 384 l->lv_idx = idx; |
385 l->lv_idx_item = item; | 385 l->lv_idx_item = item; |
386 | 386 |
387 return item; | 387 return item; |
388 } | 388 } |
392 */ | 392 */ |
393 long | 393 long |
394 list_find_nr( | 394 list_find_nr( |
395 list_T *l, | 395 list_T *l, |
396 long idx, | 396 long idx, |
397 int *errorp) /* set to TRUE when something wrong */ | 397 int *errorp) // set to TRUE when something wrong |
398 { | 398 { |
399 listitem_T *li; | 399 listitem_T *li; |
400 | 400 |
401 li = list_find(l, idx); | 401 li = list_find(l, idx); |
402 if (li == NULL) | 402 if (li == NULL) |
451 void | 451 void |
452 list_append(list_T *l, listitem_T *item) | 452 list_append(list_T *l, listitem_T *item) |
453 { | 453 { |
454 if (l->lv_last == NULL) | 454 if (l->lv_last == NULL) |
455 { | 455 { |
456 /* empty list */ | 456 // empty list |
457 l->lv_first = item; | 457 l->lv_first = item; |
458 l->lv_last = item; | 458 l->lv_last = item; |
459 item->li_prev = NULL; | 459 item->li_prev = NULL; |
460 } | 460 } |
461 else | 461 else |
583 | 583 |
584 void | 584 void |
585 list_insert(list_T *l, listitem_T *ni, listitem_T *item) | 585 list_insert(list_T *l, listitem_T *ni, listitem_T *item) |
586 { | 586 { |
587 if (item == NULL) | 587 if (item == NULL) |
588 /* Append new item at end of list. */ | 588 // Append new item at end of list. |
589 list_append(l, ni); | 589 list_append(l, ni); |
590 else | 590 else |
591 { | 591 { |
592 /* Insert new item before existing item. */ | 592 // Insert new item before existing item. |
593 ni->li_prev = item->li_prev; | 593 ni->li_prev = item->li_prev; |
594 ni->li_next = item; | 594 ni->li_next = item; |
595 if (item->li_prev == NULL) | 595 if (item->li_prev == NULL) |
596 { | 596 { |
597 l->lv_first = ni; | 597 l->lv_first = ni; |
616 list_extend(list_T *l1, list_T *l2, listitem_T *bef) | 616 list_extend(list_T *l1, list_T *l2, listitem_T *bef) |
617 { | 617 { |
618 listitem_T *item; | 618 listitem_T *item; |
619 int todo = l2->lv_len; | 619 int todo = l2->lv_len; |
620 | 620 |
621 /* We also quit the loop when we have inserted the original item count of | 621 // We also quit the loop when we have inserted the original item count of |
622 * the list, avoid a hang when we extend a list with itself. */ | 622 // the list, avoid a hang when we extend a list with itself. |
623 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next) | 623 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next) |
624 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL) | 624 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL) |
625 return FAIL; | 625 return FAIL; |
626 return OK; | 626 return OK; |
627 } | 627 } |
636 list_T *l; | 636 list_T *l; |
637 | 637 |
638 if (l1 == NULL || l2 == NULL) | 638 if (l1 == NULL || l2 == NULL) |
639 return FAIL; | 639 return FAIL; |
640 | 640 |
641 /* make a copy of the first list. */ | 641 // make a copy of the first list. |
642 l = list_copy(l1, FALSE, 0); | 642 l = list_copy(l1, FALSE, 0); |
643 if (l == NULL) | 643 if (l == NULL) |
644 return FAIL; | 644 return FAIL; |
645 tv->v_type = VAR_LIST; | 645 tv->v_type = VAR_LIST; |
646 tv->vval.v_list = l; | 646 tv->vval.v_list = l; |
647 | 647 |
648 /* append all items from the second list */ | 648 // append all items from the second list |
649 return list_extend(l, l2, NULL); | 649 return list_extend(l, l2, NULL); |
650 } | 650 } |
651 | 651 |
652 /* | 652 /* |
653 * Make a copy of list "orig". Shallow if "deep" is FALSE. | 653 * Make a copy of list "orig". Shallow if "deep" is FALSE. |
668 copy = list_alloc(); | 668 copy = list_alloc(); |
669 if (copy != NULL) | 669 if (copy != NULL) |
670 { | 670 { |
671 if (copyID != 0) | 671 if (copyID != 0) |
672 { | 672 { |
673 /* Do this before adding the items, because one of the items may | 673 // Do this before adding the items, because one of the items may |
674 * refer back to this list. */ | 674 // refer back to this list. |
675 orig->lv_copyID = copyID; | 675 orig->lv_copyID = copyID; |
676 orig->lv_copylist = copy; | 676 orig->lv_copylist = copy; |
677 } | 677 } |
678 for (item = orig->lv_first; item != NULL && !got_int; | 678 for (item = orig->lv_first; item != NULL && !got_int; |
679 item = item->li_next) | 679 item = item->li_next) |
713 void | 713 void |
714 vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2) | 714 vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2) |
715 { | 715 { |
716 listitem_T *ip; | 716 listitem_T *ip; |
717 | 717 |
718 /* notify watchers */ | 718 // notify watchers |
719 for (ip = item; ip != NULL; ip = ip->li_next) | 719 for (ip = item; ip != NULL; ip = ip->li_next) |
720 { | 720 { |
721 --l->lv_len; | 721 --l->lv_len; |
722 list_fix_watch(l, ip); | 722 list_fix_watch(l, ip); |
723 if (ip == item2) | 723 if (ip == item2) |
764 char_u *tofree; | 764 char_u *tofree; |
765 } join_T; | 765 } join_T; |
766 | 766 |
767 static int | 767 static int |
768 list_join_inner( | 768 list_join_inner( |
769 garray_T *gap, /* to store the result in */ | 769 garray_T *gap, // to store the result in |
770 list_T *l, | 770 list_T *l, |
771 char_u *sep, | 771 char_u *sep, |
772 int echo_style, | 772 int echo_style, |
773 int restore_copyID, | 773 int restore_copyID, |
774 int copyID, | 774 int copyID, |
775 garray_T *join_gap) /* to keep each list item string */ | 775 garray_T *join_gap) // to keep each list item string |
776 { | 776 { |
777 int i; | 777 int i; |
778 join_T *p; | 778 join_T *p; |
779 int len; | 779 int len; |
780 int sumlen = 0; | 780 int sumlen = 0; |
782 char_u *tofree; | 782 char_u *tofree; |
783 char_u numbuf[NUMBUFLEN]; | 783 char_u numbuf[NUMBUFLEN]; |
784 listitem_T *item; | 784 listitem_T *item; |
785 char_u *s; | 785 char_u *s; |
786 | 786 |
787 /* Stringify each item in the list. */ | 787 // Stringify each item in the list. |
788 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next) | 788 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next) |
789 { | 789 { |
790 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID, | 790 s = echo_string_core(&item->li_tv, &tofree, numbuf, copyID, |
791 echo_style, restore_copyID, !echo_style); | 791 echo_style, restore_copyID, !echo_style); |
792 if (s == NULL) | 792 if (s == NULL) |
807 p->s = vim_strnsave(s, len); | 807 p->s = vim_strnsave(s, len); |
808 p->tofree = p->s; | 808 p->tofree = p->s; |
809 } | 809 } |
810 | 810 |
811 line_breakcheck(); | 811 line_breakcheck(); |
812 if (did_echo_string_emsg) /* recursion error, bail out */ | 812 if (did_echo_string_emsg) // recursion error, bail out |
813 break; | 813 break; |
814 } | 814 } |
815 | 815 |
816 /* Allocate result buffer with its total size, avoid re-allocation and | 816 // Allocate result buffer with its total size, avoid re-allocation and |
817 * multiple copy operations. Add 2 for a tailing ']' and NUL. */ | 817 // multiple copy operations. Add 2 for a tailing ']' and NUL. |
818 if (join_gap->ga_len >= 2) | 818 if (join_gap->ga_len >= 2) |
819 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1); | 819 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1); |
820 if (ga_grow(gap, sumlen + 2) == FAIL) | 820 if (ga_grow(gap, sumlen + 2) == FAIL) |
821 return FAIL; | 821 return FAIL; |
822 | 822 |
854 int retval; | 854 int retval; |
855 join_T *p; | 855 join_T *p; |
856 int i; | 856 int i; |
857 | 857 |
858 if (l->lv_len < 1) | 858 if (l->lv_len < 1) |
859 return OK; /* nothing to do */ | 859 return OK; // nothing to do |
860 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len); | 860 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len); |
861 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID, | 861 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID, |
862 copyID, &join_ga); | 862 copyID, &join_ga); |
863 | 863 |
864 /* Dispose each item in join_ga. */ | 864 // Dispose each item in join_ga. |
865 if (join_ga.ga_data != NULL) | 865 if (join_ga.ga_data != NULL) |
866 { | 866 { |
867 p = (join_T *)join_ga.ga_data; | 867 p = (join_T *)join_ga.ga_data; |
868 for (i = 0; i < join_ga.ga_len; ++i) | 868 for (i = 0; i < join_ga.ga_len; ++i) |
869 { | 869 { |
929 } | 929 } |
930 | 930 |
931 *arg = skipwhite(*arg + 1); | 931 *arg = skipwhite(*arg + 1); |
932 while (**arg != ']' && **arg != NUL) | 932 while (**arg != ']' && **arg != NUL) |
933 { | 933 { |
934 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */ | 934 if (eval1(arg, &tv, evaluate) == FAIL) // recursive! |
935 goto failret; | 935 goto failret; |
936 if (evaluate) | 936 if (evaluate) |
937 { | 937 { |
938 item = listitem_alloc(); | 938 item = listitem_alloc(); |
939 if (item != NULL) | 939 if (item != NULL) |
1118 semsg(_(e_listidx), idx); | 1118 semsg(_(e_listidx), idx); |
1119 else | 1119 else |
1120 { | 1120 { |
1121 if (argvars[2].v_type == VAR_UNKNOWN) | 1121 if (argvars[2].v_type == VAR_UNKNOWN) |
1122 { | 1122 { |
1123 /* Remove one item, return its value. */ | 1123 // Remove one item, return its value. |
1124 vimlist_remove(l, item, item); | 1124 vimlist_remove(l, item, item); |
1125 *rettv = item->li_tv; | 1125 *rettv = item->li_tv; |
1126 vim_free(item); | 1126 vim_free(item); |
1127 } | 1127 } |
1128 else | 1128 else |
1142 { | 1142 { |
1143 ++cnt; | 1143 ++cnt; |
1144 if (li == item2) | 1144 if (li == item2) |
1145 break; | 1145 break; |
1146 } | 1146 } |
1147 if (li == NULL) /* didn't find "item2" after "item" */ | 1147 if (li == NULL) // didn't find "item2" after "item" |
1148 emsg(_(e_invrange)); | 1148 emsg(_(e_invrange)); |
1149 else | 1149 else |
1150 { | 1150 { |
1151 vimlist_remove(l, item, item2); | 1151 vimlist_remove(l, item, item2); |
1152 if (rettv_list_alloc(rettv) == OK) | 1152 if (rettv_list_alloc(rettv) == OK) |
1165 } | 1165 } |
1166 | 1166 |
1167 static int item_compare(const void *s1, const void *s2); | 1167 static int item_compare(const void *s1, const void *s2); |
1168 static int item_compare2(const void *s1, const void *s2); | 1168 static int item_compare2(const void *s1, const void *s2); |
1169 | 1169 |
1170 /* struct used in the array that's given to qsort() */ | 1170 // struct used in the array that's given to qsort() |
1171 typedef struct | 1171 typedef struct |
1172 { | 1172 { |
1173 listitem_T *item; | 1173 listitem_T *item; |
1174 int idx; | 1174 int idx; |
1175 } sortItem_T; | 1175 } sortItem_T; |
1176 | 1176 |
1177 /* struct storing information about current sort */ | 1177 // struct storing information about current sort |
1178 typedef struct | 1178 typedef struct |
1179 { | 1179 { |
1180 int item_compare_ic; | 1180 int item_compare_ic; |
1181 int item_compare_numeric; | 1181 int item_compare_numeric; |
1182 int item_compare_numbers; | 1182 int item_compare_numbers; |
1227 | 1227 |
1228 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1; | 1228 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1; |
1229 } | 1229 } |
1230 #endif | 1230 #endif |
1231 | 1231 |
1232 /* tv2string() puts quotes around a string and allocates memory. Don't do | 1232 // tv2string() puts quotes around a string and allocates memory. Don't do |
1233 * that for string variables. Use a single quote when comparing with a | 1233 // that for string variables. Use a single quote when comparing with a |
1234 * non-string to do what the docs promise. */ | 1234 // non-string to do what the docs promise. |
1235 if (tv1->v_type == VAR_STRING) | 1235 if (tv1->v_type == VAR_STRING) |
1236 { | 1236 { |
1237 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric) | 1237 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric) |
1238 p1 = (char_u *)"'"; | 1238 p1 = (char_u *)"'"; |
1239 else | 1239 else |
1267 n1 = strtod((char *)p1, (char **)&p1); | 1267 n1 = strtod((char *)p1, (char **)&p1); |
1268 n2 = strtod((char *)p2, (char **)&p2); | 1268 n2 = strtod((char *)p2, (char **)&p2); |
1269 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1; | 1269 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1; |
1270 } | 1270 } |
1271 | 1271 |
1272 /* When the result would be zero, compare the item indexes. Makes the | 1272 // When the result would be zero, compare the item indexes. Makes the |
1273 * sort stable. */ | 1273 // sort stable. |
1274 if (res == 0 && !sortinfo->item_compare_keep_zero) | 1274 if (res == 0 && !sortinfo->item_compare_keep_zero) |
1275 res = si1->idx > si2->idx ? 1 : -1; | 1275 res = si1->idx > si2->idx ? 1 : -1; |
1276 | 1276 |
1277 vim_free(tofree1); | 1277 vim_free(tofree1); |
1278 vim_free(tofree2); | 1278 vim_free(tofree2); |
1288 typval_T argv[3]; | 1288 typval_T argv[3]; |
1289 char_u *func_name; | 1289 char_u *func_name; |
1290 partial_T *partial = sortinfo->item_compare_partial; | 1290 partial_T *partial = sortinfo->item_compare_partial; |
1291 funcexe_T funcexe; | 1291 funcexe_T funcexe; |
1292 | 1292 |
1293 /* shortcut after failure in previous call; compare all items equal */ | 1293 // shortcut after failure in previous call; compare all items equal |
1294 if (sortinfo->item_compare_func_err) | 1294 if (sortinfo->item_compare_func_err) |
1295 return 0; | 1295 return 0; |
1296 | 1296 |
1297 si1 = (sortItem_T *)s1; | 1297 si1 = (sortItem_T *)s1; |
1298 si2 = (sortItem_T *)s2; | 1298 si2 = (sortItem_T *)s2; |
1300 if (partial == NULL) | 1300 if (partial == NULL) |
1301 func_name = sortinfo->item_compare_func; | 1301 func_name = sortinfo->item_compare_func; |
1302 else | 1302 else |
1303 func_name = partial_name(partial); | 1303 func_name = partial_name(partial); |
1304 | 1304 |
1305 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED | 1305 // Copy the values. This is needed to be able to set v_lock to VAR_FIXED |
1306 * in the copy without changing the original list items. */ | 1306 // in the copy without changing the original list items. |
1307 copy_tv(&si1->item->li_tv, &argv[0]); | 1307 copy_tv(&si1->item->li_tv, &argv[0]); |
1308 copy_tv(&si2->item->li_tv, &argv[1]); | 1308 copy_tv(&si2->item->li_tv, &argv[1]); |
1309 | 1309 |
1310 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */ | 1310 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this |
1311 vim_memset(&funcexe, 0, sizeof(funcexe)); | 1311 vim_memset(&funcexe, 0, sizeof(funcexe)); |
1312 funcexe.evaluate = TRUE; | 1312 funcexe.evaluate = TRUE; |
1313 funcexe.partial = partial; | 1313 funcexe.partial = partial; |
1314 funcexe.selfdict = sortinfo->item_compare_selfdict; | 1314 funcexe.selfdict = sortinfo->item_compare_selfdict; |
1315 res = call_func(func_name, -1, &rettv, 2, argv, &funcexe); | 1315 res = call_func(func_name, -1, &rettv, 2, argv, &funcexe); |
1319 if (res == FAIL) | 1319 if (res == FAIL) |
1320 res = ITEM_COMPARE_FAIL; | 1320 res = ITEM_COMPARE_FAIL; |
1321 else | 1321 else |
1322 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err); | 1322 res = (int)tv_get_number_chk(&rettv, &sortinfo->item_compare_func_err); |
1323 if (sortinfo->item_compare_func_err) | 1323 if (sortinfo->item_compare_func_err) |
1324 res = ITEM_COMPARE_FAIL; /* return value has wrong type */ | 1324 res = ITEM_COMPARE_FAIL; // return value has wrong type |
1325 clear_tv(&rettv); | 1325 clear_tv(&rettv); |
1326 | 1326 |
1327 /* When the result would be zero, compare the pointers themselves. Makes | 1327 // When the result would be zero, compare the pointers themselves. Makes |
1328 * the sort stable. */ | 1328 // the sort stable. |
1329 if (res == 0 && !sortinfo->item_compare_keep_zero) | 1329 if (res == 0 && !sortinfo->item_compare_keep_zero) |
1330 res = si1->idx > si2->idx ? 1 : -1; | 1330 res = si1->idx > si2->idx ? 1 : -1; |
1331 | 1331 |
1332 return res; | 1332 return res; |
1333 } | 1333 } |
1344 sortinfo_T *old_sortinfo; | 1344 sortinfo_T *old_sortinfo; |
1345 sortinfo_T info; | 1345 sortinfo_T info; |
1346 long len; | 1346 long len; |
1347 long i; | 1347 long i; |
1348 | 1348 |
1349 /* Pointer to current info struct used in compare function. Save and | 1349 // Pointer to current info struct used in compare function. Save and |
1350 * restore the current one for nested calls. */ | 1350 // restore the current one for nested calls. |
1351 old_sortinfo = sortinfo; | 1351 old_sortinfo = sortinfo; |
1352 sortinfo = &info; | 1352 sortinfo = &info; |
1353 | 1353 |
1354 if (argvars[0].v_type != VAR_LIST) | 1354 if (argvars[0].v_type != VAR_LIST) |
1355 semsg(_(e_listarg), sort ? "sort()" : "uniq()"); | 1355 semsg(_(e_listarg), sort ? "sort()" : "uniq()"); |
1362 goto theend; | 1362 goto theend; |
1363 rettv_list_set(rettv, l); | 1363 rettv_list_set(rettv, l); |
1364 | 1364 |
1365 len = list_len(l); | 1365 len = list_len(l); |
1366 if (len <= 1) | 1366 if (len <= 1) |
1367 goto theend; /* short list sorts pretty quickly */ | 1367 goto theend; // short list sorts pretty quickly |
1368 | 1368 |
1369 info.item_compare_ic = FALSE; | 1369 info.item_compare_ic = FALSE; |
1370 info.item_compare_numeric = FALSE; | 1370 info.item_compare_numeric = FALSE; |
1371 info.item_compare_numbers = FALSE; | 1371 info.item_compare_numbers = FALSE; |
1372 #ifdef FEAT_FLOAT | 1372 #ifdef FEAT_FLOAT |
1375 info.item_compare_func = NULL; | 1375 info.item_compare_func = NULL; |
1376 info.item_compare_partial = NULL; | 1376 info.item_compare_partial = NULL; |
1377 info.item_compare_selfdict = NULL; | 1377 info.item_compare_selfdict = NULL; |
1378 if (argvars[1].v_type != VAR_UNKNOWN) | 1378 if (argvars[1].v_type != VAR_UNKNOWN) |
1379 { | 1379 { |
1380 /* optional second argument: {func} */ | 1380 // optional second argument: {func} |
1381 if (argvars[1].v_type == VAR_FUNC) | 1381 if (argvars[1].v_type == VAR_FUNC) |
1382 info.item_compare_func = argvars[1].vval.v_string; | 1382 info.item_compare_func = argvars[1].vval.v_string; |
1383 else if (argvars[1].v_type == VAR_PARTIAL) | 1383 else if (argvars[1].v_type == VAR_PARTIAL) |
1384 info.item_compare_partial = argvars[1].vval.v_partial; | 1384 info.item_compare_partial = argvars[1].vval.v_partial; |
1385 else | 1385 else |
1386 { | 1386 { |
1387 int error = FALSE; | 1387 int error = FALSE; |
1388 | 1388 |
1389 i = (long)tv_get_number_chk(&argvars[1], &error); | 1389 i = (long)tv_get_number_chk(&argvars[1], &error); |
1390 if (error) | 1390 if (error) |
1391 goto theend; /* type error; errmsg already given */ | 1391 goto theend; // type error; errmsg already given |
1392 if (i == 1) | 1392 if (i == 1) |
1393 info.item_compare_ic = TRUE; | 1393 info.item_compare_ic = TRUE; |
1394 else if (argvars[1].v_type != VAR_NUMBER) | 1394 else if (argvars[1].v_type != VAR_NUMBER) |
1395 info.item_compare_func = tv_get_string(&argvars[1]); | 1395 info.item_compare_func = tv_get_string(&argvars[1]); |
1396 else if (i != 0) | 1396 else if (i != 0) |
1400 } | 1400 } |
1401 if (info.item_compare_func != NULL) | 1401 if (info.item_compare_func != NULL) |
1402 { | 1402 { |
1403 if (*info.item_compare_func == NUL) | 1403 if (*info.item_compare_func == NUL) |
1404 { | 1404 { |
1405 /* empty string means default sort */ | 1405 // empty string means default sort |
1406 info.item_compare_func = NULL; | 1406 info.item_compare_func = NULL; |
1407 } | 1407 } |
1408 else if (STRCMP(info.item_compare_func, "n") == 0) | 1408 else if (STRCMP(info.item_compare_func, "n") == 0) |
1409 { | 1409 { |
1410 info.item_compare_func = NULL; | 1410 info.item_compare_func = NULL; |
1430 } | 1430 } |
1431 } | 1431 } |
1432 | 1432 |
1433 if (argvars[2].v_type != VAR_UNKNOWN) | 1433 if (argvars[2].v_type != VAR_UNKNOWN) |
1434 { | 1434 { |
1435 /* optional third argument: {dict} */ | 1435 // optional third argument: {dict} |
1436 if (argvars[2].v_type != VAR_DICT) | 1436 if (argvars[2].v_type != VAR_DICT) |
1437 { | 1437 { |
1438 emsg(_(e_dictreq)); | 1438 emsg(_(e_dictreq)); |
1439 goto theend; | 1439 goto theend; |
1440 } | 1440 } |
1441 info.item_compare_selfdict = argvars[2].vval.v_dict; | 1441 info.item_compare_selfdict = argvars[2].vval.v_dict; |
1442 } | 1442 } |
1443 } | 1443 } |
1444 | 1444 |
1445 /* Make an array with each entry pointing to an item in the List. */ | 1445 // Make an array with each entry pointing to an item in the List. |
1446 ptrs = ALLOC_MULT(sortItem_T, len); | 1446 ptrs = ALLOC_MULT(sortItem_T, len); |
1447 if (ptrs == NULL) | 1447 if (ptrs == NULL) |
1448 goto theend; | 1448 goto theend; |
1449 | 1449 |
1450 i = 0; | 1450 i = 0; |
1451 if (sort) | 1451 if (sort) |
1452 { | 1452 { |
1453 /* sort(): ptrs will be the list to sort */ | 1453 // sort(): ptrs will be the list to sort |
1454 for (li = l->lv_first; li != NULL; li = li->li_next) | 1454 for (li = l->lv_first; li != NULL; li = li->li_next) |
1455 { | 1455 { |
1456 ptrs[i].item = li; | 1456 ptrs[i].item = li; |
1457 ptrs[i].idx = i; | 1457 ptrs[i].idx = i; |
1458 ++i; | 1458 ++i; |
1459 } | 1459 } |
1460 | 1460 |
1461 info.item_compare_func_err = FALSE; | 1461 info.item_compare_func_err = FALSE; |
1462 info.item_compare_keep_zero = FALSE; | 1462 info.item_compare_keep_zero = FALSE; |
1463 /* test the compare function */ | 1463 // test the compare function |
1464 if ((info.item_compare_func != NULL | 1464 if ((info.item_compare_func != NULL |
1465 || info.item_compare_partial != NULL) | 1465 || info.item_compare_partial != NULL) |
1466 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1]) | 1466 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1]) |
1467 == ITEM_COMPARE_FAIL) | 1467 == ITEM_COMPARE_FAIL) |
1468 emsg(_("E702: Sort compare function failed")); | 1468 emsg(_("E702: Sort compare function failed")); |
1469 else | 1469 else |
1470 { | 1470 { |
1471 /* Sort the array with item pointers. */ | 1471 // Sort the array with item pointers. |
1472 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T), | 1472 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T), |
1473 info.item_compare_func == NULL | 1473 info.item_compare_func == NULL |
1474 && info.item_compare_partial == NULL | 1474 && info.item_compare_partial == NULL |
1475 ? item_compare : item_compare2); | 1475 ? item_compare : item_compare2); |
1476 | 1476 |
1477 if (!info.item_compare_func_err) | 1477 if (!info.item_compare_func_err) |
1478 { | 1478 { |
1479 /* Clear the List and append the items in sorted order. */ | 1479 // Clear the List and append the items in sorted order. |
1480 l->lv_first = l->lv_last = l->lv_idx_item = NULL; | 1480 l->lv_first = l->lv_last = l->lv_idx_item = NULL; |
1481 l->lv_len = 0; | 1481 l->lv_len = 0; |
1482 for (i = 0; i < len; ++i) | 1482 for (i = 0; i < len; ++i) |
1483 list_append(l, ptrs[i].item); | 1483 list_append(l, ptrs[i].item); |
1484 } | 1484 } |
1486 } | 1486 } |
1487 else | 1487 else |
1488 { | 1488 { |
1489 int (*item_compare_func_ptr)(const void *, const void *); | 1489 int (*item_compare_func_ptr)(const void *, const void *); |
1490 | 1490 |
1491 /* f_uniq(): ptrs will be a stack of items to remove */ | 1491 // f_uniq(): ptrs will be a stack of items to remove |
1492 info.item_compare_func_err = FALSE; | 1492 info.item_compare_func_err = FALSE; |
1493 info.item_compare_keep_zero = TRUE; | 1493 info.item_compare_keep_zero = TRUE; |
1494 item_compare_func_ptr = info.item_compare_func != NULL | 1494 item_compare_func_ptr = info.item_compare_func != NULL |
1495 || info.item_compare_partial != NULL | 1495 || info.item_compare_partial != NULL |
1496 ? item_compare2 : item_compare; | 1496 ? item_compare2 : item_compare; |
1772 f_add(typval_T *argvars, typval_T *rettv) | 1772 f_add(typval_T *argvars, typval_T *rettv) |
1773 { | 1773 { |
1774 list_T *l; | 1774 list_T *l; |
1775 blob_T *b; | 1775 blob_T *b; |
1776 | 1776 |
1777 rettv->vval.v_number = 1; /* Default: Failed */ | 1777 rettv->vval.v_number = 1; // Default: Failed |
1778 if (argvars[0].v_type == VAR_LIST) | 1778 if (argvars[0].v_type == VAR_LIST) |
1779 { | 1779 { |
1780 if ((l = argvars[0].vval.v_list) != NULL | 1780 if ((l = argvars[0].vval.v_list) != NULL |
1781 && !var_check_lock(l->lv_lock, | 1781 && !var_check_lock(l->lv_lock, |
1782 (char_u *)N_("add() argument"), TRUE) | 1782 (char_u *)N_("add() argument"), TRUE) |
1933 { | 1933 { |
1934 if (argvars[2].v_type != VAR_UNKNOWN) | 1934 if (argvars[2].v_type != VAR_UNKNOWN) |
1935 { | 1935 { |
1936 before = (long)tv_get_number_chk(&argvars[2], &error); | 1936 before = (long)tv_get_number_chk(&argvars[2], &error); |
1937 if (error) | 1937 if (error) |
1938 return; /* type error; errmsg already given */ | 1938 return; // type error; errmsg already given |
1939 | 1939 |
1940 if (before == l1->lv_len) | 1940 if (before == l1->lv_len) |
1941 item = NULL; | 1941 item = NULL; |
1942 else | 1942 else |
1943 { | 1943 { |
1965 d1 = argvars[0].vval.v_dict; | 1965 d1 = argvars[0].vval.v_dict; |
1966 d2 = argvars[1].vval.v_dict; | 1966 d2 = argvars[1].vval.v_dict; |
1967 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE) | 1967 if (d1 != NULL && !var_check_lock(d1->dv_lock, arg_errmsg, TRUE) |
1968 && d2 != NULL) | 1968 && d2 != NULL) |
1969 { | 1969 { |
1970 /* Check the third argument. */ | 1970 // Check the third argument. |
1971 if (argvars[2].v_type != VAR_UNKNOWN) | 1971 if (argvars[2].v_type != VAR_UNKNOWN) |
1972 { | 1972 { |
1973 static char *(av[]) = {"keep", "force", "error"}; | 1973 static char *(av[]) = {"keep", "force", "error"}; |
1974 | 1974 |
1975 action = tv_get_string_chk(&argvars[2]); | 1975 action = tv_get_string_chk(&argvars[2]); |
1976 if (action == NULL) | 1976 if (action == NULL) |
1977 return; /* type error; errmsg already given */ | 1977 return; // type error; errmsg already given |
1978 for (i = 0; i < 3; ++i) | 1978 for (i = 0; i < 3; ++i) |
1979 if (STRCMP(action, av[i]) == 0) | 1979 if (STRCMP(action, av[i]) == 0) |
1980 break; | 1980 break; |
1981 if (i == 3) | 1981 if (i == 3) |
1982 { | 1982 { |
2049 (char_u *)N_("insert() argument"), TRUE)) | 2049 (char_u *)N_("insert() argument"), TRUE)) |
2050 { | 2050 { |
2051 if (argvars[2].v_type != VAR_UNKNOWN) | 2051 if (argvars[2].v_type != VAR_UNKNOWN) |
2052 before = (long)tv_get_number_chk(&argvars[2], &error); | 2052 before = (long)tv_get_number_chk(&argvars[2], &error); |
2053 if (error) | 2053 if (error) |
2054 return; /* type error; errmsg already given */ | 2054 return; // type error; errmsg already given |
2055 | 2055 |
2056 if (before == l->lv_len) | 2056 if (before == l->lv_len) |
2057 item = NULL; | 2057 item = NULL; |
2058 else | 2058 else |
2059 { | 2059 { |