diff options
author | Justin M. Keyes <justinkz@gmail.com> | 2017-04-23 01:02:39 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-23 01:02:39 +0200 |
commit | a9d37c928e109f8bb75faf24c5b4effb90b115ff (patch) | |
tree | 71b81415cfcfcc82deb5d43a7301a7a12a315f52 /src/nvim/eval/typval.c | |
parent | 77b6de0968172fec57412f92c19bb84d76d7dc77 (diff) | |
parent | d463c9e03a79e981faaaa985b1160c292d08e172 (diff) | |
download | rneovim-a9d37c928e109f8bb75faf24c5b4effb90b115ff.tar.gz rneovim-a9d37c928e109f8bb75faf24c5b4effb90b115ff.tar.bz2 rneovim-a9d37c928e109f8bb75faf24c5b4effb90b115ff.zip |
Merge #6513 from ZyX-I/lazier-arg_errmsg-gettext
Diffstat (limited to 'src/nvim/eval/typval.c')
-rw-r--r-- | src/nvim/eval/typval.c | 42 |
1 files changed, 31 insertions, 11 deletions
diff --git a/src/nvim/eval/typval.c b/src/nvim/eval/typval.c index a57dd42f6e..786b766689 100644 --- a/src/nvim/eval/typval.c +++ b/src/nvim/eval/typval.c @@ -1801,11 +1801,13 @@ static inline void _nothing_conv_dict_end(typval_T *const tv, #define TYPVAL_ENCODE_NAME nothing #define TYPVAL_ENCODE_FIRST_ARG_TYPE const void *const #define TYPVAL_ENCODE_FIRST_ARG_NAME ignored +#define TYPVAL_ENCODE_TRANSLATE_OBJECT_NAME #include "nvim/eval/typval_encode.c.h" #undef TYPVAL_ENCODE_SCOPE #undef TYPVAL_ENCODE_NAME #undef TYPVAL_ENCODE_FIRST_ARG_TYPE #undef TYPVAL_ENCODE_FIRST_ARG_NAME +#undef TYPVAL_ENCODE_TRANSLATE_OBJECT_NAME #undef TYPVAL_ENCODE_ALLOW_SPECIALS #undef TYPVAL_ENCODE_CONV_NIL @@ -1839,12 +1841,14 @@ static inline void _nothing_conv_dict_end(typval_T *const tv, /// @param[in,out] tv Value to free. void tv_clear(typval_T *const tv) { - static char *objname = NULL; // cached because gettext() is slow. #6437 - if (objname == NULL) { - objname = xstrdup(_("tv_clear() argument")); - } if (tv != NULL && tv->v_type != VAR_UNKNOWN) { - const int evn_ret = encode_vim_to_nothing(NULL, tv, objname); + // WARNING: do not translate the string here, gettext is slow and function + // is used *very* often. At the current state encode_vim_to_nothing() does + // not error out and does not use the argument anywhere. + // + // If situation changes and this argument will be used, translate it in the + // place where it is used. + const int evn_ret = encode_vim_to_nothing(NULL, tv, "tv_clear() argument"); (void)evn_ret; assert(evn_ret == OK); } @@ -2047,11 +2051,20 @@ bool tv_islocked(const typval_T *const tv) /// /// @param[in] lock Lock status. /// @param[in] name Variable name, used in the error message. -/// @param[in] name_len Variable name length. +/// @param[in] name_len Variable name length. Use #TV_TRANSLATE to translate +/// variable name and compute the length. Use #TV_CSTRING +/// to compute the length with strlen() without +/// translating. +/// +/// Both #TV_… values are used for optimization purposes: +/// variable name with its length is needed only in case +/// of error, when no error occurs computing them is +/// a waste of CPU resources. This especially applies to +/// gettext. /// /// @return true if variable is locked, false otherwise. -bool tv_check_lock(const VarLockStatus lock, const char *const name, - const size_t name_len) +bool tv_check_lock(const VarLockStatus lock, const char *name, + size_t name_len) FUNC_ATTR_WARN_UNUSED_RESULT { const char *error_message = NULL; @@ -2070,10 +2083,17 @@ bool tv_check_lock(const VarLockStatus lock, const char *const name, } assert(error_message != NULL); - const char *const unknown_name = _("Unknown"); + if (name == NULL) { + name = _("Unknown"); + name_len = strlen(name); + } else if (name_len == TV_TRANSLATE) { + name = _(name); + name_len = strlen(name); + } else if (name_len == TV_CSTRING) { + name_len = strlen(name); + } - emsgf(_(error_message), (name != NULL ? name_len : strlen(unknown_name)), - (name != NULL ? name : unknown_name)); + emsgf(_(error_message), (int)name_len, name); return true; } |