0
0
mirror of https://github.com/vim/vim.git synced 2025-10-09 06:14:17 -04:00

patch 8.2.2607: strcharpart() cannot include composing characters

Problem:    strcharpart() cannot include composing characters.
Solution:   Add the {skipcc} argument.
This commit is contained in:
Bram Moolenaar
2021-03-14 19:46:45 +01:00
parent 70ce8a1561
commit 02b4d9b18a
4 changed files with 49 additions and 13 deletions

View File

@@ -1575,7 +1575,7 @@ static funcentry_T global_functions[] =
ret_number, f_str2nr},
{"strcharlen", 1, 1, FEARG_1, NULL,
ret_number, f_strcharlen},
{"strcharpart", 2, 3, FEARG_1, NULL,
{"strcharpart", 2, 4, FEARG_1, NULL,
ret_string, f_strcharpart},
{"strchars", 1, 2, FEARG_1, NULL,
ret_number, f_strchars},
@@ -9316,6 +9316,7 @@ f_strcharpart(typval_T *argvars, typval_T *rettv)
int nchar;
int nbyte = 0;
int charlen;
int skipcc = FALSE;
int len = 0;
int slen;
int error = FALSE;
@@ -9326,10 +9327,24 @@ f_strcharpart(typval_T *argvars, typval_T *rettv)
nchar = (int)tv_get_number_chk(&argvars[1], &error);
if (!error)
{
if (argvars[2].v_type != VAR_UNKNOWN
&& argvars[3].v_type != VAR_UNKNOWN)
{
skipcc = tv_get_bool(&argvars[3]);
if (skipcc < 0 || skipcc > 1)
{
semsg(_(e_using_number_as_bool_nr), skipcc);
return;
}
}
if (nchar > 0)
while (nchar > 0 && nbyte < slen)
{
nbyte += MB_CPTR2LEN(p + nbyte);
if (skipcc)
nbyte += mb_ptr2len(p + nbyte);
else
nbyte += MB_CPTR2LEN(p + nbyte);
--nchar;
}
else
@@ -9344,7 +9359,12 @@ f_strcharpart(typval_T *argvars, typval_T *rettv)
if (off < 0)
len += 1;
else
len += MB_CPTR2LEN(p + off);
{
if (skipcc)
len += mb_ptr2len(p + off);
else
len += MB_CPTR2LEN(p + off);
}
--charlen;
}
}

View File

@@ -31,6 +31,14 @@ func Test_strcharpart()
call assert_equal('a', strcharpart('àxb', 0, 1))
call assert_equal('̀', strcharpart('àxb', 1, 1))
call assert_equal('x', strcharpart('àxb', 2, 1))
call assert_equal('a', strcharpart('àxb', 0, 1, 0))
call assert_equal('à', strcharpart('àxb', 0, 1, 1))
call assert_equal('x', strcharpart('àxb', 1, 1, 1))
call assert_fails("let v = strcharpart('abc', 0, 0, [])", 'E745:')
call assert_fails("let v = strcharpart('abc', 0, 0, 2)", 'E1023:')
endfunc
" vim: shiftwidth=2 sts=2 expandtab

View File

@@ -750,6 +750,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
2607,
/**/
2606,
/**/