patch 9.1.0828: string_T struct could be used more often
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>
diff --git a/src/autocmd.c b/src/autocmd.c
index 00f41bd..330db42 100644
--- a/src/autocmd.c
+++ b/src/autocmd.c
@@ -707,8 +707,8 @@
;
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 @@
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 @@
}
}
- 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 @@
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 @@
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 @@
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);
diff --git a/src/ex_docmd.c b/src/ex_docmd.c
index 807b470..d60e344 100644
--- a/src/ex_docmd.c
+++ b/src/ex_docmd.c
@@ -9530,14 +9530,16 @@
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:
diff --git a/src/highlight.c b/src/highlight.c
index d3ea2d2..1a4c76d 100644
--- a/src/highlight.c
+++ b/src/highlight.c
@@ -869,11 +869,13 @@
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 @@
}
attr |= entry->key;
- off += entry->length;
+ off += entry->value.length;
if (arg[off] == ',') // another one follows
++off;
}
@@ -1214,9 +1216,11 @@
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 @@
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 @@
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 @@
{
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 @@
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;
}
}
}
diff --git a/src/misc2.c b/src/misc2.c
index 31feebf..dc0d83f 100644
--- a/src/misc2.c
+++ b/src/misc2.c
@@ -3077,7 +3077,7 @@
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 @@
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 @@
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));
}
diff --git a/src/regexp.c b/src/regexp.c
index b020a43..ea6079b 100644
--- a/src/regexp.c
+++ b/src/regexp.c
@@ -265,8 +265,8 @@
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 @@
if (entry != NULL)
{
last_entry = entry;
- *pp += entry->length + 2;
+ *pp += entry->value.length + 2;
return entry->key;
}
}
diff --git a/src/structs.h b/src/structs.h
index d112796..33367f0 100644
--- a/src/structs.h
+++ b/src/structs.h
@@ -5087,13 +5087,12 @@
// 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)}}
diff --git a/src/syntax.c b/src/syntax.c
index eec5799..6940aa1 100644
--- a/src/syntax.c
+++ b/src/syntax.c
@@ -4066,7 +4066,7 @@
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(' ');
}
}
diff --git a/src/usercmd.c b/src/usercmd.c
index 43409ae..c0d1bdb 100644
--- a/src/usercmd.c
+++ b/src/usercmd.c
@@ -465,7 +465,7 @@
{
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 @@
kv = get_commandtype(expand);
- return (kv == NULL) ? NULL : (char_u *)kv->value;
+ return (kv == NULL) ? NULL : kv->value.string;
}
/*
@@ -514,8 +514,8 @@
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 @@
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 @@
}
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 @@
// 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
diff --git a/src/version.c b/src/version.c
index 4aa4ab7..2e89485 100644
--- a/src/version.c
+++ b/src/version.c
@@ -705,6 +705,8 @@
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
+ 828,
+/**/
827,
/**/
826,