blob: b3ef35a8022c0c4355895c1b29de8258e030b968 [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.
Bram Moolenaar22286892020-11-05 20:50:51 +010025 * Caller should take care of the reference count.
Bram Moolenaarcd524592016-07-17 14:57:05 +020026 */
27 dict_T *
28dict_alloc(void)
29{
30 dict_T *d;
31
Bram Moolenaaradc67142019-06-22 01:40:42 +020032 d = ALLOC_CLEAR_ONE(dict_T);
Bram Moolenaarcd524592016-07-17 14:57:05 +020033 if (d != NULL)
34 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010035 // Add the dict to the list of dicts for garbage collection.
Bram Moolenaarcd524592016-07-17 14:57:05 +020036 if (first_dict != NULL)
37 first_dict->dv_used_prev = d;
38 d->dv_used_next = first_dict;
39 d->dv_used_prev = NULL;
40 first_dict = d;
41
42 hash_init(&d->dv_hashtab);
43 d->dv_lock = 0;
44 d->dv_scope = 0;
45 d->dv_refcount = 0;
46 d->dv_copyID = 0;
47 }
48 return d;
49}
50
Bram Moolenaarf49cc602018-11-11 15:21:05 +010051/*
52 * dict_alloc() with an ID for alloc_fail().
53 */
54 dict_T *
55dict_alloc_id(alloc_id_T id UNUSED)
56{
57#ifdef FEAT_EVAL
Bram Moolenaar51e14382019-05-25 20:21:28 +020058 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
Bram Moolenaarf49cc602018-11-11 15:21:05 +010059 return NULL;
60#endif
61 return (dict_alloc());
62}
63
Bram Moolenaar7e1652c2017-12-16 18:27:02 +010064 dict_T *
65dict_alloc_lock(int lock)
66{
67 dict_T *d = dict_alloc();
68
69 if (d != NULL)
70 d->dv_lock = lock;
71 return d;
72}
73
Bram Moolenaarcd524592016-07-17 14:57:05 +020074/*
75 * Allocate an empty dict for a return value.
76 * Returns OK or FAIL.
77 */
78 int
79rettv_dict_alloc(typval_T *rettv)
80{
Bram Moolenaar7e1652c2017-12-16 18:27:02 +010081 dict_T *d = dict_alloc_lock(0);
Bram Moolenaarcd524592016-07-17 14:57:05 +020082
83 if (d == NULL)
84 return FAIL;
85
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020086 rettv_dict_set(rettv, d);
Bram Moolenaarcd524592016-07-17 14:57:05 +020087 return OK;
88}
89
90/*
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020091 * Set a dictionary as the return value
92 */
93 void
94rettv_dict_set(typval_T *rettv, dict_T *d)
95{
96 rettv->v_type = VAR_DICT;
97 rettv->vval.v_dict = d;
98 if (d != NULL)
99 ++d->dv_refcount;
100}
101
102/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200103 * Free a Dictionary, including all non-container items it contains.
104 * Ignores the reference count.
105 */
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100106 void
Bram Moolenaarcd524592016-07-17 14:57:05 +0200107dict_free_contents(dict_T *d)
108{
Bram Moolenaar89483d42020-05-10 15:24:44 +0200109 hashtab_free_contents(&d->dv_hashtab);
110}
111
112/*
113 * Clear hashtab "ht" and dict items it contains.
114 */
115 void
116hashtab_free_contents(hashtab_T *ht)
117{
Bram Moolenaarcd524592016-07-17 14:57:05 +0200118 int todo;
119 hashitem_T *hi;
120 dictitem_T *di;
121
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100122 // Lock the hashtab, we don't want it to resize while freeing items.
Bram Moolenaar89483d42020-05-10 15:24:44 +0200123 hash_lock(ht);
124 todo = (int)ht->ht_used;
125 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200126 {
127 if (!HASHITEM_EMPTY(hi))
128 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100129 // Remove the item before deleting it, just in case there is
130 // something recursive causing trouble.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200131 di = HI2DI(hi);
Bram Moolenaar89483d42020-05-10 15:24:44 +0200132 hash_remove(ht, hi);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100133 dictitem_free(di);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200134 --todo;
135 }
136 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100137
Bram Moolenaar89483d42020-05-10 15:24:44 +0200138 // The hashtab is still locked, it has to be re-initialized anyway.
139 hash_clear(ht);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200140}
141
142 static void
143dict_free_dict(dict_T *d)
144{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100145 // Remove the dict from the list of dicts for garbage collection.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200146 if (d->dv_used_prev == NULL)
147 first_dict = d->dv_used_next;
148 else
149 d->dv_used_prev->dv_used_next = d->dv_used_next;
150 if (d->dv_used_next != NULL)
151 d->dv_used_next->dv_used_prev = d->dv_used_prev;
152 vim_free(d);
153}
154
155 static void
156dict_free(dict_T *d)
157{
158 if (!in_free_unref_items)
159 {
160 dict_free_contents(d);
161 dict_free_dict(d);
162 }
163}
164
165/*
166 * Unreference a Dictionary: decrement the reference count and free it when it
167 * becomes zero.
168 */
169 void
170dict_unref(dict_T *d)
171{
172 if (d != NULL && --d->dv_refcount <= 0)
173 dict_free(d);
174}
175
176/*
177 * Go through the list of dicts and free items without the copyID.
178 * Returns TRUE if something was freed.
179 */
180 int
181dict_free_nonref(int copyID)
182{
183 dict_T *dd;
184 int did_free = FALSE;
185
186 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
187 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
188 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100189 // Free the Dictionary and ordinary items it contains, but don't
190 // recurse into Lists and Dictionaries, they will be in the list
191 // of dicts or list of lists.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200192 dict_free_contents(dd);
193 did_free = TRUE;
194 }
195 return did_free;
196}
197
198 void
199dict_free_items(int copyID)
200{
201 dict_T *dd, *dd_next;
202
203 for (dd = first_dict; dd != NULL; dd = dd_next)
204 {
205 dd_next = dd->dv_used_next;
206 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
207 dict_free_dict(dd);
208 }
209}
210
211/*
212 * Allocate a Dictionary item.
213 * The "key" is copied to the new item.
Bram Moolenaarc89d4b32018-07-08 17:19:02 +0200214 * Note that the type and value of the item "di_tv" still needs to be
215 * initialized!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200216 * Returns NULL when out of memory.
217 */
218 dictitem_T *
219dictitem_alloc(char_u *key)
220{
221 dictitem_T *di;
222
Bram Moolenaarb59e7352019-08-07 21:42:24 +0200223 di = alloc(offsetof(dictitem_T, di_key) + STRLEN(key) + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200224 if (di != NULL)
225 {
226 STRCPY(di->di_key, key);
227 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaarc89d4b32018-07-08 17:19:02 +0200228 di->di_tv.v_lock = 0;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200229 }
230 return di;
231}
232
233/*
234 * Make a copy of a Dictionary item.
235 */
236 static dictitem_T *
237dictitem_copy(dictitem_T *org)
238{
239 dictitem_T *di;
Bram Moolenaar3f974ff2020-10-02 18:11:56 +0200240 size_t len = STRLEN(org->di_key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200241
Bram Moolenaar3f974ff2020-10-02 18:11:56 +0200242 di = alloc(offsetof(dictitem_T, di_key) + len + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200243 if (di != NULL)
244 {
Bram Moolenaar3f974ff2020-10-02 18:11:56 +0200245 mch_memmove(di->di_key, org->di_key, len + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200246 di->di_flags = DI_FLAGS_ALLOC;
247 copy_tv(&org->di_tv, &di->di_tv);
248 }
249 return di;
250}
251
252/*
253 * Remove item "item" from Dictionary "dict" and free it.
254 */
255 void
256dictitem_remove(dict_T *dict, dictitem_T *item)
257{
258 hashitem_T *hi;
259
260 hi = hash_find(&dict->dv_hashtab, item->di_key);
261 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100262 internal_error("dictitem_remove()");
Bram Moolenaarcd524592016-07-17 14:57:05 +0200263 else
264 hash_remove(&dict->dv_hashtab, hi);
265 dictitem_free(item);
266}
267
268/*
269 * Free a dict item. Also clears the value.
270 */
271 void
272dictitem_free(dictitem_T *item)
273{
274 clear_tv(&item->di_tv);
275 if (item->di_flags & DI_FLAGS_ALLOC)
276 vim_free(item);
277}
278
279/*
280 * Make a copy of dict "d". Shallow if "deep" is FALSE.
281 * The refcount of the new dict is set to 1.
282 * See item_copy() for "copyID".
283 * Returns NULL when out of memory.
284 */
285 dict_T *
286dict_copy(dict_T *orig, int deep, int copyID)
287{
288 dict_T *copy;
289 dictitem_T *di;
290 int todo;
291 hashitem_T *hi;
292
293 if (orig == NULL)
294 return NULL;
295
296 copy = dict_alloc();
297 if (copy != NULL)
298 {
299 if (copyID != 0)
300 {
301 orig->dv_copyID = copyID;
302 orig->dv_copydict = copy;
303 }
304 todo = (int)orig->dv_hashtab.ht_used;
305 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
306 {
307 if (!HASHITEM_EMPTY(hi))
308 {
309 --todo;
310
311 di = dictitem_alloc(hi->hi_key);
312 if (di == NULL)
313 break;
314 if (deep)
315 {
316 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
317 copyID) == FAIL)
318 {
319 vim_free(di);
320 break;
321 }
322 }
323 else
324 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
325 if (dict_add(copy, di) == FAIL)
326 {
327 dictitem_free(di);
328 break;
329 }
330 }
331 }
332
333 ++copy->dv_refcount;
334 if (todo > 0)
335 {
336 dict_unref(copy);
337 copy = NULL;
338 }
339 }
340
341 return copy;
342}
343
344/*
345 * Add item "item" to Dictionary "d".
346 * Returns FAIL when out of memory and when key already exists.
347 */
348 int
349dict_add(dict_T *d, dictitem_T *item)
350{
351 return hash_add(&d->dv_hashtab, item->di_key);
352}
353
354/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200355 * Add a number or special entry to dictionary "d".
Bram Moolenaarcd524592016-07-17 14:57:05 +0200356 * Returns FAIL when out of memory and when key already exists.
357 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200358 static int
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100359dict_add_number_special(dict_T *d, char *key, varnumber_T nr, vartype_T vartype)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200360{
361 dictitem_T *item;
362
363 item = dictitem_alloc((char_u *)key);
364 if (item == NULL)
365 return FAIL;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100366 item->di_tv.v_type = vartype;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200367 item->di_tv.vval.v_number = nr;
368 if (dict_add(d, item) == FAIL)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200369 {
Bram Moolenaare0be1672018-07-08 16:50:37 +0200370 dictitem_free(item);
371 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200372 }
Bram Moolenaare0be1672018-07-08 16:50:37 +0200373 return OK;
374}
375
376/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200377 * Add a number entry to dictionary "d".
378 * Returns FAIL when out of memory and when key already exists.
379 */
380 int
381dict_add_number(dict_T *d, char *key, varnumber_T nr)
382{
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100383 return dict_add_number_special(d, key, nr, VAR_NUMBER);
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200384}
385
386/*
387 * Add a special entry to dictionary "d".
388 * Returns FAIL when out of memory and when key already exists.
389 */
390 int
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100391dict_add_bool(dict_T *d, char *key, varnumber_T nr)
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200392{
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100393 return dict_add_number_special(d, key, nr, VAR_BOOL);
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200394}
395
396/*
Bram Moolenaare0be1672018-07-08 16:50:37 +0200397 * Add a string entry to dictionary "d".
398 * Returns FAIL when out of memory and when key already exists.
399 */
400 int
401dict_add_string(dict_T *d, char *key, char_u *str)
402{
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100403 return dict_add_string_len(d, key, str, -1);
404}
405
406/*
407 * Add a string entry to dictionary "d".
408 * "str" will be copied to allocated memory.
409 * When "len" is -1 use the whole string, otherwise only this many bytes.
410 * Returns FAIL when out of memory and when key already exists.
411 */
412 int
413dict_add_string_len(dict_T *d, char *key, char_u *str, int len)
414{
Bram Moolenaare0be1672018-07-08 16:50:37 +0200415 dictitem_T *item;
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100416 char_u *val = NULL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200417
418 item = dictitem_alloc((char_u *)key);
419 if (item == NULL)
420 return FAIL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200421 item->di_tv.v_type = VAR_STRING;
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100422 if (str != NULL)
423 {
424 if (len == -1)
425 val = vim_strsave(str);
426 else
427 val = vim_strnsave(str, len);
428 }
429 item->di_tv.vval.v_string = val;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200430 if (dict_add(d, item) == FAIL)
431 {
432 dictitem_free(item);
433 return FAIL;
434 }
435 return OK;
436}
437
438/*
439 * Add a list entry to dictionary "d".
440 * Returns FAIL when out of memory and when key already exists.
441 */
442 int
443dict_add_list(dict_T *d, char *key, list_T *list)
444{
445 dictitem_T *item;
446
447 item = dictitem_alloc((char_u *)key);
448 if (item == NULL)
449 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200450 item->di_tv.v_type = VAR_LIST;
451 item->di_tv.vval.v_list = list;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100452 ++list->lv_refcount;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200453 if (dict_add(d, item) == FAIL)
454 {
455 dictitem_free(item);
456 return FAIL;
457 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200458 return OK;
459}
460
461/*
Bram Moolenaar08928322020-01-04 14:32:48 +0100462 * Add a typval_T entry to dictionary "d".
463 * Returns FAIL when out of memory and when key already exists.
464 */
465 int
466dict_add_tv(dict_T *d, char *key, typval_T *tv)
467{
468 dictitem_T *item;
469
470 item = dictitem_alloc((char_u *)key);
471 if (item == NULL)
472 return FAIL;
473 copy_tv(tv, &item->di_tv);
474 if (dict_add(d, item) == FAIL)
475 {
476 dictitem_free(item);
477 return FAIL;
478 }
479 return OK;
480}
481
482/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200483 * Add a callback to dictionary "d".
484 * Returns FAIL when out of memory and when key already exists.
485 */
486 int
487dict_add_callback(dict_T *d, char *key, callback_T *cb)
488{
489 dictitem_T *item;
490
491 item = dictitem_alloc((char_u *)key);
492 if (item == NULL)
493 return FAIL;
494 put_callback(cb, &item->di_tv);
495 if (dict_add(d, item) == FAIL)
496 {
497 dictitem_free(item);
498 return FAIL;
499 }
500 return OK;
501}
502
503/*
Bram Moolenaar45e18cb2019-04-28 18:05:35 +0200504 * Initializes "iter" for iterating over dictionary items with
505 * dict_iterate_next().
506 * If "var" is not a Dict or an empty Dict then there will be nothing to
507 * iterate over, no error is given.
508 * NOTE: The dictionary must not change until iterating is finished!
509 */
510 void
511dict_iterate_start(typval_T *var, dict_iterator_T *iter)
512{
513 if (var->v_type != VAR_DICT || var->vval.v_dict == NULL)
514 iter->dit_todo = 0;
515 else
516 {
517 dict_T *d = var->vval.v_dict;
518
519 iter->dit_todo = d->dv_hashtab.ht_used;
520 iter->dit_hi = d->dv_hashtab.ht_array;
521 }
522}
523
524/*
525 * Iterate over the items referred to by "iter". It should be initialized with
526 * dict_iterate_start().
527 * Returns a pointer to the key.
528 * "*tv_result" is set to point to the value for that key.
529 * If there are no more items, NULL is returned.
530 */
531 char_u *
532dict_iterate_next(dict_iterator_T *iter, typval_T **tv_result)
533{
534 dictitem_T *di;
535 char_u *result;
536
537 if (iter->dit_todo == 0)
538 return NULL;
539
540 while (HASHITEM_EMPTY(iter->dit_hi))
541 ++iter->dit_hi;
542
543 di = HI2DI(iter->dit_hi);
544 result = di->di_key;
545 *tv_result = &di->di_tv;
546
547 --iter->dit_todo;
548 ++iter->dit_hi;
549 return result;
550}
551
552/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200553 * Add a dict entry to dictionary "d".
554 * Returns FAIL when out of memory and when key already exists.
555 */
556 int
557dict_add_dict(dict_T *d, char *key, dict_T *dict)
558{
559 dictitem_T *item;
560
561 item = dictitem_alloc((char_u *)key);
562 if (item == NULL)
563 return FAIL;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200564 item->di_tv.v_type = VAR_DICT;
565 item->di_tv.vval.v_dict = dict;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100566 ++dict->dv_refcount;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200567 if (dict_add(d, item) == FAIL)
568 {
569 dictitem_free(item);
570 return FAIL;
571 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200572 return OK;
573}
574
575/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200576 * Get the number of items in a Dictionary.
577 */
578 long
579dict_len(dict_T *d)
580{
581 if (d == NULL)
582 return 0L;
583 return (long)d->dv_hashtab.ht_used;
584}
585
586/*
587 * Find item "key[len]" in Dictionary "d".
588 * If "len" is negative use strlen(key).
589 * Returns NULL when not found.
590 */
591 dictitem_T *
592dict_find(dict_T *d, char_u *key, int len)
593{
594#define AKEYLEN 200
595 char_u buf[AKEYLEN];
596 char_u *akey;
597 char_u *tofree = NULL;
598 hashitem_T *hi;
599
600 if (d == NULL)
601 return NULL;
602 if (len < 0)
603 akey = key;
604 else if (len >= AKEYLEN)
605 {
606 tofree = akey = vim_strnsave(key, len);
607 if (akey == NULL)
608 return NULL;
609 }
610 else
611 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100612 // Avoid a malloc/free by using buf[].
Bram Moolenaarcd524592016-07-17 14:57:05 +0200613 vim_strncpy(buf, key, len);
614 akey = buf;
615 }
616
617 hi = hash_find(&d->dv_hashtab, akey);
618 vim_free(tofree);
619 if (HASHITEM_EMPTY(hi))
620 return NULL;
621 return HI2DI(hi);
622}
623
624/*
Bram Moolenaar08928322020-01-04 14:32:48 +0100625 * Get a typval_T item from a dictionary and copy it into "rettv".
626 * Returns FAIL if the entry doesn't exist or out of memory.
627 */
628 int
629dict_get_tv(dict_T *d, char_u *key, typval_T *rettv)
630{
631 dictitem_T *di;
Bram Moolenaar08928322020-01-04 14:32:48 +0100632
633 di = dict_find(d, key, -1);
634 if (di == NULL)
635 return FAIL;
636 copy_tv(&di->di_tv, rettv);
637 return OK;
638}
639
640/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200641 * Get a string item from a dictionary.
642 * When "save" is TRUE allocate memory for it.
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +0200643 * When FALSE a shared buffer is used, can only be used once!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200644 * Returns NULL if the entry doesn't exist or out of memory.
645 */
646 char_u *
Bram Moolenaar8f667172018-12-14 15:38:31 +0100647dict_get_string(dict_T *d, char_u *key, int save)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200648{
649 dictitem_T *di;
650 char_u *s;
651
652 di = dict_find(d, key, -1);
653 if (di == NULL)
654 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100655 s = tv_get_string(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200656 if (save && s != NULL)
657 s = vim_strsave(s);
658 return s;
659}
660
661/*
662 * Get a number item from a dictionary.
663 * Returns 0 if the entry doesn't exist.
664 */
665 varnumber_T
Bram Moolenaar8f667172018-12-14 15:38:31 +0100666dict_get_number(dict_T *d, char_u *key)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200667{
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200668 return dict_get_number_def(d, key, 0);
669}
670
671/*
672 * Get a number item from a dictionary.
673 * Returns "def" if the entry doesn't exist.
674 */
675 varnumber_T
676dict_get_number_def(dict_T *d, char_u *key, int def)
677{
Bram Moolenaarcd524592016-07-17 14:57:05 +0200678 dictitem_T *di;
679
680 di = dict_find(d, key, -1);
681 if (di == NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200682 return def;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100683 return tv_get_number(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200684}
685
686/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200687 * Get a number item from a dictionary.
688 * Returns 0 if the entry doesn't exist.
689 * Give an error if the entry is not a number.
690 */
691 varnumber_T
692dict_get_number_check(dict_T *d, char_u *key)
693{
694 dictitem_T *di;
695
696 di = dict_find(d, key, -1);
697 if (di == NULL)
698 return 0;
699 if (di->di_tv.v_type != VAR_NUMBER)
700 {
701 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
702 return 0;
703 }
704 return tv_get_number(&di->di_tv);
705}
706
707/*
Bram Moolenaar55881332020-08-18 13:04:15 +0200708 * Get a bool item (number or true/false) from a dictionary.
709 * Returns "def" if the entry doesn't exist.
710 */
711 varnumber_T
712dict_get_bool(dict_T *d, char_u *key, int def)
713{
714 dictitem_T *di;
715
716 di = dict_find(d, key, -1);
717 if (di == NULL)
718 return def;
719 return tv_get_bool(&di->di_tv);
720}
721
722/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200723 * Return an allocated string with the string representation of a Dictionary.
724 * May return NULL.
725 */
726 char_u *
727dict2string(typval_T *tv, int copyID, int restore_copyID)
728{
729 garray_T ga;
730 int first = TRUE;
731 char_u *tofree;
732 char_u numbuf[NUMBUFLEN];
733 hashitem_T *hi;
734 char_u *s;
735 dict_T *d;
736 int todo;
737
738 if ((d = tv->vval.v_dict) == NULL)
739 return NULL;
740 ga_init2(&ga, (int)sizeof(char), 80);
741 ga_append(&ga, '{');
742
743 todo = (int)d->dv_hashtab.ht_used;
744 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
745 {
746 if (!HASHITEM_EMPTY(hi))
747 {
748 --todo;
749
750 if (first)
751 first = FALSE;
752 else
753 ga_concat(&ga, (char_u *)", ");
754
755 tofree = string_quote(hi->hi_key, FALSE);
756 if (tofree != NULL)
757 {
758 ga_concat(&ga, tofree);
759 vim_free(tofree);
760 }
761 ga_concat(&ga, (char_u *)": ");
762 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
763 FALSE, restore_copyID, TRUE);
764 if (s != NULL)
765 ga_concat(&ga, s);
766 vim_free(tofree);
767 if (s == NULL || did_echo_string_emsg)
768 break;
769 line_breakcheck();
770
771 }
772 }
773 if (todo > 0)
774 {
775 vim_free(ga.ga_data);
776 return NULL;
777 }
778
779 ga_append(&ga, '}');
780 ga_append(&ga, NUL);
781 return (char_u *)ga.ga_data;
782}
783
784/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +0200785 * Get the key for #{key: val} into "tv" and advance "arg".
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200786 * Return FAIL when there is no valid key.
787 */
788 static int
789get_literal_key(char_u **arg, typval_T *tv)
790{
791 char_u *p;
792
793 if (!ASCII_ISALNUM(**arg) && **arg != '_' && **arg != '-')
794 return FAIL;
795
796 for (p = *arg; ASCII_ISALNUM(*p) || *p == '_' || *p == '-'; ++p)
797 ;
798 tv->v_type = VAR_STRING;
Bram Moolenaardf44a272020-06-07 20:49:05 +0200799 tv->vval.v_string = vim_strnsave(*arg, p - *arg);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200800
Bram Moolenaardb199212020-08-12 18:01:53 +0200801 *arg = p;
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200802 return OK;
803}
804
805/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200806 * Allocate a variable for a Dictionary and fill it from "*arg".
Bram Moolenaar962d7212020-07-04 14:15:00 +0200807 * "*arg" points to the "{".
Bram Moolenaar9f9fe372019-07-27 23:12:12 +0200808 * "literal" is TRUE for #{key: val}
Bram Moolenaarcd524592016-07-17 14:57:05 +0200809 * Return OK or FAIL. Returns NOTDONE for {expr}.
810 */
811 int
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200812eval_dict(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int literal)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200813{
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200814 int evaluate = evalarg == NULL ? FALSE
815 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200816 dict_T *d = NULL;
817 typval_T tvkey;
818 typval_T tv;
819 char_u *key = NULL;
820 dictitem_T *item;
821 char_u *start = skipwhite(*arg + 1);
822 char_u buf[NUMBUFLEN];
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200823 int vim9script = in_vim9script();
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200824 int had_comma;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200825
Bram Moolenaarcd524592016-07-17 14:57:05 +0200826 /*
827 * First check if it's not a curly-braces thing: {expr}.
828 * Must do this without evaluating, otherwise a function may be called
829 * twice. Unfortunately this means we need to call eval1() twice for the
830 * first item.
831 * But {} is an empty Dictionary.
832 */
Bram Moolenaar32e35112020-05-14 22:41:15 +0200833 if (!vim9script && *start != '}')
Bram Moolenaarcd524592016-07-17 14:57:05 +0200834 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200835 if (eval1(&start, &tv, NULL) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200836 return FAIL;
Bram Moolenaarae95a392020-08-06 16:38:12 +0200837 if (*skipwhite(start) == '}')
Bram Moolenaarcd524592016-07-17 14:57:05 +0200838 return NOTDONE;
839 }
840
841 if (evaluate)
842 {
843 d = dict_alloc();
844 if (d == NULL)
845 return FAIL;
846 }
847 tvkey.v_type = VAR_UNKNOWN;
848 tv.v_type = VAR_UNKNOWN;
849
Bram Moolenaar962d7212020-07-04 14:15:00 +0200850 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200851 while (**arg != '}' && **arg != NUL)
852 {
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200853 if ((literal
854 ? get_literal_key(arg, &tvkey)
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200855 : eval1(arg, &tvkey, evalarg)) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200856 goto failret;
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200857
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +0200858 // the colon should come right after the key, but this wasn't checked
Bram Moolenaar9d489562020-07-30 20:08:50 +0200859 // previously, so only require it in Vim9 script.
860 if (!vim9script)
861 *arg = skipwhite(*arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200862 if (**arg != ':')
863 {
Bram Moolenaara2c026d2020-10-18 18:03:17 +0200864 if (*skipwhite(*arg) == ':')
865 semsg(_(e_no_white_space_allowed_before_str), ":");
866 else
867 semsg(_(e_missing_dict_colon), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200868 clear_tv(&tvkey);
869 goto failret;
870 }
871 if (evaluate)
872 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100873 key = tv_get_string_buf_chk(&tvkey, buf);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200874 if (key == NULL)
875 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100876 // "key" is NULL when tv_get_string_buf_chk() gave an errmsg
Bram Moolenaarcd524592016-07-17 14:57:05 +0200877 clear_tv(&tvkey);
878 goto failret;
879 }
880 }
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200881 if (vim9script && (*arg)[1] != NUL && !VIM_ISWHITE((*arg)[1]))
882 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200883 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaarab19d492020-06-27 17:04:05 +0200884 clear_tv(&tvkey);
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200885 goto failret;
886 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200887
Bram Moolenaar962d7212020-07-04 14:15:00 +0200888 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200889 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200890 {
891 if (evaluate)
892 clear_tv(&tvkey);
893 goto failret;
894 }
895 if (evaluate)
896 {
897 item = dict_find(d, key, -1);
898 if (item != NULL)
899 {
Bram Moolenaara2c026d2020-10-18 18:03:17 +0200900 semsg(_(e_duplicate_key), key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200901 clear_tv(&tvkey);
902 clear_tv(&tv);
903 goto failret;
904 }
905 item = dictitem_alloc(key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200906 if (item != NULL)
907 {
908 item->di_tv = tv;
909 item->di_tv.v_lock = 0;
910 if (dict_add(d, item) == FAIL)
911 dictitem_free(item);
912 }
913 }
Bram Moolenaara8931942019-09-28 17:25:10 +0200914 clear_tv(&tvkey);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200915
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +0200916 // the comma should come right after the value, but this wasn't checked
917 // previously, so only require it in Vim9 script.
918 if (!vim9script)
919 *arg = skipwhite(*arg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200920 had_comma = **arg == ',';
921 if (had_comma)
922 {
923 if (vim9script && (*arg)[1] != NUL && !VIM_ISWHITE((*arg)[1]))
924 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200925 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200926 goto failret;
927 }
928 *arg = skipwhite(*arg + 1);
929 }
930
931 // the "}" can be on the next line
Bram Moolenaar962d7212020-07-04 14:15:00 +0200932 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200933 if (**arg == '}')
934 break;
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200935 if (!had_comma)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200936 {
Bram Moolenaara2c026d2020-10-18 18:03:17 +0200937 if (**arg == ',')
938 semsg(_(e_no_white_space_allowed_before_str), ",");
939 else
940 semsg(_(e_missing_dict_comma), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200941 goto failret;
942 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200943 }
944
945 if (**arg != '}')
946 {
Bram Moolenaara2c026d2020-10-18 18:03:17 +0200947 semsg(_(e_missing_dict_end), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200948failret:
Bram Moolenaaradc67142019-06-22 01:40:42 +0200949 if (d != NULL)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200950 dict_free(d);
951 return FAIL;
952 }
953
Bram Moolenaarb07a39d2020-10-22 19:00:01 +0200954 *arg = *arg + 1;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200955 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200956 rettv_dict_set(rettv, d);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200957
958 return OK;
959}
960
961/*
962 * Go over all entries in "d2" and add them to "d1".
963 * When "action" is "error" then a duplicate key is an error.
964 * When "action" is "force" then a duplicate key is overwritten.
965 * Otherwise duplicate keys are ignored ("action" is "keep").
966 */
967 void
968dict_extend(dict_T *d1, dict_T *d2, char_u *action)
969{
970 dictitem_T *di1;
971 hashitem_T *hi2;
972 int todo;
973 char_u *arg_errmsg = (char_u *)N_("extend() argument");
974
975 todo = (int)d2->dv_hashtab.ht_used;
976 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
977 {
978 if (!HASHITEM_EMPTY(hi2))
979 {
980 --todo;
981 di1 = dict_find(d1, hi2->hi_key, -1);
982 if (d1->dv_scope != 0)
983 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100984 // Disallow replacing a builtin function in l: and g:.
985 // Check the key to be valid when adding to any scope.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200986 if (d1->dv_scope == VAR_DEF_SCOPE
987 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +0200988 && var_wrong_func_name(hi2->hi_key, di1 == NULL))
Bram Moolenaarcd524592016-07-17 14:57:05 +0200989 break;
990 if (!valid_varname(hi2->hi_key))
991 break;
992 }
993 if (di1 == NULL)
994 {
995 di1 = dictitem_copy(HI2DI(hi2));
996 if (di1 != NULL && dict_add(d1, di1) == FAIL)
997 dictitem_free(di1);
998 }
999 else if (*action == 'e')
1000 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001001 semsg(_("E737: Key already exists: %s"), hi2->hi_key);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001002 break;
1003 }
1004 else if (*action == 'f' && HI2DI(hi2) != di1)
1005 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001006 if (value_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001007 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaarcd524592016-07-17 14:57:05 +02001008 break;
1009 clear_tv(&di1->di_tv);
1010 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
1011 }
1012 }
1013 }
1014}
1015
1016/*
1017 * Return the dictitem that an entry in a hashtable points to.
1018 */
1019 dictitem_T *
1020dict_lookup(hashitem_T *hi)
1021{
1022 return HI2DI(hi);
1023}
1024
1025/*
1026 * Return TRUE when two dictionaries have exactly the same key/values.
1027 */
1028 int
1029dict_equal(
1030 dict_T *d1,
1031 dict_T *d2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001032 int ic, // ignore case for strings
1033 int recursive) // TRUE when used recursively
Bram Moolenaarcd524592016-07-17 14:57:05 +02001034{
1035 hashitem_T *hi;
1036 dictitem_T *item2;
1037 int todo;
1038
Bram Moolenaarcd524592016-07-17 14:57:05 +02001039 if (d1 == d2)
1040 return TRUE;
1041 if (dict_len(d1) != dict_len(d2))
1042 return FALSE;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001043 if (dict_len(d1) == 0)
1044 // empty and NULL dicts are considered equal
1045 return TRUE;
1046 if (d1 == NULL || d2 == NULL)
1047 return FALSE;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001048
1049 todo = (int)d1->dv_hashtab.ht_used;
1050 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
1051 {
1052 if (!HASHITEM_EMPTY(hi))
1053 {
1054 item2 = dict_find(d2, hi->hi_key, -1);
1055 if (item2 == NULL)
1056 return FALSE;
1057 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
1058 return FALSE;
1059 --todo;
1060 }
1061 }
1062 return TRUE;
1063}
1064
1065/*
1066 * Turn a dict into a list:
1067 * "what" == 0: list of keys
1068 * "what" == 1: list of values
1069 * "what" == 2: list of items
1070 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001071 static void
Bram Moolenaarcd524592016-07-17 14:57:05 +02001072dict_list(typval_T *argvars, typval_T *rettv, int what)
1073{
1074 list_T *l2;
1075 dictitem_T *di;
1076 hashitem_T *hi;
1077 listitem_T *li;
1078 listitem_T *li2;
1079 dict_T *d;
1080 int todo;
1081
1082 if (argvars[0].v_type != VAR_DICT)
1083 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001084 emsg(_(e_dictreq));
Bram Moolenaarcd524592016-07-17 14:57:05 +02001085 return;
1086 }
1087 if ((d = argvars[0].vval.v_dict) == NULL)
1088 return;
1089
1090 if (rettv_list_alloc(rettv) == FAIL)
1091 return;
1092
1093 todo = (int)d->dv_hashtab.ht_used;
1094 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1095 {
1096 if (!HASHITEM_EMPTY(hi))
1097 {
1098 --todo;
1099 di = HI2DI(hi);
1100
1101 li = listitem_alloc();
1102 if (li == NULL)
1103 break;
1104 list_append(rettv->vval.v_list, li);
1105
1106 if (what == 0)
1107 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001108 // keys()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001109 li->li_tv.v_type = VAR_STRING;
1110 li->li_tv.v_lock = 0;
1111 li->li_tv.vval.v_string = vim_strsave(di->di_key);
1112 }
1113 else if (what == 1)
1114 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001115 // values()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001116 copy_tv(&di->di_tv, &li->li_tv);
1117 }
1118 else
1119 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001120 // items()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001121 l2 = list_alloc();
1122 li->li_tv.v_type = VAR_LIST;
1123 li->li_tv.v_lock = 0;
1124 li->li_tv.vval.v_list = l2;
1125 if (l2 == NULL)
1126 break;
1127 ++l2->lv_refcount;
1128
1129 li2 = listitem_alloc();
1130 if (li2 == NULL)
1131 break;
1132 list_append(l2, li2);
1133 li2->li_tv.v_type = VAR_STRING;
1134 li2->li_tv.v_lock = 0;
1135 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
1136
1137 li2 = listitem_alloc();
1138 if (li2 == NULL)
1139 break;
1140 list_append(l2, li2);
1141 copy_tv(&di->di_tv, &li2->li_tv);
1142 }
1143 }
1144 }
1145}
1146
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001147/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001148 * "items(dict)" function
1149 */
1150 void
1151f_items(typval_T *argvars, typval_T *rettv)
1152{
1153 dict_list(argvars, rettv, 2);
1154}
1155
1156/*
1157 * "keys()" function
1158 */
1159 void
1160f_keys(typval_T *argvars, typval_T *rettv)
1161{
1162 dict_list(argvars, rettv, 0);
1163}
1164
1165/*
1166 * "values(dict)" function
1167 */
1168 void
1169f_values(typval_T *argvars, typval_T *rettv)
1170{
1171 dict_list(argvars, rettv, 1);
1172}
1173
1174/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001175 * Make each item in the dict readonly (not the value of the item).
1176 */
1177 void
1178dict_set_items_ro(dict_T *di)
1179{
1180 int todo = (int)di->dv_hashtab.ht_used;
1181 hashitem_T *hi;
1182
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001183 // Set readonly
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001184 for (hi = di->dv_hashtab.ht_array; todo > 0 ; ++hi)
1185 {
1186 if (HASHITEM_EMPTY(hi))
1187 continue;
1188 --todo;
1189 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
1190 }
1191}
1192
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001193/*
1194 * "has_key()" function
1195 */
1196 void
1197f_has_key(typval_T *argvars, typval_T *rettv)
1198{
1199 if (argvars[0].v_type != VAR_DICT)
1200 {
1201 emsg(_(e_dictreq));
1202 return;
1203 }
1204 if (argvars[0].vval.v_dict == NULL)
1205 return;
1206
1207 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
1208 tv_get_string(&argvars[1]), -1) != NULL;
1209}
1210
1211/*
1212 * "remove({dict})" function
1213 */
1214 void
1215dict_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1216{
1217 dict_T *d;
1218 char_u *key;
1219 dictitem_T *di;
1220
1221 if (argvars[2].v_type != VAR_UNKNOWN)
1222 semsg(_(e_toomanyarg), "remove()");
1223 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001224 && !value_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001225 {
1226 key = tv_get_string_chk(&argvars[1]);
1227 if (key != NULL)
1228 {
1229 di = dict_find(d, key, -1);
1230 if (di == NULL)
1231 semsg(_(e_dictkey), key);
1232 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
1233 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
1234 {
1235 *rettv = di->di_tv;
1236 init_tv(&di->di_tv);
1237 dictitem_remove(d, di);
1238 }
1239 }
1240 }
1241}
1242
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001243#endif // defined(FEAT_EVAL)