blob: 6e1d0d640f4a80a5a4a6338ca32b0cb6992fbde1 [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
18/* 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; /* list of all dicts */
22
23/*
24 * Allocate an empty header for a dictionary.
25 */
26 dict_T *
27dict_alloc(void)
28{
29 dict_T *d;
30
31 d = (dict_T *)alloc(sizeof(dict_T));
32 if (d != NULL)
33 {
34 /* Add the dict to the list of dicts for garbage collection. */
35 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
57 if (alloc_fail_id == id && alloc_does_fail((long_u)sizeof(list_T)))
58 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{
108 int todo;
109 hashitem_T *hi;
110 dictitem_T *di;
111
112 /* Lock the hashtab, we don't want it to resize while freeing items. */
113 hash_lock(&d->dv_hashtab);
114 todo = (int)d->dv_hashtab.ht_used;
115 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
116 {
117 if (!HASHITEM_EMPTY(hi))
118 {
119 /* Remove the item before deleting it, just in case there is
120 * something recursive causing trouble. */
121 di = HI2DI(hi);
122 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100123 dictitem_free(di);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200124 --todo;
125 }
126 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100127
128 /* The hashtab is still locked, it has to be re-initialized anyway */
Bram Moolenaarcd524592016-07-17 14:57:05 +0200129 hash_clear(&d->dv_hashtab);
130}
131
132 static void
133dict_free_dict(dict_T *d)
134{
135 /* Remove the dict from the list of dicts for garbage collection. */
136 if (d->dv_used_prev == NULL)
137 first_dict = d->dv_used_next;
138 else
139 d->dv_used_prev->dv_used_next = d->dv_used_next;
140 if (d->dv_used_next != NULL)
141 d->dv_used_next->dv_used_prev = d->dv_used_prev;
142 vim_free(d);
143}
144
145 static void
146dict_free(dict_T *d)
147{
148 if (!in_free_unref_items)
149 {
150 dict_free_contents(d);
151 dict_free_dict(d);
152 }
153}
154
155/*
156 * Unreference a Dictionary: decrement the reference count and free it when it
157 * becomes zero.
158 */
159 void
160dict_unref(dict_T *d)
161{
162 if (d != NULL && --d->dv_refcount <= 0)
163 dict_free(d);
164}
165
166/*
167 * Go through the list of dicts and free items without the copyID.
168 * Returns TRUE if something was freed.
169 */
170 int
171dict_free_nonref(int copyID)
172{
173 dict_T *dd;
174 int did_free = FALSE;
175
176 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
177 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
178 {
179 /* Free the Dictionary and ordinary items it contains, but don't
180 * recurse into Lists and Dictionaries, they will be in the list
181 * of dicts or list of lists. */
182 dict_free_contents(dd);
183 did_free = TRUE;
184 }
185 return did_free;
186}
187
188 void
189dict_free_items(int copyID)
190{
191 dict_T *dd, *dd_next;
192
193 for (dd = first_dict; dd != NULL; dd = dd_next)
194 {
195 dd_next = dd->dv_used_next;
196 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
197 dict_free_dict(dd);
198 }
199}
200
201/*
202 * Allocate a Dictionary item.
203 * The "key" is copied to the new item.
Bram Moolenaarc89d4b32018-07-08 17:19:02 +0200204 * Note that the type and value of the item "di_tv" still needs to be
205 * initialized!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200206 * Returns NULL when out of memory.
207 */
208 dictitem_T *
209dictitem_alloc(char_u *key)
210{
211 dictitem_T *di;
212
Bram Moolenaar964b3742019-05-24 18:54:09 +0200213 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(key));
Bram Moolenaarcd524592016-07-17 14:57:05 +0200214 if (di != NULL)
215 {
216 STRCPY(di->di_key, key);
217 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaarc89d4b32018-07-08 17:19:02 +0200218 di->di_tv.v_lock = 0;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200219 }
220 return di;
221}
222
223/*
224 * Make a copy of a Dictionary item.
225 */
226 static dictitem_T *
227dictitem_copy(dictitem_T *org)
228{
229 dictitem_T *di;
230
Bram Moolenaar964b3742019-05-24 18:54:09 +0200231 di = (dictitem_T *)alloc(sizeof(dictitem_T) + STRLEN(org->di_key));
Bram Moolenaarcd524592016-07-17 14:57:05 +0200232 if (di != NULL)
233 {
234 STRCPY(di->di_key, org->di_key);
235 di->di_flags = DI_FLAGS_ALLOC;
236 copy_tv(&org->di_tv, &di->di_tv);
237 }
238 return di;
239}
240
241/*
242 * Remove item "item" from Dictionary "dict" and free it.
243 */
244 void
245dictitem_remove(dict_T *dict, dictitem_T *item)
246{
247 hashitem_T *hi;
248
249 hi = hash_find(&dict->dv_hashtab, item->di_key);
250 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100251 internal_error("dictitem_remove()");
Bram Moolenaarcd524592016-07-17 14:57:05 +0200252 else
253 hash_remove(&dict->dv_hashtab, hi);
254 dictitem_free(item);
255}
256
257/*
258 * Free a dict item. Also clears the value.
259 */
260 void
261dictitem_free(dictitem_T *item)
262{
263 clear_tv(&item->di_tv);
264 if (item->di_flags & DI_FLAGS_ALLOC)
265 vim_free(item);
266}
267
268/*
269 * Make a copy of dict "d". Shallow if "deep" is FALSE.
270 * The refcount of the new dict is set to 1.
271 * See item_copy() for "copyID".
272 * Returns NULL when out of memory.
273 */
274 dict_T *
275dict_copy(dict_T *orig, int deep, int copyID)
276{
277 dict_T *copy;
278 dictitem_T *di;
279 int todo;
280 hashitem_T *hi;
281
282 if (orig == NULL)
283 return NULL;
284
285 copy = dict_alloc();
286 if (copy != NULL)
287 {
288 if (copyID != 0)
289 {
290 orig->dv_copyID = copyID;
291 orig->dv_copydict = copy;
292 }
293 todo = (int)orig->dv_hashtab.ht_used;
294 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
295 {
296 if (!HASHITEM_EMPTY(hi))
297 {
298 --todo;
299
300 di = dictitem_alloc(hi->hi_key);
301 if (di == NULL)
302 break;
303 if (deep)
304 {
305 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
306 copyID) == FAIL)
307 {
308 vim_free(di);
309 break;
310 }
311 }
312 else
313 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
314 if (dict_add(copy, di) == FAIL)
315 {
316 dictitem_free(di);
317 break;
318 }
319 }
320 }
321
322 ++copy->dv_refcount;
323 if (todo > 0)
324 {
325 dict_unref(copy);
326 copy = NULL;
327 }
328 }
329
330 return copy;
331}
332
333/*
334 * Add item "item" to Dictionary "d".
335 * Returns FAIL when out of memory and when key already exists.
336 */
337 int
338dict_add(dict_T *d, dictitem_T *item)
339{
340 return hash_add(&d->dv_hashtab, item->di_key);
341}
342
343/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200344 * Add a number or special entry to dictionary "d".
Bram Moolenaarcd524592016-07-17 14:57:05 +0200345 * Returns FAIL when out of memory and when key already exists.
346 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200347 static int
348dict_add_number_special(dict_T *d, char *key, varnumber_T nr, int special)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200349{
350 dictitem_T *item;
351
352 item = dictitem_alloc((char_u *)key);
353 if (item == NULL)
354 return FAIL;
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200355 item->di_tv.v_type = special ? VAR_SPECIAL : VAR_NUMBER;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200356 item->di_tv.vval.v_number = nr;
357 if (dict_add(d, item) == FAIL)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200358 {
Bram Moolenaare0be1672018-07-08 16:50:37 +0200359 dictitem_free(item);
360 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200361 }
Bram Moolenaare0be1672018-07-08 16:50:37 +0200362 return OK;
363}
364
365/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200366 * Add a number entry to dictionary "d".
367 * Returns FAIL when out of memory and when key already exists.
368 */
369 int
370dict_add_number(dict_T *d, char *key, varnumber_T nr)
371{
372 return dict_add_number_special(d, key, nr, FALSE);
373}
374
375/*
376 * Add a special entry to dictionary "d".
377 * Returns FAIL when out of memory and when key already exists.
378 */
379 int
380dict_add_special(dict_T *d, char *key, varnumber_T nr)
381{
382 return dict_add_number_special(d, key, nr, TRUE);
383}
384
385/*
Bram Moolenaare0be1672018-07-08 16:50:37 +0200386 * Add a string entry to dictionary "d".
387 * Returns FAIL when out of memory and when key already exists.
388 */
389 int
390dict_add_string(dict_T *d, char *key, char_u *str)
391{
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100392 return dict_add_string_len(d, key, str, -1);
393}
394
395/*
396 * Add a string entry to dictionary "d".
397 * "str" will be copied to allocated memory.
398 * When "len" is -1 use the whole string, otherwise only this many bytes.
399 * Returns FAIL when out of memory and when key already exists.
400 */
401 int
402dict_add_string_len(dict_T *d, char *key, char_u *str, int len)
403{
Bram Moolenaare0be1672018-07-08 16:50:37 +0200404 dictitem_T *item;
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100405 char_u *val = NULL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200406
407 item = dictitem_alloc((char_u *)key);
408 if (item == NULL)
409 return FAIL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200410 item->di_tv.v_type = VAR_STRING;
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100411 if (str != NULL)
412 {
413 if (len == -1)
414 val = vim_strsave(str);
415 else
416 val = vim_strnsave(str, len);
417 }
418 item->di_tv.vval.v_string = val;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200419 if (dict_add(d, item) == FAIL)
420 {
421 dictitem_free(item);
422 return FAIL;
423 }
424 return OK;
425}
426
427/*
428 * Add a list entry to dictionary "d".
429 * Returns FAIL when out of memory and when key already exists.
430 */
431 int
432dict_add_list(dict_T *d, char *key, list_T *list)
433{
434 dictitem_T *item;
435
436 item = dictitem_alloc((char_u *)key);
437 if (item == NULL)
438 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200439 item->di_tv.v_type = VAR_LIST;
440 item->di_tv.vval.v_list = list;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100441 ++list->lv_refcount;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200442 if (dict_add(d, item) == FAIL)
443 {
444 dictitem_free(item);
445 return FAIL;
446 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200447 return OK;
448}
449
450/*
Bram Moolenaar45e18cb2019-04-28 18:05:35 +0200451 * Initializes "iter" for iterating over dictionary items with
452 * dict_iterate_next().
453 * If "var" is not a Dict or an empty Dict then there will be nothing to
454 * iterate over, no error is given.
455 * NOTE: The dictionary must not change until iterating is finished!
456 */
457 void
458dict_iterate_start(typval_T *var, dict_iterator_T *iter)
459{
460 if (var->v_type != VAR_DICT || var->vval.v_dict == NULL)
461 iter->dit_todo = 0;
462 else
463 {
464 dict_T *d = var->vval.v_dict;
465
466 iter->dit_todo = d->dv_hashtab.ht_used;
467 iter->dit_hi = d->dv_hashtab.ht_array;
468 }
469}
470
471/*
472 * Iterate over the items referred to by "iter". It should be initialized with
473 * dict_iterate_start().
474 * Returns a pointer to the key.
475 * "*tv_result" is set to point to the value for that key.
476 * If there are no more items, NULL is returned.
477 */
478 char_u *
479dict_iterate_next(dict_iterator_T *iter, typval_T **tv_result)
480{
481 dictitem_T *di;
482 char_u *result;
483
484 if (iter->dit_todo == 0)
485 return NULL;
486
487 while (HASHITEM_EMPTY(iter->dit_hi))
488 ++iter->dit_hi;
489
490 di = HI2DI(iter->dit_hi);
491 result = di->di_key;
492 *tv_result = &di->di_tv;
493
494 --iter->dit_todo;
495 ++iter->dit_hi;
496 return result;
497}
498
499/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200500 * Add a dict entry to dictionary "d".
501 * Returns FAIL when out of memory and when key already exists.
502 */
503 int
504dict_add_dict(dict_T *d, char *key, dict_T *dict)
505{
506 dictitem_T *item;
507
508 item = dictitem_alloc((char_u *)key);
509 if (item == NULL)
510 return FAIL;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200511 item->di_tv.v_type = VAR_DICT;
512 item->di_tv.vval.v_dict = dict;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100513 ++dict->dv_refcount;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200514 if (dict_add(d, item) == FAIL)
515 {
516 dictitem_free(item);
517 return FAIL;
518 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200519 return OK;
520}
521
522/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200523 * Get the number of items in a Dictionary.
524 */
525 long
526dict_len(dict_T *d)
527{
528 if (d == NULL)
529 return 0L;
530 return (long)d->dv_hashtab.ht_used;
531}
532
533/*
534 * Find item "key[len]" in Dictionary "d".
535 * If "len" is negative use strlen(key).
536 * Returns NULL when not found.
537 */
538 dictitem_T *
539dict_find(dict_T *d, char_u *key, int len)
540{
541#define AKEYLEN 200
542 char_u buf[AKEYLEN];
543 char_u *akey;
544 char_u *tofree = NULL;
545 hashitem_T *hi;
546
547 if (d == NULL)
548 return NULL;
549 if (len < 0)
550 akey = key;
551 else if (len >= AKEYLEN)
552 {
553 tofree = akey = vim_strnsave(key, len);
554 if (akey == NULL)
555 return NULL;
556 }
557 else
558 {
559 /* Avoid a malloc/free by using buf[]. */
560 vim_strncpy(buf, key, len);
561 akey = buf;
562 }
563
564 hi = hash_find(&d->dv_hashtab, akey);
565 vim_free(tofree);
566 if (HASHITEM_EMPTY(hi))
567 return NULL;
568 return HI2DI(hi);
569}
570
571/*
572 * Get a string item from a dictionary.
573 * When "save" is TRUE allocate memory for it.
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +0200574 * When FALSE a shared buffer is used, can only be used once!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200575 * Returns NULL if the entry doesn't exist or out of memory.
576 */
577 char_u *
Bram Moolenaar8f667172018-12-14 15:38:31 +0100578dict_get_string(dict_T *d, char_u *key, int save)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200579{
580 dictitem_T *di;
581 char_u *s;
582
583 di = dict_find(d, key, -1);
584 if (di == NULL)
585 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100586 s = tv_get_string(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200587 if (save && s != NULL)
588 s = vim_strsave(s);
589 return s;
590}
591
592/*
593 * Get a number item from a dictionary.
594 * Returns 0 if the entry doesn't exist.
595 */
596 varnumber_T
Bram Moolenaar8f667172018-12-14 15:38:31 +0100597dict_get_number(dict_T *d, char_u *key)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200598{
599 dictitem_T *di;
600
601 di = dict_find(d, key, -1);
602 if (di == NULL)
603 return 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100604 return tv_get_number(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200605}
606
607/*
608 * Return an allocated string with the string representation of a Dictionary.
609 * May return NULL.
610 */
611 char_u *
612dict2string(typval_T *tv, int copyID, int restore_copyID)
613{
614 garray_T ga;
615 int first = TRUE;
616 char_u *tofree;
617 char_u numbuf[NUMBUFLEN];
618 hashitem_T *hi;
619 char_u *s;
620 dict_T *d;
621 int todo;
622
623 if ((d = tv->vval.v_dict) == NULL)
624 return NULL;
625 ga_init2(&ga, (int)sizeof(char), 80);
626 ga_append(&ga, '{');
627
628 todo = (int)d->dv_hashtab.ht_used;
629 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
630 {
631 if (!HASHITEM_EMPTY(hi))
632 {
633 --todo;
634
635 if (first)
636 first = FALSE;
637 else
638 ga_concat(&ga, (char_u *)", ");
639
640 tofree = string_quote(hi->hi_key, FALSE);
641 if (tofree != NULL)
642 {
643 ga_concat(&ga, tofree);
644 vim_free(tofree);
645 }
646 ga_concat(&ga, (char_u *)": ");
647 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
648 FALSE, restore_copyID, TRUE);
649 if (s != NULL)
650 ga_concat(&ga, s);
651 vim_free(tofree);
652 if (s == NULL || did_echo_string_emsg)
653 break;
654 line_breakcheck();
655
656 }
657 }
658 if (todo > 0)
659 {
660 vim_free(ga.ga_data);
661 return NULL;
662 }
663
664 ga_append(&ga, '}');
665 ga_append(&ga, NUL);
666 return (char_u *)ga.ga_data;
667}
668
669/*
670 * Allocate a variable for a Dictionary and fill it from "*arg".
671 * Return OK or FAIL. Returns NOTDONE for {expr}.
672 */
673 int
Bram Moolenaar8f667172018-12-14 15:38:31 +0100674dict_get_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200675{
676 dict_T *d = NULL;
677 typval_T tvkey;
678 typval_T tv;
679 char_u *key = NULL;
680 dictitem_T *item;
681 char_u *start = skipwhite(*arg + 1);
682 char_u buf[NUMBUFLEN];
683
684 /*
685 * First check if it's not a curly-braces thing: {expr}.
686 * Must do this without evaluating, otherwise a function may be called
687 * twice. Unfortunately this means we need to call eval1() twice for the
688 * first item.
689 * But {} is an empty Dictionary.
690 */
691 if (*start != '}')
692 {
693 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
694 return FAIL;
695 if (*start == '}')
696 return NOTDONE;
697 }
698
699 if (evaluate)
700 {
701 d = dict_alloc();
702 if (d == NULL)
703 return FAIL;
704 }
705 tvkey.v_type = VAR_UNKNOWN;
706 tv.v_type = VAR_UNKNOWN;
707
708 *arg = skipwhite(*arg + 1);
709 while (**arg != '}' && **arg != NUL)
710 {
711 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
712 goto failret;
713 if (**arg != ':')
714 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100715 semsg(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200716 clear_tv(&tvkey);
717 goto failret;
718 }
719 if (evaluate)
720 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100721 key = tv_get_string_buf_chk(&tvkey, buf);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200722 if (key == NULL)
723 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100724 /* "key" is NULL when tv_get_string_buf_chk() gave an errmsg */
Bram Moolenaarcd524592016-07-17 14:57:05 +0200725 clear_tv(&tvkey);
726 goto failret;
727 }
728 }
729
730 *arg = skipwhite(*arg + 1);
731 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
732 {
733 if (evaluate)
734 clear_tv(&tvkey);
735 goto failret;
736 }
737 if (evaluate)
738 {
739 item = dict_find(d, key, -1);
740 if (item != NULL)
741 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100742 semsg(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200743 clear_tv(&tvkey);
744 clear_tv(&tv);
745 goto failret;
746 }
747 item = dictitem_alloc(key);
748 clear_tv(&tvkey);
749 if (item != NULL)
750 {
751 item->di_tv = tv;
752 item->di_tv.v_lock = 0;
753 if (dict_add(d, item) == FAIL)
754 dictitem_free(item);
755 }
756 }
757
758 if (**arg == '}')
759 break;
760 if (**arg != ',')
761 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100762 semsg(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200763 goto failret;
764 }
765 *arg = skipwhite(*arg + 1);
766 }
767
768 if (**arg != '}')
769 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100770 semsg(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200771failret:
772 if (evaluate)
773 dict_free(d);
774 return FAIL;
775 }
776
777 *arg = skipwhite(*arg + 1);
778 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200779 rettv_dict_set(rettv, d);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200780
781 return OK;
782}
783
784/*
785 * Go over all entries in "d2" and add them to "d1".
786 * When "action" is "error" then a duplicate key is an error.
787 * When "action" is "force" then a duplicate key is overwritten.
788 * Otherwise duplicate keys are ignored ("action" is "keep").
789 */
790 void
791dict_extend(dict_T *d1, dict_T *d2, char_u *action)
792{
793 dictitem_T *di1;
794 hashitem_T *hi2;
795 int todo;
796 char_u *arg_errmsg = (char_u *)N_("extend() argument");
797
798 todo = (int)d2->dv_hashtab.ht_used;
799 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
800 {
801 if (!HASHITEM_EMPTY(hi2))
802 {
803 --todo;
804 di1 = dict_find(d1, hi2->hi_key, -1);
805 if (d1->dv_scope != 0)
806 {
807 /* Disallow replacing a builtin function in l: and g:.
808 * Check the key to be valid when adding to any scope. */
809 if (d1->dv_scope == VAR_DEF_SCOPE
810 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
811 && var_check_func_name(hi2->hi_key, di1 == NULL))
812 break;
813 if (!valid_varname(hi2->hi_key))
814 break;
815 }
816 if (di1 == NULL)
817 {
818 di1 = dictitem_copy(HI2DI(hi2));
819 if (di1 != NULL && dict_add(d1, di1) == FAIL)
820 dictitem_free(di1);
821 }
822 else if (*action == 'e')
823 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100824 semsg(_("E737: Key already exists: %s"), hi2->hi_key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200825 break;
826 }
827 else if (*action == 'f' && HI2DI(hi2) != di1)
828 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +0100829 if (var_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
830 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaarcd524592016-07-17 14:57:05 +0200831 break;
832 clear_tv(&di1->di_tv);
833 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
834 }
835 }
836 }
837}
838
839/*
840 * Return the dictitem that an entry in a hashtable points to.
841 */
842 dictitem_T *
843dict_lookup(hashitem_T *hi)
844{
845 return HI2DI(hi);
846}
847
848/*
849 * Return TRUE when two dictionaries have exactly the same key/values.
850 */
851 int
852dict_equal(
853 dict_T *d1,
854 dict_T *d2,
855 int ic, /* ignore case for strings */
856 int recursive) /* TRUE when used recursively */
857{
858 hashitem_T *hi;
859 dictitem_T *item2;
860 int todo;
861
862 if (d1 == NULL && d2 == NULL)
863 return TRUE;
864 if (d1 == NULL || d2 == NULL)
865 return FALSE;
866 if (d1 == d2)
867 return TRUE;
868 if (dict_len(d1) != dict_len(d2))
869 return FALSE;
870
871 todo = (int)d1->dv_hashtab.ht_used;
872 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
873 {
874 if (!HASHITEM_EMPTY(hi))
875 {
876 item2 = dict_find(d2, hi->hi_key, -1);
877 if (item2 == NULL)
878 return FALSE;
879 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
880 return FALSE;
881 --todo;
882 }
883 }
884 return TRUE;
885}
886
887/*
888 * Turn a dict into a list:
889 * "what" == 0: list of keys
890 * "what" == 1: list of values
891 * "what" == 2: list of items
892 */
893 void
894dict_list(typval_T *argvars, typval_T *rettv, int what)
895{
896 list_T *l2;
897 dictitem_T *di;
898 hashitem_T *hi;
899 listitem_T *li;
900 listitem_T *li2;
901 dict_T *d;
902 int todo;
903
904 if (argvars[0].v_type != VAR_DICT)
905 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100906 emsg(_(e_dictreq));
Bram Moolenaarcd524592016-07-17 14:57:05 +0200907 return;
908 }
909 if ((d = argvars[0].vval.v_dict) == NULL)
910 return;
911
912 if (rettv_list_alloc(rettv) == FAIL)
913 return;
914
915 todo = (int)d->dv_hashtab.ht_used;
916 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
917 {
918 if (!HASHITEM_EMPTY(hi))
919 {
920 --todo;
921 di = HI2DI(hi);
922
923 li = listitem_alloc();
924 if (li == NULL)
925 break;
926 list_append(rettv->vval.v_list, li);
927
928 if (what == 0)
929 {
930 /* keys() */
931 li->li_tv.v_type = VAR_STRING;
932 li->li_tv.v_lock = 0;
933 li->li_tv.vval.v_string = vim_strsave(di->di_key);
934 }
935 else if (what == 1)
936 {
937 /* values() */
938 copy_tv(&di->di_tv, &li->li_tv);
939 }
940 else
941 {
942 /* items() */
943 l2 = list_alloc();
944 li->li_tv.v_type = VAR_LIST;
945 li->li_tv.v_lock = 0;
946 li->li_tv.vval.v_list = l2;
947 if (l2 == NULL)
948 break;
949 ++l2->lv_refcount;
950
951 li2 = listitem_alloc();
952 if (li2 == NULL)
953 break;
954 list_append(l2, li2);
955 li2->li_tv.v_type = VAR_STRING;
956 li2->li_tv.v_lock = 0;
957 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
958
959 li2 = listitem_alloc();
960 if (li2 == NULL)
961 break;
962 list_append(l2, li2);
963 copy_tv(&di->di_tv, &li2->li_tv);
964 }
965 }
966 }
967}
968
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100969/*
970 * Make each item in the dict readonly (not the value of the item).
971 */
972 void
973dict_set_items_ro(dict_T *di)
974{
975 int todo = (int)di->dv_hashtab.ht_used;
976 hashitem_T *hi;
977
978 /* Set readonly */
979 for (hi = di->dv_hashtab.ht_array; todo > 0 ; ++hi)
980 {
981 if (HASHITEM_EMPTY(hi))
982 continue;
983 --todo;
984 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
985 }
986}
987
Bram Moolenaarcd524592016-07-17 14:57:05 +0200988#endif /* defined(FEAT_EVAL) */