blob: 9ba0231f2b5ae1254e774b055b62d70a9d5d3406 [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 Moolenaare0be1672018-07-08 16:50:37 +0200345 * Add a number entry to dictionary "d".
Bram Moolenaarcd524592016-07-17 14:57:05 +0200346 * Returns FAIL when out of memory and when key already exists.
347 */
348 int
Bram Moolenaare0be1672018-07-08 16:50:37 +0200349dict_add_number(dict_T *d, char *key, varnumber_T nr)
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 Moolenaare0be1672018-07-08 16:50:37 +0200356 item->di_tv.v_type = VAR_NUMBER;
357 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/*
367 * Add a string entry to dictionary "d".
368 * Returns FAIL when out of memory and when key already exists.
369 */
370 int
371dict_add_string(dict_T *d, char *key, char_u *str)
372{
373 dictitem_T *item;
374
375 item = dictitem_alloc((char_u *)key);
376 if (item == NULL)
377 return FAIL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200378 item->di_tv.v_type = VAR_STRING;
379 item->di_tv.vval.v_string = str != NULL ? vim_strsave(str) : NULL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200380 if (dict_add(d, item) == FAIL)
381 {
382 dictitem_free(item);
383 return FAIL;
384 }
385 return OK;
386}
387
388/*
389 * Add a list entry to dictionary "d".
390 * Returns FAIL when out of memory and when key already exists.
391 */
392 int
393dict_add_list(dict_T *d, char *key, list_T *list)
394{
395 dictitem_T *item;
396
397 item = dictitem_alloc((char_u *)key);
398 if (item == NULL)
399 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200400 item->di_tv.v_type = VAR_LIST;
401 item->di_tv.vval.v_list = list;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100402 ++list->lv_refcount;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200403 if (dict_add(d, item) == FAIL)
404 {
405 dictitem_free(item);
406 return FAIL;
407 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200408 return OK;
409}
410
411/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200412 * Add a dict entry to dictionary "d".
413 * Returns FAIL when out of memory and when key already exists.
414 */
415 int
416dict_add_dict(dict_T *d, char *key, dict_T *dict)
417{
418 dictitem_T *item;
419
420 item = dictitem_alloc((char_u *)key);
421 if (item == NULL)
422 return FAIL;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200423 item->di_tv.v_type = VAR_DICT;
424 item->di_tv.vval.v_dict = dict;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100425 ++dict->dv_refcount;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200426 if (dict_add(d, item) == FAIL)
427 {
428 dictitem_free(item);
429 return FAIL;
430 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200431 return OK;
432}
433
434/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200435 * Get the number of items in a Dictionary.
436 */
437 long
438dict_len(dict_T *d)
439{
440 if (d == NULL)
441 return 0L;
442 return (long)d->dv_hashtab.ht_used;
443}
444
445/*
446 * Find item "key[len]" in Dictionary "d".
447 * If "len" is negative use strlen(key).
448 * Returns NULL when not found.
449 */
450 dictitem_T *
451dict_find(dict_T *d, char_u *key, int len)
452{
453#define AKEYLEN 200
454 char_u buf[AKEYLEN];
455 char_u *akey;
456 char_u *tofree = NULL;
457 hashitem_T *hi;
458
459 if (d == NULL)
460 return NULL;
461 if (len < 0)
462 akey = key;
463 else if (len >= AKEYLEN)
464 {
465 tofree = akey = vim_strnsave(key, len);
466 if (akey == NULL)
467 return NULL;
468 }
469 else
470 {
471 /* Avoid a malloc/free by using buf[]. */
472 vim_strncpy(buf, key, len);
473 akey = buf;
474 }
475
476 hi = hash_find(&d->dv_hashtab, akey);
477 vim_free(tofree);
478 if (HASHITEM_EMPTY(hi))
479 return NULL;
480 return HI2DI(hi);
481}
482
483/*
484 * Get a string item from a dictionary.
485 * When "save" is TRUE allocate memory for it.
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +0200486 * When FALSE a shared buffer is used, can only be used once!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200487 * Returns NULL if the entry doesn't exist or out of memory.
488 */
489 char_u *
Bram Moolenaar8f667172018-12-14 15:38:31 +0100490dict_get_string(dict_T *d, char_u *key, int save)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200491{
492 dictitem_T *di;
493 char_u *s;
494
495 di = dict_find(d, key, -1);
496 if (di == NULL)
497 return NULL;
498 s = get_tv_string(&di->di_tv);
499 if (save && s != NULL)
500 s = vim_strsave(s);
501 return s;
502}
503
504/*
505 * Get a number item from a dictionary.
506 * Returns 0 if the entry doesn't exist.
507 */
508 varnumber_T
Bram Moolenaar8f667172018-12-14 15:38:31 +0100509dict_get_number(dict_T *d, char_u *key)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200510{
511 dictitem_T *di;
512
513 di = dict_find(d, key, -1);
514 if (di == NULL)
515 return 0;
516 return get_tv_number(&di->di_tv);
517}
518
519/*
520 * Return an allocated string with the string representation of a Dictionary.
521 * May return NULL.
522 */
523 char_u *
524dict2string(typval_T *tv, int copyID, int restore_copyID)
525{
526 garray_T ga;
527 int first = TRUE;
528 char_u *tofree;
529 char_u numbuf[NUMBUFLEN];
530 hashitem_T *hi;
531 char_u *s;
532 dict_T *d;
533 int todo;
534
535 if ((d = tv->vval.v_dict) == NULL)
536 return NULL;
537 ga_init2(&ga, (int)sizeof(char), 80);
538 ga_append(&ga, '{');
539
540 todo = (int)d->dv_hashtab.ht_used;
541 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
542 {
543 if (!HASHITEM_EMPTY(hi))
544 {
545 --todo;
546
547 if (first)
548 first = FALSE;
549 else
550 ga_concat(&ga, (char_u *)", ");
551
552 tofree = string_quote(hi->hi_key, FALSE);
553 if (tofree != NULL)
554 {
555 ga_concat(&ga, tofree);
556 vim_free(tofree);
557 }
558 ga_concat(&ga, (char_u *)": ");
559 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
560 FALSE, restore_copyID, TRUE);
561 if (s != NULL)
562 ga_concat(&ga, s);
563 vim_free(tofree);
564 if (s == NULL || did_echo_string_emsg)
565 break;
566 line_breakcheck();
567
568 }
569 }
570 if (todo > 0)
571 {
572 vim_free(ga.ga_data);
573 return NULL;
574 }
575
576 ga_append(&ga, '}');
577 ga_append(&ga, NUL);
578 return (char_u *)ga.ga_data;
579}
580
581/*
582 * Allocate a variable for a Dictionary and fill it from "*arg".
583 * Return OK or FAIL. Returns NOTDONE for {expr}.
584 */
585 int
Bram Moolenaar8f667172018-12-14 15:38:31 +0100586dict_get_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200587{
588 dict_T *d = NULL;
589 typval_T tvkey;
590 typval_T tv;
591 char_u *key = NULL;
592 dictitem_T *item;
593 char_u *start = skipwhite(*arg + 1);
594 char_u buf[NUMBUFLEN];
595
596 /*
597 * First check if it's not a curly-braces thing: {expr}.
598 * Must do this without evaluating, otherwise a function may be called
599 * twice. Unfortunately this means we need to call eval1() twice for the
600 * first item.
601 * But {} is an empty Dictionary.
602 */
603 if (*start != '}')
604 {
605 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
606 return FAIL;
607 if (*start == '}')
608 return NOTDONE;
609 }
610
611 if (evaluate)
612 {
613 d = dict_alloc();
614 if (d == NULL)
615 return FAIL;
616 }
617 tvkey.v_type = VAR_UNKNOWN;
618 tv.v_type = VAR_UNKNOWN;
619
620 *arg = skipwhite(*arg + 1);
621 while (**arg != '}' && **arg != NUL)
622 {
623 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
624 goto failret;
625 if (**arg != ':')
626 {
627 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
628 clear_tv(&tvkey);
629 goto failret;
630 }
631 if (evaluate)
632 {
633 key = get_tv_string_buf_chk(&tvkey, buf);
634 if (key == NULL)
635 {
636 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
637 clear_tv(&tvkey);
638 goto failret;
639 }
640 }
641
642 *arg = skipwhite(*arg + 1);
643 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
644 {
645 if (evaluate)
646 clear_tv(&tvkey);
647 goto failret;
648 }
649 if (evaluate)
650 {
651 item = dict_find(d, key, -1);
652 if (item != NULL)
653 {
654 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
655 clear_tv(&tvkey);
656 clear_tv(&tv);
657 goto failret;
658 }
659 item = dictitem_alloc(key);
660 clear_tv(&tvkey);
661 if (item != NULL)
662 {
663 item->di_tv = tv;
664 item->di_tv.v_lock = 0;
665 if (dict_add(d, item) == FAIL)
666 dictitem_free(item);
667 }
668 }
669
670 if (**arg == '}')
671 break;
672 if (**arg != ',')
673 {
674 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
675 goto failret;
676 }
677 *arg = skipwhite(*arg + 1);
678 }
679
680 if (**arg != '}')
681 {
682 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
683failret:
684 if (evaluate)
685 dict_free(d);
686 return FAIL;
687 }
688
689 *arg = skipwhite(*arg + 1);
690 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200691 rettv_dict_set(rettv, d);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200692
693 return OK;
694}
695
696/*
697 * Go over all entries in "d2" and add them to "d1".
698 * When "action" is "error" then a duplicate key is an error.
699 * When "action" is "force" then a duplicate key is overwritten.
700 * Otherwise duplicate keys are ignored ("action" is "keep").
701 */
702 void
703dict_extend(dict_T *d1, dict_T *d2, char_u *action)
704{
705 dictitem_T *di1;
706 hashitem_T *hi2;
707 int todo;
708 char_u *arg_errmsg = (char_u *)N_("extend() argument");
709
710 todo = (int)d2->dv_hashtab.ht_used;
711 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
712 {
713 if (!HASHITEM_EMPTY(hi2))
714 {
715 --todo;
716 di1 = dict_find(d1, hi2->hi_key, -1);
717 if (d1->dv_scope != 0)
718 {
719 /* Disallow replacing a builtin function in l: and g:.
720 * Check the key to be valid when adding to any scope. */
721 if (d1->dv_scope == VAR_DEF_SCOPE
722 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
723 && var_check_func_name(hi2->hi_key, di1 == NULL))
724 break;
725 if (!valid_varname(hi2->hi_key))
726 break;
727 }
728 if (di1 == NULL)
729 {
730 di1 = dictitem_copy(HI2DI(hi2));
731 if (di1 != NULL && dict_add(d1, di1) == FAIL)
732 dictitem_free(di1);
733 }
734 else if (*action == 'e')
735 {
736 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
737 break;
738 }
739 else if (*action == 'f' && HI2DI(hi2) != di1)
740 {
741 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
742 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
743 break;
744 clear_tv(&di1->di_tv);
745 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
746 }
747 }
748 }
749}
750
751/*
752 * Return the dictitem that an entry in a hashtable points to.
753 */
754 dictitem_T *
755dict_lookup(hashitem_T *hi)
756{
757 return HI2DI(hi);
758}
759
760/*
761 * Return TRUE when two dictionaries have exactly the same key/values.
762 */
763 int
764dict_equal(
765 dict_T *d1,
766 dict_T *d2,
767 int ic, /* ignore case for strings */
768 int recursive) /* TRUE when used recursively */
769{
770 hashitem_T *hi;
771 dictitem_T *item2;
772 int todo;
773
774 if (d1 == NULL && d2 == NULL)
775 return TRUE;
776 if (d1 == NULL || d2 == NULL)
777 return FALSE;
778 if (d1 == d2)
779 return TRUE;
780 if (dict_len(d1) != dict_len(d2))
781 return FALSE;
782
783 todo = (int)d1->dv_hashtab.ht_used;
784 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
785 {
786 if (!HASHITEM_EMPTY(hi))
787 {
788 item2 = dict_find(d2, hi->hi_key, -1);
789 if (item2 == NULL)
790 return FALSE;
791 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
792 return FALSE;
793 --todo;
794 }
795 }
796 return TRUE;
797}
798
799/*
800 * Turn a dict into a list:
801 * "what" == 0: list of keys
802 * "what" == 1: list of values
803 * "what" == 2: list of items
804 */
805 void
806dict_list(typval_T *argvars, typval_T *rettv, int what)
807{
808 list_T *l2;
809 dictitem_T *di;
810 hashitem_T *hi;
811 listitem_T *li;
812 listitem_T *li2;
813 dict_T *d;
814 int todo;
815
816 if (argvars[0].v_type != VAR_DICT)
817 {
818 EMSG(_(e_dictreq));
819 return;
820 }
821 if ((d = argvars[0].vval.v_dict) == NULL)
822 return;
823
824 if (rettv_list_alloc(rettv) == FAIL)
825 return;
826
827 todo = (int)d->dv_hashtab.ht_used;
828 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
829 {
830 if (!HASHITEM_EMPTY(hi))
831 {
832 --todo;
833 di = HI2DI(hi);
834
835 li = listitem_alloc();
836 if (li == NULL)
837 break;
838 list_append(rettv->vval.v_list, li);
839
840 if (what == 0)
841 {
842 /* keys() */
843 li->li_tv.v_type = VAR_STRING;
844 li->li_tv.v_lock = 0;
845 li->li_tv.vval.v_string = vim_strsave(di->di_key);
846 }
847 else if (what == 1)
848 {
849 /* values() */
850 copy_tv(&di->di_tv, &li->li_tv);
851 }
852 else
853 {
854 /* items() */
855 l2 = list_alloc();
856 li->li_tv.v_type = VAR_LIST;
857 li->li_tv.v_lock = 0;
858 li->li_tv.vval.v_list = l2;
859 if (l2 == NULL)
860 break;
861 ++l2->lv_refcount;
862
863 li2 = listitem_alloc();
864 if (li2 == NULL)
865 break;
866 list_append(l2, li2);
867 li2->li_tv.v_type = VAR_STRING;
868 li2->li_tv.v_lock = 0;
869 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
870
871 li2 = listitem_alloc();
872 if (li2 == NULL)
873 break;
874 list_append(l2, li2);
875 copy_tv(&di->di_tv, &li2->li_tv);
876 }
877 }
878 }
879}
880
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100881/*
882 * Make each item in the dict readonly (not the value of the item).
883 */
884 void
885dict_set_items_ro(dict_T *di)
886{
887 int todo = (int)di->dv_hashtab.ht_used;
888 hashitem_T *hi;
889
890 /* Set readonly */
891 for (hi = di->dv_hashtab.ht_array; todo > 0 ; ++hi)
892 {
893 if (HASHITEM_EMPTY(hi))
894 continue;
895 --todo;
896 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
897 }
898}
899
Bram Moolenaarcd524592016-07-17 14:57:05 +0200900#endif /* defined(FEAT_EVAL) */