blob: 876eeea4928cb3b3e2a21ebca40fc3cc6c91c6a8 [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.
Bram Moolenaar22286892020-11-05 20:50:51 +010025 * Caller should take care of the reference count.
Bram Moolenaarcd524592016-07-17 14:57:05 +020026 */
27 dict_T *
28dict_alloc(void)
29{
30 dict_T *d;
31
Bram Moolenaaradc67142019-06-22 01:40:42 +020032 d = ALLOC_CLEAR_ONE(dict_T);
Bram Moolenaarcd524592016-07-17 14:57:05 +020033 if (d != NULL)
34 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +010035 // Add the dict to the list of dicts for garbage collection.
Bram Moolenaarcd524592016-07-17 14:57:05 +020036 if (first_dict != NULL)
37 first_dict->dv_used_prev = d;
38 d->dv_used_next = first_dict;
39 d->dv_used_prev = NULL;
40 first_dict = d;
41
42 hash_init(&d->dv_hashtab);
43 d->dv_lock = 0;
44 d->dv_scope = 0;
45 d->dv_refcount = 0;
46 d->dv_copyID = 0;
47 }
48 return d;
49}
50
Bram Moolenaarf49cc602018-11-11 15:21:05 +010051/*
52 * dict_alloc() with an ID for alloc_fail().
53 */
54 dict_T *
55dict_alloc_id(alloc_id_T id UNUSED)
56{
57#ifdef FEAT_EVAL
Bram Moolenaar51e14382019-05-25 20:21:28 +020058 if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T)))
Bram Moolenaarf49cc602018-11-11 15:21:05 +010059 return NULL;
60#endif
61 return (dict_alloc());
62}
63
Bram Moolenaar7e1652c2017-12-16 18:27:02 +010064 dict_T *
65dict_alloc_lock(int lock)
66{
67 dict_T *d = dict_alloc();
68
69 if (d != NULL)
70 d->dv_lock = lock;
71 return d;
72}
73
Bram Moolenaarcd524592016-07-17 14:57:05 +020074/*
75 * Allocate an empty dict for a return value.
76 * Returns OK or FAIL.
77 */
78 int
79rettv_dict_alloc(typval_T *rettv)
80{
Bram Moolenaar7e1652c2017-12-16 18:27:02 +010081 dict_T *d = dict_alloc_lock(0);
Bram Moolenaarcd524592016-07-17 14:57:05 +020082
83 if (d == NULL)
84 return FAIL;
85
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020086 rettv_dict_set(rettv, d);
Bram Moolenaarcd524592016-07-17 14:57:05 +020087 return OK;
88}
89
90/*
Bram Moolenaar45cf6e92017-04-30 20:25:19 +020091 * Set a dictionary as the return value
92 */
93 void
94rettv_dict_set(typval_T *rettv, dict_T *d)
95{
96 rettv->v_type = VAR_DICT;
97 rettv->vval.v_dict = d;
98 if (d != NULL)
99 ++d->dv_refcount;
100}
101
102/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200103 * Free a Dictionary, including all non-container items it contains.
104 * Ignores the reference count.
105 */
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100106 void
Bram Moolenaarcd524592016-07-17 14:57:05 +0200107dict_free_contents(dict_T *d)
108{
Bram Moolenaar89483d42020-05-10 15:24:44 +0200109 hashtab_free_contents(&d->dv_hashtab);
Bram Moolenaaraa210a32021-01-02 15:41:03 +0100110 free_type(d->dv_type);
111 d->dv_type = NULL;
Bram Moolenaar89483d42020-05-10 15:24:44 +0200112}
113
114/*
115 * Clear hashtab "ht" and dict items it contains.
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100116 * If "ht" is not freed then you should call hash_init() next!
Bram Moolenaar89483d42020-05-10 15:24:44 +0200117 */
118 void
119hashtab_free_contents(hashtab_T *ht)
120{
Bram Moolenaarcd524592016-07-17 14:57:05 +0200121 int todo;
122 hashitem_T *hi;
123 dictitem_T *di;
124
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100125 // Lock the hashtab, we don't want it to resize while freeing items.
Bram Moolenaar89483d42020-05-10 15:24:44 +0200126 hash_lock(ht);
127 todo = (int)ht->ht_used;
128 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200129 {
130 if (!HASHITEM_EMPTY(hi))
131 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100132 // Remove the item before deleting it, just in case there is
133 // something recursive causing trouble.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200134 di = HI2DI(hi);
Bram Moolenaar89483d42020-05-10 15:24:44 +0200135 hash_remove(ht, hi);
Bram Moolenaar95c526e2017-02-25 14:59:34 +0100136 dictitem_free(di);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200137 --todo;
138 }
139 }
Bram Moolenaar7e1652c2017-12-16 18:27:02 +0100140
Bram Moolenaar89483d42020-05-10 15:24:44 +0200141 // The hashtab is still locked, it has to be re-initialized anyway.
142 hash_clear(ht);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200143}
144
145 static void
146dict_free_dict(dict_T *d)
147{
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100148 // Remove the dict from the list of dicts for garbage collection.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200149 if (d->dv_used_prev == NULL)
150 first_dict = d->dv_used_next;
151 else
152 d->dv_used_prev->dv_used_next = d->dv_used_next;
153 if (d->dv_used_next != NULL)
154 d->dv_used_next->dv_used_prev = d->dv_used_prev;
155 vim_free(d);
156}
157
158 static void
159dict_free(dict_T *d)
160{
161 if (!in_free_unref_items)
162 {
163 dict_free_contents(d);
164 dict_free_dict(d);
165 }
166}
167
168/*
169 * Unreference a Dictionary: decrement the reference count and free it when it
170 * becomes zero.
171 */
172 void
173dict_unref(dict_T *d)
174{
175 if (d != NULL && --d->dv_refcount <= 0)
176 dict_free(d);
177}
178
179/*
180 * Go through the list of dicts and free items without the copyID.
181 * Returns TRUE if something was freed.
182 */
183 int
184dict_free_nonref(int copyID)
185{
186 dict_T *dd;
187 int did_free = FALSE;
188
189 for (dd = first_dict; dd != NULL; dd = dd->dv_used_next)
190 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
191 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100192 // Free the Dictionary and ordinary items it contains, but don't
193 // recurse into Lists and Dictionaries, they will be in the list
194 // of dicts or list of lists.
Bram Moolenaarcd524592016-07-17 14:57:05 +0200195 dict_free_contents(dd);
196 did_free = TRUE;
197 }
198 return did_free;
199}
200
201 void
202dict_free_items(int copyID)
203{
204 dict_T *dd, *dd_next;
205
206 for (dd = first_dict; dd != NULL; dd = dd_next)
207 {
208 dd_next = dd->dv_used_next;
209 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
210 dict_free_dict(dd);
211 }
212}
213
214/*
215 * Allocate a Dictionary item.
216 * The "key" is copied to the new item.
Bram Moolenaarc89d4b32018-07-08 17:19:02 +0200217 * Note that the type and value of the item "di_tv" still needs to be
218 * initialized!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200219 * Returns NULL when out of memory.
220 */
221 dictitem_T *
222dictitem_alloc(char_u *key)
223{
224 dictitem_T *di;
225
Bram Moolenaarb59e7352019-08-07 21:42:24 +0200226 di = alloc(offsetof(dictitem_T, di_key) + STRLEN(key) + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200227 if (di != NULL)
228 {
229 STRCPY(di->di_key, key);
230 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaarc89d4b32018-07-08 17:19:02 +0200231 di->di_tv.v_lock = 0;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200232 }
233 return di;
234}
235
236/*
237 * Make a copy of a Dictionary item.
238 */
239 static dictitem_T *
240dictitem_copy(dictitem_T *org)
241{
242 dictitem_T *di;
Bram Moolenaar3f974ff2020-10-02 18:11:56 +0200243 size_t len = STRLEN(org->di_key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200244
Bram Moolenaar3f974ff2020-10-02 18:11:56 +0200245 di = alloc(offsetof(dictitem_T, di_key) + len + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200246 if (di != NULL)
247 {
Bram Moolenaar3f974ff2020-10-02 18:11:56 +0200248 mch_memmove(di->di_key, org->di_key, len + 1);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200249 di->di_flags = DI_FLAGS_ALLOC;
250 copy_tv(&org->di_tv, &di->di_tv);
251 }
252 return di;
253}
254
255/*
256 * Remove item "item" from Dictionary "dict" and free it.
257 */
258 void
259dictitem_remove(dict_T *dict, dictitem_T *item)
260{
261 hashitem_T *hi;
262
263 hi = hash_find(&dict->dv_hashtab, item->di_key);
264 if (HASHITEM_EMPTY(hi))
Bram Moolenaar95f09602016-11-10 20:01:45 +0100265 internal_error("dictitem_remove()");
Bram Moolenaarcd524592016-07-17 14:57:05 +0200266 else
267 hash_remove(&dict->dv_hashtab, hi);
268 dictitem_free(item);
269}
270
271/*
272 * Free a dict item. Also clears the value.
273 */
274 void
275dictitem_free(dictitem_T *item)
276{
277 clear_tv(&item->di_tv);
278 if (item->di_flags & DI_FLAGS_ALLOC)
279 vim_free(item);
280}
281
282/*
283 * Make a copy of dict "d". Shallow if "deep" is FALSE.
284 * The refcount of the new dict is set to 1.
285 * See item_copy() for "copyID".
286 * Returns NULL when out of memory.
287 */
288 dict_T *
289dict_copy(dict_T *orig, int deep, int copyID)
290{
291 dict_T *copy;
292 dictitem_T *di;
293 int todo;
294 hashitem_T *hi;
295
296 if (orig == NULL)
297 return NULL;
298
299 copy = dict_alloc();
300 if (copy != NULL)
301 {
302 if (copyID != 0)
303 {
304 orig->dv_copyID = copyID;
305 orig->dv_copydict = copy;
306 }
307 todo = (int)orig->dv_hashtab.ht_used;
308 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
309 {
310 if (!HASHITEM_EMPTY(hi))
311 {
312 --todo;
313
314 di = dictitem_alloc(hi->hi_key);
315 if (di == NULL)
316 break;
317 if (deep)
318 {
319 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
320 copyID) == FAIL)
321 {
322 vim_free(di);
323 break;
324 }
325 }
326 else
327 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
328 if (dict_add(copy, di) == FAIL)
329 {
330 dictitem_free(di);
331 break;
332 }
333 }
334 }
335
336 ++copy->dv_refcount;
337 if (todo > 0)
338 {
339 dict_unref(copy);
340 copy = NULL;
341 }
342 }
343
344 return copy;
345}
346
347/*
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +0200348 * Check for adding a function to g: or s:.
349 * If the name is wrong give an error message and return TRUE.
350 */
351 int
352dict_wrong_func_name(dict_T *d, typval_T *tv, char_u *name)
353{
354 return (d == get_globvar_dict()
355 || (SCRIPT_ID_VALID(current_sctx.sc_sid)
356 && d == &SCRIPT_ITEM(current_sctx.sc_sid)->sn_vars->sv_dict))
357 && (tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
358 && var_wrong_func_name(name, TRUE);
359}
360
361/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200362 * Add item "item" to Dictionary "d".
363 * Returns FAIL when out of memory and when key already exists.
364 */
365 int
366dict_add(dict_T *d, dictitem_T *item)
367{
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +0200368 if (dict_wrong_func_name(d, &item->di_tv, item->di_key))
369 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200370 return hash_add(&d->dv_hashtab, item->di_key);
371}
372
373/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200374 * Add a number or special entry to dictionary "d".
Bram Moolenaarcd524592016-07-17 14:57:05 +0200375 * Returns FAIL when out of memory and when key already exists.
376 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200377 static int
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100378dict_add_number_special(dict_T *d, char *key, varnumber_T nr, vartype_T vartype)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200379{
380 dictitem_T *item;
381
382 item = dictitem_alloc((char_u *)key);
383 if (item == NULL)
384 return FAIL;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100385 item->di_tv.v_type = vartype;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200386 item->di_tv.vval.v_number = nr;
387 if (dict_add(d, item) == FAIL)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200388 {
Bram Moolenaare0be1672018-07-08 16:50:37 +0200389 dictitem_free(item);
390 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200391 }
Bram Moolenaare0be1672018-07-08 16:50:37 +0200392 return OK;
393}
394
395/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200396 * Add a number entry to dictionary "d".
397 * Returns FAIL when out of memory and when key already exists.
398 */
399 int
400dict_add_number(dict_T *d, char *key, varnumber_T nr)
401{
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100402 return dict_add_number_special(d, key, nr, VAR_NUMBER);
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200403}
404
405/*
406 * Add a special entry to dictionary "d".
407 * Returns FAIL when out of memory and when key already exists.
408 */
409 int
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100410dict_add_bool(dict_T *d, char *key, varnumber_T nr)
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200411{
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100412 return dict_add_number_special(d, key, nr, VAR_BOOL);
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200413}
414
415/*
Bram Moolenaare0be1672018-07-08 16:50:37 +0200416 * Add a string entry to dictionary "d".
417 * Returns FAIL when out of memory and when key already exists.
418 */
419 int
420dict_add_string(dict_T *d, char *key, char_u *str)
421{
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100422 return dict_add_string_len(d, key, str, -1);
423}
424
425/*
426 * Add a string entry to dictionary "d".
427 * "str" will be copied to allocated memory.
428 * When "len" is -1 use the whole string, otherwise only this many bytes.
429 * Returns FAIL when out of memory and when key already exists.
430 */
431 int
432dict_add_string_len(dict_T *d, char *key, char_u *str, int len)
433{
Bram Moolenaare0be1672018-07-08 16:50:37 +0200434 dictitem_T *item;
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100435 char_u *val = NULL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200436
437 item = dictitem_alloc((char_u *)key);
438 if (item == NULL)
439 return FAIL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200440 item->di_tv.v_type = VAR_STRING;
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100441 if (str != NULL)
442 {
443 if (len == -1)
444 val = vim_strsave(str);
445 else
446 val = vim_strnsave(str, len);
447 }
448 item->di_tv.vval.v_string = val;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200449 if (dict_add(d, item) == FAIL)
450 {
451 dictitem_free(item);
452 return FAIL;
453 }
454 return OK;
455}
456
457/*
458 * Add a list entry to dictionary "d".
459 * Returns FAIL when out of memory and when key already exists.
460 */
461 int
462dict_add_list(dict_T *d, char *key, list_T *list)
463{
464 dictitem_T *item;
465
466 item = dictitem_alloc((char_u *)key);
467 if (item == NULL)
468 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200469 item->di_tv.v_type = VAR_LIST;
470 item->di_tv.vval.v_list = list;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100471 ++list->lv_refcount;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200472 if (dict_add(d, item) == FAIL)
473 {
474 dictitem_free(item);
475 return FAIL;
476 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200477 return OK;
478}
479
480/*
Bram Moolenaar08928322020-01-04 14:32:48 +0100481 * Add a typval_T entry to dictionary "d".
482 * Returns FAIL when out of memory and when key already exists.
483 */
484 int
485dict_add_tv(dict_T *d, char *key, typval_T *tv)
486{
487 dictitem_T *item;
488
489 item = dictitem_alloc((char_u *)key);
490 if (item == NULL)
491 return FAIL;
492 copy_tv(tv, &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 Moolenaarae943152019-06-16 22:54:14 +0200502 * Add a callback to dictionary "d".
503 * Returns FAIL when out of memory and when key already exists.
504 */
505 int
506dict_add_callback(dict_T *d, char *key, callback_T *cb)
507{
508 dictitem_T *item;
509
510 item = dictitem_alloc((char_u *)key);
511 if (item == NULL)
512 return FAIL;
513 put_callback(cb, &item->di_tv);
514 if (dict_add(d, item) == FAIL)
515 {
516 dictitem_free(item);
517 return FAIL;
518 }
519 return OK;
520}
521
522/*
Bram Moolenaar45e18cb2019-04-28 18:05:35 +0200523 * Initializes "iter" for iterating over dictionary items with
524 * dict_iterate_next().
525 * If "var" is not a Dict or an empty Dict then there will be nothing to
526 * iterate over, no error is given.
527 * NOTE: The dictionary must not change until iterating is finished!
528 */
529 void
530dict_iterate_start(typval_T *var, dict_iterator_T *iter)
531{
532 if (var->v_type != VAR_DICT || var->vval.v_dict == NULL)
533 iter->dit_todo = 0;
534 else
535 {
536 dict_T *d = var->vval.v_dict;
537
538 iter->dit_todo = d->dv_hashtab.ht_used;
539 iter->dit_hi = d->dv_hashtab.ht_array;
540 }
541}
542
543/*
544 * Iterate over the items referred to by "iter". It should be initialized with
545 * dict_iterate_start().
546 * Returns a pointer to the key.
547 * "*tv_result" is set to point to the value for that key.
548 * If there are no more items, NULL is returned.
549 */
550 char_u *
551dict_iterate_next(dict_iterator_T *iter, typval_T **tv_result)
552{
553 dictitem_T *di;
554 char_u *result;
555
556 if (iter->dit_todo == 0)
557 return NULL;
558
559 while (HASHITEM_EMPTY(iter->dit_hi))
560 ++iter->dit_hi;
561
562 di = HI2DI(iter->dit_hi);
563 result = di->di_key;
564 *tv_result = &di->di_tv;
565
566 --iter->dit_todo;
567 ++iter->dit_hi;
568 return result;
569}
570
571/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200572 * Add a dict entry to dictionary "d".
573 * Returns FAIL when out of memory and when key already exists.
574 */
575 int
576dict_add_dict(dict_T *d, char *key, dict_T *dict)
577{
578 dictitem_T *item;
579
580 item = dictitem_alloc((char_u *)key);
581 if (item == NULL)
582 return FAIL;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200583 item->di_tv.v_type = VAR_DICT;
584 item->di_tv.vval.v_dict = dict;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100585 ++dict->dv_refcount;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200586 if (dict_add(d, item) == FAIL)
587 {
588 dictitem_free(item);
589 return FAIL;
590 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200591 return OK;
592}
593
594/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200595 * Get the number of items in a Dictionary.
596 */
597 long
598dict_len(dict_T *d)
599{
600 if (d == NULL)
601 return 0L;
602 return (long)d->dv_hashtab.ht_used;
603}
604
605/*
606 * Find item "key[len]" in Dictionary "d".
607 * If "len" is negative use strlen(key).
608 * Returns NULL when not found.
609 */
610 dictitem_T *
611dict_find(dict_T *d, char_u *key, int len)
612{
613#define AKEYLEN 200
614 char_u buf[AKEYLEN];
615 char_u *akey;
616 char_u *tofree = NULL;
617 hashitem_T *hi;
618
619 if (d == NULL)
620 return NULL;
621 if (len < 0)
622 akey = key;
623 else if (len >= AKEYLEN)
624 {
625 tofree = akey = vim_strnsave(key, len);
626 if (akey == NULL)
627 return NULL;
628 }
629 else
630 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100631 // Avoid a malloc/free by using buf[].
Bram Moolenaarcd524592016-07-17 14:57:05 +0200632 vim_strncpy(buf, key, len);
633 akey = buf;
634 }
635
636 hi = hash_find(&d->dv_hashtab, akey);
637 vim_free(tofree);
638 if (HASHITEM_EMPTY(hi))
639 return NULL;
640 return HI2DI(hi);
641}
642
643/*
Bram Moolenaar08928322020-01-04 14:32:48 +0100644 * Get a typval_T item from a dictionary and copy it into "rettv".
645 * Returns FAIL if the entry doesn't exist or out of memory.
646 */
647 int
648dict_get_tv(dict_T *d, char_u *key, typval_T *rettv)
649{
650 dictitem_T *di;
Bram Moolenaar08928322020-01-04 14:32:48 +0100651
652 di = dict_find(d, key, -1);
653 if (di == NULL)
654 return FAIL;
655 copy_tv(&di->di_tv, rettv);
656 return OK;
657}
658
659/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200660 * Get a string item from a dictionary.
661 * When "save" is TRUE allocate memory for it.
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +0200662 * When FALSE a shared buffer is used, can only be used once!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200663 * Returns NULL if the entry doesn't exist or out of memory.
664 */
665 char_u *
Bram Moolenaar8f667172018-12-14 15:38:31 +0100666dict_get_string(dict_T *d, char_u *key, int save)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200667{
668 dictitem_T *di;
669 char_u *s;
670
671 di = dict_find(d, key, -1);
672 if (di == NULL)
673 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100674 s = tv_get_string(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200675 if (save && s != NULL)
676 s = vim_strsave(s);
677 return s;
678}
679
680/*
681 * Get a number item from a dictionary.
682 * Returns 0 if the entry doesn't exist.
683 */
684 varnumber_T
Bram Moolenaar8f667172018-12-14 15:38:31 +0100685dict_get_number(dict_T *d, char_u *key)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200686{
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200687 return dict_get_number_def(d, key, 0);
688}
689
690/*
691 * Get a number item from a dictionary.
692 * Returns "def" if the entry doesn't exist.
693 */
694 varnumber_T
695dict_get_number_def(dict_T *d, char_u *key, int def)
696{
Bram Moolenaarcd524592016-07-17 14:57:05 +0200697 dictitem_T *di;
698
699 di = dict_find(d, key, -1);
700 if (di == NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200701 return def;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100702 return tv_get_number(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200703}
704
705/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200706 * Get a number item from a dictionary.
707 * Returns 0 if the entry doesn't exist.
708 * Give an error if the entry is not a number.
709 */
710 varnumber_T
711dict_get_number_check(dict_T *d, char_u *key)
712{
713 dictitem_T *di;
714
715 di = dict_find(d, key, -1);
716 if (di == NULL)
717 return 0;
718 if (di->di_tv.v_type != VAR_NUMBER)
719 {
720 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
721 return 0;
722 }
723 return tv_get_number(&di->di_tv);
724}
725
726/*
Bram Moolenaar55881332020-08-18 13:04:15 +0200727 * Get a bool item (number or true/false) from a dictionary.
728 * Returns "def" if the entry doesn't exist.
729 */
730 varnumber_T
731dict_get_bool(dict_T *d, char_u *key, int def)
732{
733 dictitem_T *di;
734
735 di = dict_find(d, key, -1);
736 if (di == NULL)
737 return def;
738 return tv_get_bool(&di->di_tv);
739}
740
741/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200742 * Return an allocated string with the string representation of a Dictionary.
743 * May return NULL.
744 */
745 char_u *
746dict2string(typval_T *tv, int copyID, int restore_copyID)
747{
748 garray_T ga;
749 int first = TRUE;
750 char_u *tofree;
751 char_u numbuf[NUMBUFLEN];
752 hashitem_T *hi;
753 char_u *s;
754 dict_T *d;
755 int todo;
756
757 if ((d = tv->vval.v_dict) == NULL)
758 return NULL;
759 ga_init2(&ga, (int)sizeof(char), 80);
760 ga_append(&ga, '{');
761
762 todo = (int)d->dv_hashtab.ht_used;
763 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
764 {
765 if (!HASHITEM_EMPTY(hi))
766 {
767 --todo;
768
769 if (first)
770 first = FALSE;
771 else
772 ga_concat(&ga, (char_u *)", ");
773
774 tofree = string_quote(hi->hi_key, FALSE);
775 if (tofree != NULL)
776 {
777 ga_concat(&ga, tofree);
778 vim_free(tofree);
779 }
780 ga_concat(&ga, (char_u *)": ");
781 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
782 FALSE, restore_copyID, TRUE);
783 if (s != NULL)
784 ga_concat(&ga, s);
785 vim_free(tofree);
786 if (s == NULL || did_echo_string_emsg)
787 break;
788 line_breakcheck();
789
790 }
791 }
792 if (todo > 0)
793 {
794 vim_free(ga.ga_data);
795 return NULL;
796 }
797
798 ga_append(&ga, '}');
799 ga_append(&ga, NUL);
800 return (char_u *)ga.ga_data;
801}
802
803/*
Bram Moolenaare0de1712020-12-02 17:36:54 +0100804 * Advance over a literal key, including "-". If the first character is not a
805 * literal key character then "key" is returned.
806 */
807 char_u *
808skip_literal_key(char_u *key)
809{
810 char_u *p;
811
812 for (p = key; ASCII_ISALNUM(*p) || *p == '_' || *p == '-'; ++p)
813 ;
814 return p;
815}
816
817/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +0200818 * Get the key for #{key: val} into "tv" and advance "arg".
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200819 * Return FAIL when there is no valid key.
820 */
821 static int
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100822get_literal_key_tv(char_u **arg, typval_T *tv)
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200823{
Bram Moolenaare0de1712020-12-02 17:36:54 +0100824 char_u *p = skip_literal_key(*arg);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200825
Bram Moolenaare0de1712020-12-02 17:36:54 +0100826 if (p == *arg)
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200827 return FAIL;
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200828 tv->v_type = VAR_STRING;
Bram Moolenaardf44a272020-06-07 20:49:05 +0200829 tv->vval.v_string = vim_strnsave(*arg, p - *arg);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200830
Bram Moolenaardb199212020-08-12 18:01:53 +0200831 *arg = p;
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200832 return OK;
833}
834
835/*
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100836 * Get a literal key for a Vim9 dict:
837 * {"name": value},
838 * {'name': value},
839 * {name: value} use "name" as a literal key
840 * Return the key in allocated memory or NULL in the case of an error.
841 * "arg" is advanced to just after the key.
842 */
843 char_u *
844get_literal_key(char_u **arg)
845{
846 char_u *key;
847 char_u *end;
848 typval_T rettv;
849
850 if (**arg == '\'')
851 {
852 if (eval_lit_string(arg, &rettv, TRUE) == FAIL)
853 return NULL;
854 key = rettv.vval.v_string;
855 }
856 else if (**arg == '"')
857 {
858 if (eval_string(arg, &rettv, TRUE) == FAIL)
859 return NULL;
860 key = rettv.vval.v_string;
861 }
862 else
863 {
864 end = skip_literal_key(*arg);
865 if (end == *arg)
866 {
867 semsg(_(e_invalid_key_str), *arg);
868 return NULL;
869 }
870 key = vim_strnsave(*arg, end - *arg);
871 *arg = end;
872 }
873 return key;
874}
875
876/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200877 * Allocate a variable for a Dictionary and fill it from "*arg".
Bram Moolenaar962d7212020-07-04 14:15:00 +0200878 * "*arg" points to the "{".
Bram Moolenaar9f9fe372019-07-27 23:12:12 +0200879 * "literal" is TRUE for #{key: val}
Bram Moolenaarcd524592016-07-17 14:57:05 +0200880 * Return OK or FAIL. Returns NOTDONE for {expr}.
881 */
882 int
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200883eval_dict(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int literal)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200884{
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200885 int evaluate = evalarg == NULL ? FALSE
886 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200887 dict_T *d = NULL;
888 typval_T tvkey;
889 typval_T tv;
890 char_u *key = NULL;
891 dictitem_T *item;
892 char_u *start = skipwhite(*arg + 1);
893 char_u buf[NUMBUFLEN];
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200894 int vim9script = in_vim9script();
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200895 int had_comma;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200896
Bram Moolenaarcd524592016-07-17 14:57:05 +0200897 /*
898 * First check if it's not a curly-braces thing: {expr}.
899 * Must do this without evaluating, otherwise a function may be called
900 * twice. Unfortunately this means we need to call eval1() twice for the
901 * first item.
902 * But {} is an empty Dictionary.
903 */
Bram Moolenaar32e35112020-05-14 22:41:15 +0200904 if (!vim9script && *start != '}')
Bram Moolenaarcd524592016-07-17 14:57:05 +0200905 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200906 if (eval1(&start, &tv, NULL) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200907 return FAIL;
Bram Moolenaarae95a392020-08-06 16:38:12 +0200908 if (*skipwhite(start) == '}')
Bram Moolenaarcd524592016-07-17 14:57:05 +0200909 return NOTDONE;
910 }
911
912 if (evaluate)
913 {
914 d = dict_alloc();
915 if (d == NULL)
916 return FAIL;
917 }
918 tvkey.v_type = VAR_UNKNOWN;
919 tv.v_type = VAR_UNKNOWN;
920
Bram Moolenaar962d7212020-07-04 14:15:00 +0200921 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200922 while (**arg != '}' && **arg != NUL)
923 {
Bram Moolenaare0de1712020-12-02 17:36:54 +0100924 int has_bracket = vim9script && **arg == '[';
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100925
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100926 if (literal)
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100927 {
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100928 if (get_literal_key_tv(arg, &tvkey) == FAIL)
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100929 goto failret;
930 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100931 else if (vim9script && !has_bracket)
932 {
933 tvkey.vval.v_string = get_literal_key(arg);
934 if (tvkey.vval.v_string == NULL)
935 goto failret;
936 tvkey.v_type = VAR_STRING;
937 }
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100938 else
939 {
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100940 if (has_bracket)
941 *arg = skipwhite(*arg + 1);
942 if (eval1(arg, &tvkey, evalarg) == FAIL) // recursive!
943 goto failret;
944 if (has_bracket)
945 {
946 *arg = skipwhite(*arg);
947 if (**arg != ']')
948 {
949 emsg(_(e_missing_matching_bracket_after_dict_key));
Bram Moolenaar8bb0f542020-12-06 16:03:55 +0100950 clear_tv(&tvkey);
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100951 return FAIL;
952 }
953 ++*arg;
954 }
955 }
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200956
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +0200957 // the colon should come right after the key, but this wasn't checked
Bram Moolenaar9d489562020-07-30 20:08:50 +0200958 // previously, so only require it in Vim9 script.
959 if (!vim9script)
960 *arg = skipwhite(*arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200961 if (**arg != ':')
962 {
Bram Moolenaara2c026d2020-10-18 18:03:17 +0200963 if (*skipwhite(*arg) == ':')
Bram Moolenaarba98fb52021-02-07 18:06:29 +0100964 semsg(_(e_no_white_space_allowed_before_str_str), ":", *arg);
Bram Moolenaara2c026d2020-10-18 18:03:17 +0200965 else
966 semsg(_(e_missing_dict_colon), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200967 clear_tv(&tvkey);
968 goto failret;
969 }
970 if (evaluate)
971 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100972#ifdef FEAT_FLOAT
973 if (tvkey.v_type == VAR_FLOAT)
Bram Moolenaar9987fb02020-12-15 21:41:56 +0100974 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100975 tvkey.vval.v_string = typval_tostring(&tvkey, TRUE);
976 tvkey.v_type = VAR_STRING;
Bram Moolenaar9987fb02020-12-15 21:41:56 +0100977 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100978#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100979 key = tv_get_string_buf_chk(&tvkey, buf);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200980 if (key == NULL)
981 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100982 // "key" is NULL when tv_get_string_buf_chk() gave an errmsg
Bram Moolenaarcd524592016-07-17 14:57:05 +0200983 clear_tv(&tvkey);
984 goto failret;
985 }
986 }
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200987 if (vim9script && (*arg)[1] != NUL && !VIM_ISWHITE((*arg)[1]))
988 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +0100989 semsg(_(e_white_space_required_after_str_str), ":", *arg);
Bram Moolenaarab19d492020-06-27 17:04:05 +0200990 clear_tv(&tvkey);
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200991 goto failret;
992 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200993
Bram Moolenaar962d7212020-07-04 14:15:00 +0200994 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200995 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200996 {
997 if (evaluate)
998 clear_tv(&tvkey);
999 goto failret;
1000 }
1001 if (evaluate)
1002 {
1003 item = dict_find(d, key, -1);
1004 if (item != NULL)
1005 {
Bram Moolenaara2c026d2020-10-18 18:03:17 +02001006 semsg(_(e_duplicate_key), key);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001007 clear_tv(&tvkey);
1008 clear_tv(&tv);
1009 goto failret;
1010 }
1011 item = dictitem_alloc(key);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001012 if (item != NULL)
1013 {
1014 item->di_tv = tv;
1015 item->di_tv.v_lock = 0;
1016 if (dict_add(d, item) == FAIL)
1017 dictitem_free(item);
1018 }
1019 }
Bram Moolenaara8931942019-09-28 17:25:10 +02001020 clear_tv(&tvkey);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001021
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +02001022 // the comma should come right after the value, but this wasn't checked
1023 // previously, so only require it in Vim9 script.
1024 if (!vim9script)
1025 *arg = skipwhite(*arg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001026 had_comma = **arg == ',';
1027 if (had_comma)
1028 {
1029 if (vim9script && (*arg)[1] != NUL && !VIM_ISWHITE((*arg)[1]))
1030 {
Bram Moolenaarc3fc75d2021-02-07 15:28:09 +01001031 semsg(_(e_white_space_required_after_str_str), ",", *arg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001032 goto failret;
1033 }
1034 *arg = skipwhite(*arg + 1);
1035 }
1036
1037 // the "}" can be on the next line
Bram Moolenaar962d7212020-07-04 14:15:00 +02001038 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001039 if (**arg == '}')
1040 break;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001041 if (!had_comma)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001042 {
Bram Moolenaara2c026d2020-10-18 18:03:17 +02001043 if (**arg == ',')
Bram Moolenaarba98fb52021-02-07 18:06:29 +01001044 semsg(_(e_no_white_space_allowed_before_str_str), ",", *arg);
Bram Moolenaara2c026d2020-10-18 18:03:17 +02001045 else
1046 semsg(_(e_missing_dict_comma), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001047 goto failret;
1048 }
Bram Moolenaarcd524592016-07-17 14:57:05 +02001049 }
1050
1051 if (**arg != '}')
1052 {
Bram Moolenaar4bce26b2021-01-22 22:06:56 +01001053 if (evalarg != NULL)
1054 semsg(_(e_missing_dict_end), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001055failret:
Bram Moolenaaradc67142019-06-22 01:40:42 +02001056 if (d != NULL)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001057 dict_free(d);
1058 return FAIL;
1059 }
1060
Bram Moolenaarb07a39d2020-10-22 19:00:01 +02001061 *arg = *arg + 1;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001062 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001063 rettv_dict_set(rettv, d);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001064
1065 return OK;
1066}
1067
1068/*
1069 * Go over all entries in "d2" and add them to "d1".
1070 * When "action" is "error" then a duplicate key is an error.
1071 * When "action" is "force" then a duplicate key is overwritten.
1072 * Otherwise duplicate keys are ignored ("action" is "keep").
1073 */
1074 void
1075dict_extend(dict_T *d1, dict_T *d2, char_u *action)
1076{
1077 dictitem_T *di1;
1078 hashitem_T *hi2;
1079 int todo;
1080 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001081 type_T *type;
1082
1083 if (d1->dv_type != NULL && d1->dv_type->tt_member != NULL)
1084 type = d1->dv_type->tt_member;
1085 else
1086 type = NULL;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001087
1088 todo = (int)d2->dv_hashtab.ht_used;
1089 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
1090 {
1091 if (!HASHITEM_EMPTY(hi2))
1092 {
1093 --todo;
1094 di1 = dict_find(d1, hi2->hi_key, -1);
1095 if (d1->dv_scope != 0)
1096 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001097 // Disallow replacing a builtin function in l: and g:.
1098 // Check the key to be valid when adding to any scope.
Bram Moolenaarcd524592016-07-17 14:57:05 +02001099 if (d1->dv_scope == VAR_DEF_SCOPE
1100 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001101 && var_wrong_func_name(hi2->hi_key, di1 == NULL))
Bram Moolenaarcd524592016-07-17 14:57:05 +02001102 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001103 if (!valid_varname(hi2->hi_key, TRUE))
Bram Moolenaarcd524592016-07-17 14:57:05 +02001104 break;
1105 }
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001106
1107 if (type != NULL
Bram Moolenaarf785aa12021-02-11 21:19:34 +01001108 && check_typval_arg_type(type, &HI2DI(hi2)->di_tv, 0)
1109 == FAIL)
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001110 break;
1111
Bram Moolenaarcd524592016-07-17 14:57:05 +02001112 if (di1 == NULL)
1113 {
1114 di1 = dictitem_copy(HI2DI(hi2));
1115 if (di1 != NULL && dict_add(d1, di1) == FAIL)
1116 dictitem_free(di1);
1117 }
1118 else if (*action == 'e')
1119 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001120 semsg(_("E737: Key already exists: %s"), hi2->hi_key);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001121 break;
1122 }
1123 else if (*action == 'f' && HI2DI(hi2) != di1)
1124 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001125 if (value_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001126 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaarcd524592016-07-17 14:57:05 +02001127 break;
Bram Moolenaar6f1d2aa2021-06-01 21:21:55 +02001128 if (dict_wrong_func_name(d1, &HI2DI(hi2)->di_tv, hi2->hi_key))
1129 break;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001130 clear_tv(&di1->di_tv);
1131 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
1132 }
1133 }
1134 }
1135}
1136
1137/*
1138 * Return the dictitem that an entry in a hashtable points to.
1139 */
1140 dictitem_T *
1141dict_lookup(hashitem_T *hi)
1142{
1143 return HI2DI(hi);
1144}
1145
1146/*
1147 * Return TRUE when two dictionaries have exactly the same key/values.
1148 */
1149 int
1150dict_equal(
1151 dict_T *d1,
1152 dict_T *d2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001153 int ic, // ignore case for strings
1154 int recursive) // TRUE when used recursively
Bram Moolenaarcd524592016-07-17 14:57:05 +02001155{
1156 hashitem_T *hi;
1157 dictitem_T *item2;
1158 int todo;
1159
Bram Moolenaarcd524592016-07-17 14:57:05 +02001160 if (d1 == d2)
1161 return TRUE;
1162 if (dict_len(d1) != dict_len(d2))
1163 return FALSE;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001164 if (dict_len(d1) == 0)
1165 // empty and NULL dicts are considered equal
1166 return TRUE;
1167 if (d1 == NULL || d2 == NULL)
1168 return FALSE;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001169
1170 todo = (int)d1->dv_hashtab.ht_used;
1171 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
1172 {
1173 if (!HASHITEM_EMPTY(hi))
1174 {
1175 item2 = dict_find(d2, hi->hi_key, -1);
1176 if (item2 == NULL)
1177 return FALSE;
1178 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
1179 return FALSE;
1180 --todo;
1181 }
1182 }
1183 return TRUE;
1184}
1185
1186/*
1187 * Turn a dict into a list:
1188 * "what" == 0: list of keys
1189 * "what" == 1: list of values
1190 * "what" == 2: list of items
1191 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001192 static void
Bram Moolenaarcd524592016-07-17 14:57:05 +02001193dict_list(typval_T *argvars, typval_T *rettv, int what)
1194{
1195 list_T *l2;
1196 dictitem_T *di;
1197 hashitem_T *hi;
1198 listitem_T *li;
1199 listitem_T *li2;
1200 dict_T *d;
1201 int todo;
1202
1203 if (argvars[0].v_type != VAR_DICT)
1204 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001205 emsg(_(e_dictreq));
Bram Moolenaarcd524592016-07-17 14:57:05 +02001206 return;
1207 }
1208 if ((d = argvars[0].vval.v_dict) == NULL)
1209 return;
1210
1211 if (rettv_list_alloc(rettv) == FAIL)
1212 return;
1213
1214 todo = (int)d->dv_hashtab.ht_used;
1215 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1216 {
1217 if (!HASHITEM_EMPTY(hi))
1218 {
1219 --todo;
1220 di = HI2DI(hi);
1221
1222 li = listitem_alloc();
1223 if (li == NULL)
1224 break;
1225 list_append(rettv->vval.v_list, li);
1226
1227 if (what == 0)
1228 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001229 // keys()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001230 li->li_tv.v_type = VAR_STRING;
1231 li->li_tv.v_lock = 0;
1232 li->li_tv.vval.v_string = vim_strsave(di->di_key);
1233 }
1234 else if (what == 1)
1235 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001236 // values()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001237 copy_tv(&di->di_tv, &li->li_tv);
1238 }
1239 else
1240 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001241 // items()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001242 l2 = list_alloc();
1243 li->li_tv.v_type = VAR_LIST;
1244 li->li_tv.v_lock = 0;
1245 li->li_tv.vval.v_list = l2;
1246 if (l2 == NULL)
1247 break;
1248 ++l2->lv_refcount;
1249
1250 li2 = listitem_alloc();
1251 if (li2 == NULL)
1252 break;
1253 list_append(l2, li2);
1254 li2->li_tv.v_type = VAR_STRING;
1255 li2->li_tv.v_lock = 0;
1256 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
1257
1258 li2 = listitem_alloc();
1259 if (li2 == NULL)
1260 break;
1261 list_append(l2, li2);
1262 copy_tv(&di->di_tv, &li2->li_tv);
1263 }
1264 }
1265 }
1266}
1267
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001268/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001269 * "items(dict)" function
1270 */
1271 void
1272f_items(typval_T *argvars, typval_T *rettv)
1273{
1274 dict_list(argvars, rettv, 2);
1275}
1276
1277/*
1278 * "keys()" function
1279 */
1280 void
1281f_keys(typval_T *argvars, typval_T *rettv)
1282{
1283 dict_list(argvars, rettv, 0);
1284}
1285
1286/*
1287 * "values(dict)" function
1288 */
1289 void
1290f_values(typval_T *argvars, typval_T *rettv)
1291{
1292 dict_list(argvars, rettv, 1);
1293}
1294
1295/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001296 * Make each item in the dict readonly (not the value of the item).
1297 */
1298 void
1299dict_set_items_ro(dict_T *di)
1300{
1301 int todo = (int)di->dv_hashtab.ht_used;
1302 hashitem_T *hi;
1303
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001304 // Set readonly
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001305 for (hi = di->dv_hashtab.ht_array; todo > 0 ; ++hi)
1306 {
1307 if (HASHITEM_EMPTY(hi))
1308 continue;
1309 --todo;
1310 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
1311 }
1312}
1313
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001314/*
1315 * "has_key()" function
1316 */
1317 void
1318f_has_key(typval_T *argvars, typval_T *rettv)
1319{
1320 if (argvars[0].v_type != VAR_DICT)
1321 {
1322 emsg(_(e_dictreq));
1323 return;
1324 }
1325 if (argvars[0].vval.v_dict == NULL)
1326 return;
1327
1328 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
1329 tv_get_string(&argvars[1]), -1) != NULL;
1330}
1331
1332/*
1333 * "remove({dict})" function
1334 */
1335 void
1336dict_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1337{
1338 dict_T *d;
1339 char_u *key;
1340 dictitem_T *di;
1341
1342 if (argvars[2].v_type != VAR_UNKNOWN)
1343 semsg(_(e_toomanyarg), "remove()");
1344 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001345 && !value_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001346 {
1347 key = tv_get_string_chk(&argvars[1]);
1348 if (key != NULL)
1349 {
1350 di = dict_find(d, key, -1);
1351 if (di == NULL)
1352 semsg(_(e_dictkey), key);
1353 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
1354 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
1355 {
1356 *rettv = di->di_tv;
1357 init_tv(&di->di_tv);
1358 dictitem_remove(d, di);
1359 }
1360 }
1361 }
1362}
1363
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001364#endif // defined(FEAT_EVAL)