diff options
author | Famiu Haque <famiuhaque@proton.me> | 2025-01-01 19:45:14 +0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-01-01 07:45:14 -0600 |
commit | 9d114b720514b0bde96dbdd9b6ef328d232a1589 (patch) | |
tree | 6f0dacc350b3d5a4a987cb5ab38073bbe2b8e10d | |
parent | 59cbe640233eeb6ae96231e059449e73e15f9ea5 (diff) | |
download | rneovim-9d114b720514b0bde96dbdd9b6ef328d232a1589.tar.gz rneovim-9d114b720514b0bde96dbdd9b6ef328d232a1589.tar.bz2 rneovim-9d114b720514b0bde96dbdd9b6ef328d232a1589.zip |
refactor(options): use `const` in more places (#31791)
-rw-r--r-- | src/nvim/eval/vars.c | 14 | ||||
-rw-r--r-- | src/nvim/mapping.c | 2 | ||||
-rw-r--r-- | src/nvim/option.c | 28 | ||||
-rw-r--r-- | src/nvim/os/env.c | 2 |
4 files changed, 26 insertions, 20 deletions
diff --git a/src/nvim/eval/vars.c b/src/nvim/eval/vars.c index 6504d44eb6..b9b5a055fb 100644 --- a/src/nvim/eval/vars.c +++ b/src/nvim/eval/vars.c @@ -872,11 +872,15 @@ static char *ex_let_option(char *arg, typval_T *const tv, const bool is_const, } else { newval = BOOLEAN_OPTVAL(TRISTATE_FROM_INT(new_n)); } - } else if (!hidden && is_string - && curval.data.string.data != NULL && newval.data.string.data != NULL) { // string - OptVal newval_old = newval; - newval = CSTR_AS_OPTVAL(concat_str(curval.data.string.data, newval.data.string.data)); - optval_free(newval_old); + } else if (!hidden && is_string) { // string + const char *curval_data = curval.data.string.data; + const char *newval_data = newval.data.string.data; + + if (curval_data != NULL && newval_data != NULL) { + OptVal newval_old = newval; + newval = CSTR_AS_OPTVAL(concat_str(curval_data, newval_data)); + optval_free(newval_old); + } } } diff --git a/src/nvim/mapping.c b/src/nvim/mapping.c index d29a2c8e43..ca0349d1f6 100644 --- a/src/nvim/mapping.c +++ b/src/nvim/mapping.c @@ -1901,7 +1901,7 @@ int makemap(FILE *fd, buf_T *buf) // "what": 0 for :map lhs, 1 for :map rhs, 2 for :set // // return FAIL for failure, OK otherwise -int put_escstr(FILE *fd, char *strstart, int what) +int put_escstr(FILE *fd, const char *strstart, int what) { uint8_t *str = (uint8_t *)strstart; diff --git a/src/nvim/option.c b/src/nvim/option.c index a59e55121f..9d9e70622a 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -533,8 +533,8 @@ static void set_string_default(OptIndex opt_idx, char *val, bool allocated) /// For an option value that contains comma separated items, find "newval" in /// "origval". Return NULL if not found. -static char *find_dup_item(char *origval, const char *newval, const size_t newvallen, - uint32_t flags) +static const char *find_dup_item(const char *origval, const char *newval, const size_t newvallen, + uint32_t flags) FUNC_ATTR_NONNULL_ARG(2) { if (origval == NULL) { @@ -543,7 +543,7 @@ static char *find_dup_item(char *origval, const char *newval, const size_t newva int bs = 0; - for (char *s = origval; *s != NUL; s++) { + for (const char *s = origval; *s != NUL; s++) { if ((!(flags & kOptFlagComma) || s == origval || (s[-1] == ',' && !(bs & 1))) && strncmp(s, newval, newvallen) == 0 && (!(flags & kOptFlagComma) || s[newvallen] == ',' || s[newvallen] == NUL)) { @@ -727,7 +727,7 @@ void ex_set(exarg_T *eap) /// Copy the new string value into allocated memory for the option. /// Can't use set_option_direct(), because we need to remove the backslashes. -static char *stropt_copy_value(char *origval, char **argp, set_op_T op, +static char *stropt_copy_value(const char *origval, char **argp, set_op_T op, uint32_t flags FUNC_ATTR_UNUSED) { char *arg = *argp; @@ -774,7 +774,7 @@ static char *stropt_copy_value(char *origval, char **argp, set_op_T op, } /// Expand environment variables and ~ in string option value 'newval'. -static char *stropt_expand_envvar(OptIndex opt_idx, char *origval, char *newval, set_op_T op) +static char *stropt_expand_envvar(OptIndex opt_idx, const char *origval, char *newval, set_op_T op) { char *s = option_expand(opt_idx, newval); if (s == NULL) { @@ -794,7 +794,7 @@ static char *stropt_expand_envvar(OptIndex opt_idx, char *origval, char *newval, /// Concatenate the original and new values of a string option, adding a "," if /// needed. -static void stropt_concat_with_comma(char *origval, char *newval, set_op_T op, uint32_t flags) +static void stropt_concat_with_comma(const char *origval, char *newval, set_op_T op, uint32_t flags) { int len = 0; int comma = ((flags & kOptFlagComma) && *origval != NUL && *newval != NUL); @@ -820,7 +820,8 @@ static void stropt_concat_with_comma(char *origval, char *newval, set_op_T op, u /// Remove a value from a string option. Copy string option value in "origval" /// to "newval" and then remove the string "strval" of length "len". -static void stropt_remove_val(char *origval, char *newval, uint32_t flags, char *strval, int len) +static void stropt_remove_val(const char *origval, char *newval, uint32_t flags, const char *strval, + int len) { // Remove newval[] from origval[]. (Note: "len" has been set above // and is used here). @@ -872,13 +873,13 @@ static void stropt_remove_dupflags(char *newval, uint32_t flags) /// set {opt}={val} /// set {opt}:{val} static char *stropt_get_newval(int nextchar, OptIndex opt_idx, char **argp, void *varp, - char *origval, set_op_T *op_arg, uint32_t flags) + const char *origval, set_op_T *op_arg, uint32_t flags) { char *arg = *argp; set_op_T op = *op_arg; char *save_arg = NULL; char *newval; - char *s = NULL; + const char *s = NULL; arg++; // jump to after the '=' or ':' @@ -1194,9 +1195,10 @@ static OptVal get_option_newval(OptIndex opt_idx, int opt_flags, set_prefix_T pr break; } case kOptValTypeString: { - char *oldval_str = oldval.data.string.data; + const char *oldval_str = oldval.data.string.data; // Get the new value for the option - char *newval_str = stropt_get_newval(nextchar, opt_idx, argp, varp, oldval_str, &op, flags); + const char *newval_str = stropt_get_newval(nextchar, opt_idx, argp, varp, oldval_str, &op, + flags); newval = CSTR_AS_OPTVAL(newval_str); break; } @@ -1568,7 +1570,7 @@ char *find_shada_parameter(int type) /// These string options cannot be indirect! /// If "val" is NULL expand the current value of the option. /// Return pointer to NameBuff, or NULL when not expanded. -static char *option_expand(OptIndex opt_idx, char *val) +static char *option_expand(OptIndex opt_idx, const char *val) { // if option doesn't need expansion nothing to do if (!(options[opt_idx].flags & kOptFlagExpand) || is_option_hidden(opt_idx)) { @@ -4392,7 +4394,7 @@ static int put_set(FILE *fd, char *cmd, OptIndex opt_idx, void *varp) return FAIL; } - char *value_str = value.data.string.data; + const char *value_str = value.data.string.data; char *buf = NULL; char *part = NULL; diff --git a/src/nvim/os/env.c b/src/nvim/os/env.c index 733e52e69c..3126881266 100644 --- a/src/nvim/os/env.c +++ b/src/nvim/os/env.c @@ -580,7 +580,7 @@ void expand_env(char *src, char *dst, int dstlen) /// @param esc Escape spaces in expanded variables /// @param one `srcp` is a single filename /// @param prefix Start again after this (can be NULL) -void expand_env_esc(char *restrict srcp, char *restrict dst, int dstlen, bool esc, bool one, +void expand_env_esc(const char *restrict srcp, char *restrict dst, int dstlen, bool esc, bool one, char *prefix) FUNC_ATTR_NONNULL_ARG(1, 2) { |