diff src/dict.c @ 17368:6604ecb7a615 v8.1.1683

patch 8.1.1683: dictionary with string keys is longer than needed commit https://github.com/vim/vim/commit/d5abb4c87727eecb71b0e8ffdda60fc9598272f3 Author: Bram Moolenaar <Bram@vim.org> Date: Sat Jul 13 22:46:10 2019 +0200 patch 8.1.1683: dictionary with string keys is longer than needed Problem: Dictionary with string keys is longer than needed. Solution: Use *{key: val} for literaly keys.
author Bram Moolenaar <Bram@vim.org>
date Sat, 13 Jul 2019 23:00:05 +0200
parents 3fd0765f454f
children ef23ec1eee54
line wrap: on
line diff
--- a/src/dict.c
+++ b/src/dict.c
@@ -709,11 +709,33 @@ dict2string(typval_T *tv, int copyID, in
 }
 
 /*
+ * Get the key for *{key: val} into "tv" and advance "arg".
+ * Return FAIL when there is no valid key.
+ */
+    static int
+get_literal_key(char_u **arg, typval_T *tv)
+{
+    char_u *p;
+
+    if (!ASCII_ISALNUM(**arg) && **arg != '_' && **arg != '-')
+	return FAIL;
+
+    for (p = *arg; ASCII_ISALNUM(*p) || *p == '_' || *p == '-'; ++p)
+	;
+    tv->v_type = VAR_STRING;
+    tv->vval.v_string = vim_strnsave(*arg, (int)(p - *arg));
+
+    *arg = skipwhite(p);
+    return OK;
+}
+
+/*
  * Allocate a variable for a Dictionary and fill it from "*arg".
+ * "literal" is TRUE for *{key: val}
  * Return OK or FAIL.  Returns NOTDONE for {expr}.
  */
     int
-dict_get_tv(char_u **arg, typval_T *rettv, int evaluate)
+dict_get_tv(char_u **arg, typval_T *rettv, int evaluate, int literal)
 {
     dict_T	*d = NULL;
     typval_T	tvkey;
@@ -750,8 +772,11 @@ dict_get_tv(char_u **arg, typval_T *rett
     *arg = skipwhite(*arg + 1);
     while (**arg != '}' && **arg != NUL)
     {
-	if (eval1(arg, &tvkey, evaluate) == FAIL)	/* recursive! */
+	if ((literal
+		? get_literal_key(arg, &tvkey)
+		: eval1(arg, &tvkey, evaluate)) == FAIL)	// recursive!
 	    goto failret;
+
 	if (**arg != ':')
 	{
 	    semsg(_("E720: Missing colon in Dictionary: %s"), *arg);