blob: caa398deba9598df5c516a51b10961eb4b1c253f [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 Moolenaar32e35112020-05-14 22:41:15 +0200795eval_dict(char_u **arg, typval_T *rettv, int flags, int literal)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200796{
Bram Moolenaar32e35112020-05-14 22:41:15 +0200797 int evaluate = flags & EVAL_EVALUATE;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200798 evalarg_T evalarg;
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 Moolenaarcd524592016-07-17 14:57:05 +0200807
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200808 CLEAR_FIELD(evalarg);
809 evalarg.eval_flags = flags;
810
Bram Moolenaarcd524592016-07-17 14:57:05 +0200811 /*
812 * First check if it's not a curly-braces thing: {expr}.
813 * Must do this without evaluating, otherwise a function may be called
814 * twice. Unfortunately this means we need to call eval1() twice for the
815 * first item.
816 * But {} is an empty Dictionary.
817 */
Bram Moolenaar32e35112020-05-14 22:41:15 +0200818 if (!vim9script && *start != '}')
Bram Moolenaarcd524592016-07-17 14:57:05 +0200819 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200820 if (eval1(&start, &tv, NULL) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200821 return FAIL;
822 if (*start == '}')
823 return NOTDONE;
824 }
825
826 if (evaluate)
827 {
828 d = dict_alloc();
829 if (d == NULL)
830 return FAIL;
831 }
832 tvkey.v_type = VAR_UNKNOWN;
833 tv.v_type = VAR_UNKNOWN;
834
835 *arg = skipwhite(*arg + 1);
836 while (**arg != '}' && **arg != NUL)
837 {
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200838 if ((literal
839 ? get_literal_key(arg, &tvkey)
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200840 : eval1(arg, &tvkey, &evalarg)) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200841 goto failret;
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200842
Bram Moolenaarcd524592016-07-17 14:57:05 +0200843 if (**arg != ':')
844 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +0100845 if (evaluate)
846 semsg(_(e_missing_dict_colon), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200847 clear_tv(&tvkey);
848 goto failret;
849 }
850 if (evaluate)
851 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100852 key = tv_get_string_buf_chk(&tvkey, buf);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200853 if (key == NULL)
854 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100855 // "key" is NULL when tv_get_string_buf_chk() gave an errmsg
Bram Moolenaarcd524592016-07-17 14:57:05 +0200856 clear_tv(&tvkey);
857 goto failret;
858 }
859 }
860
861 *arg = skipwhite(*arg + 1);
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200862 if (eval1(arg, &tv, &evalarg) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200863 {
864 if (evaluate)
865 clear_tv(&tvkey);
866 goto failret;
867 }
868 if (evaluate)
869 {
870 item = dict_find(d, key, -1);
871 if (item != NULL)
872 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +0100873 if (evaluate)
874 semsg(_(e_duplicate_key), key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200875 clear_tv(&tvkey);
876 clear_tv(&tv);
877 goto failret;
878 }
879 item = dictitem_alloc(key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200880 if (item != NULL)
881 {
882 item->di_tv = tv;
883 item->di_tv.v_lock = 0;
884 if (dict_add(d, item) == FAIL)
885 dictitem_free(item);
886 }
887 }
Bram Moolenaara8931942019-09-28 17:25:10 +0200888 clear_tv(&tvkey);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200889
890 if (**arg == '}')
891 break;
892 if (**arg != ',')
893 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +0100894 if (evaluate)
895 semsg(_(e_missing_dict_comma), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200896 goto failret;
897 }
898 *arg = skipwhite(*arg + 1);
899 }
900
901 if (**arg != '}')
902 {
Bram Moolenaar33fa29c2020-03-28 19:41:33 +0100903 if (evaluate)
904 semsg(_(e_missing_dict_end), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200905failret:
Bram Moolenaaradc67142019-06-22 01:40:42 +0200906 if (d != NULL)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200907 dict_free(d);
908 return FAIL;
909 }
910
911 *arg = skipwhite(*arg + 1);
912 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +0200913 rettv_dict_set(rettv, d);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200914
915 return OK;
916}
917
918/*
919 * Go over all entries in "d2" and add them to "d1".
920 * When "action" is "error" then a duplicate key is an error.
921 * When "action" is "force" then a duplicate key is overwritten.
922 * Otherwise duplicate keys are ignored ("action" is "keep").
923 */
924 void
925dict_extend(dict_T *d1, dict_T *d2, char_u *action)
926{
927 dictitem_T *di1;
928 hashitem_T *hi2;
929 int todo;
930 char_u *arg_errmsg = (char_u *)N_("extend() argument");
931
932 todo = (int)d2->dv_hashtab.ht_used;
933 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
934 {
935 if (!HASHITEM_EMPTY(hi2))
936 {
937 --todo;
938 di1 = dict_find(d1, hi2->hi_key, -1);
939 if (d1->dv_scope != 0)
940 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100941 // Disallow replacing a builtin function in l: and g:.
942 // Check the key to be valid when adding to any scope.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200943 if (d1->dv_scope == VAR_DEF_SCOPE
944 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
945 && var_check_func_name(hi2->hi_key, di1 == NULL))
946 break;
947 if (!valid_varname(hi2->hi_key))
948 break;
949 }
950 if (di1 == NULL)
951 {
952 di1 = dictitem_copy(HI2DI(hi2));
953 if (di1 != NULL && dict_add(d1, di1) == FAIL)
954 dictitem_free(di1);
955 }
956 else if (*action == 'e')
957 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100958 semsg(_("E737: Key already exists: %s"), hi2->hi_key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200959 break;
960 }
961 else if (*action == 'f' && HI2DI(hi2) != di1)
962 {
Bram Moolenaar05c00c02019-02-11 22:00:11 +0100963 if (var_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
964 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaarcd524592016-07-17 14:57:05 +0200965 break;
966 clear_tv(&di1->di_tv);
967 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
968 }
969 }
970 }
971}
972
973/*
974 * Return the dictitem that an entry in a hashtable points to.
975 */
976 dictitem_T *
977dict_lookup(hashitem_T *hi)
978{
979 return HI2DI(hi);
980}
981
982/*
983 * Return TRUE when two dictionaries have exactly the same key/values.
984 */
985 int
986dict_equal(
987 dict_T *d1,
988 dict_T *d2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100989 int ic, // ignore case for strings
990 int recursive) // TRUE when used recursively
Bram Moolenaarcd524592016-07-17 14:57:05 +0200991{
992 hashitem_T *hi;
993 dictitem_T *item2;
994 int todo;
995
Bram Moolenaarcd524592016-07-17 14:57:05 +0200996 if (d1 == d2)
997 return TRUE;
998 if (dict_len(d1) != dict_len(d2))
999 return FALSE;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001000 if (dict_len(d1) == 0)
1001 // empty and NULL dicts are considered equal
1002 return TRUE;
1003 if (d1 == NULL || d2 == NULL)
1004 return FALSE;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001005
1006 todo = (int)d1->dv_hashtab.ht_used;
1007 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
1008 {
1009 if (!HASHITEM_EMPTY(hi))
1010 {
1011 item2 = dict_find(d2, hi->hi_key, -1);
1012 if (item2 == NULL)
1013 return FALSE;
1014 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
1015 return FALSE;
1016 --todo;
1017 }
1018 }
1019 return TRUE;
1020}
1021
1022/*
1023 * Turn a dict into a list:
1024 * "what" == 0: list of keys
1025 * "what" == 1: list of values
1026 * "what" == 2: list of items
1027 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001028 static void
Bram Moolenaarcd524592016-07-17 14:57:05 +02001029dict_list(typval_T *argvars, typval_T *rettv, int what)
1030{
1031 list_T *l2;
1032 dictitem_T *di;
1033 hashitem_T *hi;
1034 listitem_T *li;
1035 listitem_T *li2;
1036 dict_T *d;
1037 int todo;
1038
1039 if (argvars[0].v_type != VAR_DICT)
1040 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001041 emsg(_(e_dictreq));
Bram Moolenaarcd524592016-07-17 14:57:05 +02001042 return;
1043 }
1044 if ((d = argvars[0].vval.v_dict) == NULL)
1045 return;
1046
1047 if (rettv_list_alloc(rettv) == FAIL)
1048 return;
1049
1050 todo = (int)d->dv_hashtab.ht_used;
1051 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1052 {
1053 if (!HASHITEM_EMPTY(hi))
1054 {
1055 --todo;
1056 di = HI2DI(hi);
1057
1058 li = listitem_alloc();
1059 if (li == NULL)
1060 break;
1061 list_append(rettv->vval.v_list, li);
1062
1063 if (what == 0)
1064 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001065 // keys()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001066 li->li_tv.v_type = VAR_STRING;
1067 li->li_tv.v_lock = 0;
1068 li->li_tv.vval.v_string = vim_strsave(di->di_key);
1069 }
1070 else if (what == 1)
1071 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001072 // values()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001073 copy_tv(&di->di_tv, &li->li_tv);
1074 }
1075 else
1076 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001077 // items()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001078 l2 = list_alloc();
1079 li->li_tv.v_type = VAR_LIST;
1080 li->li_tv.v_lock = 0;
1081 li->li_tv.vval.v_list = l2;
1082 if (l2 == NULL)
1083 break;
1084 ++l2->lv_refcount;
1085
1086 li2 = listitem_alloc();
1087 if (li2 == NULL)
1088 break;
1089 list_append(l2, li2);
1090 li2->li_tv.v_type = VAR_STRING;
1091 li2->li_tv.v_lock = 0;
1092 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
1093
1094 li2 = listitem_alloc();
1095 if (li2 == NULL)
1096 break;
1097 list_append(l2, li2);
1098 copy_tv(&di->di_tv, &li2->li_tv);
1099 }
1100 }
1101 }
1102}
1103
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001104/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001105 * "items(dict)" function
1106 */
1107 void
1108f_items(typval_T *argvars, typval_T *rettv)
1109{
1110 dict_list(argvars, rettv, 2);
1111}
1112
1113/*
1114 * "keys()" function
1115 */
1116 void
1117f_keys(typval_T *argvars, typval_T *rettv)
1118{
1119 dict_list(argvars, rettv, 0);
1120}
1121
1122/*
1123 * "values(dict)" function
1124 */
1125 void
1126f_values(typval_T *argvars, typval_T *rettv)
1127{
1128 dict_list(argvars, rettv, 1);
1129}
1130
1131/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001132 * Make each item in the dict readonly (not the value of the item).
1133 */
1134 void
1135dict_set_items_ro(dict_T *di)
1136{
1137 int todo = (int)di->dv_hashtab.ht_used;
1138 hashitem_T *hi;
1139
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001140 // Set readonly
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001141 for (hi = di->dv_hashtab.ht_array; todo > 0 ; ++hi)
1142 {
1143 if (HASHITEM_EMPTY(hi))
1144 continue;
1145 --todo;
1146 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
1147 }
1148}
1149
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001150/*
1151 * "has_key()" function
1152 */
1153 void
1154f_has_key(typval_T *argvars, typval_T *rettv)
1155{
1156 if (argvars[0].v_type != VAR_DICT)
1157 {
1158 emsg(_(e_dictreq));
1159 return;
1160 }
1161 if (argvars[0].vval.v_dict == NULL)
1162 return;
1163
1164 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
1165 tv_get_string(&argvars[1]), -1) != NULL;
1166}
1167
1168/*
1169 * "remove({dict})" function
1170 */
1171 void
1172dict_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1173{
1174 dict_T *d;
1175 char_u *key;
1176 dictitem_T *di;
1177
1178 if (argvars[2].v_type != VAR_UNKNOWN)
1179 semsg(_(e_toomanyarg), "remove()");
1180 else if ((d = argvars[0].vval.v_dict) != NULL
1181 && !var_check_lock(d->dv_lock, arg_errmsg, TRUE))
1182 {
1183 key = tv_get_string_chk(&argvars[1]);
1184 if (key != NULL)
1185 {
1186 di = dict_find(d, key, -1);
1187 if (di == NULL)
1188 semsg(_(e_dictkey), key);
1189 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
1190 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
1191 {
1192 *rettv = di->di_tv;
1193 init_tv(&di->di_tv);
1194 dictitem_remove(d, di);
1195 }
1196 }
1197 }
1198}
1199
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001200#endif // defined(FEAT_EVAL)