blob: d26356928b9bf6aece61e73c52e19d2739a9de86 [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
Bram Moolenaaradc67142019-06-22 01:40:42 +020031 d = ALLOC_CLEAR_ONE(dict_T);
Bram Moolenaarcd524592016-07-17 14:57:05 +020032 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
Bram Moolenaar51e14382019-05-25 20:21:28 +020057 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
Bram Moolenaarf49cc602018-11-11 15:21:05 +010058 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 Moolenaarb59e7352019-08-07 21:42:24 +0200213 di = alloc(offsetof(dictitem_T, di_key) + STRLEN(key) + 1);
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 Moolenaarb59e7352019-08-07 21:42:24 +0200231 di = alloc(offsetof(dictitem_T, di_key) + STRLEN(org->di_key) + 1);
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 Moolenaarae943152019-06-16 22:54:14 +0200451 * Add a callback to dictionary "d".
452 * Returns FAIL when out of memory and when key already exists.
453 */
454 int
455dict_add_callback(dict_T *d, char *key, callback_T *cb)
456{
457 dictitem_T *item;
458
459 item = dictitem_alloc((char_u *)key);
460 if (item == NULL)
461 return FAIL;
462 put_callback(cb, &item->di_tv);
463 if (dict_add(d, item) == FAIL)
464 {
465 dictitem_free(item);
466 return FAIL;
467 }
468 return OK;
469}
470
471/*
Bram Moolenaar45e18cb2019-04-28 18:05:35 +0200472 * Initializes "iter" for iterating over dictionary items with
473 * dict_iterate_next().
474 * If "var" is not a Dict or an empty Dict then there will be nothing to
475 * iterate over, no error is given.
476 * NOTE: The dictionary must not change until iterating is finished!
477 */
478 void
479dict_iterate_start(typval_T *var, dict_iterator_T *iter)
480{
481 if (var->v_type != VAR_DICT || var->vval.v_dict == NULL)
482 iter->dit_todo = 0;
483 else
484 {
485 dict_T *d = var->vval.v_dict;
486
487 iter->dit_todo = d->dv_hashtab.ht_used;
488 iter->dit_hi = d->dv_hashtab.ht_array;
489 }
490}
491
492/*
493 * Iterate over the items referred to by "iter". It should be initialized with
494 * dict_iterate_start().
495 * Returns a pointer to the key.
496 * "*tv_result" is set to point to the value for that key.
497 * If there are no more items, NULL is returned.
498 */
499 char_u *
500dict_iterate_next(dict_iterator_T *iter, typval_T **tv_result)
501{
502 dictitem_T *di;
503 char_u *result;
504
505 if (iter->dit_todo == 0)
506 return NULL;
507
508 while (HASHITEM_EMPTY(iter->dit_hi))
509 ++iter->dit_hi;
510
511 di = HI2DI(iter->dit_hi);
512 result = di->di_key;
513 *tv_result = &di->di_tv;
514
515 --iter->dit_todo;
516 ++iter->dit_hi;
517 return result;
518}
519
520/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200521 * Add a dict entry to dictionary "d".
522 * Returns FAIL when out of memory and when key already exists.
523 */
524 int
525dict_add_dict(dict_T *d, char *key, dict_T *dict)
526{
527 dictitem_T *item;
528
529 item = dictitem_alloc((char_u *)key);
530 if (item == NULL)
531 return FAIL;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200532 item->di_tv.v_type = VAR_DICT;
533 item->di_tv.vval.v_dict = dict;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100534 ++dict->dv_refcount;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200535 if (dict_add(d, item) == FAIL)
536 {
537 dictitem_free(item);
538 return FAIL;
539 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200540 return OK;
541}
542
543/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200544 * Get the number of items in a Dictionary.
545 */
546 long
547dict_len(dict_T *d)
548{
549 if (d == NULL)
550 return 0L;
551 return (long)d->dv_hashtab.ht_used;
552}
553
554/*
555 * Find item "key[len]" in Dictionary "d".
556 * If "len" is negative use strlen(key).
557 * Returns NULL when not found.
558 */
559 dictitem_T *
560dict_find(dict_T *d, char_u *key, int len)
561{
562#define AKEYLEN 200
563 char_u buf[AKEYLEN];
564 char_u *akey;
565 char_u *tofree = NULL;
566 hashitem_T *hi;
567
568 if (d == NULL)
569 return NULL;
570 if (len < 0)
571 akey = key;
572 else if (len >= AKEYLEN)
573 {
574 tofree = akey = vim_strnsave(key, len);
575 if (akey == NULL)
576 return NULL;
577 }
578 else
579 {
580 /* Avoid a malloc/free by using buf[]. */
581 vim_strncpy(buf, key, len);
582 akey = buf;
583 }
584
585 hi = hash_find(&d->dv_hashtab, akey);
586 vim_free(tofree);
587 if (HASHITEM_EMPTY(hi))
588 return NULL;
589 return HI2DI(hi);
590}
591
592/*
593 * Get a string item from a dictionary.
594 * When "save" is TRUE allocate memory for it.
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +0200595 * When FALSE a shared buffer is used, can only be used once!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200596 * Returns NULL if the entry doesn't exist or out of memory.
597 */
598 char_u *
Bram Moolenaar8f667172018-12-14 15:38:31 +0100599dict_get_string(dict_T *d, char_u *key, int save)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200600{
601 dictitem_T *di;
602 char_u *s;
603
604 di = dict_find(d, key, -1);
605 if (di == NULL)
606 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100607 s = tv_get_string(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200608 if (save && s != NULL)
609 s = vim_strsave(s);
610 return s;
611}
612
613/*
614 * Get a number item from a dictionary.
615 * Returns 0 if the entry doesn't exist.
616 */
617 varnumber_T
Bram Moolenaar8f667172018-12-14 15:38:31 +0100618dict_get_number(dict_T *d, char_u *key)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200619{
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200620 return dict_get_number_def(d, key, 0);
621}
622
623/*
624 * Get a number item from a dictionary.
625 * Returns "def" if the entry doesn't exist.
626 */
627 varnumber_T
628dict_get_number_def(dict_T *d, char_u *key, int def)
629{
Bram Moolenaarcd524592016-07-17 14:57:05 +0200630 dictitem_T *di;
631
632 di = dict_find(d, key, -1);
633 if (di == NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200634 return def;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100635 return tv_get_number(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200636}
637
638/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200639 * Get a number item from a dictionary.
640 * Returns 0 if the entry doesn't exist.
641 * Give an error if the entry is not a number.
642 */
643 varnumber_T
644dict_get_number_check(dict_T *d, char_u *key)
645{
646 dictitem_T *di;
647
648 di = dict_find(d, key, -1);
649 if (di == NULL)
650 return 0;
651 if (di->di_tv.v_type != VAR_NUMBER)
652 {
653 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
654 return 0;
655 }
656 return tv_get_number(&di->di_tv);
657}
658
659/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200660 * Return an allocated string with the string representation of a Dictionary.
661 * May return NULL.
662 */
663 char_u *
664dict2string(typval_T *tv, int copyID, int restore_copyID)
665{
666 garray_T ga;
667 int first = TRUE;
668 char_u *tofree;
669 char_u numbuf[NUMBUFLEN];
670 hashitem_T *hi;
671 char_u *s;
672 dict_T *d;
673 int todo;
674
675 if ((d = tv->vval.v_dict) == NULL)
676 return NULL;
677 ga_init2(&ga, (int)sizeof(char), 80);
678 ga_append(&ga, '{');
679
680 todo = (int)d->dv_hashtab.ht_used;
681 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
682 {
683 if (!HASHITEM_EMPTY(hi))
684 {
685 --todo;
686
687 if (first)
688 first = FALSE;
689 else
690 ga_concat(&ga, (char_u *)", ");
691
692 tofree = string_quote(hi->hi_key, FALSE);
693 if (tofree != NULL)
694 {
695 ga_concat(&ga, tofree);
696 vim_free(tofree);
697 }
698 ga_concat(&ga, (char_u *)": ");
699 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
700 FALSE, restore_copyID, TRUE);
701 if (s != NULL)
702 ga_concat(&ga, s);
703 vim_free(tofree);
704 if (s == NULL || did_echo_string_emsg)
705 break;
706 line_breakcheck();
707
708 }
709 }
710 if (todo > 0)
711 {
712 vim_free(ga.ga_data);
713 return NULL;
714 }
715
716 ga_append(&ga, '}');
717 ga_append(&ga, NUL);
718 return (char_u *)ga.ga_data;
719}
720
721/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +0200722 * Get the key for #{key: val} into "tv" and advance "arg".
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200723 * Return FAIL when there is no valid key.
724 */
725 static int
726get_literal_key(char_u **arg, typval_T *tv)
727{
728 char_u *p;
729
730 if (!ASCII_ISALNUM(**arg) && **arg != '_' && **arg != '-')
731 return FAIL;
732
733 for (p = *arg; ASCII_ISALNUM(*p) || *p == '_' || *p == '-'; ++p)
734 ;
735 tv->v_type = VAR_STRING;
736 tv->vval.v_string = vim_strnsave(*arg, (int)(p - *arg));
737
738 *arg = skipwhite(p);
739 return OK;
740}
741
742/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200743 * Allocate a variable for a Dictionary and fill it from "*arg".
Bram Moolenaar9f9fe372019-07-27 23:12:12 +0200744 * "literal" is TRUE for #{key: val}
Bram Moolenaarcd524592016-07-17 14:57:05 +0200745 * Return OK or FAIL. Returns NOTDONE for {expr}.
746 */
747 int
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200748dict_get_tv(char_u **arg, typval_T *rettv, int evaluate, int literal)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200749{
750 dict_T *d = NULL;
751 typval_T tvkey;
752 typval_T tv;
753 char_u *key = NULL;
754 dictitem_T *item;
755 char_u *start = skipwhite(*arg + 1);
756 char_u buf[NUMBUFLEN];
757
758 /*
759 * First check if it's not a curly-braces thing: {expr}.
760 * Must do this without evaluating, otherwise a function may be called
761 * twice. Unfortunately this means we need to call eval1() twice for the
762 * first item.
763 * But {} is an empty Dictionary.
764 */
765 if (*start != '}')
766 {
767 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
768 return FAIL;
769 if (*start == '}')
770 return NOTDONE;
771 }
772
773 if (evaluate)
774 {
775 d = dict_alloc();
776 if (d == NULL)
777 return FAIL;
778 }
779 tvkey.v_type = VAR_UNKNOWN;
780 tv.v_type = VAR_UNKNOWN;
781
782 *arg = skipwhite(*arg + 1);
783 while (**arg != '}' && **arg != NUL)
784 {
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200785 if ((literal
786 ? get_literal_key(arg, &tvkey)
787 : eval1(arg, &tvkey, evaluate)) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200788 goto failret;
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200789
Bram Moolenaarcd524592016-07-17 14:57:05 +0200790 if (**arg != ':')
791 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100792 semsg(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200793 clear_tv(&tvkey);
794 goto failret;
795 }
796 if (evaluate)
797 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100798 key = tv_get_string_buf_chk(&tvkey, buf);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200799 if (key == NULL)
800 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100801 /* "key" is NULL when tv_get_string_buf_chk() gave an errmsg */
Bram Moolenaarcd524592016-07-17 14:57:05 +0200802 clear_tv(&tvkey);
803 goto failret;
804 }
805 }
806
807 *arg = skipwhite(*arg + 1);
808 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
809 {
810 if (evaluate)
811 clear_tv(&tvkey);
812 goto failret;
813 }
814 if (evaluate)
815 {
816 item = dict_find(d, key, -1);
817 if (item != NULL)
818 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100819 semsg(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200820 clear_tv(&tvkey);
821 clear_tv(&tv);
822 goto failret;
823 }
824 item = dictitem_alloc(key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200825 if (item != NULL)
826 {
827 item->di_tv = tv;
828 item->di_tv.v_lock = 0;
829 if (dict_add(d, item) == FAIL)
830 dictitem_free(item);
831 }
832 }
Bram Moolenaara8931942019-09-28 17:25:10 +0200833 clear_tv(&tvkey);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200834
835 if (**arg == '}')
836 break;
837 if (**arg != ',')
838 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100839 semsg(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200840 goto failret;
841 }
842 *arg = skipwhite(*arg + 1);
843 }
844
845 if (**arg != '}')
846 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100847 semsg(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200848failret:
Bram Moolenaaradc67142019-06-22 01:40:42 +0200849 if (d != NULL)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200850 dict_free(d);
851 return FAIL;
852 }
853
854 *arg = skipwhite(*arg + 1);
855 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200856 rettv_dict_set(rettv, d);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200857
858 return OK;
859}
860
861/*
862 * Go over all entries in "d2" and add them to "d1".
863 * When "action" is "error" then a duplicate key is an error.
864 * When "action" is "force" then a duplicate key is overwritten.
865 * Otherwise duplicate keys are ignored ("action" is "keep").
866 */
867 void
868dict_extend(dict_T *d1, dict_T *d2, char_u *action)
869{
870 dictitem_T *di1;
871 hashitem_T *hi2;
872 int todo;
873 char_u *arg_errmsg = (char_u *)N_("extend() argument");
874
875 todo = (int)d2->dv_hashtab.ht_used;
876 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
877 {
878 if (!HASHITEM_EMPTY(hi2))
879 {
880 --todo;
881 di1 = dict_find(d1, hi2->hi_key, -1);
882 if (d1->dv_scope != 0)
883 {
884 /* Disallow replacing a builtin function in l: and g:.
885 * Check the key to be valid when adding to any scope. */
886 if (d1->dv_scope == VAR_DEF_SCOPE
887 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
888 && var_check_func_name(hi2->hi_key, di1 == NULL))
889 break;
890 if (!valid_varname(hi2->hi_key))
891 break;
892 }
893 if (di1 == NULL)
894 {
895 di1 = dictitem_copy(HI2DI(hi2));
896 if (di1 != NULL && dict_add(d1, di1) == FAIL)
897 dictitem_free(di1);
898 }
899 else if (*action == 'e')
900 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100901 semsg(_("E737: Key already exists: %s"), hi2->hi_key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200902 break;
903 }
904 else if (*action == 'f' && HI2DI(hi2) != di1)
905 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +0100906 if (var_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
907 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaarcd524592016-07-17 14:57:05 +0200908 break;
909 clear_tv(&di1->di_tv);
910 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
911 }
912 }
913 }
914}
915
916/*
917 * Return the dictitem that an entry in a hashtable points to.
918 */
919 dictitem_T *
920dict_lookup(hashitem_T *hi)
921{
922 return HI2DI(hi);
923}
924
925/*
926 * Return TRUE when two dictionaries have exactly the same key/values.
927 */
928 int
929dict_equal(
930 dict_T *d1,
931 dict_T *d2,
932 int ic, /* ignore case for strings */
933 int recursive) /* TRUE when used recursively */
934{
935 hashitem_T *hi;
936 dictitem_T *item2;
937 int todo;
938
939 if (d1 == NULL && d2 == NULL)
940 return TRUE;
941 if (d1 == NULL || d2 == NULL)
942 return FALSE;
943 if (d1 == d2)
944 return TRUE;
945 if (dict_len(d1) != dict_len(d2))
946 return FALSE;
947
948 todo = (int)d1->dv_hashtab.ht_used;
949 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
950 {
951 if (!HASHITEM_EMPTY(hi))
952 {
953 item2 = dict_find(d2, hi->hi_key, -1);
954 if (item2 == NULL)
955 return FALSE;
956 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
957 return FALSE;
958 --todo;
959 }
960 }
961 return TRUE;
962}
963
964/*
965 * Turn a dict into a list:
966 * "what" == 0: list of keys
967 * "what" == 1: list of values
968 * "what" == 2: list of items
969 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +0200970 static void
Bram Moolenaarcd524592016-07-17 14:57:05 +0200971dict_list(typval_T *argvars, typval_T *rettv, int what)
972{
973 list_T *l2;
974 dictitem_T *di;
975 hashitem_T *hi;
976 listitem_T *li;
977 listitem_T *li2;
978 dict_T *d;
979 int todo;
980
981 if (argvars[0].v_type != VAR_DICT)
982 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100983 emsg(_(e_dictreq));
Bram Moolenaarcd524592016-07-17 14:57:05 +0200984 return;
985 }
986 if ((d = argvars[0].vval.v_dict) == NULL)
987 return;
988
989 if (rettv_list_alloc(rettv) == FAIL)
990 return;
991
992 todo = (int)d->dv_hashtab.ht_used;
993 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
994 {
995 if (!HASHITEM_EMPTY(hi))
996 {
997 --todo;
998 di = HI2DI(hi);
999
1000 li = listitem_alloc();
1001 if (li == NULL)
1002 break;
1003 list_append(rettv->vval.v_list, li);
1004
1005 if (what == 0)
1006 {
1007 /* keys() */
1008 li->li_tv.v_type = VAR_STRING;
1009 li->li_tv.v_lock = 0;
1010 li->li_tv.vval.v_string = vim_strsave(di->di_key);
1011 }
1012 else if (what == 1)
1013 {
1014 /* values() */
1015 copy_tv(&di->di_tv, &li->li_tv);
1016 }
1017 else
1018 {
1019 /* items() */
1020 l2 = list_alloc();
1021 li->li_tv.v_type = VAR_LIST;
1022 li->li_tv.v_lock = 0;
1023 li->li_tv.vval.v_list = l2;
1024 if (l2 == NULL)
1025 break;
1026 ++l2->lv_refcount;
1027
1028 li2 = listitem_alloc();
1029 if (li2 == NULL)
1030 break;
1031 list_append(l2, li2);
1032 li2->li_tv.v_type = VAR_STRING;
1033 li2->li_tv.v_lock = 0;
1034 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
1035
1036 li2 = listitem_alloc();
1037 if (li2 == NULL)
1038 break;
1039 list_append(l2, li2);
1040 copy_tv(&di->di_tv, &li2->li_tv);
1041 }
1042 }
1043 }
1044}
1045
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001046/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001047 * "items(dict)" function
1048 */
1049 void
1050f_items(typval_T *argvars, typval_T *rettv)
1051{
1052 dict_list(argvars, rettv, 2);
1053}
1054
1055/*
1056 * "keys()" function
1057 */
1058 void
1059f_keys(typval_T *argvars, typval_T *rettv)
1060{
1061 dict_list(argvars, rettv, 0);
1062}
1063
1064/*
1065 * "values(dict)" function
1066 */
1067 void
1068f_values(typval_T *argvars, typval_T *rettv)
1069{
1070 dict_list(argvars, rettv, 1);
1071}
1072
1073/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001074 * Make each item in the dict readonly (not the value of the item).
1075 */
1076 void
1077dict_set_items_ro(dict_T *di)
1078{
1079 int todo = (int)di->dv_hashtab.ht_used;
1080 hashitem_T *hi;
1081
1082 /* Set readonly */
1083 for (hi = di->dv_hashtab.ht_array; todo > 0 ; ++hi)
1084 {
1085 if (HASHITEM_EMPTY(hi))
1086 continue;
1087 --todo;
1088 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
1089 }
1090}
1091
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001092/*
1093 * "has_key()" function
1094 */
1095 void
1096f_has_key(typval_T *argvars, typval_T *rettv)
1097{
1098 if (argvars[0].v_type != VAR_DICT)
1099 {
1100 emsg(_(e_dictreq));
1101 return;
1102 }
1103 if (argvars[0].vval.v_dict == NULL)
1104 return;
1105
1106 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
1107 tv_get_string(&argvars[1]), -1) != NULL;
1108}
1109
1110/*
1111 * "remove({dict})" function
1112 */
1113 void
1114dict_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1115{
1116 dict_T *d;
1117 char_u *key;
1118 dictitem_T *di;
1119
1120 if (argvars[2].v_type != VAR_UNKNOWN)
1121 semsg(_(e_toomanyarg), "remove()");
1122 else if ((d = argvars[0].vval.v_dict) != NULL
1123 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
1124 {
1125 key = tv_get_string_chk(&argvars[1]);
1126 if (key != NULL)
1127 {
1128 di = dict_find(d, key, -1);
1129 if (di == NULL)
1130 semsg(_(e_dictkey), key);
1131 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
1132 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
1133 {
1134 *rettv = di->di_tv;
1135 init_tv(&di->di_tv);
1136 dictitem_remove(d, di);
1137 }
1138 }
1139 }
1140}
1141
Bram Moolenaarcd524592016-07-17 14:57:05 +02001142#endif /* defined(FEAT_EVAL) */