]> git.ipfire.org Git - thirdparty/vim.git/commitdiff
patch 9.1.0828: string_T struct could be used more often v9.1.0828
authorJohn Marriott <basilisk@internode.on.net>
Sat, 2 Nov 2024 14:59:01 +0000 (15:59 +0100)
committerChristian Brabandt <cb@256bit.org>
Sat, 2 Nov 2024 15:11:58 +0000 (16:11 +0100)
Problem:  string_T struct could be used more often
Solution: Refactor code and make use of string_T struct
          for key-value pairs, reformat overlong lines
          (John Marriott)

closes: #15975

Signed-off-by: John Marriott <basilisk@internode.on.net>
Signed-off-by: Christian Brabandt <cb@256bit.org>
src/autocmd.c
src/ex_docmd.c
src/highlight.c
src/misc2.c
src/regexp.c
src/structs.h
src/syntax.c
src/usercmd.c
src/version.c

index 00f41bddd965f39d13e32b30eb6eb1861dc8ac3d..330db42303625d7494551974bbe5a054cc790698 100644 (file)
@@ -707,8 +707,8 @@ event_name2nr(char_u *start, char_u **end)
        ;
 
     target.key = 0;
-    target.value = (char *)start;
-    target.length = (size_t)(p - start);
+    target.value.string = start;
+    target.value.length = (size_t)(p - start);
 
     // special cases:
     // BufNewFile and BufRead are searched for ALOT (especially at startup)
@@ -752,7 +752,7 @@ event_nr2name(event_T event)
     for (i = cache_last_index; cache_tab[i] >= 0; )
     {
        if ((event_T)event_tab[cache_tab[i]].key == event)
-           return (char_u *)event_tab[cache_tab[i]].value;
+           return event_tab[cache_tab[i]].value.string;
 
        if (i == 0)
            i = ARRAY_LENGTH(cache_tab) - 1;
@@ -780,7 +780,8 @@ event_nr2name(event_T event)
        }
     }
 
-    return (i == (int)ARRAY_LENGTH(event_tab)) ? (char_u *)"Unknown" : (char_u *)event_tab[i].value;
+    return (i == (int)ARRAY_LENGTH(event_tab)) ? (char_u *)"Unknown" :
+       event_tab[i].value.string;
 }
 
 /*
@@ -2880,7 +2881,7 @@ get_event_name(expand_T *xp UNUSED, int idx)
     if (i < 0 || i >= (int)ARRAY_LENGTH(event_tab))
        return NULL;
 
-    return (char_u *)event_tab[i].value;
+    return event_tab[i].value.string;
 }
 
 /*
@@ -2893,7 +2894,7 @@ get_event_name_no_group(expand_T *xp UNUSED, int idx)
     if (idx < 0 || idx >= (int)ARRAY_LENGTH(event_tab))
        return NULL;
 
-    return (char_u *)event_tab[idx].value;
+    return event_tab[idx].value.string;
 }
 
 
@@ -3365,9 +3366,11 @@ f_autocmd_get(typval_T *argvars, typval_T *rettv)
                keyvalue_T *entry;
 
                target.key = 0;
-               target.value = (char *)name;
-               target.length = (int)STRLEN(target.value);
-               entry = (keyvalue_T *)bsearch(&target, &event_tab, ARRAY_LENGTH(event_tab), sizeof(event_tab[0]), cmp_keyvalue_value_ni);
+               target.value.string = name;
+               target.value.length = STRLEN(target.value.string);
+               entry = (keyvalue_T *)bsearch(&target, &event_tab,
+                       ARRAY_LENGTH(event_tab), sizeof(event_tab[0]),
+                       cmp_keyvalue_value_ni);
                if (entry == NULL)
                {
                    semsg(_(e_no_such_event_str), name);
index 807b470d8b2e3f40c82a11845fda0077628120c6..d60e34473c4096914437f70226b7a10d9859ce7a 100644 (file)
@@ -9530,14 +9530,16 @@ find_cmdline_var(char_u *src, size_t *usedlen)
 
     case '<':
        target.key = 0;
-       target.value = (char *)src + 1;     // skip over '<'
-       target.length = 0;                  // not used, see cmp_keyvalue_value_n()
+       target.value.string = src + 1;      // skip over '<'
+       target.value.length = 0;            // not used, see cmp_keyvalue_value_n()
 
-       entry = (keyvalue_T *)bsearch(&target, &spec_str_tab, ARRAY_LENGTH(spec_str_tab), sizeof(spec_str_tab[0]), cmp_keyvalue_value_n);
+       entry = (keyvalue_T *)bsearch(&target, &spec_str_tab,
+               ARRAY_LENGTH(spec_str_tab), sizeof(spec_str_tab[0]),
+               cmp_keyvalue_value_n);
        if (entry == NULL)
            return -1;
 
-       *usedlen = entry->length + 1;
+       *usedlen = entry->value.length + 1;
        return entry->key;
 
     default:
index d3ea2d20168592d07fc2e5a6631541cf59c1a873..1a4c76d9439e3c2bb259ecc9bd303403f4d2f209 100644 (file)
@@ -869,11 +869,13 @@ highlight_set_termgui_attr(int idx, char_u *key, char_u *arg, int init)
     attr = 0;
     off = 0;
     target.key = 0;
-    target.length = 0; // not used, see cmp_keyvalue_value_ni()
+    target.value.length = 0;   // not used, see cmp_keyvalue_value_ni()
     while (arg[off] != NUL)
     {
-       target.value = (char *)arg + off;
-       entry = (keyvalue_T *)bsearch(&target, &highlight_tab, ARRAY_LENGTH(highlight_tab), sizeof(highlight_tab[0]), cmp_keyvalue_value_ni);
+       target.value.string = arg + off;
+       entry = (keyvalue_T *)bsearch(&target, &highlight_tab,
+               ARRAY_LENGTH(highlight_tab), sizeof(highlight_tab[0]),
+               cmp_keyvalue_value_ni);
        if (entry == NULL)
        {
            semsg(_(e_illegal_value_str), arg);
@@ -881,7 +883,7 @@ highlight_set_termgui_attr(int idx, char_u *key, char_u *arg, int init)
        }
 
        attr |= entry->key;
-       off += entry->length;
+       off += entry->value.length;
        if (arg[off] == ',')            // another one follows
            ++off;
     }
@@ -1214,9 +1216,11 @@ highlight_set_cterm_color(
        keyvalue_T *entry;
 
        target.key = 0;
-       target.value = (char *)arg;
-       target.length = 0;      // not used, see cmp_keyvalue_value_i()
-       entry = (keyvalue_T *)bsearch(&target, &color_name_tab, ARRAY_LENGTH(color_name_tab), sizeof(color_name_tab[0]), cmp_keyvalue_value_i);
+       target.value.string = arg;
+       target.value.length = 0;        // not used, see cmp_keyvalue_value_i()
+       entry = (keyvalue_T *)bsearch(&target, &color_name_tab,
+               ARRAY_LENGTH(color_name_tab), sizeof(color_name_tab[0]),
+               cmp_keyvalue_value_i);
        if (entry == NULL)
        {
            semsg(_(e_color_name_or_number_not_recognized_str), key_start);
@@ -2541,9 +2545,10 @@ gui_get_color_cmn(char_u *name)
        return color;
 
     target.key = 0;
-    target.value = (char *)name;
-    target.length = 0;         // not used, see cmp_keyvalue_value_i()
-    entry = (keyvalue_T *)bsearch(&target, &rgb_tab, ARRAY_LENGTH(rgb_tab), sizeof(rgb_tab[0]), cmp_keyvalue_value_i);
+    target.value.string = name;
+    target.value.length = 0;   // not used, see cmp_keyvalue_value_i()
+    entry = (keyvalue_T *)bsearch(&target, &rgb_tab, ARRAY_LENGTH(rgb_tab),
+           sizeof(rgb_tab[0]), cmp_keyvalue_value_i);
     if (entry != NULL)
        return gui_adjust_rgb((guicolor_T)entry->key);
 
@@ -3139,8 +3144,8 @@ highlight_list_arg(
                    STRCPY(buf + buflen, (char_u *)",");
                    ++buflen;
                }
-               STRCPY(buf + buflen, (char_u *)highlight_index_tab[i]->value);
-               buflen += highlight_index_tab[i]->length;
+               STRCPY(buf + buflen, highlight_index_tab[i]->value.string);
+               buflen += highlight_index_tab[i]->value.length;
                iarg &= ~highlight_index_tab[i]->key;       // don't want "inverse"/"reverse"
            }
        }
@@ -4236,8 +4241,10 @@ highlight_get_attr_dict(int hlattr)
     {
        if (hlattr & highlight_index_tab[i]->key)
        {
-           dict_add_bool(dict, highlight_index_tab[i]->value, VVAL_TRUE);
-           hlattr &= ~highlight_index_tab[i]->key;     // don't want "inverse"/"reverse"
+           dict_add_bool(dict, (char *)highlight_index_tab[i]->value.string,
+                   VVAL_TRUE);
+           // don't want "inverse"/"reverse"
+           hlattr &= ~highlight_index_tab[i]->key;
        }
     }
 
@@ -4478,14 +4485,15 @@ hldict_attr_to_str(
     p = attr_str;
     for (i = 0; i < (int)ARRAY_LENGTH(highlight_tab); ++i)
     {
-       if (dict_get_bool(attrdict, highlight_tab[i].value, VVAL_FALSE) == VVAL_TRUE)
+       if (dict_get_bool(attrdict, (char *)highlight_tab[i].value.string,
+                   VVAL_FALSE) == VVAL_TRUE)
        {
            if (p != attr_str && (size_t)(p - attr_str + 2) < len)
                STRCPY(p, (char_u *)",");
-           if (p - attr_str + highlight_tab[i].length + 1 < len)
+           if (p - attr_str + highlight_tab[i].value.length + 1 < len)
            {
-               STRCPY(p, highlight_tab[i].value);
-               p += highlight_tab[i].length;
+               STRCPY(p, highlight_tab[i].value.string);
+               p += highlight_tab[i].value.length;
            }
        }
     }
index 31feebfe497cd8fee7f2fa02039f8774af504c4e..dc0d83f5aaefb0076fe2151931260bf0a1ae1679 100644 (file)
@@ -3077,7 +3077,7 @@ cmp_keyvalue_value(const void *a, const void *b)
     keyvalue_T *kv1 = (keyvalue_T *)a;
     keyvalue_T *kv2 = (keyvalue_T *)b;
 
-    return STRCMP(kv1->value, kv2->value);
+    return STRCMP(kv1->value.string, kv2->value.string);
 }
 
 // compare two keyvalue_T structs by value with length
@@ -3087,7 +3087,8 @@ cmp_keyvalue_value_n(const void *a, const void *b)
     keyvalue_T *kv1 = (keyvalue_T *)a;
     keyvalue_T *kv2 = (keyvalue_T *)b;
 
-    return STRNCMP(kv1->value, kv2->value, MAX(kv1->length, kv2->length));
+    return STRNCMP(kv1->value.string, kv2->value.string, MAX(kv1->value.length,
+               kv2->value.length));
 }
 
 // compare two keyvalue_T structs by case insensitive value
@@ -3097,17 +3098,19 @@ cmp_keyvalue_value_i(const void *a, const void *b)
     keyvalue_T *kv1 = (keyvalue_T *)a;
     keyvalue_T *kv2 = (keyvalue_T *)b;
 
-    return STRICMP(kv1->value, kv2->value);
+    return STRICMP(kv1->value.string, kv2->value.string);
 }
 
 // compare two keyvalue_T structs by case insensitive ASCII value
-// with length
+// with value.length
     int
 cmp_keyvalue_value_ni(const void *a, const void *b)
 {
     keyvalue_T *kv1 = (keyvalue_T *)a;
     keyvalue_T *kv2 = (keyvalue_T *)b;
 
-    return vim_strnicmp_asc(kv1->value, kv2->value, MAX(kv1->length, kv2->length));
+    return vim_strnicmp_asc((char *)kv1->value.string,
+           (char *)kv2->value.string, MAX(kv1->value.length,
+                   kv2->value.length));
 }
 
index b020a43e8301c2938dd7a00ace56ff43962076a5..ea6079b00850b668732c01731c374ae7346af295 100644 (file)
@@ -265,8 +265,8 @@ get_char_class(char_u **pp)
        static keyvalue_T *last_entry = NULL;
 
        target.key = 0;
-       target.value = (char *)*pp + 2;
-       target.length = 0;                  // not used, see cmp_keyvalue_value_n()
+       target.value.string = *pp + 2;
+       target.value.length = 0;        // not used, see cmp_keyvalue_value_n()
 
        if (last_entry != NULL && cmp_keyvalue_value_n(&target, last_entry) == 0)
            entry = last_entry;
@@ -277,7 +277,7 @@ get_char_class(char_u **pp)
        if (entry != NULL)
        {
            last_entry = entry;
-           *pp += entry->length + 2;
+           *pp += entry->value.length + 2;
            return entry->key;
        }
     }
index d11279645d8a7c25469e230527915f3f04f71fd3..33367f099ad7f942ffb951e89134d79d7a107ec0 100644 (file)
@@ -5087,13 +5087,12 @@ typedef struct {
 // Return the length of a string literal
 #define STRLEN_LITERAL(s) (sizeof(s) - 1)
 
-// Store a key/value pair
+// Store a key/value (string) pair
 typedef struct
 {
     int            key;        // the key
-    char    *value;     // the value string
-    size_t  length;     // length of the value string
+    string_T value;    // the value
 } keyvalue_T;
 
 #define KEYVALUE_ENTRY(k, v) \
-    {(k), (v), STRLEN_LITERAL(v)}
+    {(k), {((char_u *)v), STRLEN_LITERAL(v)}}
index eec57999d7ad7b314e9d3cd27bd785d20f3eb731..6940aa124f542a89e4659b026a901251b2dee6f3 100644 (file)
@@ -4066,7 +4066,7 @@ syn_list_flags(keyvalue_T *nlist, int nr_entries, int flags, int attr)
     for (i = 0; i < nr_entries; ++i)
        if (flags & nlist[i].key)
        {
-           msg_puts_attr(nlist[i].value, attr);
+           msg_puts_attr((char *)nlist[i].value.string, attr);
            msg_putchar(' ');
        }
 }
index 43409ae227200e62946350a32303a0ccb3e0e3fb..c0d1bdb6905fbaa12a2491cd6c267186b742e36f 100644 (file)
@@ -465,7 +465,7 @@ get_user_cmd_complete(expand_T *xp UNUSED, int idx)
 {
     if (idx < 0 || idx >= (int)ARRAY_LENGTH(command_complete_tab))
        return NULL;
-    return (char_u *)command_complete_tab[idx].value;
+    return command_complete_tab[idx].value.string;
 }
 
 /*
@@ -494,7 +494,7 @@ cmdcomplete_type_to_str(int expand)
 
     kv = get_commandtype(expand);
 
-    return (kv == NULL) ? NULL : (char_u *)kv->value;
+    return (kv == NULL) ? NULL : kv->value.string;
 }
 
 /*
@@ -514,8 +514,8 @@ cmdcomplete_str_to_type(char_u *complete_str)
        return EXPAND_USER_LIST;
 
     target.key = 0;
-    target.value = (char *)complete_str;
-    target.length = 0;                         // not used, see cmp_keyvalue_value()
+    target.value.string = complete_str;
+    target.value.length = 0;                   // not used, see cmp_keyvalue_value()
 
     if (last_entry != NULL && cmp_keyvalue_value(&target, last_entry) == 0)
        entry = last_entry;
@@ -670,8 +670,8 @@ uc_list(char_u *name, size_t name_len)
            entry = get_commandtype(cmd->uc_compl);
            if (entry != NULL)
            {
-               STRCPY(IObuff + len, entry->value);
-               len += entry->length;
+               STRCPY(IObuff + len, entry->value.string);
+               len += entry->value.length;
 #ifdef FEAT_EVAL
                if (p_verbose > 0 && cmd->uc_compl_arg != NULL)
                {
@@ -826,8 +826,8 @@ parse_compl_arg(
     }
 
     target.key = 0;
-    target.value = (char *)value;
-    target.length = valend;
+    target.value.string = value;
+    target.value.length = valend;
 
     if (last_entry != NULL && cmp_keyvalue_value_n(&target, last_entry) == 0)
        entry = last_entry;
@@ -1637,15 +1637,19 @@ produce_cmdmods(char_u *buf, cmdmod_T *cmod, int quote)
     // the modifiers that are simple flags
     for (i = 0; i < (int)ARRAY_LENGTH(mod_entry_tab); ++i)
        if (cmod->cmod_flags & mod_entry_tab[i].key)
-           buflen += add_cmd_modifier(buf, buflen, mod_entry_tab[i].value, mod_entry_tab[i].length, &multi_mods);
+           buflen += add_cmd_modifier(buf, buflen,
+                   (char *)mod_entry_tab[i].value.string,
+                   mod_entry_tab[i].value.length, &multi_mods);
 
     // :silent
     if (cmod->cmod_flags & CMOD_SILENT)
     {
        if (cmod->cmod_flags & CMOD_ERRSILENT)
-           buflen += add_cmd_modifier(buf, buflen, "silent!", STRLEN_LITERAL("silent!"), &multi_mods);
+           buflen += add_cmd_modifier(buf, buflen, "silent!",
+                   STRLEN_LITERAL("silent!"), &multi_mods);
        else
-           buflen += add_cmd_modifier(buf, buflen, "silent", STRLEN_LITERAL("silent"), &multi_mods);
+           buflen += add_cmd_modifier(buf, buflen, "silent",
+                   STRLEN_LITERAL("silent"), &multi_mods);
     }
 
     // :verbose
index 4aa4ab7ed2e86980fd5151b5dcf3e4fedb4b95c9..2e8948545ad7b883298ecdc38486b6ca508f1148 100644 (file)
@@ -704,6 +704,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    828,
 /**/
     827,
 /**/