blob: 9c590064cceaa7ca185f61e8dd4278a90b4fbfbe [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/*
370 * Get the number of items in a Dictionary.
371 */
372 long
373dict_len(dict_T *d)
374{
375 if (d == NULL)
376 return 0L;
377 return (long)d->dv_hashtab.ht_used;
378}
379
380/*
381 * Find item "key[len]" in Dictionary "d".
382 * If "len" is negative use strlen(key).
383 * Returns NULL when not found.
384 */
385 dictitem_T *
386dict_find(dict_T *d, char_u *key, int len)
387{
388#define AKEYLEN 200
389 char_u buf[AKEYLEN];
390 char_u *akey;
391 char_u *tofree = NULL;
392 hashitem_T *hi;
393
394 if (d == NULL)
395 return NULL;
396 if (len < 0)
397 akey = key;
398 else if (len >= AKEYLEN)
399 {
400 tofree = akey = vim_strnsave(key, len);
401 if (akey == NULL)
402 return NULL;
403 }
404 else
405 {
406 /* Avoid a malloc/free by using buf[]. */
407 vim_strncpy(buf, key, len);
408 akey = buf;
409 }
410
411 hi = hash_find(&d->dv_hashtab, akey);
412 vim_free(tofree);
413 if (HASHITEM_EMPTY(hi))
414 return NULL;
415 return HI2DI(hi);
416}
417
418/*
419 * Get a string item from a dictionary.
420 * When "save" is TRUE allocate memory for it.
421 * Returns NULL if the entry doesn't exist or out of memory.
422 */
423 char_u *
424get_dict_string(dict_T *d, char_u *key, int save)
425{
426 dictitem_T *di;
427 char_u *s;
428
429 di = dict_find(d, key, -1);
430 if (di == NULL)
431 return NULL;
432 s = get_tv_string(&di->di_tv);
433 if (save && s != NULL)
434 s = vim_strsave(s);
435 return s;
436}
437
438/*
439 * Get a number item from a dictionary.
440 * Returns 0 if the entry doesn't exist.
441 */
442 varnumber_T
443get_dict_number(dict_T *d, char_u *key)
444{
445 dictitem_T *di;
446
447 di = dict_find(d, key, -1);
448 if (di == NULL)
449 return 0;
450 return get_tv_number(&di->di_tv);
451}
452
453/*
454 * Return an allocated string with the string representation of a Dictionary.
455 * May return NULL.
456 */
457 char_u *
458dict2string(typval_T *tv, int copyID, int restore_copyID)
459{
460 garray_T ga;
461 int first = TRUE;
462 char_u *tofree;
463 char_u numbuf[NUMBUFLEN];
464 hashitem_T *hi;
465 char_u *s;
466 dict_T *d;
467 int todo;
468
469 if ((d = tv->vval.v_dict) == NULL)
470 return NULL;
471 ga_init2(&ga, (int)sizeof(char), 80);
472 ga_append(&ga, '{');
473
474 todo = (int)d->dv_hashtab.ht_used;
475 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
476 {
477 if (!HASHITEM_EMPTY(hi))
478 {
479 --todo;
480
481 if (first)
482 first = FALSE;
483 else
484 ga_concat(&ga, (char_u *)", ");
485
486 tofree = string_quote(hi->hi_key, FALSE);
487 if (tofree != NULL)
488 {
489 ga_concat(&ga, tofree);
490 vim_free(tofree);
491 }
492 ga_concat(&ga, (char_u *)": ");
493 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
494 FALSE, restore_copyID, TRUE);
495 if (s != NULL)
496 ga_concat(&ga, s);
497 vim_free(tofree);
498 if (s == NULL || did_echo_string_emsg)
499 break;
500 line_breakcheck();
501
502 }
503 }
504 if (todo > 0)
505 {
506 vim_free(ga.ga_data);
507 return NULL;
508 }
509
510 ga_append(&ga, '}');
511 ga_append(&ga, NUL);
512 return (char_u *)ga.ga_data;
513}
514
515/*
516 * Allocate a variable for a Dictionary and fill it from "*arg".
517 * Return OK or FAIL. Returns NOTDONE for {expr}.
518 */
519 int
520get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
521{
522 dict_T *d = NULL;
523 typval_T tvkey;
524 typval_T tv;
525 char_u *key = NULL;
526 dictitem_T *item;
527 char_u *start = skipwhite(*arg + 1);
528 char_u buf[NUMBUFLEN];
529
530 /*
531 * First check if it's not a curly-braces thing: {expr}.
532 * Must do this without evaluating, otherwise a function may be called
533 * twice. Unfortunately this means we need to call eval1() twice for the
534 * first item.
535 * But {} is an empty Dictionary.
536 */
537 if (*start != '}')
538 {
539 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
540 return FAIL;
541 if (*start == '}')
542 return NOTDONE;
543 }
544
545 if (evaluate)
546 {
547 d = dict_alloc();
548 if (d == NULL)
549 return FAIL;
550 }
551 tvkey.v_type = VAR_UNKNOWN;
552 tv.v_type = VAR_UNKNOWN;
553
554 *arg = skipwhite(*arg + 1);
555 while (**arg != '}' && **arg != NUL)
556 {
557 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
558 goto failret;
559 if (**arg != ':')
560 {
561 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
562 clear_tv(&tvkey);
563 goto failret;
564 }
565 if (evaluate)
566 {
567 key = get_tv_string_buf_chk(&tvkey, buf);
568 if (key == NULL)
569 {
570 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
571 clear_tv(&tvkey);
572 goto failret;
573 }
574 }
575
576 *arg = skipwhite(*arg + 1);
577 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
578 {
579 if (evaluate)
580 clear_tv(&tvkey);
581 goto failret;
582 }
583 if (evaluate)
584 {
585 item = dict_find(d, key, -1);
586 if (item != NULL)
587 {
588 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
589 clear_tv(&tvkey);
590 clear_tv(&tv);
591 goto failret;
592 }
593 item = dictitem_alloc(key);
594 clear_tv(&tvkey);
595 if (item != NULL)
596 {
597 item->di_tv = tv;
598 item->di_tv.v_lock = 0;
599 if (dict_add(d, item) == FAIL)
600 dictitem_free(item);
601 }
602 }
603
604 if (**arg == '}')
605 break;
606 if (**arg != ',')
607 {
608 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
609 goto failret;
610 }
611 *arg = skipwhite(*arg + 1);
612 }
613
614 if (**arg != '}')
615 {
616 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
617failret:
618 if (evaluate)
619 dict_free(d);
620 return FAIL;
621 }
622
623 *arg = skipwhite(*arg + 1);
624 if (evaluate)
625 {
626 rettv->v_type = VAR_DICT;
627 rettv->vval.v_dict = d;
628 ++d->dv_refcount;
629 }
630
631 return OK;
632}
633
634/*
635 * Go over all entries in "d2" and add them to "d1".
636 * When "action" is "error" then a duplicate key is an error.
637 * When "action" is "force" then a duplicate key is overwritten.
638 * Otherwise duplicate keys are ignored ("action" is "keep").
639 */
640 void
641dict_extend(dict_T *d1, dict_T *d2, char_u *action)
642{
643 dictitem_T *di1;
644 hashitem_T *hi2;
645 int todo;
646 char_u *arg_errmsg = (char_u *)N_("extend() argument");
647
648 todo = (int)d2->dv_hashtab.ht_used;
649 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
650 {
651 if (!HASHITEM_EMPTY(hi2))
652 {
653 --todo;
654 di1 = dict_find(d1, hi2->hi_key, -1);
655 if (d1->dv_scope != 0)
656 {
657 /* Disallow replacing a builtin function in l: and g:.
658 * Check the key to be valid when adding to any scope. */
659 if (d1->dv_scope == VAR_DEF_SCOPE
660 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
661 && var_check_func_name(hi2->hi_key, di1 == NULL))
662 break;
663 if (!valid_varname(hi2->hi_key))
664 break;
665 }
666 if (di1 == NULL)
667 {
668 di1 = dictitem_copy(HI2DI(hi2));
669 if (di1 != NULL && dict_add(d1, di1) == FAIL)
670 dictitem_free(di1);
671 }
672 else if (*action == 'e')
673 {
674 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
675 break;
676 }
677 else if (*action == 'f' && HI2DI(hi2) != di1)
678 {
679 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
680 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
681 break;
682 clear_tv(&di1->di_tv);
683 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
684 }
685 }
686 }
687}
688
689/*
690 * Return the dictitem that an entry in a hashtable points to.
691 */
692 dictitem_T *
693dict_lookup(hashitem_T *hi)
694{
695 return HI2DI(hi);
696}
697
698/*
699 * Return TRUE when two dictionaries have exactly the same key/values.
700 */
701 int
702dict_equal(
703 dict_T *d1,
704 dict_T *d2,
705 int ic, /* ignore case for strings */
706 int recursive) /* TRUE when used recursively */
707{
708 hashitem_T *hi;
709 dictitem_T *item2;
710 int todo;
711
712 if (d1 == NULL && d2 == NULL)
713 return TRUE;
714 if (d1 == NULL || d2 == NULL)
715 return FALSE;
716 if (d1 == d2)
717 return TRUE;
718 if (dict_len(d1) != dict_len(d2))
719 return FALSE;
720
721 todo = (int)d1->dv_hashtab.ht_used;
722 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
723 {
724 if (!HASHITEM_EMPTY(hi))
725 {
726 item2 = dict_find(d2, hi->hi_key, -1);
727 if (item2 == NULL)
728 return FALSE;
729 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
730 return FALSE;
731 --todo;
732 }
733 }
734 return TRUE;
735}
736
737/*
738 * Turn a dict into a list:
739 * "what" == 0: list of keys
740 * "what" == 1: list of values
741 * "what" == 2: list of items
742 */
743 void
744dict_list(typval_T *argvars, typval_T *rettv, int what)
745{
746 list_T *l2;
747 dictitem_T *di;
748 hashitem_T *hi;
749 listitem_T *li;
750 listitem_T *li2;
751 dict_T *d;
752 int todo;
753
754 if (argvars[0].v_type != VAR_DICT)
755 {
756 EMSG(_(e_dictreq));
757 return;
758 }
759 if ((d = argvars[0].vval.v_dict) == NULL)
760 return;
761
762 if (rettv_list_alloc(rettv) == FAIL)
763 return;
764
765 todo = (int)d->dv_hashtab.ht_used;
766 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
767 {
768 if (!HASHITEM_EMPTY(hi))
769 {
770 --todo;
771 di = HI2DI(hi);
772
773 li = listitem_alloc();
774 if (li == NULL)
775 break;
776 list_append(rettv->vval.v_list, li);
777
778 if (what == 0)
779 {
780 /* keys() */
781 li->li_tv.v_type = VAR_STRING;
782 li->li_tv.v_lock = 0;
783 li->li_tv.vval.v_string = vim_strsave(di->di_key);
784 }
785 else if (what == 1)
786 {
787 /* values() */
788 copy_tv(&di->di_tv, &li->li_tv);
789 }
790 else
791 {
792 /* items() */
793 l2 = list_alloc();
794 li->li_tv.v_type = VAR_LIST;
795 li->li_tv.v_lock = 0;
796 li->li_tv.vval.v_list = l2;
797 if (l2 == NULL)
798 break;
799 ++l2->lv_refcount;
800
801 li2 = listitem_alloc();
802 if (li2 == NULL)
803 break;
804 list_append(l2, li2);
805 li2->li_tv.v_type = VAR_STRING;
806 li2->li_tv.v_lock = 0;
807 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
808
809 li2 = listitem_alloc();
810 if (li2 == NULL)
811 break;
812 list_append(l2, li2);
813 copy_tv(&di->di_tv, &li2->li_tv);
814 }
815 }
816 }
817}
818
819#endif /* defined(FEAT_EVAL) */