diff options
author | Bram Moolenaar <Bram@vim.org> | 2020-08-23 17:34:46 +0200 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2020-08-23 17:34:46 +0200 |
commit | 6c53fca02301ff871cddc1c74c388e23e53a424a (patch) | |
tree | a4cec7dd695a51465d06922b5c18a30d6b45509b /src | |
parent | 430deb1945cbc7a17ed42c5c737fc08d2eef327a (diff) |
patch 8.2.1517: cannot easily get the character under the cursorv8.2.1517
Problem: Cannot easily get the character under the cursor.
Solution: Add the {chars} argument to strpart().
Diffstat (limited to 'src')
-rw-r--r-- | src/evalfunc.c | 18 | ||||
-rw-r--r-- | src/testdir/test_functions.vim | 4 | ||||
-rw-r--r-- | src/version.c | 2 |
3 files changed, 19 insertions, 5 deletions
diff --git a/src/evalfunc.c b/src/evalfunc.c index c8747e2c14..fcfd4b10de 100644 --- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -950,7 +950,7 @@ static funcentry_T global_functions[] = {"stridx", 2, 3, FEARG_1, ret_number, f_stridx}, {"string", 1, 1, FEARG_1, ret_string, f_string}, {"strlen", 1, 1, FEARG_1, ret_number, f_strlen}, - {"strpart", 2, 3, FEARG_1, ret_string, f_strpart}, + {"strpart", 2, 4, FEARG_1, ret_string, f_strpart}, {"strptime", 2, 2, FEARG_1, ret_number, #ifdef HAVE_STRPTIME f_strptime @@ -8270,10 +8270,8 @@ f_strpart(typval_T *argvars, typval_T *rettv) else len = slen - n; // default len: all bytes that are available. - /* - * Only return the overlap between the specified part and the actual - * string. - */ + // Only return the overlap between the specified part and the actual + // string. if (n < 0) { len += n; @@ -8286,6 +8284,16 @@ f_strpart(typval_T *argvars, typval_T *rettv) else if (n + len > slen) len = slen - n; + if (argvars[2].v_type != VAR_UNKNOWN && argvars[3].v_type != VAR_UNKNOWN) + { + int off; + + // length in characters + for (off = n; off < slen && len > 0; --len) + off += mb_ptr2len(p + off); + len = off - n; + } + rettv->v_type = VAR_STRING; rettv->vval.v_string = vim_strnsave(p + n, len); } diff --git a/src/testdir/test_functions.vim b/src/testdir/test_functions.vim index dc06bd72ec..e15199b786 100644 --- a/src/testdir/test_functions.vim +++ b/src/testdir/test_functions.vim @@ -513,6 +513,10 @@ func Test_strpart() call assert_equal('lép', strpart('éléphant', 2, 4)) call assert_equal('léphant', strpart('éléphant', 2)) + + call assert_equal('é', strpart('éléphant', 0, 1, 1)) + call assert_equal('ép', strpart('éléphant', 3, 2, v:true)) + call assert_equal('ó', strpart('cómposed', 1, 1, 1)) endfunc func Test_tolower() diff --git a/src/version.c b/src/version.c index 4c2794af0b..9eb448b2df 100644 --- a/src/version.c +++ b/src/version.c @@ -755,6 +755,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 1517, +/**/ 1516, /**/ 1515, |