Mercurial > vim
diff src/getchar.c @ 24838:3f9053c21765 v8.2.2957
patch 8.2.2957: using getchar() in Vim9 script is problematic
Commit: https://github.com/vim/vim/commit/3a7503c34c65ed15cc08deb5b54aaf2ea51525b4
Author: Bram Moolenaar <Bram@vim.org>
Date: Mon Jun 7 18:29:17 2021 +0200
patch 8.2.2957: using getchar() in Vim9 script is problematic
Problem: Using getchar() in Vim9 script is problematic.
Solution: Add getcharstr(). (closes https://github.com/vim/vim/issues/8343)
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Mon, 07 Jun 2021 18:30:04 +0200 |
parents | 826ba03d0d22 |
children | fdc6a7769045 |
line wrap: on
line diff
--- a/src/getchar.c +++ b/src/getchar.c @@ -2016,10 +2016,10 @@ char_avail(void) #if defined(FEAT_EVAL) || defined(PROTO) /* - * "getchar()" function + * "getchar()" and "getcharstr()" functions */ - void -f_getchar(typval_T *argvars, typval_T *rettv) + static void +getchar_common(typval_T *argvars, typval_T *rettv) { varnumber_T n; int error = FALSE; @@ -2127,6 +2127,42 @@ f_getchar(typval_T *argvars, typval_T *r } /* + * "getchar()" function + */ + void +f_getchar(typval_T *argvars, typval_T *rettv) +{ + getchar_common(argvars, rettv); +} + +/* + * "getcharstr()" function + */ + void +f_getcharstr(typval_T *argvars, typval_T *rettv) +{ + getchar_common(argvars, rettv); + + if (rettv->v_type == VAR_NUMBER) + { + char_u temp[7]; // mbyte-char: 6, NUL: 1 + varnumber_T n = rettv->vval.v_number; + int i = 0; + + if (n != 0) + { + if (has_mbyte) + i += (*mb_char2bytes)(n, temp + i); + else + temp[i++] = n; + } + temp[i++] = NUL; + rettv->v_type = VAR_STRING; + rettv->vval.v_string = vim_strsave(temp); + } +} + +/* * "getcharmod()" function */ void