blob: d78d42d5210de84c70081174a782684b33590174 [file] [log] [blame]
Bram Moolenaarcd524592016-07-17 14:57:05 +02001/* vi:set ts=8 sts=4 sw=4:
2 *
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
50/*
51 * Allocate an empty dict for a return value.
52 * Returns OK or FAIL.
53 */
54 int
55rettv_dict_alloc(typval_T *rettv)
56{
57 dict_T *d = dict_alloc();
58
59 if (d == NULL)
60 return FAIL;
61
62 rettv->vval.v_dict = d;
63 rettv->v_type = VAR_DICT;
64 rettv->v_lock = 0;
65 ++d->dv_refcount;
66 return OK;
67}
68
69/*
70 * Free a Dictionary, including all non-container items it contains.
71 * Ignores the reference count.
72 */
73 static void
74dict_free_contents(dict_T *d)
75{
76 int todo;
77 hashitem_T *hi;
78 dictitem_T *di;
79
80 /* Lock the hashtab, we don't want it to resize while freeing items. */
81 hash_lock(&d->dv_hashtab);
82 todo = (int)d->dv_hashtab.ht_used;
83 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
84 {
85 if (!HASHITEM_EMPTY(hi))
86 {
87 /* Remove the item before deleting it, just in case there is
88 * something recursive causing trouble. */
89 di = HI2DI(hi);
90 hash_remove(&d->dv_hashtab, hi);
91 clear_tv(&di->di_tv);
92 vim_free(di);
93 --todo;
94 }
95 }
96 hash_clear(&d->dv_hashtab);
97}
98
99 static void
100dict_free_dict(dict_T *d)
101{
102 /* Remove the dict from the list of dicts for garbage collection. */
103 if (d->dv_used_prev == NULL)
104 first_dict = d->dv_used_next;
105 else
106 d->dv_used_prev->dv_used_next = d->dv_used_next;
107 if (d->dv_used_next != NULL)
108 d->dv_used_next->dv_used_prev = d->dv_used_prev;
109 vim_free(d);
110}
111
112 static void
113dict_free(dict_T *d)
114{
115 if (!in_free_unref_items)
116 {
117 dict_free_contents(d);
118 dict_free_dict(d);
119 }
120}
121
122/*
123 * Unreference a Dictionary: decrement the reference count and free it when it
124 * becomes zero.
125 */
126 void
127dict_unref(dict_T *d)
128{
129 if (d != NULL && --d->dv_refcount <= 0)
130 dict_free(d);
131}
132
133/*
134 * Go through the list of dicts and free items without the copyID.
135 * Returns TRUE if something was freed.
136 */
137 int
138dict_free_nonref(int copyID)
139{
140 dict_T *dd;
141 int did_free = FALSE;
142
143 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
144 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
145 {
146 /* Free the Dictionary and ordinary items it contains, but don't
147 * recurse into Lists and Dictionaries, they will be in the list
148 * of dicts or list of lists. */
149 dict_free_contents(dd);
150 did_free = TRUE;
151 }
152 return did_free;
153}
154
155 void
156dict_free_items(int copyID)
157{
158 dict_T *dd, *dd_next;
159
160 for (dd = first_dict; dd != NULL; dd = dd_next)
161 {
162 dd_next = dd->dv_used_next;
163 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
164 dict_free_dict(dd);
165 }
166}
167
168/*
169 * Allocate a Dictionary item.
170 * The "key" is copied to the new item.
171 * Note that the value of the item "di_tv" still needs to be initialized!
172 * Returns NULL when out of memory.
173 */
174 dictitem_T *
175dictitem_alloc(char_u *key)
176{
177 dictitem_T *di;
178
179 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
180 if (di != NULL)
181 {
182 STRCPY(di->di_key, key);
183 di->di_flags = DI_FLAGS_ALLOC;
184 }
185 return di;
186}
187
188/*
189 * Make a copy of a Dictionary item.
190 */
191 static dictitem_T *
192dictitem_copy(dictitem_T *org)
193{
194 dictitem_T *di;
195
196 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
197 + STRLEN(org->di_key)));
198 if (di != NULL)
199 {
200 STRCPY(di->di_key, org->di_key);
201 di->di_flags = DI_FLAGS_ALLOC;
202 copy_tv(&org->di_tv, &di->di_tv);
203 }
204 return di;
205}
206
207/*
208 * Remove item "item" from Dictionary "dict" and free it.
209 */
210 void
211dictitem_remove(dict_T *dict, dictitem_T *item)
212{
213 hashitem_T *hi;
214
215 hi = hash_find(&dict->dv_hashtab, item->di_key);
216 if (HASHITEM_EMPTY(hi))
217 EMSG2(_(e_intern2), "dictitem_remove()");
218 else
219 hash_remove(&dict->dv_hashtab, hi);
220 dictitem_free(item);
221}
222
223/*
224 * Free a dict item. Also clears the value.
225 */
226 void
227dictitem_free(dictitem_T *item)
228{
229 clear_tv(&item->di_tv);
230 if (item->di_flags & DI_FLAGS_ALLOC)
231 vim_free(item);
232}
233
234/*
235 * Make a copy of dict "d". Shallow if "deep" is FALSE.
236 * The refcount of the new dict is set to 1.
237 * See item_copy() for "copyID".
238 * Returns NULL when out of memory.
239 */
240 dict_T *
241dict_copy(dict_T *orig, int deep, int copyID)
242{
243 dict_T *copy;
244 dictitem_T *di;
245 int todo;
246 hashitem_T *hi;
247
248 if (orig == NULL)
249 return NULL;
250
251 copy = dict_alloc();
252 if (copy != NULL)
253 {
254 if (copyID != 0)
255 {
256 orig->dv_copyID = copyID;
257 orig->dv_copydict = copy;
258 }
259 todo = (int)orig->dv_hashtab.ht_used;
260 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
261 {
262 if (!HASHITEM_EMPTY(hi))
263 {
264 --todo;
265
266 di = dictitem_alloc(hi->hi_key);
267 if (di == NULL)
268 break;
269 if (deep)
270 {
271 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
272 copyID) == FAIL)
273 {
274 vim_free(di);
275 break;
276 }
277 }
278 else
279 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
280 if (dict_add(copy, di) == FAIL)
281 {
282 dictitem_free(di);
283 break;
284 }
285 }
286 }
287
288 ++copy->dv_refcount;
289 if (todo > 0)
290 {
291 dict_unref(copy);
292 copy = NULL;
293 }
294 }
295
296 return copy;
297}
298
299/*
300 * Add item "item" to Dictionary "d".
301 * Returns FAIL when out of memory and when key already exists.
302 */
303 int
304dict_add(dict_T *d, dictitem_T *item)
305{
306 return hash_add(&d->dv_hashtab, item->di_key);
307}
308
309/*
310 * Add a number or string entry to dictionary "d".
311 * When "str" is NULL use number "nr", otherwise use "str".
312 * Returns FAIL when out of memory and when key already exists.
313 */
314 int
315dict_add_nr_str(
316 dict_T *d,
317 char *key,
318 varnumber_T nr,
319 char_u *str)
320{
321 dictitem_T *item;
322
323 item = dictitem_alloc((char_u *)key);
324 if (item == NULL)
325 return FAIL;
326 item->di_tv.v_lock = 0;
327 if (str == NULL)
328 {
329 item->di_tv.v_type = VAR_NUMBER;
330 item->di_tv.vval.v_number = nr;
331 }
332 else
333 {
334 item->di_tv.v_type = VAR_STRING;
335 item->di_tv.vval.v_string = vim_strsave(str);
336 }
337 if (dict_add(d, item) == FAIL)
338 {
339 dictitem_free(item);
340 return FAIL;
341 }
342 return OK;
343}
344
345/*
346 * Add a list entry to dictionary "d".
347 * Returns FAIL when out of memory and when key already exists.
348 */
349 int
350dict_add_list(dict_T *d, char *key, list_T *list)
351{
352 dictitem_T *item;
353
354 item = dictitem_alloc((char_u *)key);
355 if (item == NULL)
356 return FAIL;
357 item->di_tv.v_lock = 0;
358 item->di_tv.v_type = VAR_LIST;
359 item->di_tv.vval.v_list = list;
360 if (dict_add(d, item) == FAIL)
361 {
362 dictitem_free(item);
363 return FAIL;
364 }
365 ++list->lv_refcount;
366 return OK;
367}
368
369/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200370 * Add a dict entry to dictionary "d".
371 * Returns FAIL when out of memory and when key already exists.
372 */
373 int
374dict_add_dict(dict_T *d, char *key, dict_T *dict)
375{
376 dictitem_T *item;
377
378 item = dictitem_alloc((char_u *)key);
379 if (item == NULL)
380 return FAIL;
381 item->di_tv.v_lock = 0;
382 item->di_tv.v_type = VAR_DICT;
383 item->di_tv.vval.v_dict = dict;
384 if (dict_add(d, item) == FAIL)
385 {
386 dictitem_free(item);
387 return FAIL;
388 }
389 ++dict->dv_refcount;
390 return OK;
391}
392
393/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200394 * Get the number of items in a Dictionary.
395 */
396 long
397dict_len(dict_T *d)
398{
399 if (d == NULL)
400 return 0L;
401 return (long)d->dv_hashtab.ht_used;
402}
403
404/*
405 * Find item "key[len]" in Dictionary "d".
406 * If "len" is negative use strlen(key).
407 * Returns NULL when not found.
408 */
409 dictitem_T *
410dict_find(dict_T *d, char_u *key, int len)
411{
412#define AKEYLEN 200
413 char_u buf[AKEYLEN];
414 char_u *akey;
415 char_u *tofree = NULL;
416 hashitem_T *hi;
417
418 if (d == NULL)
419 return NULL;
420 if (len < 0)
421 akey = key;
422 else if (len >= AKEYLEN)
423 {
424 tofree = akey = vim_strnsave(key, len);
425 if (akey == NULL)
426 return NULL;
427 }
428 else
429 {
430 /* Avoid a malloc/free by using buf[]. */
431 vim_strncpy(buf, key, len);
432 akey = buf;
433 }
434
435 hi = hash_find(&d->dv_hashtab, akey);
436 vim_free(tofree);
437 if (HASHITEM_EMPTY(hi))
438 return NULL;
439 return HI2DI(hi);
440}
441
442/*
443 * Get a string item from a dictionary.
444 * When "save" is TRUE allocate memory for it.
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +0200445 * When FALSE a shared buffer is used, can only be used once!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200446 * Returns NULL if the entry doesn't exist or out of memory.
447 */
448 char_u *
449get_dict_string(dict_T *d, char_u *key, int save)
450{
451 dictitem_T *di;
452 char_u *s;
453
454 di = dict_find(d, key, -1);
455 if (di == NULL)
456 return NULL;
457 s = get_tv_string(&di->di_tv);
458 if (save && s != NULL)
459 s = vim_strsave(s);
460 return s;
461}
462
463/*
464 * Get a number item from a dictionary.
465 * Returns 0 if the entry doesn't exist.
466 */
467 varnumber_T
468get_dict_number(dict_T *d, char_u *key)
469{
470 dictitem_T *di;
471
472 di = dict_find(d, key, -1);
473 if (di == NULL)
474 return 0;
475 return get_tv_number(&di->di_tv);
476}
477
478/*
479 * Return an allocated string with the string representation of a Dictionary.
480 * May return NULL.
481 */
482 char_u *
483dict2string(typval_T *tv, int copyID, int restore_copyID)
484{
485 garray_T ga;
486 int first = TRUE;
487 char_u *tofree;
488 char_u numbuf[NUMBUFLEN];
489 hashitem_T *hi;
490 char_u *s;
491 dict_T *d;
492 int todo;
493
494 if ((d = tv->vval.v_dict) == NULL)
495 return NULL;
496 ga_init2(&ga, (int)sizeof(char), 80);
497 ga_append(&ga, '{');
498
499 todo = (int)d->dv_hashtab.ht_used;
500 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
501 {
502 if (!HASHITEM_EMPTY(hi))
503 {
504 --todo;
505
506 if (first)
507 first = FALSE;
508 else
509 ga_concat(&ga, (char_u *)", ");
510
511 tofree = string_quote(hi->hi_key, FALSE);
512 if (tofree != NULL)
513 {
514 ga_concat(&ga, tofree);
515 vim_free(tofree);
516 }
517 ga_concat(&ga, (char_u *)": ");
518 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
519 FALSE, restore_copyID, TRUE);
520 if (s != NULL)
521 ga_concat(&ga, s);
522 vim_free(tofree);
523 if (s == NULL || did_echo_string_emsg)
524 break;
525 line_breakcheck();
526
527 }
528 }
529 if (todo > 0)
530 {
531 vim_free(ga.ga_data);
532 return NULL;
533 }
534
535 ga_append(&ga, '}');
536 ga_append(&ga, NUL);
537 return (char_u *)ga.ga_data;
538}
539
540/*
541 * Allocate a variable for a Dictionary and fill it from "*arg".
542 * Return OK or FAIL. Returns NOTDONE for {expr}.
543 */
544 int
545get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
546{
547 dict_T *d = NULL;
548 typval_T tvkey;
549 typval_T tv;
550 char_u *key = NULL;
551 dictitem_T *item;
552 char_u *start = skipwhite(*arg + 1);
553 char_u buf[NUMBUFLEN];
554
555 /*
556 * First check if it's not a curly-braces thing: {expr}.
557 * Must do this without evaluating, otherwise a function may be called
558 * twice. Unfortunately this means we need to call eval1() twice for the
559 * first item.
560 * But {} is an empty Dictionary.
561 */
562 if (*start != '}')
563 {
564 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
565 return FAIL;
566 if (*start == '}')
567 return NOTDONE;
568 }
569
570 if (evaluate)
571 {
572 d = dict_alloc();
573 if (d == NULL)
574 return FAIL;
575 }
576 tvkey.v_type = VAR_UNKNOWN;
577 tv.v_type = VAR_UNKNOWN;
578
579 *arg = skipwhite(*arg + 1);
580 while (**arg != '}' && **arg != NUL)
581 {
582 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
583 goto failret;
584 if (**arg != ':')
585 {
586 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
587 clear_tv(&tvkey);
588 goto failret;
589 }
590 if (evaluate)
591 {
592 key = get_tv_string_buf_chk(&tvkey, buf);
593 if (key == NULL)
594 {
595 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
596 clear_tv(&tvkey);
597 goto failret;
598 }
599 }
600
601 *arg = skipwhite(*arg + 1);
602 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
603 {
604 if (evaluate)
605 clear_tv(&tvkey);
606 goto failret;
607 }
608 if (evaluate)
609 {
610 item = dict_find(d, key, -1);
611 if (item != NULL)
612 {
613 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
614 clear_tv(&tvkey);
615 clear_tv(&tv);
616 goto failret;
617 }
618 item = dictitem_alloc(key);
619 clear_tv(&tvkey);
620 if (item != NULL)
621 {
622 item->di_tv = tv;
623 item->di_tv.v_lock = 0;
624 if (dict_add(d, item) == FAIL)
625 dictitem_free(item);
626 }
627 }
628
629 if (**arg == '}')
630 break;
631 if (**arg != ',')
632 {
633 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
634 goto failret;
635 }
636 *arg = skipwhite(*arg + 1);
637 }
638
639 if (**arg != '}')
640 {
641 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
642failret:
643 if (evaluate)
644 dict_free(d);
645 return FAIL;
646 }
647
648 *arg = skipwhite(*arg + 1);
649 if (evaluate)
650 {
651 rettv->v_type = VAR_DICT;
652 rettv->vval.v_dict = d;
653 ++d->dv_refcount;
654 }
655
656 return OK;
657}
658
659/*
660 * Go over all entries in "d2" and add them to "d1".
661 * When "action" is "error" then a duplicate key is an error.
662 * When "action" is "force" then a duplicate key is overwritten.
663 * Otherwise duplicate keys are ignored ("action" is "keep").
664 */
665 void
666dict_extend(dict_T *d1, dict_T *d2, char_u *action)
667{
668 dictitem_T *di1;
669 hashitem_T *hi2;
670 int todo;
671 char_u *arg_errmsg = (char_u *)N_("extend() argument");
672
673 todo = (int)d2->dv_hashtab.ht_used;
674 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
675 {
676 if (!HASHITEM_EMPTY(hi2))
677 {
678 --todo;
679 di1 = dict_find(d1, hi2->hi_key, -1);
680 if (d1->dv_scope != 0)
681 {
682 /* Disallow replacing a builtin function in l: and g:.
683 * Check the key to be valid when adding to any scope. */
684 if (d1->dv_scope == VAR_DEF_SCOPE
685 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
686 && var_check_func_name(hi2->hi_key, di1 == NULL))
687 break;
688 if (!valid_varname(hi2->hi_key))
689 break;
690 }
691 if (di1 == NULL)
692 {
693 di1 = dictitem_copy(HI2DI(hi2));
694 if (di1 != NULL && dict_add(d1, di1) == FAIL)
695 dictitem_free(di1);
696 }
697 else if (*action == 'e')
698 {
699 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
700 break;
701 }
702 else if (*action == 'f' && HI2DI(hi2) != di1)
703 {
704 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
705 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
706 break;
707 clear_tv(&di1->di_tv);
708 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
709 }
710 }
711 }
712}
713
714/*
715 * Return the dictitem that an entry in a hashtable points to.
716 */
717 dictitem_T *
718dict_lookup(hashitem_T *hi)
719{
720 return HI2DI(hi);
721}
722
723/*
724 * Return TRUE when two dictionaries have exactly the same key/values.
725 */
726 int
727dict_equal(
728 dict_T *d1,
729 dict_T *d2,
730 int ic, /* ignore case for strings */
731 int recursive) /* TRUE when used recursively */
732{
733 hashitem_T *hi;
734 dictitem_T *item2;
735 int todo;
736
737 if (d1 == NULL && d2 == NULL)
738 return TRUE;
739 if (d1 == NULL || d2 == NULL)
740 return FALSE;
741 if (d1 == d2)
742 return TRUE;
743 if (dict_len(d1) != dict_len(d2))
744 return FALSE;
745
746 todo = (int)d1->dv_hashtab.ht_used;
747 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
748 {
749 if (!HASHITEM_EMPTY(hi))
750 {
751 item2 = dict_find(d2, hi->hi_key, -1);
752 if (item2 == NULL)
753 return FALSE;
754 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
755 return FALSE;
756 --todo;
757 }
758 }
759 return TRUE;
760}
761
762/*
763 * Turn a dict into a list:
764 * "what" == 0: list of keys
765 * "what" == 1: list of values
766 * "what" == 2: list of items
767 */
768 void
769dict_list(typval_T *argvars, typval_T *rettv, int what)
770{
771 list_T *l2;
772 dictitem_T *di;
773 hashitem_T *hi;
774 listitem_T *li;
775 listitem_T *li2;
776 dict_T *d;
777 int todo;
778
779 if (argvars[0].v_type != VAR_DICT)
780 {
781 EMSG(_(e_dictreq));
782 return;
783 }
784 if ((d = argvars[0].vval.v_dict) == NULL)
785 return;
786
787 if (rettv_list_alloc(rettv) == FAIL)
788 return;
789
790 todo = (int)d->dv_hashtab.ht_used;
791 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
792 {
793 if (!HASHITEM_EMPTY(hi))
794 {
795 --todo;
796 di = HI2DI(hi);
797
798 li = listitem_alloc();
799 if (li == NULL)
800 break;
801 list_append(rettv->vval.v_list, li);
802
803 if (what == 0)
804 {
805 /* keys() */
806 li->li_tv.v_type = VAR_STRING;
807 li->li_tv.v_lock = 0;
808 li->li_tv.vval.v_string = vim_strsave(di->di_key);
809 }
810 else if (what == 1)
811 {
812 /* values() */
813 copy_tv(&di->di_tv, &li->li_tv);
814 }
815 else
816 {
817 /* items() */
818 l2 = list_alloc();
819 li->li_tv.v_type = VAR_LIST;
820 li->li_tv.v_lock = 0;
821 li->li_tv.vval.v_list = l2;
822 if (l2 == NULL)
823 break;
824 ++l2->lv_refcount;
825
826 li2 = listitem_alloc();
827 if (li2 == NULL)
828 break;
829 list_append(l2, li2);
830 li2->li_tv.v_type = VAR_STRING;
831 li2->li_tv.v_lock = 0;
832 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
833
834 li2 = listitem_alloc();
835 if (li2 == NULL)
836 break;
837 list_append(l2, li2);
838 copy_tv(&di->di_tv, &li2->li_tv);
839 }
840 }
841 }
842}
843
844#endif /* defined(FEAT_EVAL) */