diff options
author | ckelsel <ckelsel@hotmail.com> | 2017-07-10 08:10:15 +0800 |
---|---|---|
committer | ckelsel <ckelsel@hotmail.com> | 2017-07-10 08:10:15 +0800 |
commit | 465bbee520d1b1b57477fd7d80fbdeaf5e1e1e77 (patch) | |
tree | edae38568202ba41dee4a49f78884da313fd114b /src/nvim/eval/typval.c | |
parent | 1514cdc7d8863eeee6b04883b1c50aac40048b49 (diff) | |
parent | 6725667d31591e8025589c4c1df34469f3bfdb52 (diff) | |
download | rneovim-465bbee520d1b1b57477fd7d80fbdeaf5e1e1e77.tar.gz rneovim-465bbee520d1b1b57477fd7d80fbdeaf5e1e1e77.tar.bz2 rneovim-465bbee520d1b1b57477fd7d80fbdeaf5e1e1e77.zip |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'src/nvim/eval/typval.c')
-rw-r--r-- | src/nvim/eval/typval.c | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c index 4521085519..c339a5cdd2 100644 --- a/src/nvim/eval/typval.c +++ b/src/nvim/eval/typval.c @@ -1387,11 +1387,32 @@ int tv_dict_add_str(dict_T *const d, const char *const val) FUNC_ATTR_NONNULL_ALL { + return tv_dict_add_allocated_str(d, key, key_len, xstrdup(val)); +} + +/// Add a string entry to dictionary +/// +/// Unlike tv_dict_add_str() saves val to the new dictionary item in place of +/// creating a new copy. +/// +/// @warning String will be freed even in case addition fails. +/// +/// @param[out] d Dictionary to add entry to. +/// @param[in] key Key to add. +/// @param[in] key_len Key length. +/// @param[in] val String to add. +/// +/// @return OK in case of success, FAIL when key already exists. +int tv_dict_add_allocated_str(dict_T *const d, + const char *const key, const size_t key_len, + char *const val) + FUNC_ATTR_NONNULL_ALL +{ dictitem_T *const item = tv_dict_item_alloc_len(key, key_len); item->di_tv.v_lock = VAR_UNLOCKED; item->di_tv.v_type = VAR_STRING; - item->di_tv.vval.v_string = (char_u *)xstrdup(val); + item->di_tv.vval.v_string = (char_u *)val; if (tv_dict_add(d, item) == FAIL) { tv_dict_item_free(item); return FAIL; |