blob: 05281cb70d8cf7f46f689f440c4b4c01b27dcf5b [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaarcd524592016-07-17 14:57:05 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * dict.c: Dictionary support
12 */
Bram Moolenaarcd524592016-07-17 14:57:05 +020013
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010018// List head for garbage collection. Although there can be a reference loop
19// from partial to dict to partial, we don't need to keep track of the partial,
20// since it will get freed when the dict is unused and gets freed.
21static dict_T *first_dict = NULL;
Bram Moolenaarcd524592016-07-17 14:57:05 +020022
23/*
24 * Allocate an empty header for a dictionary.
25 */
26 dict_T *
27dict_alloc(void)
28{
29 dict_T *d;
30
Bram Moolenaaradc67142019-06-22 01:40:42 +020031 d = ALLOC_CLEAR_ONE(dict_T);
Bram Moolenaarcd524592016-07-17 14:57:05 +020032 if (d != NULL)
33 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010034 // Add the dict to the list of dicts for garbage collection.
Bram Moolenaarcd524592016-07-17 14:57:05 +020035 if (first_dict != NULL)
36 first_dict->dv_used_prev = d;
37 d->dv_used_next = first_dict;
38 d->dv_used_prev = NULL;
39 first_dict = d;
40
41 hash_init(&d->dv_hashtab);
42 d->dv_lock = 0;
43 d->dv_scope = 0;
44 d->dv_refcount = 0;
45 d->dv_copyID = 0;
46 }
47 return d;
48}
49
Bram Moolenaarf49cc602018-11-11 15:21:05 +010050/*
51 * dict_alloc() with an ID for alloc_fail().
52 */
53 dict_T *
54dict_alloc_id(alloc_id_T id UNUSED)
55{
56#ifdef FEAT_EVAL
Bram Moolenaar51e14382019-05-25 20:21:28 +020057 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
Bram Moolenaarf49cc602018-11-11 15:21:05 +010058 return NULL;
59#endif
60 return (dict_alloc());
61}
62
Bram Moolenaar7e1652c2017-12-16 18:27:02 +010063 dict_T *
64dict_alloc_lock(int lock)
65{
66 dict_T *d = dict_alloc();
67
68 if (d != NULL)
69 d->dv_lock = lock;
70 return d;
71}
72
Bram Moolenaarcd524592016-07-17 14:57:05 +020073/*
74 * Allocate an empty dict for a return value.
75 * Returns OK or FAIL.
76 */
77 int
78rettv_dict_alloc(typval_T *rettv)
79{
Bram Moolenaar7e1652c2017-12-16 18:27:02 +010080 dict_T *d = dict_alloc_lock(0);
Bram Moolenaarcd524592016-07-17 14:57:05 +020081
82 if (d == NULL)
83 return FAIL;
84
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020085 rettv_dict_set(rettv, d);
Bram Moolenaarcd524592016-07-17 14:57:05 +020086 return OK;
87}
88
89/*
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020090 * Set a dictionary as the return value
91 */
92 void
93rettv_dict_set(typval_T *rettv, dict_T *d)
94{
95 rettv->v_type = VAR_DICT;
96 rettv->vval.v_dict = d;
97 if (d != NULL)
98 ++d->dv_refcount;
99}
100
101/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200102 * Free a Dictionary, including all non-container items it contains.
103 * Ignores the reference count.
104 */
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100105 void
Bram Moolenaarcd524592016-07-17 14:57:05 +0200106dict_free_contents(dict_T *d)
107{
Bram Moolenaar89483d42020-05-10 15:24:44 +0200108 hashtab_free_contents(&d->dv_hashtab);
109}
110
111/*
112 * Clear hashtab "ht" and dict items it contains.
113 */
114 void
115hashtab_free_contents(hashtab_T *ht)
116{
Bram Moolenaarcd524592016-07-17 14:57:05 +0200117 int todo;
118 hashitem_T *hi;
119 dictitem_T *di;
120
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100121 // Lock the hashtab, we don't want it to resize while freeing items.
Bram Moolenaar89483d42020-05-10 15:24:44 +0200122 hash_lock(ht);
123 todo = (int)ht->ht_used;
124 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200125 {
126 if (!HASHITEM_EMPTY(hi))
127 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100128 // Remove the item before deleting it, just in case there is
129 // something recursive causing trouble.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200130 di = HI2DI(hi);
Bram Moolenaar89483d42020-05-10 15:24:44 +0200131 hash_remove(ht, hi);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100132 dictitem_free(di);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200133 --todo;
134 }
135 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100136
Bram Moolenaar89483d42020-05-10 15:24:44 +0200137 // The hashtab is still locked, it has to be re-initialized anyway.
138 hash_clear(ht);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200139}
140
141 static void
142dict_free_dict(dict_T *d)
143{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100144 // Remove the dict from the list of dicts for garbage collection.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200145 if (d->dv_used_prev == NULL)
146 first_dict = d->dv_used_next;
147 else
148 d->dv_used_prev->dv_used_next = d->dv_used_next;
149 if (d->dv_used_next != NULL)
150 d->dv_used_next->dv_used_prev = d->dv_used_prev;
151 vim_free(d);
152}
153
154 static void
155dict_free(dict_T *d)
156{
157 if (!in_free_unref_items)
158 {
159 dict_free_contents(d);
160 dict_free_dict(d);
161 }
162}
163
164/*
165 * Unreference a Dictionary: decrement the reference count and free it when it
166 * becomes zero.
167 */
168 void
169dict_unref(dict_T *d)
170{
171 if (d != NULL && --d->dv_refcount <= 0)
172 dict_free(d);
173}
174
175/*
176 * Go through the list of dicts and free items without the copyID.
177 * Returns TRUE if something was freed.
178 */
179 int
180dict_free_nonref(int copyID)
181{
182 dict_T *dd;
183 int did_free = FALSE;
184
185 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
186 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
187 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100188 // Free the Dictionary and ordinary items it contains, but don't
189 // recurse into Lists and Dictionaries, they will be in the list
190 // of dicts or list of lists.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200191 dict_free_contents(dd);
192 did_free = TRUE;
193 }
194 return did_free;
195}
196
197 void
198dict_free_items(int copyID)
199{
200 dict_T *dd, *dd_next;
201
202 for (dd = first_dict; dd != NULL; dd = dd_next)
203 {
204 dd_next = dd->dv_used_next;
205 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
206 dict_free_dict(dd);
207 }
208}
209
210/*
211 * Allocate a Dictionary item.
212 * The "key" is copied to the new item.
Bram Moolenaarc89d4b32018-07-08 17:19:02 +0200213 * Note that the type and value of the item "di_tv" still needs to be
214 * initialized!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200215 * Returns NULL when out of memory.
216 */
217 dictitem_T *
218dictitem_alloc(char_u *key)
219{
220 dictitem_T *di;
221
Bram Moolenaarb59e7352019-08-07 21:42:24 +0200222 di = alloc(offsetof(dictitem_T, di_key) + STRLEN(key) + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200223 if (di != NULL)
224 {
225 STRCPY(di->di_key, key);
226 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaarc89d4b32018-07-08 17:19:02 +0200227 di->di_tv.v_lock = 0;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200228 }
229 return di;
230}
231
232/*
233 * Make a copy of a Dictionary item.
234 */
235 static dictitem_T *
236dictitem_copy(dictitem_T *org)
237{
238 dictitem_T *di;
239
Bram Moolenaarb59e7352019-08-07 21:42:24 +0200240 di = alloc(offsetof(dictitem_T, di_key) + STRLEN(org->di_key) + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200241 if (di != NULL)
242 {
243 STRCPY(di->di_key, org->di_key);
244 di->di_flags = DI_FLAGS_ALLOC;
245 copy_tv(&org->di_tv, &di->di_tv);
246 }
247 return di;
248}
249
250/*
251 * Remove item "item" from Dictionary "dict" and free it.
252 */
253 void
254dictitem_remove(dict_T *dict, dictitem_T *item)
255{
256 hashitem_T *hi;
257
258 hi = hash_find(&dict->dv_hashtab, item->di_key);
259 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100260 internal_error("dictitem_remove()");
Bram Moolenaarcd524592016-07-17 14:57:05 +0200261 else
262 hash_remove(&dict->dv_hashtab, hi);
263 dictitem_free(item);
264}
265
266/*
267 * Free a dict item. Also clears the value.
268 */
269 void
270dictitem_free(dictitem_T *item)
271{
272 clear_tv(&item->di_tv);
273 if (item->di_flags & DI_FLAGS_ALLOC)
274 vim_free(item);
275}
276
277/*
278 * Make a copy of dict "d". Shallow if "deep" is FALSE.
279 * The refcount of the new dict is set to 1.
280 * See item_copy() for "copyID".
281 * Returns NULL when out of memory.
282 */
283 dict_T *
284dict_copy(dict_T *orig, int deep, int copyID)
285{
286 dict_T *copy;
287 dictitem_T *di;
288 int todo;
289 hashitem_T *hi;
290
291 if (orig == NULL)
292 return NULL;
293
294 copy = dict_alloc();
295 if (copy != NULL)
296 {
297 if (copyID != 0)
298 {
299 orig->dv_copyID = copyID;
300 orig->dv_copydict = copy;
301 }
302 todo = (int)orig->dv_hashtab.ht_used;
303 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
304 {
305 if (!HASHITEM_EMPTY(hi))
306 {
307 --todo;
308
309 di = dictitem_alloc(hi->hi_key);
310 if (di == NULL)
311 break;
312 if (deep)
313 {
314 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
315 copyID) == FAIL)
316 {
317 vim_free(di);
318 break;
319 }
320 }
321 else
322 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
323 if (dict_add(copy, di) == FAIL)
324 {
325 dictitem_free(di);
326 break;
327 }
328 }
329 }
330
331 ++copy->dv_refcount;
332 if (todo > 0)
333 {
334 dict_unref(copy);
335 copy = NULL;
336 }
337 }
338
339 return copy;
340}
341
342/*
343 * Add item "item" to Dictionary "d".
344 * Returns FAIL when out of memory and when key already exists.
345 */
346 int
347dict_add(dict_T *d, dictitem_T *item)
348{
349 return hash_add(&d->dv_hashtab, item->di_key);
350}
351
352/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200353 * Add a number or special entry to dictionary "d".
Bram Moolenaarcd524592016-07-17 14:57:05 +0200354 * Returns FAIL when out of memory and when key already exists.
355 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200356 static int
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100357dict_add_number_special(dict_T *d, char *key, varnumber_T nr, vartype_T vartype)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200358{
359 dictitem_T *item;
360
361 item = dictitem_alloc((char_u *)key);
362 if (item == NULL)
363 return FAIL;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100364 item->di_tv.v_type = vartype;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200365 item->di_tv.vval.v_number = nr;
366 if (dict_add(d, item) == FAIL)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200367 {
Bram Moolenaare0be1672018-07-08 16:50:37 +0200368 dictitem_free(item);
369 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200370 }
Bram Moolenaare0be1672018-07-08 16:50:37 +0200371 return OK;
372}
373
374/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200375 * Add a number entry to dictionary "d".
376 * Returns FAIL when out of memory and when key already exists.
377 */
378 int
379dict_add_number(dict_T *d, char *key, varnumber_T nr)
380{
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100381 return dict_add_number_special(d, key, nr, VAR_NUMBER);
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200382}
383
384/*
385 * Add a special entry to dictionary "d".
386 * Returns FAIL when out of memory and when key already exists.
387 */
388 int
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100389dict_add_bool(dict_T *d, char *key, varnumber_T nr)
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200390{
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100391 return dict_add_number_special(d, key, nr, VAR_BOOL);
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200392}
393
394/*
Bram Moolenaare0be1672018-07-08 16:50:37 +0200395 * Add a string entry to dictionary "d".
396 * Returns FAIL when out of memory and when key already exists.
397 */
398 int
399dict_add_string(dict_T *d, char *key, char_u *str)
400{
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100401 return dict_add_string_len(d, key, str, -1);
402}
403
404/*
405 * Add a string entry to dictionary "d".
406 * "str" will be copied to allocated memory.
407 * When "len" is -1 use the whole string, otherwise only this many bytes.
408 * Returns FAIL when out of memory and when key already exists.
409 */
410 int
411dict_add_string_len(dict_T *d, char *key, char_u *str, int len)
412{
Bram Moolenaare0be1672018-07-08 16:50:37 +0200413 dictitem_T *item;
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100414 char_u *val = NULL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200415
416 item = dictitem_alloc((char_u *)key);
417 if (item == NULL)
418 return FAIL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200419 item->di_tv.v_type = VAR_STRING;
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100420 if (str != NULL)
421 {
422 if (len == -1)
423 val = vim_strsave(str);
424 else
425 val = vim_strnsave(str, len);
426 }
427 item->di_tv.vval.v_string = val;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200428 if (dict_add(d, item) == FAIL)
429 {
430 dictitem_free(item);
431 return FAIL;
432 }
433 return OK;
434}
435
436/*
437 * Add a list entry to dictionary "d".
438 * Returns FAIL when out of memory and when key already exists.
439 */
440 int
441dict_add_list(dict_T *d, char *key, list_T *list)
442{
443 dictitem_T *item;
444
445 item = dictitem_alloc((char_u *)key);
446 if (item == NULL)
447 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200448 item->di_tv.v_type = VAR_LIST;
449 item->di_tv.vval.v_list = list;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100450 ++list->lv_refcount;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200451 if (dict_add(d, item) == FAIL)
452 {
453 dictitem_free(item);
454 return FAIL;
455 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200456 return OK;
457}
458
459/*
Bram Moolenaar08928322020-01-04 14:32:48 +0100460 * Add a typval_T entry to dictionary "d".
461 * Returns FAIL when out of memory and when key already exists.
462 */
463 int
464dict_add_tv(dict_T *d, char *key, typval_T *tv)
465{
466 dictitem_T *item;
467
468 item = dictitem_alloc((char_u *)key);
469 if (item == NULL)
470 return FAIL;
471 copy_tv(tv, &item->di_tv);
472 if (dict_add(d, item) == FAIL)
473 {
474 dictitem_free(item);
475 return FAIL;
476 }
477 return OK;
478}
479
480/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200481 * Add a callback to dictionary "d".
482 * Returns FAIL when out of memory and when key already exists.
483 */
484 int
485dict_add_callback(dict_T *d, char *key, callback_T *cb)
486{
487 dictitem_T *item;
488
489 item = dictitem_alloc((char_u *)key);
490 if (item == NULL)
491 return FAIL;
492 put_callback(cb, &item->di_tv);
493 if (dict_add(d, item) == FAIL)
494 {
495 dictitem_free(item);
496 return FAIL;
497 }
498 return OK;
499}
500
501/*
Bram Moolenaar45e18cb2019-04-28 18:05:35 +0200502 * Initializes "iter" for iterating over dictionary items with
503 * dict_iterate_next().
504 * If "var" is not a Dict or an empty Dict then there will be nothing to
505 * iterate over, no error is given.
506 * NOTE: The dictionary must not change until iterating is finished!
507 */
508 void
509dict_iterate_start(typval_T *var, dict_iterator_T *iter)
510{
511 if (var->v_type != VAR_DICT || var->vval.v_dict == NULL)
512 iter->dit_todo = 0;
513 else
514 {
515 dict_T *d = var->vval.v_dict;
516
517 iter->dit_todo = d->dv_hashtab.ht_used;
518 iter->dit_hi = d->dv_hashtab.ht_array;
519 }
520}
521
522/*
523 * Iterate over the items referred to by "iter". It should be initialized with
524 * dict_iterate_start().
525 * Returns a pointer to the key.
526 * "*tv_result" is set to point to the value for that key.
527 * If there are no more items, NULL is returned.
528 */
529 char_u *
530dict_iterate_next(dict_iterator_T *iter, typval_T **tv_result)
531{
532 dictitem_T *di;
533 char_u *result;
534
535 if (iter->dit_todo == 0)
536 return NULL;
537
538 while (HASHITEM_EMPTY(iter->dit_hi))
539 ++iter->dit_hi;
540
541 di = HI2DI(iter->dit_hi);
542 result = di->di_key;
543 *tv_result = &di->di_tv;
544
545 --iter->dit_todo;
546 ++iter->dit_hi;
547 return result;
548}
549
550/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200551 * Add a dict entry to dictionary "d".
552 * Returns FAIL when out of memory and when key already exists.
553 */
554 int
555dict_add_dict(dict_T *d, char *key, dict_T *dict)
556{
557 dictitem_T *item;
558
559 item = dictitem_alloc((char_u *)key);
560 if (item == NULL)
561 return FAIL;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200562 item->di_tv.v_type = VAR_DICT;
563 item->di_tv.vval.v_dict = dict;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100564 ++dict->dv_refcount;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200565 if (dict_add(d, item) == FAIL)
566 {
567 dictitem_free(item);
568 return FAIL;
569 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200570 return OK;
571}
572
573/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200574 * Get the number of items in a Dictionary.
575 */
576 long
577dict_len(dict_T *d)
578{
579 if (d == NULL)
580 return 0L;
581 return (long)d->dv_hashtab.ht_used;
582}
583
584/*
585 * Find item "key[len]" in Dictionary "d".
586 * If "len" is negative use strlen(key).
587 * Returns NULL when not found.
588 */
589 dictitem_T *
590dict_find(dict_T *d, char_u *key, int len)
591{
592#define AKEYLEN 200
593 char_u buf[AKEYLEN];
594 char_u *akey;
595 char_u *tofree = NULL;
596 hashitem_T *hi;
597
598 if (d == NULL)
599 return NULL;
600 if (len < 0)
601 akey = key;
602 else if (len >= AKEYLEN)
603 {
604 tofree = akey = vim_strnsave(key, len);
605 if (akey == NULL)
606 return NULL;
607 }
608 else
609 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100610 // Avoid a malloc/free by using buf[].
Bram Moolenaarcd524592016-07-17 14:57:05 +0200611 vim_strncpy(buf, key, len);
612 akey = buf;
613 }
614
615 hi = hash_find(&d->dv_hashtab, akey);
616 vim_free(tofree);
617 if (HASHITEM_EMPTY(hi))
618 return NULL;
619 return HI2DI(hi);
620}
621
622/*
Bram Moolenaar08928322020-01-04 14:32:48 +0100623 * Get a typval_T item from a dictionary and copy it into "rettv".
624 * Returns FAIL if the entry doesn't exist or out of memory.
625 */
626 int
627dict_get_tv(dict_T *d, char_u *key, typval_T *rettv)
628{
629 dictitem_T *di;
Bram Moolenaar08928322020-01-04 14:32:48 +0100630
631 di = dict_find(d, key, -1);
632 if (di == NULL)
633 return FAIL;
634 copy_tv(&di->di_tv, rettv);
635 return OK;
636}
637
638/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200639 * Get a string item from a dictionary.
640 * When "save" is TRUE allocate memory for it.
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +0200641 * When FALSE a shared buffer is used, can only be used once!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200642 * Returns NULL if the entry doesn't exist or out of memory.
643 */
644 char_u *
Bram Moolenaar8f667172018-12-14 15:38:31 +0100645dict_get_string(dict_T *d, char_u *key, int save)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200646{
647 dictitem_T *di;
648 char_u *s;
649
650 di = dict_find(d, key, -1);
651 if (di == NULL)
652 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100653 s = tv_get_string(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200654 if (save && s != NULL)
655 s = vim_strsave(s);
656 return s;
657}
658
659/*
660 * Get a number item from a dictionary.
661 * Returns 0 if the entry doesn't exist.
662 */
663 varnumber_T
Bram Moolenaar8f667172018-12-14 15:38:31 +0100664dict_get_number(dict_T *d, char_u *key)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200665{
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200666 return dict_get_number_def(d, key, 0);
667}
668
669/*
670 * Get a number item from a dictionary.
671 * Returns "def" if the entry doesn't exist.
672 */
673 varnumber_T
674dict_get_number_def(dict_T *d, char_u *key, int def)
675{
Bram Moolenaarcd524592016-07-17 14:57:05 +0200676 dictitem_T *di;
677
678 di = dict_find(d, key, -1);
679 if (di == NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200680 return def;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100681 return tv_get_number(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200682}
683
684/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200685 * Get a number item from a dictionary.
686 * Returns 0 if the entry doesn't exist.
687 * Give an error if the entry is not a number.
688 */
689 varnumber_T
690dict_get_number_check(dict_T *d, char_u *key)
691{
692 dictitem_T *di;
693
694 di = dict_find(d, key, -1);
695 if (di == NULL)
696 return 0;
697 if (di->di_tv.v_type != VAR_NUMBER)
698 {
699 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
700 return 0;
701 }
702 return tv_get_number(&di->di_tv);
703}
704
705/*
Bram Moolenaar55881332020-08-18 13:04:15 +0200706 * Get a bool item (number or true/false) from a dictionary.
707 * Returns "def" if the entry doesn't exist.
708 */
709 varnumber_T
710dict_get_bool(dict_T *d, char_u *key, int def)
711{
712 dictitem_T *di;
713
714 di = dict_find(d, key, -1);
715 if (di == NULL)
716 return def;
717 return tv_get_bool(&di->di_tv);
718}
719
720/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200721 * Return an allocated string with the string representation of a Dictionary.
722 * May return NULL.
723 */
724 char_u *
725dict2string(typval_T *tv, int copyID, int restore_copyID)
726{
727 garray_T ga;
728 int first = TRUE;
729 char_u *tofree;
730 char_u numbuf[NUMBUFLEN];
731 hashitem_T *hi;
732 char_u *s;
733 dict_T *d;
734 int todo;
735
736 if ((d = tv->vval.v_dict) == NULL)
737 return NULL;
738 ga_init2(&ga, (int)sizeof(char), 80);
739 ga_append(&ga, '{');
740
741 todo = (int)d->dv_hashtab.ht_used;
742 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
743 {
744 if (!HASHITEM_EMPTY(hi))
745 {
746 --todo;
747
748 if (first)
749 first = FALSE;
750 else
751 ga_concat(&ga, (char_u *)", ");
752
753 tofree = string_quote(hi->hi_key, FALSE);
754 if (tofree != NULL)
755 {
756 ga_concat(&ga, tofree);
757 vim_free(tofree);
758 }
759 ga_concat(&ga, (char_u *)": ");
760 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
761 FALSE, restore_copyID, TRUE);
762 if (s != NULL)
763 ga_concat(&ga, s);
764 vim_free(tofree);
765 if (s == NULL || did_echo_string_emsg)
766 break;
767 line_breakcheck();
768
769 }
770 }
771 if (todo > 0)
772 {
773 vim_free(ga.ga_data);
774 return NULL;
775 }
776
777 ga_append(&ga, '}');
778 ga_append(&ga, NUL);
779 return (char_u *)ga.ga_data;
780}
781
782/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +0200783 * Get the key for #{key: val} into "tv" and advance "arg".
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200784 * Return FAIL when there is no valid key.
785 */
786 static int
787get_literal_key(char_u **arg, typval_T *tv)
788{
789 char_u *p;
790
791 if (!ASCII_ISALNUM(**arg) && **arg != '_' && **arg != '-')
792 return FAIL;
793
794 for (p = *arg; ASCII_ISALNUM(*p) || *p == '_' || *p == '-'; ++p)
795 ;
796 tv->v_type = VAR_STRING;
Bram Moolenaardf44a272020-06-07 20:49:05 +0200797 tv->vval.v_string = vim_strnsave(*arg, p - *arg);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200798
Bram Moolenaardb199212020-08-12 18:01:53 +0200799 *arg = p;
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200800 return OK;
801}
802
803/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200804 * Allocate a variable for a Dictionary and fill it from "*arg".
Bram Moolenaar962d7212020-07-04 14:15:00 +0200805 * "*arg" points to the "{".
Bram Moolenaar9f9fe372019-07-27 23:12:12 +0200806 * "literal" is TRUE for #{key: val}
Bram Moolenaarcd524592016-07-17 14:57:05 +0200807 * Return OK or FAIL. Returns NOTDONE for {expr}.
808 */
809 int
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200810eval_dict(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int literal)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200811{
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200812 int evaluate = evalarg == NULL ? FALSE
813 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200814 dict_T *d = NULL;
815 typval_T tvkey;
816 typval_T tv;
817 char_u *key = NULL;
818 dictitem_T *item;
819 char_u *start = skipwhite(*arg + 1);
820 char_u buf[NUMBUFLEN];
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200821 int vim9script = in_vim9script();
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200822 int had_comma;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200823
Bram Moolenaarcd524592016-07-17 14:57:05 +0200824 /*
825 * First check if it's not a curly-braces thing: {expr}.
826 * Must do this without evaluating, otherwise a function may be called
827 * twice. Unfortunately this means we need to call eval1() twice for the
828 * first item.
829 * But {} is an empty Dictionary.
830 */
Bram Moolenaar32e35112020-05-14 22:41:15 +0200831 if (!vim9script && *start != '}')
Bram Moolenaarcd524592016-07-17 14:57:05 +0200832 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200833 if (eval1(&start, &tv, NULL) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200834 return FAIL;
Bram Moolenaarae95a392020-08-06 16:38:12 +0200835 if (*skipwhite(start) == '}')
Bram Moolenaarcd524592016-07-17 14:57:05 +0200836 return NOTDONE;
837 }
838
839 if (evaluate)
840 {
841 d = dict_alloc();
842 if (d == NULL)
843 return FAIL;
844 }
845 tvkey.v_type = VAR_UNKNOWN;
846 tv.v_type = VAR_UNKNOWN;
847
Bram Moolenaar962d7212020-07-04 14:15:00 +0200848 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200849 while (**arg != '}' && **arg != NUL)
850 {
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200851 if ((literal
852 ? get_literal_key(arg, &tvkey)
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200853 : eval1(arg, &tvkey, evalarg)) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200854 goto failret;
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200855
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +0200856 // the colon should come right after the key, but this wasn't checked
Bram Moolenaar9d489562020-07-30 20:08:50 +0200857 // previously, so only require it in Vim9 script.
858 if (!vim9script)
859 *arg = skipwhite(*arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200860 if (**arg != ':')
861 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +0100862 if (evaluate)
Bram Moolenaardb199212020-08-12 18:01:53 +0200863 {
864 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200865 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaardb199212020-08-12 18:01:53 +0200866 else
867 semsg(_(e_missing_dict_colon), *arg);
868 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200869 clear_tv(&tvkey);
870 goto failret;
871 }
872 if (evaluate)
873 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100874 key = tv_get_string_buf_chk(&tvkey, buf);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200875 if (key == NULL)
876 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100877 // "key" is NULL when tv_get_string_buf_chk() gave an errmsg
Bram Moolenaarcd524592016-07-17 14:57:05 +0200878 clear_tv(&tvkey);
879 goto failret;
880 }
881 }
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200882 if (vim9script && (*arg)[1] != NUL && !VIM_ISWHITE((*arg)[1]))
883 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200884 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaarab19d492020-06-27 17:04:05 +0200885 clear_tv(&tvkey);
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200886 goto failret;
887 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200888
Bram Moolenaar962d7212020-07-04 14:15:00 +0200889 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200890 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200891 {
892 if (evaluate)
893 clear_tv(&tvkey);
894 goto failret;
895 }
896 if (evaluate)
897 {
898 item = dict_find(d, key, -1);
899 if (item != NULL)
900 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +0100901 if (evaluate)
902 semsg(_(e_duplicate_key), key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200903 clear_tv(&tvkey);
904 clear_tv(&tv);
905 goto failret;
906 }
907 item = dictitem_alloc(key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200908 if (item != NULL)
909 {
910 item->di_tv = tv;
911 item->di_tv.v_lock = 0;
912 if (dict_add(d, item) == FAIL)
913 dictitem_free(item);
914 }
915 }
Bram Moolenaara8931942019-09-28 17:25:10 +0200916 clear_tv(&tvkey);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200917
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +0200918 // the comma should come right after the value, but this wasn't checked
919 // previously, so only require it in Vim9 script.
920 if (!vim9script)
921 *arg = skipwhite(*arg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200922 had_comma = **arg == ',';
923 if (had_comma)
924 {
925 if (vim9script && (*arg)[1] != NUL && !VIM_ISWHITE((*arg)[1]))
926 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200927 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200928 goto failret;
929 }
930 *arg = skipwhite(*arg + 1);
931 }
932
933 // the "}" can be on the next line
Bram Moolenaar962d7212020-07-04 14:15:00 +0200934 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200935 if (**arg == '}')
936 break;
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200937 if (!had_comma)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200938 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +0100939 if (evaluate)
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +0200940 {
941 if (**arg == ',')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200942 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +0200943 else
944 semsg(_(e_missing_dict_comma), *arg);
945 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200946 goto failret;
947 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200948 }
949
950 if (**arg != '}')
951 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +0100952 if (evaluate)
953 semsg(_(e_missing_dict_end), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200954failret:
Bram Moolenaaradc67142019-06-22 01:40:42 +0200955 if (d != NULL)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200956 dict_free(d);
957 return FAIL;
958 }
959
960 *arg = skipwhite(*arg + 1);
961 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200962 rettv_dict_set(rettv, d);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200963
964 return OK;
965}
966
967/*
968 * Go over all entries in "d2" and add them to "d1".
969 * When "action" is "error" then a duplicate key is an error.
970 * When "action" is "force" then a duplicate key is overwritten.
971 * Otherwise duplicate keys are ignored ("action" is "keep").
972 */
973 void
974dict_extend(dict_T *d1, dict_T *d2, char_u *action)
975{
976 dictitem_T *di1;
977 hashitem_T *hi2;
978 int todo;
979 char_u *arg_errmsg = (char_u *)N_("extend() argument");
980
981 todo = (int)d2->dv_hashtab.ht_used;
982 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
983 {
984 if (!HASHITEM_EMPTY(hi2))
985 {
986 --todo;
987 di1 = dict_find(d1, hi2->hi_key, -1);
988 if (d1->dv_scope != 0)
989 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100990 // Disallow replacing a builtin function in l: and g:.
991 // Check the key to be valid when adding to any scope.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200992 if (d1->dv_scope == VAR_DEF_SCOPE
993 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +0200994 && var_wrong_func_name(hi2->hi_key, di1 == NULL))
Bram Moolenaarcd524592016-07-17 14:57:05 +0200995 break;
996 if (!valid_varname(hi2->hi_key))
997 break;
998 }
999 if (di1 == NULL)
1000 {
1001 di1 = dictitem_copy(HI2DI(hi2));
1002 if (di1 != NULL && dict_add(d1, di1) == FAIL)
1003 dictitem_free(di1);
1004 }
1005 else if (*action == 'e')
1006 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001007 semsg(_("E737: Key already exists: %s"), hi2->hi_key);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001008 break;
1009 }
1010 else if (*action == 'f' && HI2DI(hi2) != di1)
1011 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001012 if (value_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001013 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaarcd524592016-07-17 14:57:05 +02001014 break;
1015 clear_tv(&di1->di_tv);
1016 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
1017 }
1018 }
1019 }
1020}
1021
1022/*
1023 * Return the dictitem that an entry in a hashtable points to.
1024 */
1025 dictitem_T *
1026dict_lookup(hashitem_T *hi)
1027{
1028 return HI2DI(hi);
1029}
1030
1031/*
1032 * Return TRUE when two dictionaries have exactly the same key/values.
1033 */
1034 int
1035dict_equal(
1036 dict_T *d1,
1037 dict_T *d2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001038 int ic, // ignore case for strings
1039 int recursive) // TRUE when used recursively
Bram Moolenaarcd524592016-07-17 14:57:05 +02001040{
1041 hashitem_T *hi;
1042 dictitem_T *item2;
1043 int todo;
1044
Bram Moolenaarcd524592016-07-17 14:57:05 +02001045 if (d1 == d2)
1046 return TRUE;
1047 if (dict_len(d1) != dict_len(d2))
1048 return FALSE;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001049 if (dict_len(d1) == 0)
1050 // empty and NULL dicts are considered equal
1051 return TRUE;
1052 if (d1 == NULL || d2 == NULL)
1053 return FALSE;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001054
1055 todo = (int)d1->dv_hashtab.ht_used;
1056 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
1057 {
1058 if (!HASHITEM_EMPTY(hi))
1059 {
1060 item2 = dict_find(d2, hi->hi_key, -1);
1061 if (item2 == NULL)
1062 return FALSE;
1063 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
1064 return FALSE;
1065 --todo;
1066 }
1067 }
1068 return TRUE;
1069}
1070
1071/*
1072 * Turn a dict into a list:
1073 * "what" == 0: list of keys
1074 * "what" == 1: list of values
1075 * "what" == 2: list of items
1076 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001077 static void
Bram Moolenaarcd524592016-07-17 14:57:05 +02001078dict_list(typval_T *argvars, typval_T *rettv, int what)
1079{
1080 list_T *l2;
1081 dictitem_T *di;
1082 hashitem_T *hi;
1083 listitem_T *li;
1084 listitem_T *li2;
1085 dict_T *d;
1086 int todo;
1087
1088 if (argvars[0].v_type != VAR_DICT)
1089 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001090 emsg(_(e_dictreq));
Bram Moolenaarcd524592016-07-17 14:57:05 +02001091 return;
1092 }
1093 if ((d = argvars[0].vval.v_dict) == NULL)
1094 return;
1095
1096 if (rettv_list_alloc(rettv) == FAIL)
1097 return;
1098
1099 todo = (int)d->dv_hashtab.ht_used;
1100 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1101 {
1102 if (!HASHITEM_EMPTY(hi))
1103 {
1104 --todo;
1105 di = HI2DI(hi);
1106
1107 li = listitem_alloc();
1108 if (li == NULL)
1109 break;
1110 list_append(rettv->vval.v_list, li);
1111
1112 if (what == 0)
1113 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001114 // keys()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001115 li->li_tv.v_type = VAR_STRING;
1116 li->li_tv.v_lock = 0;
1117 li->li_tv.vval.v_string = vim_strsave(di->di_key);
1118 }
1119 else if (what == 1)
1120 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001121 // values()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001122 copy_tv(&di->di_tv, &li->li_tv);
1123 }
1124 else
1125 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001126 // items()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001127 l2 = list_alloc();
1128 li->li_tv.v_type = VAR_LIST;
1129 li->li_tv.v_lock = 0;
1130 li->li_tv.vval.v_list = l2;
1131 if (l2 == NULL)
1132 break;
1133 ++l2->lv_refcount;
1134
1135 li2 = listitem_alloc();
1136 if (li2 == NULL)
1137 break;
1138 list_append(l2, li2);
1139 li2->li_tv.v_type = VAR_STRING;
1140 li2->li_tv.v_lock = 0;
1141 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
1142
1143 li2 = listitem_alloc();
1144 if (li2 == NULL)
1145 break;
1146 list_append(l2, li2);
1147 copy_tv(&di->di_tv, &li2->li_tv);
1148 }
1149 }
1150 }
1151}
1152
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001153/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001154 * "items(dict)" function
1155 */
1156 void
1157f_items(typval_T *argvars, typval_T *rettv)
1158{
1159 dict_list(argvars, rettv, 2);
1160}
1161
1162/*
1163 * "keys()" function
1164 */
1165 void
1166f_keys(typval_T *argvars, typval_T *rettv)
1167{
1168 dict_list(argvars, rettv, 0);
1169}
1170
1171/*
1172 * "values(dict)" function
1173 */
1174 void
1175f_values(typval_T *argvars, typval_T *rettv)
1176{
1177 dict_list(argvars, rettv, 1);
1178}
1179
1180/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001181 * Make each item in the dict readonly (not the value of the item).
1182 */
1183 void
1184dict_set_items_ro(dict_T *di)
1185{
1186 int todo = (int)di->dv_hashtab.ht_used;
1187 hashitem_T *hi;
1188
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001189 // Set readonly
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001190 for (hi = di->dv_hashtab.ht_array; todo > 0 ; ++hi)
1191 {
1192 if (HASHITEM_EMPTY(hi))
1193 continue;
1194 --todo;
1195 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
1196 }
1197}
1198
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001199/*
1200 * "has_key()" function
1201 */
1202 void
1203f_has_key(typval_T *argvars, typval_T *rettv)
1204{
1205 if (argvars[0].v_type != VAR_DICT)
1206 {
1207 emsg(_(e_dictreq));
1208 return;
1209 }
1210 if (argvars[0].vval.v_dict == NULL)
1211 return;
1212
1213 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
1214 tv_get_string(&argvars[1]), -1) != NULL;
1215}
1216
1217/*
1218 * "remove({dict})" function
1219 */
1220 void
1221dict_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1222{
1223 dict_T *d;
1224 char_u *key;
1225 dictitem_T *di;
1226
1227 if (argvars[2].v_type != VAR_UNKNOWN)
1228 semsg(_(e_toomanyarg), "remove()");
1229 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001230 && !value_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001231 {
1232 key = tv_get_string_chk(&argvars[1]);
1233 if (key != NULL)
1234 {
1235 di = dict_find(d, key, -1);
1236 if (di == NULL)
1237 semsg(_(e_dictkey), key);
1238 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
1239 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
1240 {
1241 *rettv = di->di_tv;
1242 init_tv(&di->di_tv);
1243 dictitem_remove(d, di);
1244 }
1245 }
1246 }
1247}
1248
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001249#endif // defined(FEAT_EVAL)