blob: 260f229850f042e3ec4addffcaba27fccb6c00e2 [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/*
348 * Add item "item" to Dictionary "d".
349 * Returns FAIL when out of memory and when key already exists.
350 */
351 int
352dict_add(dict_T *d, dictitem_T *item)
353{
354 return hash_add(&d->dv_hashtab, item->di_key);
355}
356
357/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200358 * Add a number or special entry to dictionary "d".
Bram Moolenaarcd524592016-07-17 14:57:05 +0200359 * Returns FAIL when out of memory and when key already exists.
360 */
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200361 static int
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100362dict_add_number_special(dict_T *d, char *key, varnumber_T nr, vartype_T vartype)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200363{
364 dictitem_T *item;
365
366 item = dictitem_alloc((char_u *)key);
367 if (item == NULL)
368 return FAIL;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100369 item->di_tv.v_type = vartype;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200370 item->di_tv.vval.v_number = nr;
371 if (dict_add(d, item) == FAIL)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200372 {
Bram Moolenaare0be1672018-07-08 16:50:37 +0200373 dictitem_free(item);
374 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200375 }
Bram Moolenaare0be1672018-07-08 16:50:37 +0200376 return OK;
377}
378
379/*
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200380 * Add a number entry to dictionary "d".
381 * Returns FAIL when out of memory and when key already exists.
382 */
383 int
384dict_add_number(dict_T *d, char *key, varnumber_T nr)
385{
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100386 return dict_add_number_special(d, key, nr, VAR_NUMBER);
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200387}
388
389/*
390 * Add a special entry to dictionary "d".
391 * Returns FAIL when out of memory and when key already exists.
392 */
393 int
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100394dict_add_bool(dict_T *d, char *key, varnumber_T nr)
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200395{
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100396 return dict_add_number_special(d, key, nr, VAR_BOOL);
Bram Moolenaard7f246c2019-04-08 18:15:41 +0200397}
398
399/*
Bram Moolenaare0be1672018-07-08 16:50:37 +0200400 * Add a string entry to dictionary "d".
401 * Returns FAIL when out of memory and when key already exists.
402 */
403 int
404dict_add_string(dict_T *d, char *key, char_u *str)
405{
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100406 return dict_add_string_len(d, key, str, -1);
407}
408
409/*
410 * Add a string entry to dictionary "d".
411 * "str" will be copied to allocated memory.
412 * When "len" is -1 use the whole string, otherwise only this many bytes.
413 * Returns FAIL when out of memory and when key already exists.
414 */
415 int
416dict_add_string_len(dict_T *d, char *key, char_u *str, int len)
417{
Bram Moolenaare0be1672018-07-08 16:50:37 +0200418 dictitem_T *item;
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100419 char_u *val = NULL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200420
421 item = dictitem_alloc((char_u *)key);
422 if (item == NULL)
423 return FAIL;
Bram Moolenaare0be1672018-07-08 16:50:37 +0200424 item->di_tv.v_type = VAR_STRING;
Bram Moolenaare6fdf792018-12-26 22:57:42 +0100425 if (str != NULL)
426 {
427 if (len == -1)
428 val = vim_strsave(str);
429 else
430 val = vim_strnsave(str, len);
431 }
432 item->di_tv.vval.v_string = val;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200433 if (dict_add(d, item) == FAIL)
434 {
435 dictitem_free(item);
436 return FAIL;
437 }
438 return OK;
439}
440
441/*
442 * Add a list entry to dictionary "d".
443 * Returns FAIL when out of memory and when key already exists.
444 */
445 int
446dict_add_list(dict_T *d, char *key, list_T *list)
447{
448 dictitem_T *item;
449
450 item = dictitem_alloc((char_u *)key);
451 if (item == NULL)
452 return FAIL;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200453 item->di_tv.v_type = VAR_LIST;
454 item->di_tv.vval.v_list = list;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100455 ++list->lv_refcount;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200456 if (dict_add(d, item) == FAIL)
457 {
458 dictitem_free(item);
459 return FAIL;
460 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200461 return OK;
462}
463
464/*
Bram Moolenaar08928322020-01-04 14:32:48 +0100465 * Add a typval_T entry to dictionary "d".
466 * Returns FAIL when out of memory and when key already exists.
467 */
468 int
469dict_add_tv(dict_T *d, char *key, typval_T *tv)
470{
471 dictitem_T *item;
472
473 item = dictitem_alloc((char_u *)key);
474 if (item == NULL)
475 return FAIL;
476 copy_tv(tv, &item->di_tv);
477 if (dict_add(d, item) == FAIL)
478 {
479 dictitem_free(item);
480 return FAIL;
481 }
482 return OK;
483}
484
485/*
Bram Moolenaarae943152019-06-16 22:54:14 +0200486 * Add a callback to dictionary "d".
487 * Returns FAIL when out of memory and when key already exists.
488 */
489 int
490dict_add_callback(dict_T *d, char *key, callback_T *cb)
491{
492 dictitem_T *item;
493
494 item = dictitem_alloc((char_u *)key);
495 if (item == NULL)
496 return FAIL;
497 put_callback(cb, &item->di_tv);
498 if (dict_add(d, item) == FAIL)
499 {
500 dictitem_free(item);
501 return FAIL;
502 }
503 return OK;
504}
505
506/*
Bram Moolenaar45e18cb2019-04-28 18:05:35 +0200507 * Initializes "iter" for iterating over dictionary items with
508 * dict_iterate_next().
509 * If "var" is not a Dict or an empty Dict then there will be nothing to
510 * iterate over, no error is given.
511 * NOTE: The dictionary must not change until iterating is finished!
512 */
513 void
514dict_iterate_start(typval_T *var, dict_iterator_T *iter)
515{
516 if (var->v_type != VAR_DICT || var->vval.v_dict == NULL)
517 iter->dit_todo = 0;
518 else
519 {
520 dict_T *d = var->vval.v_dict;
521
522 iter->dit_todo = d->dv_hashtab.ht_used;
523 iter->dit_hi = d->dv_hashtab.ht_array;
524 }
525}
526
527/*
528 * Iterate over the items referred to by "iter". It should be initialized with
529 * dict_iterate_start().
530 * Returns a pointer to the key.
531 * "*tv_result" is set to point to the value for that key.
532 * If there are no more items, NULL is returned.
533 */
534 char_u *
535dict_iterate_next(dict_iterator_T *iter, typval_T **tv_result)
536{
537 dictitem_T *di;
538 char_u *result;
539
540 if (iter->dit_todo == 0)
541 return NULL;
542
543 while (HASHITEM_EMPTY(iter->dit_hi))
544 ++iter->dit_hi;
545
546 di = HI2DI(iter->dit_hi);
547 result = di->di_key;
548 *tv_result = &di->di_tv;
549
550 --iter->dit_todo;
551 ++iter->dit_hi;
552 return result;
553}
554
555/*
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200556 * Add a dict entry to dictionary "d".
557 * Returns FAIL when out of memory and when key already exists.
558 */
559 int
560dict_add_dict(dict_T *d, char *key, dict_T *dict)
561{
562 dictitem_T *item;
563
564 item = dictitem_alloc((char_u *)key);
565 if (item == NULL)
566 return FAIL;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200567 item->di_tv.v_type = VAR_DICT;
568 item->di_tv.vval.v_dict = dict;
Bram Moolenaar42f45b82017-03-14 22:17:14 +0100569 ++dict->dv_refcount;
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200570 if (dict_add(d, item) == FAIL)
571 {
572 dictitem_free(item);
573 return FAIL;
574 }
Bram Moolenaarb5ae48e2016-08-12 22:23:25 +0200575 return OK;
576}
577
578/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200579 * Get the number of items in a Dictionary.
580 */
581 long
582dict_len(dict_T *d)
583{
584 if (d == NULL)
585 return 0L;
586 return (long)d->dv_hashtab.ht_used;
587}
588
589/*
590 * Find item "key[len]" in Dictionary "d".
591 * If "len" is negative use strlen(key).
592 * Returns NULL when not found.
593 */
594 dictitem_T *
595dict_find(dict_T *d, char_u *key, int len)
596{
597#define AKEYLEN 200
598 char_u buf[AKEYLEN];
599 char_u *akey;
600 char_u *tofree = NULL;
601 hashitem_T *hi;
602
603 if (d == NULL)
604 return NULL;
605 if (len < 0)
606 akey = key;
607 else if (len >= AKEYLEN)
608 {
609 tofree = akey = vim_strnsave(key, len);
610 if (akey == NULL)
611 return NULL;
612 }
613 else
614 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100615 // Avoid a malloc/free by using buf[].
Bram Moolenaarcd524592016-07-17 14:57:05 +0200616 vim_strncpy(buf, key, len);
617 akey = buf;
618 }
619
620 hi = hash_find(&d->dv_hashtab, akey);
621 vim_free(tofree);
622 if (HASHITEM_EMPTY(hi))
623 return NULL;
624 return HI2DI(hi);
625}
626
627/*
Bram Moolenaar08928322020-01-04 14:32:48 +0100628 * Get a typval_T item from a dictionary and copy it into "rettv".
629 * Returns FAIL if the entry doesn't exist or out of memory.
630 */
631 int
632dict_get_tv(dict_T *d, char_u *key, typval_T *rettv)
633{
634 dictitem_T *di;
Bram Moolenaar08928322020-01-04 14:32:48 +0100635
636 di = dict_find(d, key, -1);
637 if (di == NULL)
638 return FAIL;
639 copy_tv(&di->di_tv, rettv);
640 return OK;
641}
642
643/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200644 * Get a string item from a dictionary.
645 * When "save" is TRUE allocate memory for it.
Bram Moolenaar7dc5e2e2016-08-05 22:22:06 +0200646 * When FALSE a shared buffer is used, can only be used once!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200647 * Returns NULL if the entry doesn't exist or out of memory.
648 */
649 char_u *
Bram Moolenaar8f667172018-12-14 15:38:31 +0100650dict_get_string(dict_T *d, char_u *key, int save)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200651{
652 dictitem_T *di;
653 char_u *s;
654
655 di = dict_find(d, key, -1);
656 if (di == NULL)
657 return NULL;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100658 s = tv_get_string(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200659 if (save && s != NULL)
660 s = vim_strsave(s);
661 return s;
662}
663
664/*
665 * Get a number item from a dictionary.
666 * Returns 0 if the entry doesn't exist.
667 */
668 varnumber_T
Bram Moolenaar8f667172018-12-14 15:38:31 +0100669dict_get_number(dict_T *d, char_u *key)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200670{
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200671 return dict_get_number_def(d, key, 0);
672}
673
674/*
675 * Get a number item from a dictionary.
676 * Returns "def" if the entry doesn't exist.
677 */
678 varnumber_T
679dict_get_number_def(dict_T *d, char_u *key, int def)
680{
Bram Moolenaarcd524592016-07-17 14:57:05 +0200681 dictitem_T *di;
682
683 di = dict_find(d, key, -1);
684 if (di == NULL)
Bram Moolenaar8c6173c2019-08-30 22:08:34 +0200685 return def;
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100686 return tv_get_number(&di->di_tv);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200687}
688
689/*
Bram Moolenaarb0ebbda2019-06-02 16:51:21 +0200690 * Get a number item from a dictionary.
691 * Returns 0 if the entry doesn't exist.
692 * Give an error if the entry is not a number.
693 */
694 varnumber_T
695dict_get_number_check(dict_T *d, char_u *key)
696{
697 dictitem_T *di;
698
699 di = dict_find(d, key, -1);
700 if (di == NULL)
701 return 0;
702 if (di->di_tv.v_type != VAR_NUMBER)
703 {
704 semsg(_(e_invarg2), tv_get_string(&di->di_tv));
705 return 0;
706 }
707 return tv_get_number(&di->di_tv);
708}
709
710/*
Bram Moolenaar55881332020-08-18 13:04:15 +0200711 * Get a bool item (number or true/false) from a dictionary.
712 * Returns "def" if the entry doesn't exist.
713 */
714 varnumber_T
715dict_get_bool(dict_T *d, char_u *key, int def)
716{
717 dictitem_T *di;
718
719 di = dict_find(d, key, -1);
720 if (di == NULL)
721 return def;
722 return tv_get_bool(&di->di_tv);
723}
724
725/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200726 * Return an allocated string with the string representation of a Dictionary.
727 * May return NULL.
728 */
729 char_u *
730dict2string(typval_T *tv, int copyID, int restore_copyID)
731{
732 garray_T ga;
733 int first = TRUE;
734 char_u *tofree;
735 char_u numbuf[NUMBUFLEN];
736 hashitem_T *hi;
737 char_u *s;
738 dict_T *d;
739 int todo;
740
741 if ((d = tv->vval.v_dict) == NULL)
742 return NULL;
743 ga_init2(&ga, (int)sizeof(char), 80);
744 ga_append(&ga, '{');
745
746 todo = (int)d->dv_hashtab.ht_used;
747 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
748 {
749 if (!HASHITEM_EMPTY(hi))
750 {
751 --todo;
752
753 if (first)
754 first = FALSE;
755 else
756 ga_concat(&ga, (char_u *)", ");
757
758 tofree = string_quote(hi->hi_key, FALSE);
759 if (tofree != NULL)
760 {
761 ga_concat(&ga, tofree);
762 vim_free(tofree);
763 }
764 ga_concat(&ga, (char_u *)": ");
765 s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID,
766 FALSE, restore_copyID, TRUE);
767 if (s != NULL)
768 ga_concat(&ga, s);
769 vim_free(tofree);
770 if (s == NULL || did_echo_string_emsg)
771 break;
772 line_breakcheck();
773
774 }
775 }
776 if (todo > 0)
777 {
778 vim_free(ga.ga_data);
779 return NULL;
780 }
781
782 ga_append(&ga, '}');
783 ga_append(&ga, NUL);
784 return (char_u *)ga.ga_data;
785}
786
787/*
Bram Moolenaare0de1712020-12-02 17:36:54 +0100788 * Advance over a literal key, including "-". If the first character is not a
789 * literal key character then "key" is returned.
790 */
791 char_u *
792skip_literal_key(char_u *key)
793{
794 char_u *p;
795
796 for (p = key; ASCII_ISALNUM(*p) || *p == '_' || *p == '-'; ++p)
797 ;
798 return p;
799}
800
801/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +0200802 * Get the key for #{key: val} into "tv" and advance "arg".
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200803 * Return FAIL when there is no valid key.
804 */
805 static int
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100806get_literal_key_tv(char_u **arg, typval_T *tv)
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200807{
Bram Moolenaare0de1712020-12-02 17:36:54 +0100808 char_u *p = skip_literal_key(*arg);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200809
Bram Moolenaare0de1712020-12-02 17:36:54 +0100810 if (p == *arg)
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200811 return FAIL;
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200812 tv->v_type = VAR_STRING;
Bram Moolenaardf44a272020-06-07 20:49:05 +0200813 tv->vval.v_string = vim_strnsave(*arg, p - *arg);
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200814
Bram Moolenaardb199212020-08-12 18:01:53 +0200815 *arg = p;
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200816 return OK;
817}
818
819/*
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100820 * Get a literal key for a Vim9 dict:
821 * {"name": value},
822 * {'name': value},
823 * {name: value} use "name" as a literal key
824 * Return the key in allocated memory or NULL in the case of an error.
825 * "arg" is advanced to just after the key.
826 */
827 char_u *
828get_literal_key(char_u **arg)
829{
830 char_u *key;
831 char_u *end;
832 typval_T rettv;
833
834 if (**arg == '\'')
835 {
836 if (eval_lit_string(arg, &rettv, TRUE) == FAIL)
837 return NULL;
838 key = rettv.vval.v_string;
839 }
840 else if (**arg == '"')
841 {
842 if (eval_string(arg, &rettv, TRUE) == FAIL)
843 return NULL;
844 key = rettv.vval.v_string;
845 }
846 else
847 {
848 end = skip_literal_key(*arg);
849 if (end == *arg)
850 {
851 semsg(_(e_invalid_key_str), *arg);
852 return NULL;
853 }
854 key = vim_strnsave(*arg, end - *arg);
855 *arg = end;
856 }
857 return key;
858}
859
860/*
Bram Moolenaarcd524592016-07-17 14:57:05 +0200861 * Allocate a variable for a Dictionary and fill it from "*arg".
Bram Moolenaar962d7212020-07-04 14:15:00 +0200862 * "*arg" points to the "{".
Bram Moolenaar9f9fe372019-07-27 23:12:12 +0200863 * "literal" is TRUE for #{key: val}
Bram Moolenaarcd524592016-07-17 14:57:05 +0200864 * Return OK or FAIL. Returns NOTDONE for {expr}.
865 */
866 int
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200867eval_dict(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int literal)
Bram Moolenaarcd524592016-07-17 14:57:05 +0200868{
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200869 int evaluate = evalarg == NULL ? FALSE
870 : evalarg->eval_flags & EVAL_EVALUATE;
Bram Moolenaarcd524592016-07-17 14:57:05 +0200871 dict_T *d = NULL;
872 typval_T tvkey;
873 typval_T tv;
874 char_u *key = NULL;
875 dictitem_T *item;
876 char_u *start = skipwhite(*arg + 1);
877 char_u buf[NUMBUFLEN];
Bram Moolenaareb6880b2020-07-12 17:07:05 +0200878 int vim9script = in_vim9script();
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200879 int had_comma;
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200880
Bram Moolenaarcd524592016-07-17 14:57:05 +0200881 /*
882 * First check if it's not a curly-braces thing: {expr}.
883 * Must do this without evaluating, otherwise a function may be called
884 * twice. Unfortunately this means we need to call eval1() twice for the
885 * first item.
886 * But {} is an empty Dictionary.
887 */
Bram Moolenaar32e35112020-05-14 22:41:15 +0200888 if (!vim9script && *start != '}')
Bram Moolenaarcd524592016-07-17 14:57:05 +0200889 {
Bram Moolenaar5409f5d2020-06-24 18:37:35 +0200890 if (eval1(&start, &tv, NULL) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200891 return FAIL;
Bram Moolenaarae95a392020-08-06 16:38:12 +0200892 if (*skipwhite(start) == '}')
Bram Moolenaarcd524592016-07-17 14:57:05 +0200893 return NOTDONE;
894 }
895
896 if (evaluate)
897 {
898 d = dict_alloc();
899 if (d == NULL)
900 return FAIL;
901 }
902 tvkey.v_type = VAR_UNKNOWN;
903 tv.v_type = VAR_UNKNOWN;
904
Bram Moolenaar962d7212020-07-04 14:15:00 +0200905 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200906 while (**arg != '}' && **arg != NUL)
907 {
Bram Moolenaare0de1712020-12-02 17:36:54 +0100908 int has_bracket = vim9script && **arg == '[';
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100909
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100910 if (literal)
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100911 {
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100912 if (get_literal_key_tv(arg, &tvkey) == FAIL)
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100913 goto failret;
914 }
Bram Moolenaarc5e6a712020-12-04 19:12:14 +0100915 else if (vim9script && !has_bracket)
916 {
917 tvkey.vval.v_string = get_literal_key(arg);
918 if (tvkey.vval.v_string == NULL)
919 goto failret;
920 tvkey.v_type = VAR_STRING;
921 }
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100922 else
923 {
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100924 if (has_bracket)
925 *arg = skipwhite(*arg + 1);
926 if (eval1(arg, &tvkey, evalarg) == FAIL) // recursive!
927 goto failret;
928 if (has_bracket)
929 {
930 *arg = skipwhite(*arg);
931 if (**arg != ']')
932 {
933 emsg(_(e_missing_matching_bracket_after_dict_key));
Bram Moolenaar8bb0f542020-12-06 16:03:55 +0100934 clear_tv(&tvkey);
Bram Moolenaar67d1c682020-11-19 19:01:43 +0100935 return FAIL;
936 }
937 ++*arg;
938 }
939 }
Bram Moolenaard5abb4c2019-07-13 22:46:10 +0200940
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +0200941 // the colon should come right after the key, but this wasn't checked
Bram Moolenaar9d489562020-07-30 20:08:50 +0200942 // previously, so only require it in Vim9 script.
943 if (!vim9script)
944 *arg = skipwhite(*arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200945 if (**arg != ':')
946 {
Bram Moolenaara2c026d2020-10-18 18:03:17 +0200947 if (*skipwhite(*arg) == ':')
948 semsg(_(e_no_white_space_allowed_before_str), ":");
949 else
950 semsg(_(e_missing_dict_colon), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200951 clear_tv(&tvkey);
952 goto failret;
953 }
954 if (evaluate)
955 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100956#ifdef FEAT_FLOAT
957 if (tvkey.v_type == VAR_FLOAT)
Bram Moolenaar9987fb02020-12-15 21:41:56 +0100958 {
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100959 tvkey.vval.v_string = typval_tostring(&tvkey, TRUE);
960 tvkey.v_type = VAR_STRING;
Bram Moolenaar9987fb02020-12-15 21:41:56 +0100961 }
Bram Moolenaar2e5910b2021-02-03 17:41:24 +0100962#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100963 key = tv_get_string_buf_chk(&tvkey, buf);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200964 if (key == NULL)
965 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +0100966 // "key" is NULL when tv_get_string_buf_chk() gave an errmsg
Bram Moolenaarcd524592016-07-17 14:57:05 +0200967 clear_tv(&tvkey);
968 goto failret;
969 }
970 }
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200971 if (vim9script && (*arg)[1] != NUL && !VIM_ISWHITE((*arg)[1]))
972 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +0200973 semsg(_(e_white_space_required_after_str), ":");
Bram Moolenaarab19d492020-06-27 17:04:05 +0200974 clear_tv(&tvkey);
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200975 goto failret;
976 }
Bram Moolenaarcd524592016-07-17 14:57:05 +0200977
Bram Moolenaar962d7212020-07-04 14:15:00 +0200978 *arg = skipwhite_and_linebreak(*arg + 1, evalarg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +0200979 if (eval1(arg, &tv, evalarg) == FAIL) // recursive!
Bram Moolenaarcd524592016-07-17 14:57:05 +0200980 {
981 if (evaluate)
982 clear_tv(&tvkey);
983 goto failret;
984 }
985 if (evaluate)
986 {
987 item = dict_find(d, key, -1);
988 if (item != NULL)
989 {
Bram Moolenaara2c026d2020-10-18 18:03:17 +0200990 semsg(_(e_duplicate_key), key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200991 clear_tv(&tvkey);
992 clear_tv(&tv);
993 goto failret;
994 }
995 item = dictitem_alloc(key);
Bram Moolenaarcd524592016-07-17 14:57:05 +0200996 if (item != NULL)
997 {
998 item->di_tv = tv;
999 item->di_tv.v_lock = 0;
1000 if (dict_add(d, item) == FAIL)
1001 dictitem_free(item);
1002 }
1003 }
Bram Moolenaara8931942019-09-28 17:25:10 +02001004 clear_tv(&tvkey);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001005
Bram Moolenaar4d4d1cd2020-07-30 22:14:33 +02001006 // the comma should come right after the value, but this wasn't checked
1007 // previously, so only require it in Vim9 script.
1008 if (!vim9script)
1009 *arg = skipwhite(*arg);
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001010 had_comma = **arg == ',';
1011 if (had_comma)
1012 {
1013 if (vim9script && (*arg)[1] != NUL && !VIM_ISWHITE((*arg)[1]))
1014 {
Bram Moolenaar7cb6fc22020-08-21 22:36:47 +02001015 semsg(_(e_white_space_required_after_str), ",");
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001016 goto failret;
1017 }
1018 *arg = skipwhite(*arg + 1);
1019 }
1020
1021 // the "}" can be on the next line
Bram Moolenaar962d7212020-07-04 14:15:00 +02001022 *arg = skipwhite_and_linebreak(*arg, evalarg);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001023 if (**arg == '}')
1024 break;
Bram Moolenaar8ea93902020-06-27 14:11:53 +02001025 if (!had_comma)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001026 {
Bram Moolenaara2c026d2020-10-18 18:03:17 +02001027 if (**arg == ',')
1028 semsg(_(e_no_white_space_allowed_before_str), ",");
1029 else
1030 semsg(_(e_missing_dict_comma), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001031 goto failret;
1032 }
Bram Moolenaarcd524592016-07-17 14:57:05 +02001033 }
1034
1035 if (**arg != '}')
1036 {
Bram Moolenaar4bce26b2021-01-22 22:06:56 +01001037 if (evalarg != NULL)
1038 semsg(_(e_missing_dict_end), *arg);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001039failret:
Bram Moolenaaradc67142019-06-22 01:40:42 +02001040 if (d != NULL)
Bram Moolenaarcd524592016-07-17 14:57:05 +02001041 dict_free(d);
1042 return FAIL;
1043 }
1044
Bram Moolenaarb07a39d2020-10-22 19:00:01 +02001045 *arg = *arg + 1;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001046 if (evaluate)
Bram Moolenaar45cf6e92017-04-30 20:25:19 +02001047 rettv_dict_set(rettv, d);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001048
1049 return OK;
1050}
1051
1052/*
1053 * Go over all entries in "d2" and add them to "d1".
1054 * When "action" is "error" then a duplicate key is an error.
1055 * When "action" is "force" then a duplicate key is overwritten.
1056 * Otherwise duplicate keys are ignored ("action" is "keep").
1057 */
1058 void
1059dict_extend(dict_T *d1, dict_T *d2, char_u *action)
1060{
1061 dictitem_T *di1;
1062 hashitem_T *hi2;
1063 int todo;
1064 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001065 type_T *type;
1066
1067 if (d1->dv_type != NULL && d1->dv_type->tt_member != NULL)
1068 type = d1->dv_type->tt_member;
1069 else
1070 type = NULL;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001071
1072 todo = (int)d2->dv_hashtab.ht_used;
1073 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
1074 {
1075 if (!HASHITEM_EMPTY(hi2))
1076 {
1077 --todo;
1078 di1 = dict_find(d1, hi2->hi_key, -1);
1079 if (d1->dv_scope != 0)
1080 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001081 // Disallow replacing a builtin function in l: and g:.
1082 // Check the key to be valid when adding to any scope.
Bram Moolenaarcd524592016-07-17 14:57:05 +02001083 if (d1->dv_scope == VAR_DEF_SCOPE
1084 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
Bram Moolenaar98b4f142020-08-08 15:46:01 +02001085 && var_wrong_func_name(hi2->hi_key, di1 == NULL))
Bram Moolenaarcd524592016-07-17 14:57:05 +02001086 break;
Bram Moolenaar03290b82020-12-19 16:30:44 +01001087 if (!valid_varname(hi2->hi_key, TRUE))
Bram Moolenaarcd524592016-07-17 14:57:05 +02001088 break;
1089 }
Bram Moolenaaraa210a32021-01-02 15:41:03 +01001090
1091 if (type != NULL
1092 && check_typval_type(type, &HI2DI(hi2)->di_tv, 0) == FAIL)
1093 break;
1094
Bram Moolenaarcd524592016-07-17 14:57:05 +02001095 if (di1 == NULL)
1096 {
1097 di1 = dictitem_copy(HI2DI(hi2));
1098 if (di1 != NULL && dict_add(d1, di1) == FAIL)
1099 dictitem_free(di1);
1100 }
1101 else if (*action == 'e')
1102 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001103 semsg(_("E737: Key already exists: %s"), hi2->hi_key);
Bram Moolenaarcd524592016-07-17 14:57:05 +02001104 break;
1105 }
1106 else if (*action == 'f' && HI2DI(hi2) != di1)
1107 {
Bram Moolenaara187c432020-09-16 21:08:28 +02001108 if (value_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
Bram Moolenaar05c00c02019-02-11 22:00:11 +01001109 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaarcd524592016-07-17 14:57:05 +02001110 break;
1111 clear_tv(&di1->di_tv);
1112 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
1113 }
1114 }
1115 }
1116}
1117
1118/*
1119 * Return the dictitem that an entry in a hashtable points to.
1120 */
1121 dictitem_T *
1122dict_lookup(hashitem_T *hi)
1123{
1124 return HI2DI(hi);
1125}
1126
1127/*
1128 * Return TRUE when two dictionaries have exactly the same key/values.
1129 */
1130 int
1131dict_equal(
1132 dict_T *d1,
1133 dict_T *d2,
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001134 int ic, // ignore case for strings
1135 int recursive) // TRUE when used recursively
Bram Moolenaarcd524592016-07-17 14:57:05 +02001136{
1137 hashitem_T *hi;
1138 dictitem_T *item2;
1139 int todo;
1140
Bram Moolenaarcd524592016-07-17 14:57:05 +02001141 if (d1 == d2)
1142 return TRUE;
1143 if (dict_len(d1) != dict_len(d2))
1144 return FALSE;
Bram Moolenaarea04a6e2020-04-23 13:38:02 +02001145 if (dict_len(d1) == 0)
1146 // empty and NULL dicts are considered equal
1147 return TRUE;
1148 if (d1 == NULL || d2 == NULL)
1149 return FALSE;
Bram Moolenaarcd524592016-07-17 14:57:05 +02001150
1151 todo = (int)d1->dv_hashtab.ht_used;
1152 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
1153 {
1154 if (!HASHITEM_EMPTY(hi))
1155 {
1156 item2 = dict_find(d2, hi->hi_key, -1);
1157 if (item2 == NULL)
1158 return FALSE;
1159 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
1160 return FALSE;
1161 --todo;
1162 }
1163 }
1164 return TRUE;
1165}
1166
1167/*
1168 * Turn a dict into a list:
1169 * "what" == 0: list of keys
1170 * "what" == 1: list of values
1171 * "what" == 2: list of items
1172 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001173 static void
Bram Moolenaarcd524592016-07-17 14:57:05 +02001174dict_list(typval_T *argvars, typval_T *rettv, int what)
1175{
1176 list_T *l2;
1177 dictitem_T *di;
1178 hashitem_T *hi;
1179 listitem_T *li;
1180 listitem_T *li2;
1181 dict_T *d;
1182 int todo;
1183
1184 if (argvars[0].v_type != VAR_DICT)
1185 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001186 emsg(_(e_dictreq));
Bram Moolenaarcd524592016-07-17 14:57:05 +02001187 return;
1188 }
1189 if ((d = argvars[0].vval.v_dict) == NULL)
1190 return;
1191
1192 if (rettv_list_alloc(rettv) == FAIL)
1193 return;
1194
1195 todo = (int)d->dv_hashtab.ht_used;
1196 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
1197 {
1198 if (!HASHITEM_EMPTY(hi))
1199 {
1200 --todo;
1201 di = HI2DI(hi);
1202
1203 li = listitem_alloc();
1204 if (li == NULL)
1205 break;
1206 list_append(rettv->vval.v_list, li);
1207
1208 if (what == 0)
1209 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001210 // keys()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001211 li->li_tv.v_type = VAR_STRING;
1212 li->li_tv.v_lock = 0;
1213 li->li_tv.vval.v_string = vim_strsave(di->di_key);
1214 }
1215 else if (what == 1)
1216 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001217 // values()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001218 copy_tv(&di->di_tv, &li->li_tv);
1219 }
1220 else
1221 {
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001222 // items()
Bram Moolenaarcd524592016-07-17 14:57:05 +02001223 l2 = list_alloc();
1224 li->li_tv.v_type = VAR_LIST;
1225 li->li_tv.v_lock = 0;
1226 li->li_tv.vval.v_list = l2;
1227 if (l2 == NULL)
1228 break;
1229 ++l2->lv_refcount;
1230
1231 li2 = listitem_alloc();
1232 if (li2 == NULL)
1233 break;
1234 list_append(l2, li2);
1235 li2->li_tv.v_type = VAR_STRING;
1236 li2->li_tv.v_lock = 0;
1237 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
1238
1239 li2 = listitem_alloc();
1240 if (li2 == NULL)
1241 break;
1242 list_append(l2, li2);
1243 copy_tv(&di->di_tv, &li2->li_tv);
1244 }
1245 }
1246 }
1247}
1248
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001249/*
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001250 * "items(dict)" function
1251 */
1252 void
1253f_items(typval_T *argvars, typval_T *rettv)
1254{
1255 dict_list(argvars, rettv, 2);
1256}
1257
1258/*
1259 * "keys()" function
1260 */
1261 void
1262f_keys(typval_T *argvars, typval_T *rettv)
1263{
1264 dict_list(argvars, rettv, 0);
1265}
1266
1267/*
1268 * "values(dict)" function
1269 */
1270 void
1271f_values(typval_T *argvars, typval_T *rettv)
1272{
1273 dict_list(argvars, rettv, 1);
1274}
1275
1276/*
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001277 * Make each item in the dict readonly (not the value of the item).
1278 */
1279 void
1280dict_set_items_ro(dict_T *di)
1281{
1282 int todo = (int)di->dv_hashtab.ht_used;
1283 hashitem_T *hi;
1284
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001285 // Set readonly
Bram Moolenaar7e1652c2017-12-16 18:27:02 +01001286 for (hi = di->dv_hashtab.ht_array; todo > 0 ; ++hi)
1287 {
1288 if (HASHITEM_EMPTY(hi))
1289 continue;
1290 --todo;
1291 HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
1292 }
1293}
1294
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001295/*
1296 * "has_key()" function
1297 */
1298 void
1299f_has_key(typval_T *argvars, typval_T *rettv)
1300{
1301 if (argvars[0].v_type != VAR_DICT)
1302 {
1303 emsg(_(e_dictreq));
1304 return;
1305 }
1306 if (argvars[0].vval.v_dict == NULL)
1307 return;
1308
1309 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
1310 tv_get_string(&argvars[1]), -1) != NULL;
1311}
1312
1313/*
1314 * "remove({dict})" function
1315 */
1316 void
1317dict_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg)
1318{
1319 dict_T *d;
1320 char_u *key;
1321 dictitem_T *di;
1322
1323 if (argvars[2].v_type != VAR_UNKNOWN)
1324 semsg(_(e_toomanyarg), "remove()");
1325 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaara187c432020-09-16 21:08:28 +02001326 && !value_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar9f9fe372019-07-27 23:12:12 +02001327 {
1328 key = tv_get_string_chk(&argvars[1]);
1329 if (key != NULL)
1330 {
1331 di = dict_find(d, key, -1);
1332 if (di == NULL)
1333 semsg(_(e_dictkey), key);
1334 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
1335 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
1336 {
1337 *rettv = di->di_tv;
1338 init_tv(&di->di_tv);
1339 dictitem_remove(d, di);
1340 }
1341 }
1342 }
1343}
1344
Bram Moolenaar5d18efe2019-12-01 21:11:22 +01001345#endif // defined(FEAT_EVAL)