Mercurial > vim
comparison src/dict.c @ 14301:3c80092eb211 v8.1.0166
patch 8.1.0166: using dict_add_nr_str() is clumsy
commit https://github.com/vim/vim/commit/e0be167a805fd547c25ec1ec97fd4c7f13046236
Author: Bram Moolenaar <Bram@vim.org>
Date: Sun Jul 8 16:50:37 2018 +0200
patch 8.1.0166: using dict_add_nr_str() is clumsy
Problem: Using dict_add_nr_str() is clumsy.
Solution: Split into two functions. (Ozaki Kiichi, closes https://github.com/vim/vim/issues/3154)
author | Christian Brabandt <cb@256bit.org> |
---|---|
date | Sun, 08 Jul 2018 17:00:07 +0200 |
parents | 6e81a68d63a1 |
children | f761a55a8aed |
comparison
equal
deleted
inserted
replaced
14300:7a0639d9fdc8 | 14301:3c80092eb211 |
---|---|
325 { | 325 { |
326 return hash_add(&d->dv_hashtab, item->di_key); | 326 return hash_add(&d->dv_hashtab, item->di_key); |
327 } | 327 } |
328 | 328 |
329 /* | 329 /* |
330 * Add a number or string entry to dictionary "d". | 330 * Add a number entry to dictionary "d". |
331 * When "str" is NULL use number "nr", otherwise use "str". | |
332 * Returns FAIL when out of memory and when key already exists. | 331 * Returns FAIL when out of memory and when key already exists. |
333 */ | 332 */ |
334 int | 333 int |
335 dict_add_nr_str( | 334 dict_add_number(dict_T *d, char *key, varnumber_T nr) |
336 dict_T *d, | |
337 char *key, | |
338 varnumber_T nr, | |
339 char_u *str) | |
340 { | 335 { |
341 dictitem_T *item; | 336 dictitem_T *item; |
342 | 337 |
343 item = dictitem_alloc((char_u *)key); | 338 item = dictitem_alloc((char_u *)key); |
344 if (item == NULL) | 339 if (item == NULL) |
345 return FAIL; | 340 return FAIL; |
346 item->di_tv.v_lock = 0; | 341 item->di_tv.v_lock = 0; |
347 if (str == NULL) | 342 item->di_tv.v_type = VAR_NUMBER; |
348 { | 343 item->di_tv.vval.v_number = nr; |
349 item->di_tv.v_type = VAR_NUMBER; | 344 if (dict_add(d, item) == FAIL) |
350 item->di_tv.vval.v_number = nr; | 345 { |
351 } | 346 dictitem_free(item); |
352 else | 347 return FAIL; |
353 { | 348 } |
354 item->di_tv.v_type = VAR_STRING; | 349 return OK; |
355 item->di_tv.vval.v_string = vim_strsave(str); | 350 } |
356 } | 351 |
352 /* | |
353 * Add a string entry to dictionary "d". | |
354 * Returns FAIL when out of memory and when key already exists. | |
355 */ | |
356 int | |
357 dict_add_string(dict_T *d, char *key, char_u *str) | |
358 { | |
359 dictitem_T *item; | |
360 | |
361 item = dictitem_alloc((char_u *)key); | |
362 if (item == NULL) | |
363 return FAIL; | |
364 item->di_tv.v_lock = 0; | |
365 item->di_tv.v_type = VAR_STRING; | |
366 item->di_tv.vval.v_string = str != NULL ? vim_strsave(str) : NULL; | |
357 if (dict_add(d, item) == FAIL) | 367 if (dict_add(d, item) == FAIL) |
358 { | 368 { |
359 dictitem_free(item); | 369 dictitem_free(item); |
360 return FAIL; | 370 return FAIL; |
361 } | 371 } |