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>
This commit is contained in:
John Marriott
2024-11-02 15:59:01 +01:00
committed by Christian Brabandt
parent f1d83c4c71
commit 8d4477ef22
9 changed files with 76 additions and 55 deletions

View 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);