comparison src/list.c @ 31728:238ca27dbfd2 v9.0.1196

patch 9.0.1196: code is indented more than necessary Commit: https://github.com/vim/vim/commit/e8575988969579f9e1439181ae338b2ff74054a8 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Sat Jan 14 12:32:28 2023 +0000 patch 9.0.1196: code is indented more than necessary Problem: Code is indented more than necessary. Solution: Use an early return where it makes sense. (Yegappan Lakshmanan, closes #11813)
author Bram Moolenaar <Bram@vim.org>
date Sat, 14 Jan 2023 13:45:04 +0100
parents b1c66bff0a66
children 97255d909654
comparison
equal deleted inserted replaced
31727:2b50a7f0feec 31728:238ca27dbfd2
122 if (l == NULL) 122 if (l == NULL)
123 return NULL; 123 return NULL;
124 124
125 list_init(l); 125 list_init(l);
126 126
127 if (count > 0) 127 if (count <= 0)
128 { 128 return l;
129 listitem_T *li = (listitem_T *)(l + 1); 129
130 int i; 130 listitem_T *li = (listitem_T *)(l + 1);
131 131 int i;
132 l->lv_len = count; 132
133 l->lv_with_items = count; 133 l->lv_len = count;
134 l->lv_first = li; 134 l->lv_with_items = count;
135 l->lv_u.mat.lv_last = li + count - 1; 135 l->lv_first = li;
136 for (i = 0; i < count; ++i) 136 l->lv_u.mat.lv_last = li + count - 1;
137 { 137 for (i = 0; i < count; ++i)
138 if (i == 0) 138 {
139 li->li_prev = NULL; 139 if (i == 0)
140 else 140 li->li_prev = NULL;
141 li->li_prev = li - 1; 141 else
142 if (i == count - 1) 142 li->li_prev = li - 1;
143 li->li_next = NULL; 143 if (i == count - 1)
144 else 144 li->li_next = NULL;
145 li->li_next = li + 1; 145 else
146 ++li; 146 li->li_next = li + 1;
147 } 147 ++li;
148 } 148 }
149 149
150 return l; 150 return l;
151 } 151 }
152 152
295 } 295 }
296 296
297 void 297 void
298 list_free(list_T *l) 298 list_free(list_T *l)
299 { 299 {
300 if (!in_free_unref_items) 300 if (in_free_unref_items)
301 { 301 return;
302 list_free_contents(l); 302
303 list_free_list(l); 303 list_free_contents(l);
304 } 304 list_free_list(l);
305 } 305 }
306 306
307 /* 307 /*
308 * Allocate a list item. 308 * Allocate a list item.
309 * It is not initialized, don't forget to set v_lock. 309 * It is not initialized, don't forget to set v_lock.
538 listitem_T * 538 listitem_T *
539 list_find_index(list_T *l, long *idx) 539 list_find_index(list_T *l, long *idx)
540 { 540 {
541 listitem_T *li = list_find(l, *idx); 541 listitem_T *li = list_find(l, *idx);
542 542
543 if (li == NULL) 543 if (li != NULL)
544 { 544 return li;
545 if (*idx < 0) 545
546 { 546 if (*idx < 0)
547 *idx = 0; 547 {
548 li = list_find(l, *idx); 548 *idx = 0;
549 } 549 li = list_find(l, *idx);
550 } 550 }
551 return li; 551 return li;
552 } 552 }
553 553
554 /* 554 /*
770 check_range_index_one(list_T *l, long *n1, int can_append, int quiet) 770 check_range_index_one(list_T *l, long *n1, int can_append, int quiet)
771 { 771 {
772 long orig_n1 = *n1; 772 long orig_n1 = *n1;
773 listitem_T *li = list_find_index(l, n1); 773 listitem_T *li = list_find_index(l, n1);
774 774
775 if (li != NULL)
776 return li;
777
778 // Vim9: Allow for adding an item at the end.
779 if (can_append && in_vim9script()
780 && *n1 == l->lv_len && l->lv_lock == 0)
781 {
782 list_append_number(l, 0);
783 li = list_find_index(l, n1);
784 }
775 if (li == NULL) 785 if (li == NULL)
776 { 786 {
777 // Vim9: Allow for adding an item at the end. 787 if (!quiet)
778 if (can_append && in_vim9script() 788 semsg(_(e_list_index_out_of_range_nr), orig_n1);
779 && *n1 == l->lv_len && l->lv_lock == 0) 789 return NULL;
780 {
781 list_append_number(l, 0);
782 li = list_find_index(l, n1);
783 }
784 if (li == NULL)
785 {
786 if (!quiet)
787 semsg(_(e_list_index_out_of_range_nr), orig_n1);
788 return NULL;
789 }
790 } 790 }
791 return li; 791 return li;
792 } 792 }
793 793
794 /* 794 /*
1284 1284
1285 if (orig == NULL) 1285 if (orig == NULL)
1286 return NULL; 1286 return NULL;
1287 1287
1288 copy = list_alloc(); 1288 copy = list_alloc();
1289 if (copy != NULL) 1289 if (copy == NULL)
1290 { 1290 return NULL;
1291 if (orig->lv_type == NULL || top || deep) 1291
1292 copy->lv_type = NULL; 1292 if (orig->lv_type == NULL || top || deep)
1293 copy->lv_type = NULL;
1294 else
1295 copy->lv_type = alloc_type(orig->lv_type);
1296 if (copyID != 0)
1297 {
1298 // Do this before adding the items, because one of the items may
1299 // refer back to this list.
1300 orig->lv_copyID = copyID;
1301 orig->lv_copylist = copy;
1302 }
1303 CHECK_LIST_MATERIALIZE(orig);
1304 for (item = orig->lv_first; item != NULL && !got_int;
1305 item = item->li_next)
1306 {
1307 ni = listitem_alloc();
1308 if (ni == NULL)
1309 break;
1310 if (deep)
1311 {
1312 if (item_copy(&item->li_tv, &ni->li_tv,
1313 deep, FALSE, copyID) == FAIL)
1314 {
1315 vim_free(ni);
1316 break;
1317 }
1318 }
1293 else 1319 else
1294 copy->lv_type = alloc_type(orig->lv_type); 1320 copy_tv(&item->li_tv, &ni->li_tv);
1295 if (copyID != 0) 1321 list_append(copy, ni);
1296 { 1322 }
1297 // Do this before adding the items, because one of the items may 1323 ++copy->lv_refcount;
1298 // refer back to this list. 1324 if (item != NULL)
1299 orig->lv_copyID = copyID; 1325 {
1300 orig->lv_copylist = copy; 1326 list_unref(copy);
1301 } 1327 copy = NULL;
1302 CHECK_LIST_MATERIALIZE(orig);
1303 for (item = orig->lv_first; item != NULL && !got_int;
1304 item = item->li_next)
1305 {
1306 ni = listitem_alloc();
1307 if (ni == NULL)
1308 break;
1309 if (deep)
1310 {
1311 if (item_copy(&item->li_tv, &ni->li_tv,
1312 deep, FALSE, copyID) == FAIL)
1313 {
1314 vim_free(ni);
1315 break;
1316 }
1317 }
1318 else
1319 copy_tv(&item->li_tv, &ni->li_tv);
1320 list_append(copy, ni);
1321 }
1322 ++copy->lv_refcount;
1323 if (item != NULL)
1324 {
1325 list_unref(copy);
1326 copy = NULL;
1327 }
1328 } 1328 }
1329 1329
1330 return copy; 1330 return copy;
1331 } 1331 }
1332 1332
1489 return OK; // nothing to do 1489 return OK; // nothing to do
1490 ga_init2(&join_ga, sizeof(join_T), l->lv_len); 1490 ga_init2(&join_ga, sizeof(join_T), l->lv_len);
1491 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID, 1491 retval = list_join_inner(gap, l, sep, echo_style, restore_copyID,
1492 copyID, &join_ga); 1492 copyID, &join_ga);
1493 1493
1494 if (join_ga.ga_data == NULL)
1495 return retval;
1496
1494 // Dispose each item in join_ga. 1497 // Dispose each item in join_ga.
1495 if (join_ga.ga_data != NULL) 1498 p = (join_T *)join_ga.ga_data;
1496 { 1499 for (i = 0; i < join_ga.ga_len; ++i)
1497 p = (join_T *)join_ga.ga_data; 1500 {
1498 for (i = 0; i < join_ga.ga_len; ++i) 1501 vim_free(p->tofree);
1499 { 1502 ++p;
1500 vim_free(p->tofree); 1503 }
1501 ++p; 1504 ga_clear(&join_ga);
1502 }
1503 ga_clear(&join_ga);
1504 }
1505 1505
1506 return retval; 1506 return retval;
1507 } 1507 }
1508 1508
1509 /* 1509 /*
2558 2558
2559 // On type errors, the preceding call has already displayed an error 2559 // On type errors, the preceding call has already displayed an error
2560 // message. Avoid a misleading error message for an empty string that 2560 // message. Avoid a misleading error message for an empty string that
2561 // was not passed as argument. 2561 // was not passed as argument.
2562 expr = &argvars[1]; 2562 expr = &argvars[1];
2563 if (expr->v_type != VAR_UNKNOWN) 2563 if (expr->v_type == VAR_UNKNOWN)
2564 { 2564 return;
2565 typval_T save_val; 2565
2566 typval_T save_key; 2566 typval_T save_val;
2567 2567 typval_T save_key;
2568 prepare_vimvar(VV_VAL, &save_val); 2568
2569 prepare_vimvar(VV_KEY, &save_key); 2569 prepare_vimvar(VV_VAL, &save_val);
2570 2570 prepare_vimvar(VV_KEY, &save_key);
2571 // We reset "did_emsg" to be able to detect whether an error 2571
2572 // occurred during evaluation of the expression. 2572 // We reset "did_emsg" to be able to detect whether an error
2573 save_did_emsg = did_emsg; 2573 // occurred during evaluation of the expression.
2574 did_emsg = FALSE; 2574 save_did_emsg = did_emsg;
2575 2575 did_emsg = FALSE;
2576 if (argvars[0].v_type == VAR_DICT) 2576
2577 dict_filter_map(argvars[0].vval.v_dict, filtermap, type, func_name, 2577 if (argvars[0].v_type == VAR_DICT)
2578 arg_errmsg, expr, rettv); 2578 dict_filter_map(argvars[0].vval.v_dict, filtermap, type, func_name,
2579 else if (argvars[0].v_type == VAR_BLOB) 2579 arg_errmsg, expr, rettv);
2580 blob_filter_map(argvars[0].vval.v_blob, filtermap, expr, rettv); 2580 else if (argvars[0].v_type == VAR_BLOB)
2581 else if (argvars[0].v_type == VAR_STRING) 2581 blob_filter_map(argvars[0].vval.v_blob, filtermap, expr, rettv);
2582 string_filter_map(tv_get_string(&argvars[0]), filtermap, expr, 2582 else if (argvars[0].v_type == VAR_STRING)
2583 rettv); 2583 string_filter_map(tv_get_string(&argvars[0]), filtermap, expr,
2584 else // argvars[0].v_type == VAR_LIST 2584 rettv);
2585 list_filter_map(argvars[0].vval.v_list, filtermap, type, func_name, 2585 else // argvars[0].v_type == VAR_LIST
2586 arg_errmsg, expr, rettv); 2586 list_filter_map(argvars[0].vval.v_list, filtermap, type, func_name,
2587 2587 arg_errmsg, expr, rettv);
2588 restore_vimvar(VV_KEY, &save_key); 2588
2589 restore_vimvar(VV_VAL, &save_val); 2589 restore_vimvar(VV_KEY, &save_key);
2590 2590 restore_vimvar(VV_VAL, &save_val);
2591 did_emsg |= save_did_emsg; 2591
2592 } 2592 did_emsg |= save_did_emsg;
2593 } 2593 }
2594 2594
2595 /* 2595 /*
2596 * "filter()" function 2596 * "filter()" function
2597 */ 2597 */