aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/eval
diff options
context:
space:
mode:
authorJustin M. Keyes <justinkz@gmail.com>2017-07-03 23:33:08 +0200
committerGitHub <noreply@github.com>2017-07-03 23:33:08 +0200
commit008b604bacbbeeaf0e04f94b1d331b11ebec631a (patch)
tree45a004c00c61f0bbe84cbf291093b6dffcf0bfb1 /src/nvim/eval
parente333957a1a9ae64b7daa36e08fd1df583114d4ba (diff)
parent35898cff5d1d6dc60e0d7b87bfe106539453b031 (diff)
downloadrneovim-008b604bacbbeeaf0e04f94b1d331b11ebec631a.tar.gz
rneovim-008b604bacbbeeaf0e04f94b1d331b11ebec631a.tar.bz2
rneovim-008b604bacbbeeaf0e04f94b1d331b11ebec631a.zip
Merge #6947 from ZyX-I/consistent-get_keymap
Diffstat (limited to 'src/nvim/eval')
-rw-r--r--src/nvim/eval/typval.c23
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;