refactor: add function xstrnsave

xstrnsave is a clone of vim_strnsave that uses char* instead of char_u*.
Its purpose short-term is to help reduce the number of casts and for
long-term to replace vim_strnsave as the need to use char_u is
eliminated.
This commit is contained in:
Dundar Goc 2022-04-15 19:49:06 +02:00
parent 7a2fcbbbec
commit 88270a5735
4 changed files with 13 additions and 6 deletions

View File

@ -388,12 +388,12 @@ char *get_exception_string(void *value, except_type_T type, char_u *cmdname, int
mesg = ((struct msglist *)value)->throw_msg; mesg = ((struct msglist *)value)->throw_msg;
if (cmdname != NULL && *cmdname != NUL) { if (cmdname != NULL && *cmdname != NUL) {
size_t cmdlen = STRLEN(cmdname); size_t cmdlen = STRLEN(cmdname);
ret = (char *)vim_strnsave((char_u *)"Vim(", 4 + cmdlen + 2 + STRLEN(mesg)); ret = xstrnsave("Vim(", 4 + cmdlen + 2 + STRLEN(mesg));
STRCPY(&ret[4], cmdname); STRCPY(&ret[4], cmdname);
STRCPY(&ret[4 + cmdlen], "):"); STRCPY(&ret[4 + cmdlen], "):");
val = ret + 4 + cmdlen + 2; val = ret + 4 + cmdlen + 2;
} else { } else {
ret = (char *)vim_strnsave((char_u *)"Vim:", 4 + STRLEN(mesg)); ret = xstrnsave("Vim:", 4 + STRLEN(mesg));
val = ret + 4; val = ret + 4;
} }

View File

@ -433,7 +433,7 @@ static int cs_add_common(char *arg1, char *arg2, char *flags)
if (fname == NULL) { if (fname == NULL) {
goto add_err; goto add_err;
} }
fname = (char *)vim_strnsave((char_u *)fname, len); fname = xstrnsave(fname, len);
xfree(fbuf); xfree(fbuf);
FileInfo file_info; FileInfo file_info;
bool file_info_ok = os_fileinfo(fname, &file_info); bool file_info_ok = os_fileinfo(fname, &file_info);

View File

@ -1275,7 +1275,7 @@ static char *popup_mode_name(char *name, int idx)
size_t len = STRLEN(name); size_t len = STRLEN(name);
assert(len >= 4); assert(len >= 4);
char *p = (char *)vim_strnsave((char_u *)name, len + 1); char *p = xstrnsave(name, len + 1);
memmove(p + 6, p + 5, len - 4); memmove(p + 6, p + 5, len - 4);
p[5] = menu_mode_chars[idx]; p[5] = menu_mode_chars[idx];
@ -1308,7 +1308,7 @@ static char *menu_text(const char *str, int *mnemonic, char **actext)
*actext = xstrdup(p + 1); *actext = xstrdup(p + 1);
} }
assert(p >= str); assert(p >= str);
text = (char *)vim_strnsave((char_u *)str, (size_t)(p - str)); text = xstrnsave(str, (size_t)(p - str));
} else { } else {
text = xstrdup(str); text = xstrdup(str);
} }
@ -1560,7 +1560,7 @@ void ex_menutranslate(exarg_T *eap)
from = xstrdup(from); from = xstrdup(from);
from_noamp = menu_text(from, NULL, NULL); from_noamp = menu_text(from, NULL, NULL);
assert(arg >= to); assert(arg >= to);
to = (char *)vim_strnsave((char_u *)to, (size_t)(arg - to)); to = xstrnsave(to, (size_t)(arg - to));
menu_translate_tab_and_shift(from); menu_translate_tab_and_shift(from);
menu_translate_tab_and_shift(to); menu_translate_tab_and_shift(to);
menu_unescape_name(from); menu_unescape_name(from);

View File

@ -67,6 +67,13 @@ char_u *vim_strnsave(const char_u *string, size_t len)
return (char_u *)strncpy(xmallocz(len), (char *)string, len); return (char_u *)strncpy(xmallocz(len), (char *)string, len);
} }
/// A clone of vim_strnsave() that uses char* instead of char_u*
char *xstrnsave(const char *string, size_t len)
FUNC_ATTR_NONNULL_RET FUNC_ATTR_MALLOC FUNC_ATTR_NONNULL_ALL
{
return strncpy(xmallocz(len), string, len); // NOLINT(runtime/printf)
}
/* /*
* Same as vim_strsave(), but any characters found in esc_chars are preceded * Same as vim_strsave(), but any characters found in esc_chars are preceded
* by a backslash. * by a backslash.