diff src/evalfunc.c @ 21935:62f933f64447 v8.2.1517

patch 8.2.1517: cannot easily get the character under the cursor Commit: https://github.com/vim/vim/commit/6c53fca02301ff871cddc1c74c388e23e53a424a Author: Bram Moolenaar <Bram@vim.org> Date: Sun Aug 23 17:34:46 2020 +0200 patch 8.2.1517: cannot easily get the character under the cursor Problem: Cannot easily get the character under the cursor. Solution: Add the {chars} argument to strpart().
author Bram Moolenaar <Bram@vim.org>
date Sun, 23 Aug 2020 17:45:03 +0200
parents a427f5f26419
children bf68435a695a
line wrap: on
line diff
--- 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 *r
     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 *r
     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);
 }