blob: 7d49599efa5ffb5a579f67e22b0bce25e0c719b0 [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
213 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
214 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
231 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
232 + STRLEN(org->di_key)));
233 if (di != NULL)
234 {
235 STRCPY(di->di_key, org->di_key);
236 di->di_flags = DI_FLAGS_ALLOC;
237 copy_tv(&org->di_tv, &di->di_tv);
238 }
239 return di;
240}
241
242/*
243 * Remove item "item" from Dictionary "dict" and free it.
244 */
245 void
246dictitem_remove(dict_T *dict, dictitem_T *item)
247{
248 hashitem_T *hi;
249
250 hi = hash_find(&dict->dv_hashtab, item->di_key);
251 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100252 internal_error("dictitem_remove()");
Bram Moolenaarcd524592016-07-17 14:57:05 +0200253 else
254 hash_remove(&dict->dv_hashtab, hi);
255 dictitem_free(item);
256}
257
258/*
259 * Free a dict item. Also clears the value.
260 */
261 void
262dictitem_free(dictitem_T *item)
263{
264 clear_tv(&item->di_tv);
265 if (item->di_flags & DI_FLAGS_ALLOC)
266 vim_free(item);
267}
268
269/*
270 * Make a copy of dict "d". Shallow if "deep" is FALSE.
271 * The refcount of the new dict is set to 1.
272 * See item_copy() for "copyID".
273 * Returns NULL when out of memory.
274 */
275 dict_T *
276dict_copy(dict_T *orig, int deep, int copyID)
277{
278 dict_T *copy;
279 dictitem_T *di;
280 int todo;
281 hashitem_T *hi;
282
283 if (orig == NULL)
284 return NULL;
285
286 copy = dict_alloc();
287 if (copy != NULL)
288 {
289 if (copyID != 0)
290 {
291 orig->dv_copyID = copyID;
292 orig->dv_copydict = copy;
293 }
294 todo = (int)orig->dv_hashtab.ht_used;
295 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
296 {
297 if (!HASHITEM_EMPTY(hi))
298 {
299 --todo;
300
301 di = dictitem_alloc(hi->hi_key);
302 if (di == NULL)
303 break;
304 if (deep)
305 {
306 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
307 copyID) == FAIL)
308 {
309 vim_free(di);
310 break;
311 }
312 }
313 else
314 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
315 if (dict_add(copy, di) == FAIL)
316 {
317 dictitem_free(di);
318 break;
319 }
320 }
321 }
322
323 ++copy->dv_refcount;
324 if (todo > 0)
325 {
326 dict_unref(copy);
327 copy = NULL;
328 }
329 }
330
331 return copy;
332}
333
334/*
335 * Add item "item" to Dictionary "d".
336 * Returns FAIL when out of memory and when key already exists.
337 */
338 int
339dict_add(dict_T *d, dictitem_T *item)
340{
341 return hash_add(&d->dv_hashtab, item->di_key);
342}
343
344/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200345 * Add a number or special entry to dictionary "d".
Bram Moolenaarcd524592016-07-17 14:57:05 +0200346 * Returns FAIL when out of memory and when key already exists.
347 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200348 static int
349dict_add_number_special(dict_T *d, char *key, varnumber_T nr, int special)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200350{
351 dictitem_T *item;
352
353 item = dictitem_alloc((char_u *)key);
354 if (item == NULL)
355 return FAIL;
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200356 item->di_tv.v_type = special ? VAR_SPECIAL : VAR_NUMBER;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200357 item->di_tv.vval.v_number = nr;
358 if (dict_add(d, item) == FAIL)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200359 {
Bram Moolenaare0be1672018-07-08 16:50:37 +0200360 dictitem_free(item);
361 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200362 }
Bram Moolenaare0be1672018-07-08 16:50:37 +0200363 return OK;
364}
365
366/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200367 * Add a number entry to dictionary "d".
368 * Returns FAIL when out of memory and when key already exists.
369 */
370 int
371dict_add_number(dict_T *d, char *key, varnumber_T nr)
372{
373 return dict_add_number_special(d, key, nr, FALSE);
374}
375
376/*
377 * Add a special entry to dictionary "d".
378 * Returns FAIL when out of memory and when key already exists.
379 */
380 int
381dict_add_special(dict_T *d, char *key, varnumber_T nr)
382{
383 return dict_add_number_special(d, key, nr, TRUE);
384}
385
386/*
Bram Moolenaare0be1672018-07-08 16:50:37 +0200387 * Add a string entry to dictionary "d".
388 * Returns FAIL when out of memory and when key already exists.
389 */
390 int
391dict_add_string(dict_T *d, char *key, char_u *str)
392{
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100393 return dict_add_string_len(d, key, str, -1);
394}
395
396/*
397 * Add a string entry to dictionary "d".
398 * "str" will be copied to allocated memory.
399 * When "len" is -1 use the whole string, otherwise only this many bytes.
400 * Returns FAIL when out of memory and when key already exists.
401 */
402 int
403dict_add_string_len(dict_T *d, char *key, char_u *str, int len)
404{
Bram Moolenaare0be1672018-07-08 16:50:37 +0200405 dictitem_T *item;
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100406 char_u *val = NULL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200407
408 item = dictitem_alloc((char_u *)key);
409 if (item == NULL)
410 return FAIL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200411 item->di_tv.v_type = VAR_STRING;
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100412 if (str != NULL)
413 {
414 if (len == -1)
415 val = vim_strsave(str);
416 else
417 val = vim_strnsave(str, len);
418 }
419 item->di_tv.vval.v_string = val;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200420 if (dict_add(d, item) == FAIL)
421 {
422 dictitem_free(item);
423 return FAIL;
424 }
425 return OK;
426}
427
428/*
429 * Add a list entry to dictionary "d".
430 * Returns FAIL when out of memory and when key already exists.
431 */
432 int
433dict_add_list(dict_T *d, char *key, list_T *list)
434{
435 dictitem_T *item;
436
437 item = dictitem_alloc((char_u *)key);
438 if (item == NULL)
439 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200440 item->di_tv.v_type = VAR_LIST;
441 item->di_tv.vval.v_list = list;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100442 ++list->lv_refcount;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200443 if (dict_add(d, item) == FAIL)
444 {
445 dictitem_free(item);
446 return FAIL;
447 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200448 return OK;
449}
450
451/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200452 * Add a dict entry to dictionary "d".
453 * Returns FAIL when out of memory and when key already exists.
454 */
455 int
456dict_add_dict(dict_T *d, char *key, dict_T *dict)
457{
458 dictitem_T *item;
459
460 item = dictitem_alloc((char_u *)key);
461 if (item == NULL)
462 return FAIL;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200463 item->di_tv.v_type = VAR_DICT;
464 item->di_tv.vval.v_dict = dict;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100465 ++dict->dv_refcount;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200466 if (dict_add(d, item) == FAIL)
467 {
468 dictitem_free(item);
469 return FAIL;
470 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200471 return OK;
472}
473
474/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200475 * Get the number of items in a Dictionary.
476 */
477 long
478dict_len(dict_T *d)
479{
480 if (d == NULL)
481 return 0L;
482 return (long)d->dv_hashtab.ht_used;
483}
484
485/*
486 * Find item "key[len]" in Dictionary "d".
487 * If "len" is negative use strlen(key).
488 * Returns NULL when not found.
489 */
490 dictitem_T *
491dict_find(dict_T *d, char_u *key, int len)
492{
493#define AKEYLEN 200
494 char_u buf[AKEYLEN];
495 char_u *akey;
496 char_u *tofree = NULL;
497 hashitem_T *hi;
498
499 if (d == NULL)
500 return NULL;
501 if (len < 0)
502 akey = key;
503 else if (len >= AKEYLEN)
504 {
505 tofree = akey = vim_strnsave(key, len);
506 if (akey == NULL)
507 return NULL;
508 }
509 else
510 {
511 /* Avoid a malloc/free by using buf[]. */
512 vim_strncpy(buf, key, len);
513 akey = buf;
514 }
515
516 hi = hash_find(&d->dv_hashtab, akey);
517 vim_free(tofree);
518 if (HASHITEM_EMPTY(hi))
519 return NULL;
520 return HI2DI(hi);
521}
522
523/*
524 * Get a string item from a dictionary.
525 * When "save" is TRUE allocate memory for it.
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +0200526 * When FALSE a shared buffer is used, can only be used once!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200527 * Returns NULL if the entry doesn't exist or out of memory.
528 */
529 char_u *
Bram Moolenaar8f667172018-12-14 15:38:31 +0100530dict_get_string(dict_T *d, char_u *key, int save)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200531{
532 dictitem_T *di;
533 char_u *s;
534
535 di = dict_find(d, key, -1);
536 if (di == NULL)
537 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100538 s = tv_get_string(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200539 if (save && s != NULL)
540 s = vim_strsave(s);
541 return s;
542}
543
544/*
545 * Get a number item from a dictionary.
546 * Returns 0 if the entry doesn't exist.
547 */
548 varnumber_T
Bram Moolenaar8f667172018-12-14 15:38:31 +0100549dict_get_number(dict_T *d, char_u *key)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200550{
551 dictitem_T *di;
552
553 di = dict_find(d, key, -1);
554 if (di == NULL)
555 return 0;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100556 return tv_get_number(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200557}
558
559/*
560 * Return an allocated string with the string representation of a Dictionary.
561 * May return NULL.
562 */
563 char_u *
564dict2string(typval_T *tv, int copyID, int restore_copyID)
565{
566 garray_T ga;
567 int first = TRUE;
568 char_u *tofree;
569 char_u numbuf[NUMBUFLEN];
570 hashitem_T *hi;
571 char_u *s;
572 dict_T *d;
573 int todo;
574
575 if ((d = tv->vval.v_dict) == NULL)
576 return NULL;
577 ga_init2(&ga, (int)sizeof(char), 80);
578 ga_append(&ga, '{');
579
580 todo = (int)d->dv_hashtab.ht_used;
581 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
582 {
583 if (!HASHITEM_EMPTY(hi))
584 {
585 --todo;
586
587 if (first)
588 first = FALSE;
589 else
590 ga_concat(&ga, (char_u *)", ");
591
592 tofree = string_quote(hi->hi_key, FALSE);
593 if (tofree != NULL)
594 {
595 ga_concat(&ga, tofree);
596 vim_free(tofree);
597 }
598 ga_concat(&ga, (char_u *)": ");
599 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
600 FALSE, restore_copyID, TRUE);
601 if (s != NULL)
602 ga_concat(&ga, s);
603 vim_free(tofree);
604 if (s == NULL || did_echo_string_emsg)
605 break;
606 line_breakcheck();
607
608 }
609 }
610 if (todo > 0)
611 {
612 vim_free(ga.ga_data);
613 return NULL;
614 }
615
616 ga_append(&ga, '}');
617 ga_append(&ga, NUL);
618 return (char_u *)ga.ga_data;
619}
620
621/*
622 * Allocate a variable for a Dictionary and fill it from "*arg".
623 * Return OK or FAIL. Returns NOTDONE for {expr}.
624 */
625 int
Bram Moolenaar8f667172018-12-14 15:38:31 +0100626dict_get_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200627{
628 dict_T *d = NULL;
629 typval_T tvkey;
630 typval_T tv;
631 char_u *key = NULL;
632 dictitem_T *item;
633 char_u *start = skipwhite(*arg + 1);
634 char_u buf[NUMBUFLEN];
635
636 /*
637 * First check if it's not a curly-braces thing: {expr}.
638 * Must do this without evaluating, otherwise a function may be called
639 * twice. Unfortunately this means we need to call eval1() twice for the
640 * first item.
641 * But {} is an empty Dictionary.
642 */
643 if (*start != '}')
644 {
645 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
646 return FAIL;
647 if (*start == '}')
648 return NOTDONE;
649 }
650
651 if (evaluate)
652 {
653 d = dict_alloc();
654 if (d == NULL)
655 return FAIL;
656 }
657 tvkey.v_type = VAR_UNKNOWN;
658 tv.v_type = VAR_UNKNOWN;
659
660 *arg = skipwhite(*arg + 1);
661 while (**arg != '}' && **arg != NUL)
662 {
663 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
664 goto failret;
665 if (**arg != ':')
666 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100667 semsg(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200668 clear_tv(&tvkey);
669 goto failret;
670 }
671 if (evaluate)
672 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100673 key = tv_get_string_buf_chk(&tvkey, buf);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200674 if (key == NULL)
675 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100676 /* "key" is NULL when tv_get_string_buf_chk() gave an errmsg */
Bram Moolenaarcd524592016-07-17 14:57:05 +0200677 clear_tv(&tvkey);
678 goto failret;
679 }
680 }
681
682 *arg = skipwhite(*arg + 1);
683 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
684 {
685 if (evaluate)
686 clear_tv(&tvkey);
687 goto failret;
688 }
689 if (evaluate)
690 {
691 item = dict_find(d, key, -1);
692 if (item != NULL)
693 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100694 semsg(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200695 clear_tv(&tvkey);
696 clear_tv(&tv);
697 goto failret;
698 }
699 item = dictitem_alloc(key);
700 clear_tv(&tvkey);
701 if (item != NULL)
702 {
703 item->di_tv = tv;
704 item->di_tv.v_lock = 0;
705 if (dict_add(d, item) == FAIL)
706 dictitem_free(item);
707 }
708 }
709
710 if (**arg == '}')
711 break;
712 if (**arg != ',')
713 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100714 semsg(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200715 goto failret;
716 }
717 *arg = skipwhite(*arg + 1);
718 }
719
720 if (**arg != '}')
721 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100722 semsg(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200723failret:
724 if (evaluate)
725 dict_free(d);
726 return FAIL;
727 }
728
729 *arg = skipwhite(*arg + 1);
730 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200731 rettv_dict_set(rettv, d);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200732
733 return OK;
734}
735
736/*
737 * Go over all entries in "d2" and add them to "d1".
738 * When "action" is "error" then a duplicate key is an error.
739 * When "action" is "force" then a duplicate key is overwritten.
740 * Otherwise duplicate keys are ignored ("action" is "keep").
741 */
742 void
743dict_extend(dict_T *d1, dict_T *d2, char_u *action)
744{
745 dictitem_T *di1;
746 hashitem_T *hi2;
747 int todo;
748 char_u *arg_errmsg = (char_u *)N_("extend() argument");
749
750 todo = (int)d2->dv_hashtab.ht_used;
751 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
752 {
753 if (!HASHITEM_EMPTY(hi2))
754 {
755 --todo;
756 di1 = dict_find(d1, hi2->hi_key, -1);
757 if (d1->dv_scope != 0)
758 {
759 /* Disallow replacing a builtin function in l: and g:.
760 * Check the key to be valid when adding to any scope. */
761 if (d1->dv_scope == VAR_DEF_SCOPE
762 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
763 && var_check_func_name(hi2->hi_key, di1 == NULL))
764 break;
765 if (!valid_varname(hi2->hi_key))
766 break;
767 }
768 if (di1 == NULL)
769 {
770 di1 = dictitem_copy(HI2DI(hi2));
771 if (di1 != NULL && dict_add(d1, di1) == FAIL)
772 dictitem_free(di1);
773 }
774 else if (*action == 'e')
775 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100776 semsg(_("E737: Key already exists: %s"), hi2->hi_key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200777 break;
778 }
779 else if (*action == 'f' && HI2DI(hi2) != di1)
780 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +0100781 if (var_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
782 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaarcd524592016-07-17 14:57:05 +0200783 break;
784 clear_tv(&di1->di_tv);
785 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
786 }
787 }
788 }
789}
790
791/*
792 * Return the dictitem that an entry in a hashtable points to.
793 */
794 dictitem_T *
795dict_lookup(hashitem_T *hi)
796{
797 return HI2DI(hi);
798}
799
800/*
801 * Return TRUE when two dictionaries have exactly the same key/values.
802 */
803 int
804dict_equal(
805 dict_T *d1,
806 dict_T *d2,
807 int ic, /* ignore case for strings */
808 int recursive) /* TRUE when used recursively */
809{
810 hashitem_T *hi;
811 dictitem_T *item2;
812 int todo;
813
814 if (d1 == NULL && d2 == NULL)
815 return TRUE;
816 if (d1 == NULL || d2 == NULL)
817 return FALSE;
818 if (d1 == d2)
819 return TRUE;
820 if (dict_len(d1) != dict_len(d2))
821 return FALSE;
822
823 todo = (int)d1->dv_hashtab.ht_used;
824 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
825 {
826 if (!HASHITEM_EMPTY(hi))
827 {
828 item2 = dict_find(d2, hi->hi_key, -1);
829 if (item2 == NULL)
830 return FALSE;
831 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
832 return FALSE;
833 --todo;
834 }
835 }
836 return TRUE;
837}
838
839/*
840 * Turn a dict into a list:
841 * "what" == 0: list of keys
842 * "what" == 1: list of values
843 * "what" == 2: list of items
844 */
845 void
846dict_list(typval_T *argvars, typval_T *rettv, int what)
847{
848 list_T *l2;
849 dictitem_T *di;
850 hashitem_T *hi;
851 listitem_T *li;
852 listitem_T *li2;
853 dict_T *d;
854 int todo;
855
856 if (argvars[0].v_type != VAR_DICT)
857 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100858 emsg(_(e_dictreq));
Bram Moolenaarcd524592016-07-17 14:57:05 +0200859 return;
860 }
861 if ((d = argvars[0].vval.v_dict) == NULL)
862 return;
863
864 if (rettv_list_alloc(rettv) == FAIL)
865 return;
866
867 todo = (int)d->dv_hashtab.ht_used;
868 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
869 {
870 if (!HASHITEM_EMPTY(hi))
871 {
872 --todo;
873 di = HI2DI(hi);
874
875 li = listitem_alloc();
876 if (li == NULL)
877 break;
878 list_append(rettv->vval.v_list, li);
879
880 if (what == 0)
881 {
882 /* keys() */
883 li->li_tv.v_type = VAR_STRING;
884 li->li_tv.v_lock = 0;
885 li->li_tv.vval.v_string = vim_strsave(di->di_key);
886 }
887 else if (what == 1)
888 {
889 /* values() */
890 copy_tv(&di->di_tv, &li->li_tv);
891 }
892 else
893 {
894 /* items() */
895 l2 = list_alloc();
896 li->li_tv.v_type = VAR_LIST;
897 li->li_tv.v_lock = 0;
898 li->li_tv.vval.v_list = l2;
899 if (l2 == NULL)
900 break;
901 ++l2->lv_refcount;
902
903 li2 = listitem_alloc();
904 if (li2 == NULL)
905 break;
906 list_append(l2, li2);
907 li2->li_tv.v_type = VAR_STRING;
908 li2->li_tv.v_lock = 0;
909 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
910
911 li2 = listitem_alloc();
912 if (li2 == NULL)
913 break;
914 list_append(l2, li2);
915 copy_tv(&di->di_tv, &li2->li_tv);
916 }
917 }
918 }
919}
920
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100921/*
922 * Make each item in the dict readonly (not the value of the item).
923 */
924 void
925dict_set_items_ro(dict_T *di)
926{
927 int todo = (int)di->dv_hashtab.ht_used;
928 hashitem_T *hi;
929
930 /* Set readonly */
931 for (hi = di->dv_hashtab.ht_array; todo > 0 ; ++hi)
932 {
933 if (HASHITEM_EMPTY(hi))
934 continue;
935 --todo;
936 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
937 }
938}
939
Bram Moolenaarcd524592016-07-17 14:57:05 +0200940#endif /* defined(FEAT_EVAL) */