blob: 5725140df31cbba78b0680873e1d7ce60282cc7b [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;
Bram Moolenaar3f974ff2020-10-02 18:11:56 +0200239 size_t len = STRLEN(org->di_key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200240
Bram Moolenaar3f974ff2020-10-02 18:11:56 +0200241 di = alloc(offsetof(dictitem_T, di_key) + len + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200242 if (di != NULL)
243 {
Bram Moolenaar3f974ff2020-10-02 18:11:56 +0200244 mch_memmove(di->di_key, org->di_key, len + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200245 di->di_flags = DI_FLAGS_ALLOC;
246 copy_tv(&org->di_tv, &di->di_tv);
247 }
248 return di;
249}
250
251/*
252 * Remove item "item" from Dictionary "dict" and free it.
253 */
254 void
255dictitem_remove(dict_T *dict, dictitem_T *item)
256{
257 hashitem_T *hi;
258
259 hi = hash_find(&dict->dv_hashtab, item->di_key);
260 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100261 internal_error("dictitem_remove()");
Bram Moolenaarcd524592016-07-17 14:57:05 +0200262 else
263 hash_remove(&dict->dv_hashtab, hi);
264 dictitem_free(item);
265}
266
267/*
268 * Free a dict item. Also clears the value.
269 */
270 void
271dictitem_free(dictitem_T *item)
272{
273 clear_tv(&item->di_tv);
274 if (item->di_flags & DI_FLAGS_ALLOC)
275 vim_free(item);
276}
277
278/*
279 * Make a copy of dict "d". Shallow if "deep" is FALSE.
280 * The refcount of the new dict is set to 1.
281 * See item_copy() for "copyID".
282 * Returns NULL when out of memory.
283 */
284 dict_T *
285dict_copy(dict_T *orig, int deep, int copyID)
286{
287 dict_T *copy;
288 dictitem_T *di;
289 int todo;
290 hashitem_T *hi;
291
292 if (orig == NULL)
293 return NULL;
294
295 copy = dict_alloc();
296 if (copy != NULL)
297 {
298 if (copyID != 0)
299 {
300 orig->dv_copyID = copyID;
301 orig->dv_copydict = copy;
302 }
303 todo = (int)orig->dv_hashtab.ht_used;
304 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
305 {
306 if (!HASHITEM_EMPTY(hi))
307 {
308 --todo;
309
310 di = dictitem_alloc(hi->hi_key);
311 if (di == NULL)
312 break;
313 if (deep)
314 {
315 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
316 copyID) == FAIL)
317 {
318 vim_free(di);
319 break;
320 }
321 }
322 else
323 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
324 if (dict_add(copy, di) == FAIL)
325 {
326 dictitem_free(di);
327 break;
328 }
329 }
330 }
331
332 ++copy->dv_refcount;
333 if (todo > 0)
334 {
335 dict_unref(copy);
336 copy = NULL;
337 }
338 }
339
340 return copy;
341}
342
343/*
344 * Add item "item" to Dictionary "d".
345 * Returns FAIL when out of memory and when key already exists.
346 */
347 int
348dict_add(dict_T *d, dictitem_T *item)
349{
350 return hash_add(&d->dv_hashtab, item->di_key);
351}
352
353/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200354 * Add a number or special entry to dictionary "d".
Bram Moolenaarcd524592016-07-17 14:57:05 +0200355 * Returns FAIL when out of memory and when key already exists.
356 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200357 static int
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100358dict_add_number_special(dict_T *d, char *key, varnumber_T nr, vartype_T vartype)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200359{
360 dictitem_T *item;
361
362 item = dictitem_alloc((char_u *)key);
363 if (item == NULL)
364 return FAIL;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100365 item->di_tv.v_type = vartype;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200366 item->di_tv.vval.v_number = nr;
367 if (dict_add(d, item) == FAIL)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200368 {
Bram Moolenaare0be1672018-07-08 16:50:37 +0200369 dictitem_free(item);
370 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200371 }
Bram Moolenaare0be1672018-07-08 16:50:37 +0200372 return OK;
373}
374
375/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200376 * Add a number entry to dictionary "d".
377 * Returns FAIL when out of memory and when key already exists.
378 */
379 int
380dict_add_number(dict_T *d, char *key, varnumber_T nr)
381{
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100382 return dict_add_number_special(d, key, nr, VAR_NUMBER);
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200383}
384
385/*
386 * Add a special entry to dictionary "d".
387 * Returns FAIL when out of memory and when key already exists.
388 */
389 int
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100390dict_add_bool(dict_T *d, char *key, varnumber_T nr)
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200391{
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100392 return dict_add_number_special(d, key, nr, VAR_BOOL);
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200393}
394
395/*
Bram Moolenaare0be1672018-07-08 16:50:37 +0200396 * Add a string entry to dictionary "d".
397 * Returns FAIL when out of memory and when key already exists.
398 */
399 int
400dict_add_string(dict_T *d, char *key, char_u *str)
401{
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100402 return dict_add_string_len(d, key, str, -1);
403}
404
405/*
406 * Add a string entry to dictionary "d".
407 * "str" will be copied to allocated memory.
408 * When "len" is -1 use the whole string, otherwise only this many bytes.
409 * Returns FAIL when out of memory and when key already exists.
410 */
411 int
412dict_add_string_len(dict_T *d, char *key, char_u *str, int len)
413{
Bram Moolenaare0be1672018-07-08 16:50:37 +0200414 dictitem_T *item;
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100415 char_u *val = NULL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200416
417 item = dictitem_alloc((char_u *)key);
418 if (item == NULL)
419 return FAIL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200420 item->di_tv.v_type = VAR_STRING;
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100421 if (str != NULL)
422 {
423 if (len == -1)
424 val = vim_strsave(str);
425 else
426 val = vim_strnsave(str, len);
427 }
428 item->di_tv.vval.v_string = val;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200429 if (dict_add(d, item) == FAIL)
430 {
431 dictitem_free(item);
432 return FAIL;
433 }
434 return OK;
435}
436
437/*
438 * Add a list entry to dictionary "d".
439 * Returns FAIL when out of memory and when key already exists.
440 */
441 int
442dict_add_list(dict_T *d, char *key, list_T *list)
443{
444 dictitem_T *item;
445
446 item = dictitem_alloc((char_u *)key);
447 if (item == NULL)
448 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200449 item->di_tv.v_type = VAR_LIST;
450 item->di_tv.vval.v_list = list;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100451 ++list->lv_refcount;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200452 if (dict_add(d, item) == FAIL)
453 {
454 dictitem_free(item);
455 return FAIL;
456 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200457 return OK;
458}
459
460/*
Bram Moolenaar08928322020-01-04 14:32:48 +0100461 * Add a typval_T entry to dictionary "d".
462 * Returns FAIL when out of memory and when key already exists.
463 */
464 int
465dict_add_tv(dict_T *d, char *key, typval_T *tv)
466{
467 dictitem_T *item;
468
469 item = dictitem_alloc((char_u *)key);
470 if (item == NULL)
471 return FAIL;
472 copy_tv(tv, &item->di_tv);
473 if (dict_add(d, item) == FAIL)
474 {
475 dictitem_free(item);
476 return FAIL;
477 }
478 return OK;
479}
480
481/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200482 * Add a callback to dictionary "d".
483 * Returns FAIL when out of memory and when key already exists.
484 */
485 int
486dict_add_callback(dict_T *d, char *key, callback_T *cb)
487{
488 dictitem_T *item;
489
490 item = dictitem_alloc((char_u *)key);
491 if (item == NULL)
492 return FAIL;
493 put_callback(cb, &item->di_tv);
494 if (dict_add(d, item) == FAIL)
495 {
496 dictitem_free(item);
497 return FAIL;
498 }
499 return OK;
500}
501
502/*
Bram Moolenaar45e18cb2019-04-28 18:05:35 +0200503 * Initializes "iter" for iterating over dictionary items with
504 * dict_iterate_next().
505 * If "var" is not a Dict or an empty Dict then there will be nothing to
506 * iterate over, no error is given.
507 * NOTE: The dictionary must not change until iterating is finished!
508 */
509 void
510dict_iterate_start(typval_T *var, dict_iterator_T *iter)
511{
512 if (var->v_type != VAR_DICT || var->vval.v_dict == NULL)
513 iter->dit_todo = 0;
514 else
515 {
516 dict_T *d = var->vval.v_dict;
517
518 iter->dit_todo = d->dv_hashtab.ht_used;
519 iter->dit_hi = d->dv_hashtab.ht_array;
520 }
521}
522
523/*
524 * Iterate over the items referred to by "iter". It should be initialized with
525 * dict_iterate_start().
526 * Returns a pointer to the key.
527 * "*tv_result" is set to point to the value for that key.
528 * If there are no more items, NULL is returned.
529 */
530 char_u *
531dict_iterate_next(dict_iterator_T *iter, typval_T **tv_result)
532{
533 dictitem_T *di;
534 char_u *result;
535
536 if (iter->dit_todo == 0)
537 return NULL;
538
539 while (HASHITEM_EMPTY(iter->dit_hi))
540 ++iter->dit_hi;
541
542 di = HI2DI(iter->dit_hi);
543 result = di->di_key;
544 *tv_result = &di->di_tv;
545
546 --iter->dit_todo;
547 ++iter->dit_hi;
548 return result;
549}
550
551/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200552 * Add a dict entry to dictionary "d".
553 * Returns FAIL when out of memory and when key already exists.
554 */
555 int
556dict_add_dict(dict_T *d, char *key, dict_T *dict)
557{
558 dictitem_T *item;
559
560 item = dictitem_alloc((char_u *)key);
561 if (item == NULL)
562 return FAIL;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200563 item->di_tv.v_type = VAR_DICT;
564 item->di_tv.vval.v_dict = dict;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100565 ++dict->dv_refcount;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200566 if (dict_add(d, item) == FAIL)
567 {
568 dictitem_free(item);
569 return FAIL;
570 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200571 return OK;
572}
573
574/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200575 * Get the number of items in a Dictionary.
576 */
577 long
578dict_len(dict_T *d)
579{
580 if (d == NULL)
581 return 0L;
582 return (long)d->dv_hashtab.ht_used;
583}
584
585/*
586 * Find item "key[len]" in Dictionary "d".
587 * If "len" is negative use strlen(key).
588 * Returns NULL when not found.
589 */
590 dictitem_T *
591dict_find(dict_T *d, char_u *key, int len)
592{
593#define AKEYLEN 200
594 char_u buf[AKEYLEN];
595 char_u *akey;
596 char_u *tofree = NULL;
597 hashitem_T *hi;
598
599 if (d == NULL)
600 return NULL;
601 if (len < 0)
602 akey = key;
603 else if (len >= AKEYLEN)
604 {
605 tofree = akey = vim_strnsave(key, len);
606 if (akey == NULL)
607 return NULL;
608 }
609 else
610 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100611 // Avoid a malloc/free by using buf[].
Bram Moolenaarcd524592016-07-17 14:57:05 +0200612 vim_strncpy(buf, key, len);
613 akey = buf;
614 }
615
616 hi = hash_find(&d->dv_hashtab, akey);
617 vim_free(tofree);
618 if (HASHITEM_EMPTY(hi))
619 return NULL;
620 return HI2DI(hi);
621}
622
623/*
Bram Moolenaar08928322020-01-04 14:32:48 +0100624 * Get a typval_T item from a dictionary and copy it into "rettv".
625 * Returns FAIL if the entry doesn't exist or out of memory.
626 */
627 int
628dict_get_tv(dict_T *d, char_u *key, typval_T *rettv)
629{
630 dictitem_T *di;
Bram Moolenaar08928322020-01-04 14:32:48 +0100631
632 di = dict_find(d, key, -1);
633 if (di == NULL)
634 return FAIL;
635 copy_tv(&di->di_tv, rettv);
636 return OK;
637}
638
639/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200640 * Get a string item from a dictionary.
641 * When "save" is TRUE allocate memory for it.
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +0200642 * When FALSE a shared buffer is used, can only be used once!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200643 * Returns NULL if the entry doesn't exist or out of memory.
644 */
645 char_u *
Bram Moolenaar8f667172018-12-14 15:38:31 +0100646dict_get_string(dict_T *d, char_u *key, int save)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200647{
648 dictitem_T *di;
649 char_u *s;
650
651 di = dict_find(d, key, -1);
652 if (di == NULL)
653 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100654 s = tv_get_string(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200655 if (save && s != NULL)
656 s = vim_strsave(s);
657 return s;
658}
659
660/*
661 * Get a number item from a dictionary.
662 * Returns 0 if the entry doesn't exist.
663 */
664 varnumber_T
Bram Moolenaar8f667172018-12-14 15:38:31 +0100665dict_get_number(dict_T *d, char_u *key)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200666{
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200667 return dict_get_number_def(d, key, 0);
668}
669
670/*
671 * Get a number item from a dictionary.
672 * Returns "def" if the entry doesn't exist.
673 */
674 varnumber_T
675dict_get_number_def(dict_T *d, char_u *key, int def)
676{
Bram Moolenaarcd524592016-07-17 14:57:05 +0200677 dictitem_T *di;
678
679 di = dict_find(d, key, -1);
680 if (di == NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200681 return def;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100682 return tv_get_number(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200683}
684
685/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200686 * Get a number item from a dictionary.
687 * Returns 0 if the entry doesn't exist.
688 * Give an error if the entry is not a number.
689 */
690 varnumber_T
691dict_get_number_check(dict_T *d, char_u *key)
692{
693 dictitem_T *di;
694
695 di = dict_find(d, key, -1);
696 if (di == NULL)
697 return 0;
698 if (di->di_tv.v_type != VAR_NUMBER)
699 {
700 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
701 return 0;
702 }
703 return tv_get_number(&di->di_tv);
704}
705
706/*
Bram Moolenaar55881332020-08-18 13:04:15 +0200707 * Get a bool item (number or true/false) from a dictionary.
708 * Returns "def" if the entry doesn't exist.
709 */
710 varnumber_T
711dict_get_bool(dict_T *d, char_u *key, int def)
712{
713 dictitem_T *di;
714
715 di = dict_find(d, key, -1);
716 if (di == NULL)
717 return def;
718 return tv_get_bool(&di->di_tv);
719}
720
721/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200722 * Return an allocated string with the string representation of a Dictionary.
723 * May return NULL.
724 */
725 char_u *
726dict2string(typval_T *tv, int copyID, int restore_copyID)
727{
728 garray_T ga;
729 int first = TRUE;
730 char_u *tofree;
731 char_u numbuf[NUMBUFLEN];
732 hashitem_T *hi;
733 char_u *s;
734 dict_T *d;
735 int todo;
736
737 if ((d = tv->vval.v_dict) == NULL)
738 return NULL;
739 ga_init2(&ga, (int)sizeof(char), 80);
740 ga_append(&ga, '{');
741
742 todo = (int)d->dv_hashtab.ht_used;
743 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
744 {
745 if (!HASHITEM_EMPTY(hi))
746 {
747 --todo;
748
749 if (first)
750 first = FALSE;
751 else
752 ga_concat(&ga, (char_u *)", ");
753
754 tofree = string_quote(hi->hi_key, FALSE);
755 if (tofree != NULL)
756 {
757 ga_concat(&ga, tofree);
758 vim_free(tofree);
759 }
760 ga_concat(&ga, (char_u *)": ");
761 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
762 FALSE, restore_copyID, TRUE);
763 if (s != NULL)
764 ga_concat(&ga, s);
765 vim_free(tofree);
766 if (s == NULL || did_echo_string_emsg)
767 break;
768 line_breakcheck();
769
770 }
771 }
772 if (todo > 0)
773 {
774 vim_free(ga.ga_data);
775 return NULL;
776 }
777
778 ga_append(&ga, '}');
779 ga_append(&ga, NUL);
780 return (char_u *)ga.ga_data;
781}
782
783/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +0200784 * Get the key for #{key: val} into "tv" and advance "arg".
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200785 * Return FAIL when there is no valid key.
786 */
787 static int
788get_literal_key(char_u **arg, typval_T *tv)
789{
790 char_u *p;
791
792 if (!ASCII_ISALNUM(**arg) && **arg != '_' && **arg != '-')
793 return FAIL;
794
795 for (p = *arg; ASCII_ISALNUM(*p) || *p == '_' || *p == '-'; ++p)
796 ;
797 tv->v_type = VAR_STRING;
Bram Moolenaardf44a272020-06-07 20:49:05 +0200798 tv->vval.v_string = vim_strnsave(*arg, p - *arg);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200799
Bram Moolenaardb199212020-08-12 18:01:53 +0200800 *arg = p;
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200801 return OK;
802}
803
804/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200805 * Allocate a variable for a Dictionary and fill it from "*arg".
Bram Moolenaar962d7212020-07-04 14:15:00 +0200806 * "*arg" points to the "{".
Bram Moolenaar9f9fe372019-07-27 23:12:12 +0200807 * "literal" is TRUE for #{key: val}
Bram Moolenaarcd524592016-07-17 14:57:05 +0200808 * Return OK or FAIL. Returns NOTDONE for {expr}.
809 */
810 int
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200811eval_dict(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int literal)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200812{
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200813 int evaluate = evalarg == NULL ? FALSE
814 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200815 dict_T *d = NULL;
816 typval_T tvkey;
817 typval_T tv;
818 char_u *key = NULL;
819 dictitem_T *item;
820 char_u *start = skipwhite(*arg + 1);
821 char_u buf[NUMBUFLEN];
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200822 int vim9script = in_vim9script();
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200823 int had_comma;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200824
Bram Moolenaarcd524592016-07-17 14:57:05 +0200825 /*
826 * First check if it's not a curly-braces thing: {expr}.
827 * Must do this without evaluating, otherwise a function may be called
828 * twice. Unfortunately this means we need to call eval1() twice for the
829 * first item.
830 * But {} is an empty Dictionary.
831 */
Bram Moolenaar32e35112020-05-14 22:41:15 +0200832 if (!vim9script && *start != '}')
Bram Moolenaarcd524592016-07-17 14:57:05 +0200833 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200834 if (eval1(&start, &tv, NULL) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200835 return FAIL;
Bram Moolenaarae95a392020-08-06 16:38:12 +0200836 if (*skipwhite(start) == '}')
Bram Moolenaarcd524592016-07-17 14:57:05 +0200837 return NOTDONE;
838 }
839
840 if (evaluate)
841 {
842 d = dict_alloc();
843 if (d == NULL)
844 return FAIL;
845 }
846 tvkey.v_type = VAR_UNKNOWN;
847 tv.v_type = VAR_UNKNOWN;
848
Bram Moolenaar962d7212020-07-04 14:15:00 +0200849 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200850 while (**arg != '}' && **arg != NUL)
851 {
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200852 if ((literal
853 ? get_literal_key(arg, &tvkey)
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200854 : eval1(arg, &tvkey, evalarg)) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200855 goto failret;
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200856
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +0200857 // the colon should come right after the key, but this wasn't checked
Bram Moolenaar9d489562020-07-30 20:08:50 +0200858 // previously, so only require it in Vim9 script.
859 if (!vim9script)
860 *arg = skipwhite(*arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200861 if (**arg != ':')
862 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +0100863 if (evaluate)
Bram Moolenaardb199212020-08-12 18:01:53 +0200864 {
865 if (*skipwhite(*arg) == ':')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200866 semsg(_(e_no_white_space_allowed_before_str), ":");
Bram Moolenaardb199212020-08-12 18:01:53 +0200867 else
868 semsg(_(e_missing_dict_colon), *arg);
869 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200870 clear_tv(&tvkey);
871 goto failret;
872 }
873 if (evaluate)
874 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100875 key = tv_get_string_buf_chk(&tvkey, buf);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200876 if (key == NULL)
877 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100878 // "key" is NULL when tv_get_string_buf_chk() gave an errmsg
Bram Moolenaarcd524592016-07-17 14:57:05 +0200879 clear_tv(&tvkey);
880 goto failret;
881 }
882 }
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200883 if (vim9script && (*arg)[1] != NUL && !VIM_ISWHITE((*arg)[1]))
884 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200885 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaarab19d492020-06-27 17:04:05 +0200886 clear_tv(&tvkey);
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200887 goto failret;
888 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200889
Bram Moolenaar962d7212020-07-04 14:15:00 +0200890 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200891 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200892 {
893 if (evaluate)
894 clear_tv(&tvkey);
895 goto failret;
896 }
897 if (evaluate)
898 {
899 item = dict_find(d, key, -1);
900 if (item != NULL)
901 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +0100902 if (evaluate)
903 semsg(_(e_duplicate_key), key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200904 clear_tv(&tvkey);
905 clear_tv(&tv);
906 goto failret;
907 }
908 item = dictitem_alloc(key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200909 if (item != NULL)
910 {
911 item->di_tv = tv;
912 item->di_tv.v_lock = 0;
913 if (dict_add(d, item) == FAIL)
914 dictitem_free(item);
915 }
916 }
Bram Moolenaara8931942019-09-28 17:25:10 +0200917 clear_tv(&tvkey);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200918
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +0200919 // the comma should come right after the value, but this wasn't checked
920 // previously, so only require it in Vim9 script.
921 if (!vim9script)
922 *arg = skipwhite(*arg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200923 had_comma = **arg == ',';
924 if (had_comma)
925 {
926 if (vim9script && (*arg)[1] != NUL && !VIM_ISWHITE((*arg)[1]))
927 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200928 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200929 goto failret;
930 }
931 *arg = skipwhite(*arg + 1);
932 }
933
934 // the "}" can be on the next line
Bram Moolenaar962d7212020-07-04 14:15:00 +0200935 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200936 if (**arg == '}')
937 break;
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200938 if (!had_comma)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200939 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +0100940 if (evaluate)
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +0200941 {
942 if (**arg == ',')
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200943 semsg(_(e_no_white_space_allowed_before_str), ",");
Bram Moolenaarc3d6e8a2020-08-12 18:34:28 +0200944 else
945 semsg(_(e_missing_dict_comma), *arg);
946 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200947 goto failret;
948 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200949 }
950
951 if (**arg != '}')
952 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +0100953 if (evaluate)
954 semsg(_(e_missing_dict_end), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200955failret:
Bram Moolenaaradc67142019-06-22 01:40:42 +0200956 if (d != NULL)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200957 dict_free(d);
958 return FAIL;
959 }
960
961 *arg = skipwhite(*arg + 1);
962 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200963 rettv_dict_set(rettv, d);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200964
965 return OK;
966}
967
968/*
969 * Go over all entries in "d2" and add them to "d1".
970 * When "action" is "error" then a duplicate key is an error.
971 * When "action" is "force" then a duplicate key is overwritten.
972 * Otherwise duplicate keys are ignored ("action" is "keep").
973 */
974 void
975dict_extend(dict_T *d1, dict_T *d2, char_u *action)
976{
977 dictitem_T *di1;
978 hashitem_T *hi2;
979 int todo;
980 char_u *arg_errmsg = (char_u *)N_("extend() argument");
981
982 todo = (int)d2->dv_hashtab.ht_used;
983 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
984 {
985 if (!HASHITEM_EMPTY(hi2))
986 {
987 --todo;
988 di1 = dict_find(d1, hi2->hi_key, -1);
989 if (d1->dv_scope != 0)
990 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100991 // Disallow replacing a builtin function in l: and g:.
992 // Check the key to be valid when adding to any scope.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200993 if (d1->dv_scope == VAR_DEF_SCOPE
994 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +0200995 && var_wrong_func_name(hi2->hi_key, di1 == NULL))
Bram Moolenaarcd524592016-07-17 14:57:05 +0200996 break;
997 if (!valid_varname(hi2->hi_key))
998 break;
999 }
1000 if (di1 == NULL)
1001 {
1002 di1 = dictitem_copy(HI2DI(hi2));
1003 if (di1 != NULL && dict_add(d1, di1) == FAIL)
1004 dictitem_free(di1);
1005 }
1006 else if (*action == 'e')
1007 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001008 semsg(_("E737: Key already exists: %s"), hi2->hi_key);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001009 break;
1010 }
1011 else if (*action == 'f' && HI2DI(hi2) != di1)
1012 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001013 if (value_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001014 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaarcd524592016-07-17 14:57:05 +02001015 break;
1016 clear_tv(&di1->di_tv);
1017 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
1018 }
1019 }
1020 }
1021}
1022
1023/*
1024 * Return the dictitem that an entry in a hashtable points to.
1025 */
1026 dictitem_T *
1027dict_lookup(hashitem_T *hi)
1028{
1029 return HI2DI(hi);
1030}
1031
1032/*
1033 * Return TRUE when two dictionaries have exactly the same key/values.
1034 */
1035 int
1036dict_equal(
1037 dict_T *d1,
1038 dict_T *d2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001039 int ic, // ignore case for strings
1040 int recursive) // TRUE when used recursively
Bram Moolenaarcd524592016-07-17 14:57:05 +02001041{
1042 hashitem_T *hi;
1043 dictitem_T *item2;
1044 int todo;
1045
Bram Moolenaarcd524592016-07-17 14:57:05 +02001046 if (d1 == d2)
1047 return TRUE;
1048 if (dict_len(d1) != dict_len(d2))
1049 return FALSE;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001050 if (dict_len(d1) == 0)
1051 // empty and NULL dicts are considered equal
1052 return TRUE;
1053 if (d1 == NULL || d2 == NULL)
1054 return FALSE;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001055
1056 todo = (int)d1->dv_hashtab.ht_used;
1057 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
1058 {
1059 if (!HASHITEM_EMPTY(hi))
1060 {
1061 item2 = dict_find(d2, hi->hi_key, -1);
1062 if (item2 == NULL)
1063 return FALSE;
1064 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
1065 return FALSE;
1066 --todo;
1067 }
1068 }
1069 return TRUE;
1070}
1071
1072/*
1073 * Turn a dict into a list:
1074 * "what" == 0: list of keys
1075 * "what" == 1: list of values
1076 * "what" == 2: list of items
1077 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001078 static void
Bram Moolenaarcd524592016-07-17 14:57:05 +02001079dict_list(typval_T *argvars, typval_T *rettv, int what)
1080{
1081 list_T *l2;
1082 dictitem_T *di;
1083 hashitem_T *hi;
1084 listitem_T *li;
1085 listitem_T *li2;
1086 dict_T *d;
1087 int todo;
1088
1089 if (argvars[0].v_type != VAR_DICT)
1090 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001091 emsg(_(e_dictreq));
Bram Moolenaarcd524592016-07-17 14:57:05 +02001092 return;
1093 }
1094 if ((d = argvars[0].vval.v_dict) == NULL)
1095 return;
1096
1097 if (rettv_list_alloc(rettv) == FAIL)
1098 return;
1099
1100 todo = (int)d->dv_hashtab.ht_used;
1101 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1102 {
1103 if (!HASHITEM_EMPTY(hi))
1104 {
1105 --todo;
1106 di = HI2DI(hi);
1107
1108 li = listitem_alloc();
1109 if (li == NULL)
1110 break;
1111 list_append(rettv->vval.v_list, li);
1112
1113 if (what == 0)
1114 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001115 // keys()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001116 li->li_tv.v_type = VAR_STRING;
1117 li->li_tv.v_lock = 0;
1118 li->li_tv.vval.v_string = vim_strsave(di->di_key);
1119 }
1120 else if (what == 1)
1121 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001122 // values()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001123 copy_tv(&di->di_tv, &li->li_tv);
1124 }
1125 else
1126 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001127 // items()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001128 l2 = list_alloc();
1129 li->li_tv.v_type = VAR_LIST;
1130 li->li_tv.v_lock = 0;
1131 li->li_tv.vval.v_list = l2;
1132 if (l2 == NULL)
1133 break;
1134 ++l2->lv_refcount;
1135
1136 li2 = listitem_alloc();
1137 if (li2 == NULL)
1138 break;
1139 list_append(l2, li2);
1140 li2->li_tv.v_type = VAR_STRING;
1141 li2->li_tv.v_lock = 0;
1142 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
1143
1144 li2 = listitem_alloc();
1145 if (li2 == NULL)
1146 break;
1147 list_append(l2, li2);
1148 copy_tv(&di->di_tv, &li2->li_tv);
1149 }
1150 }
1151 }
1152}
1153
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001154/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001155 * "items(dict)" function
1156 */
1157 void
1158f_items(typval_T *argvars, typval_T *rettv)
1159{
1160 dict_list(argvars, rettv, 2);
1161}
1162
1163/*
1164 * "keys()" function
1165 */
1166 void
1167f_keys(typval_T *argvars, typval_T *rettv)
1168{
1169 dict_list(argvars, rettv, 0);
1170}
1171
1172/*
1173 * "values(dict)" function
1174 */
1175 void
1176f_values(typval_T *argvars, typval_T *rettv)
1177{
1178 dict_list(argvars, rettv, 1);
1179}
1180
1181/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001182 * Make each item in the dict readonly (not the value of the item).
1183 */
1184 void
1185dict_set_items_ro(dict_T *di)
1186{
1187 int todo = (int)di->dv_hashtab.ht_used;
1188 hashitem_T *hi;
1189
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001190 // Set readonly
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001191 for (hi = di->dv_hashtab.ht_array; todo > 0 ; ++hi)
1192 {
1193 if (HASHITEM_EMPTY(hi))
1194 continue;
1195 --todo;
1196 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
1197 }
1198}
1199
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001200/*
1201 * "has_key()" function
1202 */
1203 void
1204f_has_key(typval_T *argvars, typval_T *rettv)
1205{
1206 if (argvars[0].v_type != VAR_DICT)
1207 {
1208 emsg(_(e_dictreq));
1209 return;
1210 }
1211 if (argvars[0].vval.v_dict == NULL)
1212 return;
1213
1214 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
1215 tv_get_string(&argvars[1]), -1) != NULL;
1216}
1217
1218/*
1219 * "remove({dict})" function
1220 */
1221 void
1222dict_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1223{
1224 dict_T *d;
1225 char_u *key;
1226 dictitem_T *di;
1227
1228 if (argvars[2].v_type != VAR_UNKNOWN)
1229 semsg(_(e_toomanyarg), "remove()");
1230 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001231 && !value_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001232 {
1233 key = tv_get_string_chk(&argvars[1]);
1234 if (key != NULL)
1235 {
1236 di = dict_find(d, key, -1);
1237 if (di == NULL)
1238 semsg(_(e_dictkey), key);
1239 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
1240 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
1241 {
1242 *rettv = di->di_tv;
1243 init_tv(&di->di_tv);
1244 dictitem_remove(d, di);
1245 }
1246 }
1247 }
1248}
1249
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001250#endif // defined(FEAT_EVAL)