Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 2 | * |
| 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 Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 13 | |
| 14 | #include "vim.h" |
| 15 | |
| 16 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 17 | |
Bram Moolenaar | 5d18efe | 2019-12-01 21:11:22 +0100 | [diff] [blame] | 18 | // List head for garbage collection. Although there can be a reference loop |
| 19 | // from partial to dict to partial, we don't need to keep track of the partial, |
| 20 | // since it will get freed when the dict is unused and gets freed. |
| 21 | static dict_T *first_dict = NULL; |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 22 | |
| 23 | /* |
| 24 | * Allocate an empty header for a dictionary. |
| 25 | */ |
| 26 | dict_T * |
| 27 | dict_alloc(void) |
| 28 | { |
| 29 | dict_T *d; |
| 30 | |
Bram Moolenaar | adc6714 | 2019-06-22 01:40:42 +0200 | [diff] [blame] | 31 | d = ALLOC_CLEAR_ONE(dict_T); |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 32 | if (d != NULL) |
| 33 | { |
Bram Moolenaar | 5d18efe | 2019-12-01 21:11:22 +0100 | [diff] [blame] | 34 | // Add the dict to the list of dicts for garbage collection. |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 35 | if (first_dict != NULL) |
| 36 | first_dict->dv_used_prev = d; |
| 37 | d->dv_used_next = first_dict; |
| 38 | d->dv_used_prev = NULL; |
| 39 | first_dict = d; |
| 40 | |
| 41 | hash_init(&d->dv_hashtab); |
| 42 | d->dv_lock = 0; |
| 43 | d->dv_scope = 0; |
| 44 | d->dv_refcount = 0; |
| 45 | d->dv_copyID = 0; |
| 46 | } |
| 47 | return d; |
| 48 | } |
| 49 | |
Bram Moolenaar | f49cc60 | 2018-11-11 15:21:05 +0100 | [diff] [blame] | 50 | /* |
| 51 | * dict_alloc() with an ID for alloc_fail(). |
| 52 | */ |
| 53 | dict_T * |
| 54 | dict_alloc_id(alloc_id_T id UNUSED) |
| 55 | { |
| 56 | #ifdef FEAT_EVAL |
Bram Moolenaar | 51e1438 | 2019-05-25 20:21:28 +0200 | [diff] [blame] | 57 | if (alloc_fail_id == id && alloc_does_fail(sizeof(list_T))) |
Bram Moolenaar | f49cc60 | 2018-11-11 15:21:05 +0100 | [diff] [blame] | 58 | return NULL; |
| 59 | #endif |
| 60 | return (dict_alloc()); |
| 61 | } |
| 62 | |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 63 | dict_T * |
| 64 | dict_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 Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 73 | /* |
| 74 | * Allocate an empty dict for a return value. |
| 75 | * Returns OK or FAIL. |
| 76 | */ |
| 77 | int |
| 78 | rettv_dict_alloc(typval_T *rettv) |
| 79 | { |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 80 | dict_T *d = dict_alloc_lock(0); |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 81 | |
| 82 | if (d == NULL) |
| 83 | return FAIL; |
| 84 | |
Bram Moolenaar | 45cf6e9 | 2017-04-30 20:25:19 +0200 | [diff] [blame] | 85 | rettv_dict_set(rettv, d); |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 86 | return OK; |
| 87 | } |
| 88 | |
| 89 | /* |
Bram Moolenaar | 45cf6e9 | 2017-04-30 20:25:19 +0200 | [diff] [blame] | 90 | * Set a dictionary as the return value |
| 91 | */ |
| 92 | void |
| 93 | rettv_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 Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 102 | * Free a Dictionary, including all non-container items it contains. |
| 103 | * Ignores the reference count. |
| 104 | */ |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 105 | void |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 106 | dict_free_contents(dict_T *d) |
| 107 | { |
| 108 | int todo; |
| 109 | hashitem_T *hi; |
| 110 | dictitem_T *di; |
| 111 | |
Bram Moolenaar | 5d18efe | 2019-12-01 21:11:22 +0100 | [diff] [blame] | 112 | // Lock the hashtab, we don't want it to resize while freeing items. |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 113 | hash_lock(&d->dv_hashtab); |
| 114 | todo = (int)d->dv_hashtab.ht_used; |
| 115 | for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi) |
| 116 | { |
| 117 | if (!HASHITEM_EMPTY(hi)) |
| 118 | { |
Bram Moolenaar | 5d18efe | 2019-12-01 21:11:22 +0100 | [diff] [blame] | 119 | // Remove the item before deleting it, just in case there is |
| 120 | // something recursive causing trouble. |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 121 | di = HI2DI(hi); |
| 122 | hash_remove(&d->dv_hashtab, hi); |
Bram Moolenaar | 95c526e | 2017-02-25 14:59:34 +0100 | [diff] [blame] | 123 | dictitem_free(di); |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 124 | --todo; |
| 125 | } |
| 126 | } |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 127 | |
Bram Moolenaar | 5d18efe | 2019-12-01 21:11:22 +0100 | [diff] [blame] | 128 | // The hashtab is still locked, it has to be re-initialized anyway |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 129 | hash_clear(&d->dv_hashtab); |
| 130 | } |
| 131 | |
| 132 | static void |
| 133 | dict_free_dict(dict_T *d) |
| 134 | { |
Bram Moolenaar | 5d18efe | 2019-12-01 21:11:22 +0100 | [diff] [blame] | 135 | // Remove the dict from the list of dicts for garbage collection. |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 136 | if (d->dv_used_prev == NULL) |
| 137 | first_dict = d->dv_used_next; |
| 138 | else |
| 139 | d->dv_used_prev->dv_used_next = d->dv_used_next; |
| 140 | if (d->dv_used_next != NULL) |
| 141 | d->dv_used_next->dv_used_prev = d->dv_used_prev; |
| 142 | vim_free(d); |
| 143 | } |
| 144 | |
| 145 | static void |
| 146 | dict_free(dict_T *d) |
| 147 | { |
| 148 | if (!in_free_unref_items) |
| 149 | { |
| 150 | dict_free_contents(d); |
| 151 | dict_free_dict(d); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Unreference a Dictionary: decrement the reference count and free it when it |
| 157 | * becomes zero. |
| 158 | */ |
| 159 | void |
| 160 | dict_unref(dict_T *d) |
| 161 | { |
| 162 | if (d != NULL && --d->dv_refcount <= 0) |
| 163 | dict_free(d); |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * Go through the list of dicts and free items without the copyID. |
| 168 | * Returns TRUE if something was freed. |
| 169 | */ |
| 170 | int |
| 171 | dict_free_nonref(int copyID) |
| 172 | { |
| 173 | dict_T *dd; |
| 174 | int did_free = FALSE; |
| 175 | |
| 176 | for (dd = first_dict; dd != NULL; dd = dd->dv_used_next) |
| 177 | if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)) |
| 178 | { |
Bram Moolenaar | 5d18efe | 2019-12-01 21:11:22 +0100 | [diff] [blame] | 179 | // Free the Dictionary and ordinary items it contains, but don't |
| 180 | // recurse into Lists and Dictionaries, they will be in the list |
| 181 | // of dicts or list of lists. |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 182 | dict_free_contents(dd); |
| 183 | did_free = TRUE; |
| 184 | } |
| 185 | return did_free; |
| 186 | } |
| 187 | |
| 188 | void |
| 189 | dict_free_items(int copyID) |
| 190 | { |
| 191 | dict_T *dd, *dd_next; |
| 192 | |
| 193 | for (dd = first_dict; dd != NULL; dd = dd_next) |
| 194 | { |
| 195 | dd_next = dd->dv_used_next; |
| 196 | if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)) |
| 197 | dict_free_dict(dd); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * Allocate a Dictionary item. |
| 203 | * The "key" is copied to the new item. |
Bram Moolenaar | c89d4b3 | 2018-07-08 17:19:02 +0200 | [diff] [blame] | 204 | * Note that the type and value of the item "di_tv" still needs to be |
| 205 | * initialized! |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 206 | * Returns NULL when out of memory. |
| 207 | */ |
| 208 | dictitem_T * |
| 209 | dictitem_alloc(char_u *key) |
| 210 | { |
| 211 | dictitem_T *di; |
| 212 | |
Bram Moolenaar | b59e735 | 2019-08-07 21:42:24 +0200 | [diff] [blame] | 213 | di = alloc(offsetof(dictitem_T, di_key) + STRLEN(key) + 1); |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 214 | if (di != NULL) |
| 215 | { |
| 216 | STRCPY(di->di_key, key); |
| 217 | di->di_flags = DI_FLAGS_ALLOC; |
Bram Moolenaar | c89d4b3 | 2018-07-08 17:19:02 +0200 | [diff] [blame] | 218 | di->di_tv.v_lock = 0; |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 219 | } |
| 220 | return di; |
| 221 | } |
| 222 | |
| 223 | /* |
| 224 | * Make a copy of a Dictionary item. |
| 225 | */ |
| 226 | static dictitem_T * |
| 227 | dictitem_copy(dictitem_T *org) |
| 228 | { |
| 229 | dictitem_T *di; |
| 230 | |
Bram Moolenaar | b59e735 | 2019-08-07 21:42:24 +0200 | [diff] [blame] | 231 | di = alloc(offsetof(dictitem_T, di_key) + STRLEN(org->di_key) + 1); |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 232 | if (di != NULL) |
| 233 | { |
| 234 | STRCPY(di->di_key, org->di_key); |
| 235 | di->di_flags = DI_FLAGS_ALLOC; |
| 236 | copy_tv(&org->di_tv, &di->di_tv); |
| 237 | } |
| 238 | return di; |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Remove item "item" from Dictionary "dict" and free it. |
| 243 | */ |
| 244 | void |
| 245 | dictitem_remove(dict_T *dict, dictitem_T *item) |
| 246 | { |
| 247 | hashitem_T *hi; |
| 248 | |
| 249 | hi = hash_find(&dict->dv_hashtab, item->di_key); |
| 250 | if (HASHITEM_EMPTY(hi)) |
Bram Moolenaar | 95f0960 | 2016-11-10 20:01:45 +0100 | [diff] [blame] | 251 | internal_error("dictitem_remove()"); |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 252 | else |
| 253 | hash_remove(&dict->dv_hashtab, hi); |
| 254 | dictitem_free(item); |
| 255 | } |
| 256 | |
| 257 | /* |
| 258 | * Free a dict item. Also clears the value. |
| 259 | */ |
| 260 | void |
| 261 | dictitem_free(dictitem_T *item) |
| 262 | { |
| 263 | clear_tv(&item->di_tv); |
| 264 | if (item->di_flags & DI_FLAGS_ALLOC) |
| 265 | vim_free(item); |
| 266 | } |
| 267 | |
| 268 | /* |
| 269 | * Make a copy of dict "d". Shallow if "deep" is FALSE. |
| 270 | * The refcount of the new dict is set to 1. |
| 271 | * See item_copy() for "copyID". |
| 272 | * Returns NULL when out of memory. |
| 273 | */ |
| 274 | dict_T * |
| 275 | dict_copy(dict_T *orig, int deep, int copyID) |
| 276 | { |
| 277 | dict_T *copy; |
| 278 | dictitem_T *di; |
| 279 | int todo; |
| 280 | hashitem_T *hi; |
| 281 | |
| 282 | if (orig == NULL) |
| 283 | return NULL; |
| 284 | |
| 285 | copy = dict_alloc(); |
| 286 | if (copy != NULL) |
| 287 | { |
| 288 | if (copyID != 0) |
| 289 | { |
| 290 | orig->dv_copyID = copyID; |
| 291 | orig->dv_copydict = copy; |
| 292 | } |
| 293 | todo = (int)orig->dv_hashtab.ht_used; |
| 294 | for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi) |
| 295 | { |
| 296 | if (!HASHITEM_EMPTY(hi)) |
| 297 | { |
| 298 | --todo; |
| 299 | |
| 300 | di = dictitem_alloc(hi->hi_key); |
| 301 | if (di == NULL) |
| 302 | break; |
| 303 | if (deep) |
| 304 | { |
| 305 | if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep, |
| 306 | copyID) == FAIL) |
| 307 | { |
| 308 | vim_free(di); |
| 309 | break; |
| 310 | } |
| 311 | } |
| 312 | else |
| 313 | copy_tv(&HI2DI(hi)->di_tv, &di->di_tv); |
| 314 | if (dict_add(copy, di) == FAIL) |
| 315 | { |
| 316 | dictitem_free(di); |
| 317 | break; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | ++copy->dv_refcount; |
| 323 | if (todo > 0) |
| 324 | { |
| 325 | dict_unref(copy); |
| 326 | copy = NULL; |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | return copy; |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * Add item "item" to Dictionary "d". |
| 335 | * Returns FAIL when out of memory and when key already exists. |
| 336 | */ |
| 337 | int |
| 338 | dict_add(dict_T *d, dictitem_T *item) |
| 339 | { |
| 340 | return hash_add(&d->dv_hashtab, item->di_key); |
| 341 | } |
| 342 | |
| 343 | /* |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 344 | * Add a number or special entry to dictionary "d". |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 345 | * Returns FAIL when out of memory and when key already exists. |
| 346 | */ |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 347 | static int |
Bram Moolenaar | 9b4a15d | 2020-01-11 16:05:23 +0100 | [diff] [blame] | 348 | dict_add_number_special(dict_T *d, char *key, varnumber_T nr, vartype_T vartype) |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 349 | { |
| 350 | dictitem_T *item; |
| 351 | |
| 352 | item = dictitem_alloc((char_u *)key); |
| 353 | if (item == NULL) |
| 354 | return FAIL; |
Bram Moolenaar | 9b4a15d | 2020-01-11 16:05:23 +0100 | [diff] [blame] | 355 | item->di_tv.v_type = vartype; |
Bram Moolenaar | e0be167 | 2018-07-08 16:50:37 +0200 | [diff] [blame] | 356 | item->di_tv.vval.v_number = nr; |
| 357 | if (dict_add(d, item) == FAIL) |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 358 | { |
Bram Moolenaar | e0be167 | 2018-07-08 16:50:37 +0200 | [diff] [blame] | 359 | dictitem_free(item); |
| 360 | return FAIL; |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 361 | } |
Bram Moolenaar | e0be167 | 2018-07-08 16:50:37 +0200 | [diff] [blame] | 362 | return OK; |
| 363 | } |
| 364 | |
| 365 | /* |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 366 | * Add a number entry to dictionary "d". |
| 367 | * Returns FAIL when out of memory and when key already exists. |
| 368 | */ |
| 369 | int |
| 370 | dict_add_number(dict_T *d, char *key, varnumber_T nr) |
| 371 | { |
Bram Moolenaar | 9b4a15d | 2020-01-11 16:05:23 +0100 | [diff] [blame] | 372 | return dict_add_number_special(d, key, nr, VAR_NUMBER); |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /* |
| 376 | * Add a special entry to dictionary "d". |
| 377 | * Returns FAIL when out of memory and when key already exists. |
| 378 | */ |
| 379 | int |
Bram Moolenaar | 9b4a15d | 2020-01-11 16:05:23 +0100 | [diff] [blame] | 380 | dict_add_bool(dict_T *d, char *key, varnumber_T nr) |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 381 | { |
Bram Moolenaar | 9b4a15d | 2020-01-11 16:05:23 +0100 | [diff] [blame] | 382 | return dict_add_number_special(d, key, nr, VAR_BOOL); |
Bram Moolenaar | d7f246c | 2019-04-08 18:15:41 +0200 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /* |
Bram Moolenaar | e0be167 | 2018-07-08 16:50:37 +0200 | [diff] [blame] | 386 | * Add a string entry to dictionary "d". |
| 387 | * Returns FAIL when out of memory and when key already exists. |
| 388 | */ |
| 389 | int |
| 390 | dict_add_string(dict_T *d, char *key, char_u *str) |
| 391 | { |
Bram Moolenaar | e6fdf79 | 2018-12-26 22:57:42 +0100 | [diff] [blame] | 392 | return dict_add_string_len(d, key, str, -1); |
| 393 | } |
| 394 | |
| 395 | /* |
| 396 | * Add a string entry to dictionary "d". |
| 397 | * "str" will be copied to allocated memory. |
| 398 | * When "len" is -1 use the whole string, otherwise only this many bytes. |
| 399 | * Returns FAIL when out of memory and when key already exists. |
| 400 | */ |
| 401 | int |
| 402 | dict_add_string_len(dict_T *d, char *key, char_u *str, int len) |
| 403 | { |
Bram Moolenaar | e0be167 | 2018-07-08 16:50:37 +0200 | [diff] [blame] | 404 | dictitem_T *item; |
Bram Moolenaar | e6fdf79 | 2018-12-26 22:57:42 +0100 | [diff] [blame] | 405 | char_u *val = NULL; |
Bram Moolenaar | e0be167 | 2018-07-08 16:50:37 +0200 | [diff] [blame] | 406 | |
| 407 | item = dictitem_alloc((char_u *)key); |
| 408 | if (item == NULL) |
| 409 | return FAIL; |
Bram Moolenaar | e0be167 | 2018-07-08 16:50:37 +0200 | [diff] [blame] | 410 | item->di_tv.v_type = VAR_STRING; |
Bram Moolenaar | e6fdf79 | 2018-12-26 22:57:42 +0100 | [diff] [blame] | 411 | if (str != NULL) |
| 412 | { |
| 413 | if (len == -1) |
| 414 | val = vim_strsave(str); |
| 415 | else |
| 416 | val = vim_strnsave(str, len); |
| 417 | } |
| 418 | item->di_tv.vval.v_string = val; |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 419 | if (dict_add(d, item) == FAIL) |
| 420 | { |
| 421 | dictitem_free(item); |
| 422 | return FAIL; |
| 423 | } |
| 424 | return OK; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * Add a list entry to dictionary "d". |
| 429 | * Returns FAIL when out of memory and when key already exists. |
| 430 | */ |
| 431 | int |
| 432 | dict_add_list(dict_T *d, char *key, list_T *list) |
| 433 | { |
| 434 | dictitem_T *item; |
| 435 | |
| 436 | item = dictitem_alloc((char_u *)key); |
| 437 | if (item == NULL) |
| 438 | return FAIL; |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 439 | item->di_tv.v_type = VAR_LIST; |
| 440 | item->di_tv.vval.v_list = list; |
Bram Moolenaar | 42f45b8 | 2017-03-14 22:17:14 +0100 | [diff] [blame] | 441 | ++list->lv_refcount; |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 442 | if (dict_add(d, item) == FAIL) |
| 443 | { |
| 444 | dictitem_free(item); |
| 445 | return FAIL; |
| 446 | } |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 447 | return OK; |
| 448 | } |
| 449 | |
| 450 | /* |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 451 | * Add a typval_T entry to dictionary "d". |
| 452 | * Returns FAIL when out of memory and when key already exists. |
| 453 | */ |
| 454 | int |
| 455 | dict_add_tv(dict_T *d, char *key, typval_T *tv) |
| 456 | { |
| 457 | dictitem_T *item; |
| 458 | |
| 459 | item = dictitem_alloc((char_u *)key); |
| 460 | if (item == NULL) |
| 461 | return FAIL; |
| 462 | copy_tv(tv, &item->di_tv); |
| 463 | if (dict_add(d, item) == FAIL) |
| 464 | { |
| 465 | dictitem_free(item); |
| 466 | return FAIL; |
| 467 | } |
| 468 | return OK; |
| 469 | } |
| 470 | |
| 471 | /* |
Bram Moolenaar | ae94315 | 2019-06-16 22:54:14 +0200 | [diff] [blame] | 472 | * Add a callback to dictionary "d". |
| 473 | * Returns FAIL when out of memory and when key already exists. |
| 474 | */ |
| 475 | int |
| 476 | dict_add_callback(dict_T *d, char *key, callback_T *cb) |
| 477 | { |
| 478 | dictitem_T *item; |
| 479 | |
| 480 | item = dictitem_alloc((char_u *)key); |
| 481 | if (item == NULL) |
| 482 | return FAIL; |
| 483 | put_callback(cb, &item->di_tv); |
| 484 | if (dict_add(d, item) == FAIL) |
| 485 | { |
| 486 | dictitem_free(item); |
| 487 | return FAIL; |
| 488 | } |
| 489 | return OK; |
| 490 | } |
| 491 | |
| 492 | /* |
Bram Moolenaar | 45e18cb | 2019-04-28 18:05:35 +0200 | [diff] [blame] | 493 | * Initializes "iter" for iterating over dictionary items with |
| 494 | * dict_iterate_next(). |
| 495 | * If "var" is not a Dict or an empty Dict then there will be nothing to |
| 496 | * iterate over, no error is given. |
| 497 | * NOTE: The dictionary must not change until iterating is finished! |
| 498 | */ |
| 499 | void |
| 500 | dict_iterate_start(typval_T *var, dict_iterator_T *iter) |
| 501 | { |
| 502 | if (var->v_type != VAR_DICT || var->vval.v_dict == NULL) |
| 503 | iter->dit_todo = 0; |
| 504 | else |
| 505 | { |
| 506 | dict_T *d = var->vval.v_dict; |
| 507 | |
| 508 | iter->dit_todo = d->dv_hashtab.ht_used; |
| 509 | iter->dit_hi = d->dv_hashtab.ht_array; |
| 510 | } |
| 511 | } |
| 512 | |
| 513 | /* |
| 514 | * Iterate over the items referred to by "iter". It should be initialized with |
| 515 | * dict_iterate_start(). |
| 516 | * Returns a pointer to the key. |
| 517 | * "*tv_result" is set to point to the value for that key. |
| 518 | * If there are no more items, NULL is returned. |
| 519 | */ |
| 520 | char_u * |
| 521 | dict_iterate_next(dict_iterator_T *iter, typval_T **tv_result) |
| 522 | { |
| 523 | dictitem_T *di; |
| 524 | char_u *result; |
| 525 | |
| 526 | if (iter->dit_todo == 0) |
| 527 | return NULL; |
| 528 | |
| 529 | while (HASHITEM_EMPTY(iter->dit_hi)) |
| 530 | ++iter->dit_hi; |
| 531 | |
| 532 | di = HI2DI(iter->dit_hi); |
| 533 | result = di->di_key; |
| 534 | *tv_result = &di->di_tv; |
| 535 | |
| 536 | --iter->dit_todo; |
| 537 | ++iter->dit_hi; |
| 538 | return result; |
| 539 | } |
| 540 | |
| 541 | /* |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 542 | * Add a dict entry to dictionary "d". |
| 543 | * Returns FAIL when out of memory and when key already exists. |
| 544 | */ |
| 545 | int |
| 546 | dict_add_dict(dict_T *d, char *key, dict_T *dict) |
| 547 | { |
| 548 | dictitem_T *item; |
| 549 | |
| 550 | item = dictitem_alloc((char_u *)key); |
| 551 | if (item == NULL) |
| 552 | return FAIL; |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 553 | item->di_tv.v_type = VAR_DICT; |
| 554 | item->di_tv.vval.v_dict = dict; |
Bram Moolenaar | 42f45b8 | 2017-03-14 22:17:14 +0100 | [diff] [blame] | 555 | ++dict->dv_refcount; |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 556 | if (dict_add(d, item) == FAIL) |
| 557 | { |
| 558 | dictitem_free(item); |
| 559 | return FAIL; |
| 560 | } |
Bram Moolenaar | b5ae48e | 2016-08-12 22:23:25 +0200 | [diff] [blame] | 561 | return OK; |
| 562 | } |
| 563 | |
| 564 | /* |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 565 | * Get the number of items in a Dictionary. |
| 566 | */ |
| 567 | long |
| 568 | dict_len(dict_T *d) |
| 569 | { |
| 570 | if (d == NULL) |
| 571 | return 0L; |
| 572 | return (long)d->dv_hashtab.ht_used; |
| 573 | } |
| 574 | |
| 575 | /* |
| 576 | * Find item "key[len]" in Dictionary "d". |
| 577 | * If "len" is negative use strlen(key). |
| 578 | * Returns NULL when not found. |
| 579 | */ |
| 580 | dictitem_T * |
| 581 | dict_find(dict_T *d, char_u *key, int len) |
| 582 | { |
| 583 | #define AKEYLEN 200 |
| 584 | char_u buf[AKEYLEN]; |
| 585 | char_u *akey; |
| 586 | char_u *tofree = NULL; |
| 587 | hashitem_T *hi; |
| 588 | |
| 589 | if (d == NULL) |
| 590 | return NULL; |
| 591 | if (len < 0) |
| 592 | akey = key; |
| 593 | else if (len >= AKEYLEN) |
| 594 | { |
| 595 | tofree = akey = vim_strnsave(key, len); |
| 596 | if (akey == NULL) |
| 597 | return NULL; |
| 598 | } |
| 599 | else |
| 600 | { |
Bram Moolenaar | 5d18efe | 2019-12-01 21:11:22 +0100 | [diff] [blame] | 601 | // Avoid a malloc/free by using buf[]. |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 602 | vim_strncpy(buf, key, len); |
| 603 | akey = buf; |
| 604 | } |
| 605 | |
| 606 | hi = hash_find(&d->dv_hashtab, akey); |
| 607 | vim_free(tofree); |
| 608 | if (HASHITEM_EMPTY(hi)) |
| 609 | return NULL; |
| 610 | return HI2DI(hi); |
| 611 | } |
| 612 | |
| 613 | /* |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 614 | * Get a typval_T item from a dictionary and copy it into "rettv". |
| 615 | * Returns FAIL if the entry doesn't exist or out of memory. |
| 616 | */ |
| 617 | int |
| 618 | dict_get_tv(dict_T *d, char_u *key, typval_T *rettv) |
| 619 | { |
| 620 | dictitem_T *di; |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 621 | |
| 622 | di = dict_find(d, key, -1); |
| 623 | if (di == NULL) |
| 624 | return FAIL; |
| 625 | copy_tv(&di->di_tv, rettv); |
| 626 | return OK; |
| 627 | } |
| 628 | |
| 629 | /* |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 630 | * Get a string item from a dictionary. |
| 631 | * When "save" is TRUE allocate memory for it. |
Bram Moolenaar | 7dc5e2e | 2016-08-05 22:22:06 +0200 | [diff] [blame] | 632 | * When FALSE a shared buffer is used, can only be used once! |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 633 | * Returns NULL if the entry doesn't exist or out of memory. |
| 634 | */ |
| 635 | char_u * |
Bram Moolenaar | 8f66717 | 2018-12-14 15:38:31 +0100 | [diff] [blame] | 636 | dict_get_string(dict_T *d, char_u *key, int save) |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 637 | { |
| 638 | dictitem_T *di; |
| 639 | char_u *s; |
| 640 | |
| 641 | di = dict_find(d, key, -1); |
| 642 | if (di == NULL) |
| 643 | return NULL; |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 644 | s = tv_get_string(&di->di_tv); |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 645 | if (save && s != NULL) |
| 646 | s = vim_strsave(s); |
| 647 | return s; |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | * Get a number item from a dictionary. |
| 652 | * Returns 0 if the entry doesn't exist. |
| 653 | */ |
| 654 | varnumber_T |
Bram Moolenaar | 8f66717 | 2018-12-14 15:38:31 +0100 | [diff] [blame] | 655 | dict_get_number(dict_T *d, char_u *key) |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 656 | { |
Bram Moolenaar | 8c6173c | 2019-08-30 22:08:34 +0200 | [diff] [blame] | 657 | return dict_get_number_def(d, key, 0); |
| 658 | } |
| 659 | |
| 660 | /* |
| 661 | * Get a number item from a dictionary. |
| 662 | * Returns "def" if the entry doesn't exist. |
| 663 | */ |
| 664 | varnumber_T |
| 665 | dict_get_number_def(dict_T *d, char_u *key, int def) |
| 666 | { |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 667 | dictitem_T *di; |
| 668 | |
| 669 | di = dict_find(d, key, -1); |
| 670 | if (di == NULL) |
Bram Moolenaar | 8c6173c | 2019-08-30 22:08:34 +0200 | [diff] [blame] | 671 | return def; |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 672 | return tv_get_number(&di->di_tv); |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | /* |
Bram Moolenaar | b0ebbda | 2019-06-02 16:51:21 +0200 | [diff] [blame] | 676 | * Get a number item from a dictionary. |
| 677 | * Returns 0 if the entry doesn't exist. |
| 678 | * Give an error if the entry is not a number. |
| 679 | */ |
| 680 | varnumber_T |
| 681 | dict_get_number_check(dict_T *d, char_u *key) |
| 682 | { |
| 683 | dictitem_T *di; |
| 684 | |
| 685 | di = dict_find(d, key, -1); |
| 686 | if (di == NULL) |
| 687 | return 0; |
| 688 | if (di->di_tv.v_type != VAR_NUMBER) |
| 689 | { |
| 690 | semsg(_(e_invarg2), tv_get_string(&di->di_tv)); |
| 691 | return 0; |
| 692 | } |
| 693 | return tv_get_number(&di->di_tv); |
| 694 | } |
| 695 | |
| 696 | /* |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 697 | * Return an allocated string with the string representation of a Dictionary. |
| 698 | * May return NULL. |
| 699 | */ |
| 700 | char_u * |
| 701 | dict2string(typval_T *tv, int copyID, int restore_copyID) |
| 702 | { |
| 703 | garray_T ga; |
| 704 | int first = TRUE; |
| 705 | char_u *tofree; |
| 706 | char_u numbuf[NUMBUFLEN]; |
| 707 | hashitem_T *hi; |
| 708 | char_u *s; |
| 709 | dict_T *d; |
| 710 | int todo; |
| 711 | |
| 712 | if ((d = tv->vval.v_dict) == NULL) |
| 713 | return NULL; |
| 714 | ga_init2(&ga, (int)sizeof(char), 80); |
| 715 | ga_append(&ga, '{'); |
| 716 | |
| 717 | todo = (int)d->dv_hashtab.ht_used; |
| 718 | for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi) |
| 719 | { |
| 720 | if (!HASHITEM_EMPTY(hi)) |
| 721 | { |
| 722 | --todo; |
| 723 | |
| 724 | if (first) |
| 725 | first = FALSE; |
| 726 | else |
| 727 | ga_concat(&ga, (char_u *)", "); |
| 728 | |
| 729 | tofree = string_quote(hi->hi_key, FALSE); |
| 730 | if (tofree != NULL) |
| 731 | { |
| 732 | ga_concat(&ga, tofree); |
| 733 | vim_free(tofree); |
| 734 | } |
| 735 | ga_concat(&ga, (char_u *)": "); |
| 736 | s = echo_string_core(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID, |
| 737 | FALSE, restore_copyID, TRUE); |
| 738 | if (s != NULL) |
| 739 | ga_concat(&ga, s); |
| 740 | vim_free(tofree); |
| 741 | if (s == NULL || did_echo_string_emsg) |
| 742 | break; |
| 743 | line_breakcheck(); |
| 744 | |
| 745 | } |
| 746 | } |
| 747 | if (todo > 0) |
| 748 | { |
| 749 | vim_free(ga.ga_data); |
| 750 | return NULL; |
| 751 | } |
| 752 | |
| 753 | ga_append(&ga, '}'); |
| 754 | ga_append(&ga, NUL); |
| 755 | return (char_u *)ga.ga_data; |
| 756 | } |
| 757 | |
| 758 | /* |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 759 | * Get the key for #{key: val} into "tv" and advance "arg". |
Bram Moolenaar | d5abb4c | 2019-07-13 22:46:10 +0200 | [diff] [blame] | 760 | * Return FAIL when there is no valid key. |
| 761 | */ |
| 762 | static int |
| 763 | get_literal_key(char_u **arg, typval_T *tv) |
| 764 | { |
| 765 | char_u *p; |
| 766 | |
| 767 | if (!ASCII_ISALNUM(**arg) && **arg != '_' && **arg != '-') |
| 768 | return FAIL; |
| 769 | |
| 770 | for (p = *arg; ASCII_ISALNUM(*p) || *p == '_' || *p == '-'; ++p) |
| 771 | ; |
| 772 | tv->v_type = VAR_STRING; |
| 773 | tv->vval.v_string = vim_strnsave(*arg, (int)(p - *arg)); |
| 774 | |
| 775 | *arg = skipwhite(p); |
| 776 | return OK; |
| 777 | } |
| 778 | |
| 779 | /* |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 780 | * Allocate a variable for a Dictionary and fill it from "*arg". |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 781 | * "literal" is TRUE for #{key: val} |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 782 | * Return OK or FAIL. Returns NOTDONE for {expr}. |
| 783 | */ |
| 784 | int |
Bram Moolenaar | 0892832 | 2020-01-04 14:32:48 +0100 | [diff] [blame] | 785 | eval_dict(char_u **arg, typval_T *rettv, int evaluate, int literal) |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 786 | { |
| 787 | dict_T *d = NULL; |
| 788 | typval_T tvkey; |
| 789 | typval_T tv; |
| 790 | char_u *key = NULL; |
| 791 | dictitem_T *item; |
| 792 | char_u *start = skipwhite(*arg + 1); |
| 793 | char_u buf[NUMBUFLEN]; |
| 794 | |
| 795 | /* |
| 796 | * First check if it's not a curly-braces thing: {expr}. |
| 797 | * Must do this without evaluating, otherwise a function may be called |
| 798 | * twice. Unfortunately this means we need to call eval1() twice for the |
| 799 | * first item. |
| 800 | * But {} is an empty Dictionary. |
| 801 | */ |
| 802 | if (*start != '}') |
| 803 | { |
Bram Moolenaar | 5d18efe | 2019-12-01 21:11:22 +0100 | [diff] [blame] | 804 | if (eval1(&start, &tv, FALSE) == FAIL) // recursive! |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 805 | return FAIL; |
| 806 | if (*start == '}') |
| 807 | return NOTDONE; |
| 808 | } |
| 809 | |
| 810 | if (evaluate) |
| 811 | { |
| 812 | d = dict_alloc(); |
| 813 | if (d == NULL) |
| 814 | return FAIL; |
| 815 | } |
| 816 | tvkey.v_type = VAR_UNKNOWN; |
| 817 | tv.v_type = VAR_UNKNOWN; |
| 818 | |
| 819 | *arg = skipwhite(*arg + 1); |
| 820 | while (**arg != '}' && **arg != NUL) |
| 821 | { |
Bram Moolenaar | d5abb4c | 2019-07-13 22:46:10 +0200 | [diff] [blame] | 822 | if ((literal |
| 823 | ? get_literal_key(arg, &tvkey) |
| 824 | : eval1(arg, &tvkey, evaluate)) == FAIL) // recursive! |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 825 | goto failret; |
Bram Moolenaar | d5abb4c | 2019-07-13 22:46:10 +0200 | [diff] [blame] | 826 | |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 827 | if (**arg != ':') |
| 828 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 829 | if (evaluate) |
| 830 | semsg(_(e_missing_dict_colon), *arg); |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 831 | clear_tv(&tvkey); |
| 832 | goto failret; |
| 833 | } |
| 834 | if (evaluate) |
| 835 | { |
Bram Moolenaar | d155d7a | 2018-12-21 16:04:21 +0100 | [diff] [blame] | 836 | key = tv_get_string_buf_chk(&tvkey, buf); |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 837 | if (key == NULL) |
| 838 | { |
Bram Moolenaar | 5d18efe | 2019-12-01 21:11:22 +0100 | [diff] [blame] | 839 | // "key" is NULL when tv_get_string_buf_chk() gave an errmsg |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 840 | clear_tv(&tvkey); |
| 841 | goto failret; |
| 842 | } |
| 843 | } |
| 844 | |
| 845 | *arg = skipwhite(*arg + 1); |
Bram Moolenaar | 5d18efe | 2019-12-01 21:11:22 +0100 | [diff] [blame] | 846 | if (eval1(arg, &tv, evaluate) == FAIL) // recursive! |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 847 | { |
| 848 | if (evaluate) |
| 849 | clear_tv(&tvkey); |
| 850 | goto failret; |
| 851 | } |
| 852 | if (evaluate) |
| 853 | { |
| 854 | item = dict_find(d, key, -1); |
| 855 | if (item != NULL) |
| 856 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 857 | if (evaluate) |
| 858 | semsg(_(e_duplicate_key), key); |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 859 | clear_tv(&tvkey); |
| 860 | clear_tv(&tv); |
| 861 | goto failret; |
| 862 | } |
| 863 | item = dictitem_alloc(key); |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 864 | if (item != NULL) |
| 865 | { |
| 866 | item->di_tv = tv; |
| 867 | item->di_tv.v_lock = 0; |
| 868 | if (dict_add(d, item) == FAIL) |
| 869 | dictitem_free(item); |
| 870 | } |
| 871 | } |
Bram Moolenaar | a893194 | 2019-09-28 17:25:10 +0200 | [diff] [blame] | 872 | clear_tv(&tvkey); |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 873 | |
| 874 | if (**arg == '}') |
| 875 | break; |
| 876 | if (**arg != ',') |
| 877 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 878 | if (evaluate) |
| 879 | semsg(_(e_missing_dict_comma), *arg); |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 880 | goto failret; |
| 881 | } |
| 882 | *arg = skipwhite(*arg + 1); |
| 883 | } |
| 884 | |
| 885 | if (**arg != '}') |
| 886 | { |
Bram Moolenaar | 33fa29c | 2020-03-28 19:41:33 +0100 | [diff] [blame] | 887 | if (evaluate) |
| 888 | semsg(_(e_missing_dict_end), *arg); |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 889 | failret: |
Bram Moolenaar | adc6714 | 2019-06-22 01:40:42 +0200 | [diff] [blame] | 890 | if (d != NULL) |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 891 | dict_free(d); |
| 892 | return FAIL; |
| 893 | } |
| 894 | |
| 895 | *arg = skipwhite(*arg + 1); |
| 896 | if (evaluate) |
Bram Moolenaar | 45cf6e9 | 2017-04-30 20:25:19 +0200 | [diff] [blame] | 897 | rettv_dict_set(rettv, d); |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 898 | |
| 899 | return OK; |
| 900 | } |
| 901 | |
| 902 | /* |
| 903 | * Go over all entries in "d2" and add them to "d1". |
| 904 | * When "action" is "error" then a duplicate key is an error. |
| 905 | * When "action" is "force" then a duplicate key is overwritten. |
| 906 | * Otherwise duplicate keys are ignored ("action" is "keep"). |
| 907 | */ |
| 908 | void |
| 909 | dict_extend(dict_T *d1, dict_T *d2, char_u *action) |
| 910 | { |
| 911 | dictitem_T *di1; |
| 912 | hashitem_T *hi2; |
| 913 | int todo; |
| 914 | char_u *arg_errmsg = (char_u *)N_("extend() argument"); |
| 915 | |
| 916 | todo = (int)d2->dv_hashtab.ht_used; |
| 917 | for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2) |
| 918 | { |
| 919 | if (!HASHITEM_EMPTY(hi2)) |
| 920 | { |
| 921 | --todo; |
| 922 | di1 = dict_find(d1, hi2->hi_key, -1); |
| 923 | if (d1->dv_scope != 0) |
| 924 | { |
Bram Moolenaar | 5d18efe | 2019-12-01 21:11:22 +0100 | [diff] [blame] | 925 | // Disallow replacing a builtin function in l: and g:. |
| 926 | // Check the key to be valid when adding to any scope. |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 927 | if (d1->dv_scope == VAR_DEF_SCOPE |
| 928 | && HI2DI(hi2)->di_tv.v_type == VAR_FUNC |
| 929 | && var_check_func_name(hi2->hi_key, di1 == NULL)) |
| 930 | break; |
| 931 | if (!valid_varname(hi2->hi_key)) |
| 932 | break; |
| 933 | } |
| 934 | if (di1 == NULL) |
| 935 | { |
| 936 | di1 = dictitem_copy(HI2DI(hi2)); |
| 937 | if (di1 != NULL && dict_add(d1, di1) == FAIL) |
| 938 | dictitem_free(di1); |
| 939 | } |
| 940 | else if (*action == 'e') |
| 941 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 942 | semsg(_("E737: Key already exists: %s"), hi2->hi_key); |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 943 | break; |
| 944 | } |
| 945 | else if (*action == 'f' && HI2DI(hi2) != di1) |
| 946 | { |
Bram Moolenaar | 05c00c0 | 2019-02-11 22:00:11 +0100 | [diff] [blame] | 947 | if (var_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE) |
| 948 | || var_check_ro(di1->di_flags, arg_errmsg, TRUE)) |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 949 | break; |
| 950 | clear_tv(&di1->di_tv); |
| 951 | copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv); |
| 952 | } |
| 953 | } |
| 954 | } |
| 955 | } |
| 956 | |
| 957 | /* |
| 958 | * Return the dictitem that an entry in a hashtable points to. |
| 959 | */ |
| 960 | dictitem_T * |
| 961 | dict_lookup(hashitem_T *hi) |
| 962 | { |
| 963 | return HI2DI(hi); |
| 964 | } |
| 965 | |
| 966 | /* |
| 967 | * Return TRUE when two dictionaries have exactly the same key/values. |
| 968 | */ |
| 969 | int |
| 970 | dict_equal( |
| 971 | dict_T *d1, |
| 972 | dict_T *d2, |
Bram Moolenaar | 5d18efe | 2019-12-01 21:11:22 +0100 | [diff] [blame] | 973 | int ic, // ignore case for strings |
| 974 | int recursive) // TRUE when used recursively |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 975 | { |
| 976 | hashitem_T *hi; |
| 977 | dictitem_T *item2; |
| 978 | int todo; |
| 979 | |
| 980 | if (d1 == NULL && d2 == NULL) |
| 981 | return TRUE; |
| 982 | if (d1 == NULL || d2 == NULL) |
| 983 | return FALSE; |
| 984 | if (d1 == d2) |
| 985 | return TRUE; |
| 986 | if (dict_len(d1) != dict_len(d2)) |
| 987 | return FALSE; |
| 988 | |
| 989 | todo = (int)d1->dv_hashtab.ht_used; |
| 990 | for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi) |
| 991 | { |
| 992 | if (!HASHITEM_EMPTY(hi)) |
| 993 | { |
| 994 | item2 = dict_find(d2, hi->hi_key, -1); |
| 995 | if (item2 == NULL) |
| 996 | return FALSE; |
| 997 | if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive)) |
| 998 | return FALSE; |
| 999 | --todo; |
| 1000 | } |
| 1001 | } |
| 1002 | return TRUE; |
| 1003 | } |
| 1004 | |
| 1005 | /* |
| 1006 | * Turn a dict into a list: |
| 1007 | * "what" == 0: list of keys |
| 1008 | * "what" == 1: list of values |
| 1009 | * "what" == 2: list of items |
| 1010 | */ |
Bram Moolenaar | 5843f5f | 2019-08-20 20:13:45 +0200 | [diff] [blame] | 1011 | static void |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 1012 | dict_list(typval_T *argvars, typval_T *rettv, int what) |
| 1013 | { |
| 1014 | list_T *l2; |
| 1015 | dictitem_T *di; |
| 1016 | hashitem_T *hi; |
| 1017 | listitem_T *li; |
| 1018 | listitem_T *li2; |
| 1019 | dict_T *d; |
| 1020 | int todo; |
| 1021 | |
| 1022 | if (argvars[0].v_type != VAR_DICT) |
| 1023 | { |
Bram Moolenaar | f9e3e09 | 2019-01-13 23:38:42 +0100 | [diff] [blame] | 1024 | emsg(_(e_dictreq)); |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 1025 | return; |
| 1026 | } |
| 1027 | if ((d = argvars[0].vval.v_dict) == NULL) |
| 1028 | return; |
| 1029 | |
| 1030 | if (rettv_list_alloc(rettv) == FAIL) |
| 1031 | return; |
| 1032 | |
| 1033 | todo = (int)d->dv_hashtab.ht_used; |
| 1034 | for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi) |
| 1035 | { |
| 1036 | if (!HASHITEM_EMPTY(hi)) |
| 1037 | { |
| 1038 | --todo; |
| 1039 | di = HI2DI(hi); |
| 1040 | |
| 1041 | li = listitem_alloc(); |
| 1042 | if (li == NULL) |
| 1043 | break; |
| 1044 | list_append(rettv->vval.v_list, li); |
| 1045 | |
| 1046 | if (what == 0) |
| 1047 | { |
Bram Moolenaar | 5d18efe | 2019-12-01 21:11:22 +0100 | [diff] [blame] | 1048 | // keys() |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 1049 | li->li_tv.v_type = VAR_STRING; |
| 1050 | li->li_tv.v_lock = 0; |
| 1051 | li->li_tv.vval.v_string = vim_strsave(di->di_key); |
| 1052 | } |
| 1053 | else if (what == 1) |
| 1054 | { |
Bram Moolenaar | 5d18efe | 2019-12-01 21:11:22 +0100 | [diff] [blame] | 1055 | // values() |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 1056 | copy_tv(&di->di_tv, &li->li_tv); |
| 1057 | } |
| 1058 | else |
| 1059 | { |
Bram Moolenaar | 5d18efe | 2019-12-01 21:11:22 +0100 | [diff] [blame] | 1060 | // items() |
Bram Moolenaar | cd52459 | 2016-07-17 14:57:05 +0200 | [diff] [blame] | 1061 | l2 = list_alloc(); |
| 1062 | li->li_tv.v_type = VAR_LIST; |
| 1063 | li->li_tv.v_lock = 0; |
| 1064 | li->li_tv.vval.v_list = l2; |
| 1065 | if (l2 == NULL) |
| 1066 | break; |
| 1067 | ++l2->lv_refcount; |
| 1068 | |
| 1069 | li2 = listitem_alloc(); |
| 1070 | if (li2 == NULL) |
| 1071 | break; |
| 1072 | list_append(l2, li2); |
| 1073 | li2->li_tv.v_type = VAR_STRING; |
| 1074 | li2->li_tv.v_lock = 0; |
| 1075 | li2->li_tv.vval.v_string = vim_strsave(di->di_key); |
| 1076 | |
| 1077 | li2 = listitem_alloc(); |
| 1078 | if (li2 == NULL) |
| 1079 | break; |
| 1080 | list_append(l2, li2); |
| 1081 | copy_tv(&di->di_tv, &li2->li_tv); |
| 1082 | } |
| 1083 | } |
| 1084 | } |
| 1085 | } |
| 1086 | |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1087 | /* |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1088 | * "items(dict)" function |
| 1089 | */ |
| 1090 | void |
| 1091 | f_items(typval_T *argvars, typval_T *rettv) |
| 1092 | { |
| 1093 | dict_list(argvars, rettv, 2); |
| 1094 | } |
| 1095 | |
| 1096 | /* |
| 1097 | * "keys()" function |
| 1098 | */ |
| 1099 | void |
| 1100 | f_keys(typval_T *argvars, typval_T *rettv) |
| 1101 | { |
| 1102 | dict_list(argvars, rettv, 0); |
| 1103 | } |
| 1104 | |
| 1105 | /* |
| 1106 | * "values(dict)" function |
| 1107 | */ |
| 1108 | void |
| 1109 | f_values(typval_T *argvars, typval_T *rettv) |
| 1110 | { |
| 1111 | dict_list(argvars, rettv, 1); |
| 1112 | } |
| 1113 | |
| 1114 | /* |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1115 | * Make each item in the dict readonly (not the value of the item). |
| 1116 | */ |
| 1117 | void |
| 1118 | dict_set_items_ro(dict_T *di) |
| 1119 | { |
| 1120 | int todo = (int)di->dv_hashtab.ht_used; |
| 1121 | hashitem_T *hi; |
| 1122 | |
Bram Moolenaar | 5d18efe | 2019-12-01 21:11:22 +0100 | [diff] [blame] | 1123 | // Set readonly |
Bram Moolenaar | 7e1652c | 2017-12-16 18:27:02 +0100 | [diff] [blame] | 1124 | for (hi = di->dv_hashtab.ht_array; todo > 0 ; ++hi) |
| 1125 | { |
| 1126 | if (HASHITEM_EMPTY(hi)) |
| 1127 | continue; |
| 1128 | --todo; |
| 1129 | HI2DI(hi)->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX; |
| 1130 | } |
| 1131 | } |
| 1132 | |
Bram Moolenaar | 9f9fe37 | 2019-07-27 23:12:12 +0200 | [diff] [blame] | 1133 | /* |
| 1134 | * "has_key()" function |
| 1135 | */ |
| 1136 | void |
| 1137 | f_has_key(typval_T *argvars, typval_T *rettv) |
| 1138 | { |
| 1139 | if (argvars[0].v_type != VAR_DICT) |
| 1140 | { |
| 1141 | emsg(_(e_dictreq)); |
| 1142 | return; |
| 1143 | } |
| 1144 | if (argvars[0].vval.v_dict == NULL) |
| 1145 | return; |
| 1146 | |
| 1147 | rettv->vval.v_number = dict_find(argvars[0].vval.v_dict, |
| 1148 | tv_get_string(&argvars[1]), -1) != NULL; |
| 1149 | } |
| 1150 | |
| 1151 | /* |
| 1152 | * "remove({dict})" function |
| 1153 | */ |
| 1154 | void |
| 1155 | dict_remove(typval_T *argvars, typval_T *rettv, char_u *arg_errmsg) |
| 1156 | { |
| 1157 | dict_T *d; |
| 1158 | char_u *key; |
| 1159 | dictitem_T *di; |
| 1160 | |
| 1161 | if (argvars[2].v_type != VAR_UNKNOWN) |
| 1162 | semsg(_(e_toomanyarg), "remove()"); |
| 1163 | else if ((d = argvars[0].vval.v_dict) != NULL |
| 1164 | && !var_check_lock(d->dv_lock, arg_errmsg, TRUE)) |
| 1165 | { |
| 1166 | key = tv_get_string_chk(&argvars[1]); |
| 1167 | if (key != NULL) |
| 1168 | { |
| 1169 | di = dict_find(d, key, -1); |
| 1170 | if (di == NULL) |
| 1171 | semsg(_(e_dictkey), key); |
| 1172 | else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE) |
| 1173 | && !var_check_ro(di->di_flags, arg_errmsg, TRUE)) |
| 1174 | { |
| 1175 | *rettv = di->di_tv; |
| 1176 | init_tv(&di->di_tv); |
| 1177 | dictitem_remove(d, di); |
| 1178 | } |
| 1179 | } |
| 1180 | } |
| 1181 | } |
| 1182 | |
Bram Moolenaar | 5d18efe | 2019-12-01 21:11:22 +0100 | [diff] [blame] | 1183 | #endif // defined(FEAT_EVAL) |