blob: 748a9917ebc98d70f03f86bbb986466558169a14 [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
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010018// 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;
Bram Moolenaarcd524592016-07-17 14:57:05 +020022
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 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010034 // Add the dict to the list of dicts for garbage collection.
Bram Moolenaarcd524592016-07-17 14:57:05 +020035 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{
Bram Moolenaar89483d42020-05-10 15:24:44 +0200108 hashtab_free_contents(&d->dv_hashtab);
109}
110
111/*
112 * Clear hashtab "ht" and dict items it contains.
113 */
114 void
115hashtab_free_contents(hashtab_T *ht)
116{
Bram Moolenaarcd524592016-07-17 14:57:05 +0200117 int todo;
118 hashitem_T *hi;
119 dictitem_T *di;
120
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100121 // Lock the hashtab, we don't want it to resize while freeing items.
Bram Moolenaar89483d42020-05-10 15:24:44 +0200122 hash_lock(ht);
123 todo = (int)ht->ht_used;
124 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200125 {
126 if (!HASHITEM_EMPTY(hi))
127 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100128 // Remove the item before deleting it, just in case there is
129 // something recursive causing trouble.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200130 di = HI2DI(hi);
Bram Moolenaar89483d42020-05-10 15:24:44 +0200131 hash_remove(ht, hi);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100132 dictitem_free(di);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200133 --todo;
134 }
135 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100136
Bram Moolenaar89483d42020-05-10 15:24:44 +0200137 // The hashtab is still locked, it has to be re-initialized anyway.
138 hash_clear(ht);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200139}
140
141 static void
142dict_free_dict(dict_T *d)
143{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100144 // Remove the dict from the list of dicts for garbage collection.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200145 if (d->dv_used_prev == NULL)
146 first_dict = d->dv_used_next;
147 else
148 d->dv_used_prev->dv_used_next = d->dv_used_next;
149 if (d->dv_used_next != NULL)
150 d->dv_used_next->dv_used_prev = d->dv_used_prev;
151 vim_free(d);
152}
153
154 static void
155dict_free(dict_T *d)
156{
157 if (!in_free_unref_items)
158 {
159 dict_free_contents(d);
160 dict_free_dict(d);
161 }
162}
163
164/*
165 * Unreference a Dictionary: decrement the reference count and free it when it
166 * becomes zero.
167 */
168 void
169dict_unref(dict_T *d)
170{
171 if (d != NULL && --d->dv_refcount <= 0)
172 dict_free(d);
173}
174
175/*
176 * Go through the list of dicts and free items without the copyID.
177 * Returns TRUE if something was freed.
178 */
179 int
180dict_free_nonref(int copyID)
181{
182 dict_T *dd;
183 int did_free = FALSE;
184
185 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
186 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
187 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100188 // Free the Dictionary and ordinary items it contains, but don't
189 // recurse into Lists and Dictionaries, they will be in the list
190 // of dicts or list of lists.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200191 dict_free_contents(dd);
192 did_free = TRUE;
193 }
194 return did_free;
195}
196
197 void
198dict_free_items(int copyID)
199{
200 dict_T *dd, *dd_next;
201
202 for (dd = first_dict; dd != NULL; dd = dd_next)
203 {
204 dd_next = dd->dv_used_next;
205 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
206 dict_free_dict(dd);
207 }
208}
209
210/*
211 * Allocate a Dictionary item.
212 * The "key" is copied to the new item.
Bram Moolenaarc89d4b32018-07-08 17:19:02 +0200213 * Note that the type and value of the item "di_tv" still needs to be
214 * initialized!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200215 * Returns NULL when out of memory.
216 */
217 dictitem_T *
218dictitem_alloc(char_u *key)
219{
220 dictitem_T *di;
221
Bram Moolenaarb59e7352019-08-07 21:42:24 +0200222 di = alloc(offsetof(dictitem_T, di_key) + STRLEN(key) + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200223 if (di != NULL)
224 {
225 STRCPY(di->di_key, key);
226 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaarc89d4b32018-07-08 17:19:02 +0200227 di->di_tv.v_lock = 0;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200228 }
229 return di;
230}
231
232/*
233 * Make a copy of a Dictionary item.
234 */
235 static dictitem_T *
236dictitem_copy(dictitem_T *org)
237{
238 dictitem_T *di;
239
Bram Moolenaarb59e7352019-08-07 21:42:24 +0200240 di = alloc(offsetof(dictitem_T, di_key) + STRLEN(org->di_key) + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200241 if (di != NULL)
242 {
243 STRCPY(di->di_key, org->di_key);
244 di->di_flags = DI_FLAGS_ALLOC;
245 copy_tv(&org->di_tv, &di->di_tv);
246 }
247 return di;
248}
249
250/*
251 * Remove item "item" from Dictionary "dict" and free it.
252 */
253 void
254dictitem_remove(dict_T *dict, dictitem_T *item)
255{
256 hashitem_T *hi;
257
258 hi = hash_find(&dict->dv_hashtab, item->di_key);
259 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100260 internal_error("dictitem_remove()");
Bram Moolenaarcd524592016-07-17 14:57:05 +0200261 else
262 hash_remove(&dict->dv_hashtab, hi);
263 dictitem_free(item);
264}
265
266/*
267 * Free a dict item. Also clears the value.
268 */
269 void
270dictitem_free(dictitem_T *item)
271{
272 clear_tv(&item->di_tv);
273 if (item->di_flags & DI_FLAGS_ALLOC)
274 vim_free(item);
275}
276
277/*
278 * Make a copy of dict "d". Shallow if "deep" is FALSE.
279 * The refcount of the new dict is set to 1.
280 * See item_copy() for "copyID".
281 * Returns NULL when out of memory.
282 */
283 dict_T *
284dict_copy(dict_T *orig, int deep, int copyID)
285{
286 dict_T *copy;
287 dictitem_T *di;
288 int todo;
289 hashitem_T *hi;
290
291 if (orig == NULL)
292 return NULL;
293
294 copy = dict_alloc();
295 if (copy != NULL)
296 {
297 if (copyID != 0)
298 {
299 orig->dv_copyID = copyID;
300 orig->dv_copydict = copy;
301 }
302 todo = (int)orig->dv_hashtab.ht_used;
303 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
304 {
305 if (!HASHITEM_EMPTY(hi))
306 {
307 --todo;
308
309 di = dictitem_alloc(hi->hi_key);
310 if (di == NULL)
311 break;
312 if (deep)
313 {
314 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
315 copyID) == FAIL)
316 {
317 vim_free(di);
318 break;
319 }
320 }
321 else
322 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
323 if (dict_add(copy, di) == FAIL)
324 {
325 dictitem_free(di);
326 break;
327 }
328 }
329 }
330
331 ++copy->dv_refcount;
332 if (todo > 0)
333 {
334 dict_unref(copy);
335 copy = NULL;
336 }
337 }
338
339 return copy;
340}
341
342/*
343 * Add item "item" to Dictionary "d".
344 * Returns FAIL when out of memory and when key already exists.
345 */
346 int
347dict_add(dict_T *d, dictitem_T *item)
348{
349 return hash_add(&d->dv_hashtab, item->di_key);
350}
351
352/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200353 * Add a number or special entry to dictionary "d".
Bram Moolenaarcd524592016-07-17 14:57:05 +0200354 * Returns FAIL when out of memory and when key already exists.
355 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200356 static int
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100357dict_add_number_special(dict_T *d, char *key, varnumber_T nr, vartype_T vartype)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200358{
359 dictitem_T *item;
360
361 item = dictitem_alloc((char_u *)key);
362 if (item == NULL)
363 return FAIL;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100364 item->di_tv.v_type = vartype;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200365 item->di_tv.vval.v_number = nr;
366 if (dict_add(d, item) == FAIL)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200367 {
Bram Moolenaare0be1672018-07-08 16:50:37 +0200368 dictitem_free(item);
369 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200370 }
Bram Moolenaare0be1672018-07-08 16:50:37 +0200371 return OK;
372}
373
374/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200375 * Add a number entry to dictionary "d".
376 * Returns FAIL when out of memory and when key already exists.
377 */
378 int
379dict_add_number(dict_T *d, char *key, varnumber_T nr)
380{
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100381 return dict_add_number_special(d, key, nr, VAR_NUMBER);
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200382}
383
384/*
385 * Add a special entry to dictionary "d".
386 * Returns FAIL when out of memory and when key already exists.
387 */
388 int
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100389dict_add_bool(dict_T *d, char *key, varnumber_T nr)
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200390{
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100391 return dict_add_number_special(d, key, nr, VAR_BOOL);
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200392}
393
394/*
Bram Moolenaare0be1672018-07-08 16:50:37 +0200395 * Add a string entry to dictionary "d".
396 * Returns FAIL when out of memory and when key already exists.
397 */
398 int
399dict_add_string(dict_T *d, char *key, char_u *str)
400{
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100401 return dict_add_string_len(d, key, str, -1);
402}
403
404/*
405 * Add a string entry to dictionary "d".
406 * "str" will be copied to allocated memory.
407 * When "len" is -1 use the whole string, otherwise only this many bytes.
408 * Returns FAIL when out of memory and when key already exists.
409 */
410 int
411dict_add_string_len(dict_T *d, char *key, char_u *str, int len)
412{
Bram Moolenaare0be1672018-07-08 16:50:37 +0200413 dictitem_T *item;
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100414 char_u *val = NULL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200415
416 item = dictitem_alloc((char_u *)key);
417 if (item == NULL)
418 return FAIL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200419 item->di_tv.v_type = VAR_STRING;
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100420 if (str != NULL)
421 {
422 if (len == -1)
423 val = vim_strsave(str);
424 else
425 val = vim_strnsave(str, len);
426 }
427 item->di_tv.vval.v_string = val;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200428 if (dict_add(d, item) == FAIL)
429 {
430 dictitem_free(item);
431 return FAIL;
432 }
433 return OK;
434}
435
436/*
437 * Add a list entry to dictionary "d".
438 * Returns FAIL when out of memory and when key already exists.
439 */
440 int
441dict_add_list(dict_T *d, char *key, list_T *list)
442{
443 dictitem_T *item;
444
445 item = dictitem_alloc((char_u *)key);
446 if (item == NULL)
447 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200448 item->di_tv.v_type = VAR_LIST;
449 item->di_tv.vval.v_list = list;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100450 ++list->lv_refcount;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200451 if (dict_add(d, item) == FAIL)
452 {
453 dictitem_free(item);
454 return FAIL;
455 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200456 return OK;
457}
458
459/*
Bram Moolenaar08928322020-01-04 14:32:48 +0100460 * Add a typval_T entry to dictionary "d".
461 * Returns FAIL when out of memory and when key already exists.
462 */
463 int
464dict_add_tv(dict_T *d, char *key, typval_T *tv)
465{
466 dictitem_T *item;
467
468 item = dictitem_alloc((char_u *)key);
469 if (item == NULL)
470 return FAIL;
471 copy_tv(tv, &item->di_tv);
472 if (dict_add(d, item) == FAIL)
473 {
474 dictitem_free(item);
475 return FAIL;
476 }
477 return OK;
478}
479
480/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200481 * Add a callback to dictionary "d".
482 * Returns FAIL when out of memory and when key already exists.
483 */
484 int
485dict_add_callback(dict_T *d, char *key, callback_T *cb)
486{
487 dictitem_T *item;
488
489 item = dictitem_alloc((char_u *)key);
490 if (item == NULL)
491 return FAIL;
492 put_callback(cb, &item->di_tv);
493 if (dict_add(d, item) == FAIL)
494 {
495 dictitem_free(item);
496 return FAIL;
497 }
498 return OK;
499}
500
501/*
Bram Moolenaar45e18cb2019-04-28 18:05:35 +0200502 * Initializes "iter" for iterating over dictionary items with
503 * dict_iterate_next().
504 * If "var" is not a Dict or an empty Dict then there will be nothing to
505 * iterate over, no error is given.
506 * NOTE: The dictionary must not change until iterating is finished!
507 */
508 void
509dict_iterate_start(typval_T *var, dict_iterator_T *iter)
510{
511 if (var->v_type != VAR_DICT || var->vval.v_dict == NULL)
512 iter->dit_todo = 0;
513 else
514 {
515 dict_T *d = var->vval.v_dict;
516
517 iter->dit_todo = d->dv_hashtab.ht_used;
518 iter->dit_hi = d->dv_hashtab.ht_array;
519 }
520}
521
522/*
523 * Iterate over the items referred to by "iter". It should be initialized with
524 * dict_iterate_start().
525 * Returns a pointer to the key.
526 * "*tv_result" is set to point to the value for that key.
527 * If there are no more items, NULL is returned.
528 */
529 char_u *
530dict_iterate_next(dict_iterator_T *iter, typval_T **tv_result)
531{
532 dictitem_T *di;
533 char_u *result;
534
535 if (iter->dit_todo == 0)
536 return NULL;
537
538 while (HASHITEM_EMPTY(iter->dit_hi))
539 ++iter->dit_hi;
540
541 di = HI2DI(iter->dit_hi);
542 result = di->di_key;
543 *tv_result = &di->di_tv;
544
545 --iter->dit_todo;
546 ++iter->dit_hi;
547 return result;
548}
549
550/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200551 * Add a dict entry to dictionary "d".
552 * Returns FAIL when out of memory and when key already exists.
553 */
554 int
555dict_add_dict(dict_T *d, char *key, dict_T *dict)
556{
557 dictitem_T *item;
558
559 item = dictitem_alloc((char_u *)key);
560 if (item == NULL)
561 return FAIL;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200562 item->di_tv.v_type = VAR_DICT;
563 item->di_tv.vval.v_dict = dict;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100564 ++dict->dv_refcount;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200565 if (dict_add(d, item) == FAIL)
566 {
567 dictitem_free(item);
568 return FAIL;
569 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200570 return OK;
571}
572
573/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200574 * Get the number of items in a Dictionary.
575 */
576 long
577dict_len(dict_T *d)
578{
579 if (d == NULL)
580 return 0L;
581 return (long)d->dv_hashtab.ht_used;
582}
583
584/*
585 * Find item "key[len]" in Dictionary "d".
586 * If "len" is negative use strlen(key).
587 * Returns NULL when not found.
588 */
589 dictitem_T *
590dict_find(dict_T *d, char_u *key, int len)
591{
592#define AKEYLEN 200
593 char_u buf[AKEYLEN];
594 char_u *akey;
595 char_u *tofree = NULL;
596 hashitem_T *hi;
597
598 if (d == NULL)
599 return NULL;
600 if (len < 0)
601 akey = key;
602 else if (len >= AKEYLEN)
603 {
604 tofree = akey = vim_strnsave(key, len);
605 if (akey == NULL)
606 return NULL;
607 }
608 else
609 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100610 // Avoid a malloc/free by using buf[].
Bram Moolenaarcd524592016-07-17 14:57:05 +0200611 vim_strncpy(buf, key, len);
612 akey = buf;
613 }
614
615 hi = hash_find(&d->dv_hashtab, akey);
616 vim_free(tofree);
617 if (HASHITEM_EMPTY(hi))
618 return NULL;
619 return HI2DI(hi);
620}
621
622/*
Bram Moolenaar08928322020-01-04 14:32:48 +0100623 * Get a typval_T item from a dictionary and copy it into "rettv".
624 * Returns FAIL if the entry doesn't exist or out of memory.
625 */
626 int
627dict_get_tv(dict_T *d, char_u *key, typval_T *rettv)
628{
629 dictitem_T *di;
Bram Moolenaar08928322020-01-04 14:32:48 +0100630
631 di = dict_find(d, key, -1);
632 if (di == NULL)
633 return FAIL;
634 copy_tv(&di->di_tv, rettv);
635 return OK;
636}
637
638/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200639 * Get a string item from a dictionary.
640 * When "save" is TRUE allocate memory for it.
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +0200641 * When FALSE a shared buffer is used, can only be used once!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200642 * Returns NULL if the entry doesn't exist or out of memory.
643 */
644 char_u *
Bram Moolenaar8f667172018-12-14 15:38:31 +0100645dict_get_string(dict_T *d, char_u *key, int save)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200646{
647 dictitem_T *di;
648 char_u *s;
649
650 di = dict_find(d, key, -1);
651 if (di == NULL)
652 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100653 s = tv_get_string(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200654 if (save && s != NULL)
655 s = vim_strsave(s);
656 return s;
657}
658
659/*
660 * Get a number item from a dictionary.
661 * Returns 0 if the entry doesn't exist.
662 */
663 varnumber_T
Bram Moolenaar8f667172018-12-14 15:38:31 +0100664dict_get_number(dict_T *d, char_u *key)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200665{
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200666 return dict_get_number_def(d, key, 0);
667}
668
669/*
670 * Get a number item from a dictionary.
671 * Returns "def" if the entry doesn't exist.
672 */
673 varnumber_T
674dict_get_number_def(dict_T *d, char_u *key, int def)
675{
Bram Moolenaarcd524592016-07-17 14:57:05 +0200676 dictitem_T *di;
677
678 di = dict_find(d, key, -1);
679 if (di == NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200680 return def;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100681 return tv_get_number(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200682}
683
684/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200685 * Get a number item from a dictionary.
686 * Returns 0 if the entry doesn't exist.
687 * Give an error if the entry is not a number.
688 */
689 varnumber_T
690dict_get_number_check(dict_T *d, char_u *key)
691{
692 dictitem_T *di;
693
694 di = dict_find(d, key, -1);
695 if (di == NULL)
696 return 0;
697 if (di->di_tv.v_type != VAR_NUMBER)
698 {
699 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
700 return 0;
701 }
702 return tv_get_number(&di->di_tv);
703}
704
705/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200706 * Return an allocated string with the string representation of a Dictionary.
707 * May return NULL.
708 */
709 char_u *
710dict2string(typval_T *tv, int copyID, int restore_copyID)
711{
712 garray_T ga;
713 int first = TRUE;
714 char_u *tofree;
715 char_u numbuf[NUMBUFLEN];
716 hashitem_T *hi;
717 char_u *s;
718 dict_T *d;
719 int todo;
720
721 if ((d = tv->vval.v_dict) == NULL)
722 return NULL;
723 ga_init2(&ga, (int)sizeof(char), 80);
724 ga_append(&ga, '{');
725
726 todo = (int)d->dv_hashtab.ht_used;
727 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
728 {
729 if (!HASHITEM_EMPTY(hi))
730 {
731 --todo;
732
733 if (first)
734 first = FALSE;
735 else
736 ga_concat(&ga, (char_u *)", ");
737
738 tofree = string_quote(hi->hi_key, FALSE);
739 if (tofree != NULL)
740 {
741 ga_concat(&ga, tofree);
742 vim_free(tofree);
743 }
744 ga_concat(&ga, (char_u *)": ");
745 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
746 FALSE, restore_copyID, TRUE);
747 if (s != NULL)
748 ga_concat(&ga, s);
749 vim_free(tofree);
750 if (s == NULL || did_echo_string_emsg)
751 break;
752 line_breakcheck();
753
754 }
755 }
756 if (todo > 0)
757 {
758 vim_free(ga.ga_data);
759 return NULL;
760 }
761
762 ga_append(&ga, '}');
763 ga_append(&ga, NUL);
764 return (char_u *)ga.ga_data;
765}
766
767/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +0200768 * Get the key for #{key: val} into "tv" and advance "arg".
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200769 * Return FAIL when there is no valid key.
770 */
771 static int
772get_literal_key(char_u **arg, typval_T *tv)
773{
774 char_u *p;
775
776 if (!ASCII_ISALNUM(**arg) && **arg != '_' && **arg != '-')
777 return FAIL;
778
779 for (p = *arg; ASCII_ISALNUM(*p) || *p == '_' || *p == '-'; ++p)
780 ;
781 tv->v_type = VAR_STRING;
Bram Moolenaardf44a272020-06-07 20:49:05 +0200782 tv->vval.v_string = vim_strnsave(*arg, p - *arg);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200783
784 *arg = skipwhite(p);
785 return OK;
786}
787
788/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200789 * Allocate a variable for a Dictionary and fill it from "*arg".
Bram Moolenaar9f9fe372019-07-27 23:12:12 +0200790 * "literal" is TRUE for #{key: val}
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200791 * "flags" can have EVAL_EVALUATE and other EVAL_ flags.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200792 * Return OK or FAIL. Returns NOTDONE for {expr}.
793 */
794 int
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200795eval_dict(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int literal)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200796{
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200797 int evaluate = evalarg == NULL ? FALSE
798 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200799 dict_T *d = NULL;
800 typval_T tvkey;
801 typval_T tv;
802 char_u *key = NULL;
803 dictitem_T *item;
804 char_u *start = skipwhite(*arg + 1);
805 char_u buf[NUMBUFLEN];
Bram Moolenaar32e35112020-05-14 22:41:15 +0200806 int vim9script = current_sctx.sc_version == SCRIPT_VERSION_VIM9;
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200807 int had_comma;
808 int getnext;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200809
Bram Moolenaarcd524592016-07-17 14:57:05 +0200810 /*
811 * First check if it's not a curly-braces thing: {expr}.
812 * Must do this without evaluating, otherwise a function may be called
813 * twice. Unfortunately this means we need to call eval1() twice for the
814 * first item.
815 * But {} is an empty Dictionary.
816 */
Bram Moolenaar32e35112020-05-14 22:41:15 +0200817 if (!vim9script && *start != '}')
Bram Moolenaarcd524592016-07-17 14:57:05 +0200818 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200819 if (eval1(&start, &tv, NULL) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200820 return FAIL;
821 if (*start == '}')
822 return NOTDONE;
823 }
824
825 if (evaluate)
826 {
827 d = dict_alloc();
828 if (d == NULL)
829 return FAIL;
830 }
831 tvkey.v_type = VAR_UNKNOWN;
832 tv.v_type = VAR_UNKNOWN;
833
834 *arg = skipwhite(*arg + 1);
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200835 eval_next_non_blank(*arg, evalarg, &getnext);
836 if (getnext)
837 *arg = eval_next_line(evalarg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200838 while (**arg != '}' && **arg != NUL)
839 {
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200840 if ((literal
841 ? get_literal_key(arg, &tvkey)
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200842 : eval1(arg, &tvkey, evalarg)) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200843 goto failret;
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200844
Bram Moolenaarcd524592016-07-17 14:57:05 +0200845 if (**arg != ':')
846 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +0100847 if (evaluate)
848 semsg(_(e_missing_dict_colon), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200849 clear_tv(&tvkey);
850 goto failret;
851 }
852 if (evaluate)
853 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100854 key = tv_get_string_buf_chk(&tvkey, buf);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200855 if (key == NULL)
856 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100857 // "key" is NULL when tv_get_string_buf_chk() gave an errmsg
Bram Moolenaarcd524592016-07-17 14:57:05 +0200858 clear_tv(&tvkey);
859 goto failret;
860 }
861 }
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200862 if (vim9script && (*arg)[1] != NUL && !VIM_ISWHITE((*arg)[1]))
863 {
864 semsg(_(e_white_after), ":");
865 goto failret;
866 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200867
868 *arg = skipwhite(*arg + 1);
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200869 eval_next_non_blank(*arg, evalarg, &getnext);
870 if (getnext)
871 *arg = eval_next_line(evalarg);
872 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200873 {
874 if (evaluate)
875 clear_tv(&tvkey);
876 goto failret;
877 }
878 if (evaluate)
879 {
880 item = dict_find(d, key, -1);
881 if (item != NULL)
882 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +0100883 if (evaluate)
884 semsg(_(e_duplicate_key), key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200885 clear_tv(&tvkey);
886 clear_tv(&tv);
887 goto failret;
888 }
889 item = dictitem_alloc(key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200890 if (item != NULL)
891 {
892 item->di_tv = tv;
893 item->di_tv.v_lock = 0;
894 if (dict_add(d, item) == FAIL)
895 dictitem_free(item);
896 }
897 }
Bram Moolenaara8931942019-09-28 17:25:10 +0200898 clear_tv(&tvkey);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200899
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200900 // the comma must come after the value
901 had_comma = **arg == ',';
902 if (had_comma)
903 {
904 if (vim9script && (*arg)[1] != NUL && !VIM_ISWHITE((*arg)[1]))
905 {
906 semsg(_(e_white_after), ",");
907 goto failret;
908 }
909 *arg = skipwhite(*arg + 1);
910 }
911
912 // the "}" can be on the next line
913 eval_next_non_blank(*arg, evalarg, &getnext);
914 if (getnext)
915 *arg = eval_next_line(evalarg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200916 if (**arg == '}')
917 break;
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200918 if (!had_comma)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200919 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +0100920 if (evaluate)
921 semsg(_(e_missing_dict_comma), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200922 goto failret;
923 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200924 }
925
926 if (**arg != '}')
927 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +0100928 if (evaluate)
929 semsg(_(e_missing_dict_end), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200930failret:
Bram Moolenaaradc67142019-06-22 01:40:42 +0200931 if (d != NULL)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200932 dict_free(d);
933 return FAIL;
934 }
935
936 *arg = skipwhite(*arg + 1);
937 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200938 rettv_dict_set(rettv, d);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200939
940 return OK;
941}
942
943/*
944 * Go over all entries in "d2" and add them to "d1".
945 * When "action" is "error" then a duplicate key is an error.
946 * When "action" is "force" then a duplicate key is overwritten.
947 * Otherwise duplicate keys are ignored ("action" is "keep").
948 */
949 void
950dict_extend(dict_T *d1, dict_T *d2, char_u *action)
951{
952 dictitem_T *di1;
953 hashitem_T *hi2;
954 int todo;
955 char_u *arg_errmsg = (char_u *)N_("extend() argument");
956
957 todo = (int)d2->dv_hashtab.ht_used;
958 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
959 {
960 if (!HASHITEM_EMPTY(hi2))
961 {
962 --todo;
963 di1 = dict_find(d1, hi2->hi_key, -1);
964 if (d1->dv_scope != 0)
965 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100966 // Disallow replacing a builtin function in l: and g:.
967 // Check the key to be valid when adding to any scope.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200968 if (d1->dv_scope == VAR_DEF_SCOPE
969 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
970 && var_check_func_name(hi2->hi_key, di1 == NULL))
971 break;
972 if (!valid_varname(hi2->hi_key))
973 break;
974 }
975 if (di1 == NULL)
976 {
977 di1 = dictitem_copy(HI2DI(hi2));
978 if (di1 != NULL && dict_add(d1, di1) == FAIL)
979 dictitem_free(di1);
980 }
981 else if (*action == 'e')
982 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100983 semsg(_("E737: Key already exists: %s"), hi2->hi_key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200984 break;
985 }
986 else if (*action == 'f' && HI2DI(hi2) != di1)
987 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +0100988 if (var_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
989 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaarcd524592016-07-17 14:57:05 +0200990 break;
991 clear_tv(&di1->di_tv);
992 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
993 }
994 }
995 }
996}
997
998/*
999 * Return the dictitem that an entry in a hashtable points to.
1000 */
1001 dictitem_T *
1002dict_lookup(hashitem_T *hi)
1003{
1004 return HI2DI(hi);
1005}
1006
1007/*
1008 * Return TRUE when two dictionaries have exactly the same key/values.
1009 */
1010 int
1011dict_equal(
1012 dict_T *d1,
1013 dict_T *d2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001014 int ic, // ignore case for strings
1015 int recursive) // TRUE when used recursively
Bram Moolenaarcd524592016-07-17 14:57:05 +02001016{
1017 hashitem_T *hi;
1018 dictitem_T *item2;
1019 int todo;
1020
Bram Moolenaarcd524592016-07-17 14:57:05 +02001021 if (d1 == d2)
1022 return TRUE;
1023 if (dict_len(d1) != dict_len(d2))
1024 return FALSE;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001025 if (dict_len(d1) == 0)
1026 // empty and NULL dicts are considered equal
1027 return TRUE;
1028 if (d1 == NULL || d2 == NULL)
1029 return FALSE;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001030
1031 todo = (int)d1->dv_hashtab.ht_used;
1032 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
1033 {
1034 if (!HASHITEM_EMPTY(hi))
1035 {
1036 item2 = dict_find(d2, hi->hi_key, -1);
1037 if (item2 == NULL)
1038 return FALSE;
1039 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
1040 return FALSE;
1041 --todo;
1042 }
1043 }
1044 return TRUE;
1045}
1046
1047/*
1048 * Turn a dict into a list:
1049 * "what" == 0: list of keys
1050 * "what" == 1: list of values
1051 * "what" == 2: list of items
1052 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001053 static void
Bram Moolenaarcd524592016-07-17 14:57:05 +02001054dict_list(typval_T *argvars, typval_T *rettv, int what)
1055{
1056 list_T *l2;
1057 dictitem_T *di;
1058 hashitem_T *hi;
1059 listitem_T *li;
1060 listitem_T *li2;
1061 dict_T *d;
1062 int todo;
1063
1064 if (argvars[0].v_type != VAR_DICT)
1065 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001066 emsg(_(e_dictreq));
Bram Moolenaarcd524592016-07-17 14:57:05 +02001067 return;
1068 }
1069 if ((d = argvars[0].vval.v_dict) == NULL)
1070 return;
1071
1072 if (rettv_list_alloc(rettv) == FAIL)
1073 return;
1074
1075 todo = (int)d->dv_hashtab.ht_used;
1076 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1077 {
1078 if (!HASHITEM_EMPTY(hi))
1079 {
1080 --todo;
1081 di = HI2DI(hi);
1082
1083 li = listitem_alloc();
1084 if (li == NULL)
1085 break;
1086 list_append(rettv->vval.v_list, li);
1087
1088 if (what == 0)
1089 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001090 // keys()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001091 li->li_tv.v_type = VAR_STRING;
1092 li->li_tv.v_lock = 0;
1093 li->li_tv.vval.v_string = vim_strsave(di->di_key);
1094 }
1095 else if (what == 1)
1096 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001097 // values()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001098 copy_tv(&di->di_tv, &li->li_tv);
1099 }
1100 else
1101 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001102 // items()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001103 l2 = list_alloc();
1104 li->li_tv.v_type = VAR_LIST;
1105 li->li_tv.v_lock = 0;
1106 li->li_tv.vval.v_list = l2;
1107 if (l2 == NULL)
1108 break;
1109 ++l2->lv_refcount;
1110
1111 li2 = listitem_alloc();
1112 if (li2 == NULL)
1113 break;
1114 list_append(l2, li2);
1115 li2->li_tv.v_type = VAR_STRING;
1116 li2->li_tv.v_lock = 0;
1117 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
1118
1119 li2 = listitem_alloc();
1120 if (li2 == NULL)
1121 break;
1122 list_append(l2, li2);
1123 copy_tv(&di->di_tv, &li2->li_tv);
1124 }
1125 }
1126 }
1127}
1128
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001129/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001130 * "items(dict)" function
1131 */
1132 void
1133f_items(typval_T *argvars, typval_T *rettv)
1134{
1135 dict_list(argvars, rettv, 2);
1136}
1137
1138/*
1139 * "keys()" function
1140 */
1141 void
1142f_keys(typval_T *argvars, typval_T *rettv)
1143{
1144 dict_list(argvars, rettv, 0);
1145}
1146
1147/*
1148 * "values(dict)" function
1149 */
1150 void
1151f_values(typval_T *argvars, typval_T *rettv)
1152{
1153 dict_list(argvars, rettv, 1);
1154}
1155
1156/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001157 * Make each item in the dict readonly (not the value of the item).
1158 */
1159 void
1160dict_set_items_ro(dict_T *di)
1161{
1162 int todo = (int)di->dv_hashtab.ht_used;
1163 hashitem_T *hi;
1164
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001165 // Set readonly
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001166 for (hi = di->dv_hashtab.ht_array; todo > 0 ; ++hi)
1167 {
1168 if (HASHITEM_EMPTY(hi))
1169 continue;
1170 --todo;
1171 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
1172 }
1173}
1174
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001175/*
1176 * "has_key()" function
1177 */
1178 void
1179f_has_key(typval_T *argvars, typval_T *rettv)
1180{
1181 if (argvars[0].v_type != VAR_DICT)
1182 {
1183 emsg(_(e_dictreq));
1184 return;
1185 }
1186 if (argvars[0].vval.v_dict == NULL)
1187 return;
1188
1189 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
1190 tv_get_string(&argvars[1]), -1) != NULL;
1191}
1192
1193/*
1194 * "remove({dict})" function
1195 */
1196 void
1197dict_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1198{
1199 dict_T *d;
1200 char_u *key;
1201 dictitem_T *di;
1202
1203 if (argvars[2].v_type != VAR_UNKNOWN)
1204 semsg(_(e_toomanyarg), "remove()");
1205 else if ((d = argvars[0].vval.v_dict) != NULL
1206 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
1207 {
1208 key = tv_get_string_chk(&argvars[1]);
1209 if (key != NULL)
1210 {
1211 di = dict_find(d, key, -1);
1212 if (di == NULL)
1213 semsg(_(e_dictkey), key);
1214 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
1215 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
1216 {
1217 *rettv = di->di_tv;
1218 init_tv(&di->di_tv);
1219 dictitem_remove(d, di);
1220 }
1221 }
1222 }
1223}
1224
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001225#endif // defined(FEAT_EVAL)