Hi Bram and List,
2016-4-23(Sat) 20:56:05 UTC+9 Bram Moolenaar:
> Patch 7.4.1779
> Problem: Using negative index in strcharpart(). (Yegappan Lakshmanan)
> Solution: Assume single byte when using a negative iindex.
> Files: src/eval.c
>
>
> *** ../vim-7.4.1778/src/eval.c 2016-04-22 20:46:42.988145173 +0200
> --- src/eval.c 2016-04-23 13:50:34.475097381 +0200
> ***************
> *** 19774,19780 ****
> charlen = get_tv_number(&argvars[2]);
> while (charlen > 0 && nbyte + len < slen)
> {
> ! len += mb_char2len(p[nbyte + len]);
> --charlen;
> }
> }
> --- 19774,19785 ----
> charlen = get_tv_number(&argvars[2]);
> while (charlen > 0 && nbyte + len < slen)
> {
> ! int off = nbyte + len;
> !
> ! if (off < 0)
> ! len += 1;
> ! else
> ! len += mb_char2len(p[off]);
> --charlen;
> }
> }
> *** ../vim-7.4.1778/src/version.c 2016-04-22 21:11:03.836844513 +0200
> --- src/version.c 2016-04-23 13:53:14.573485085 +0200
> ***************
> *** 755,756 ****
> --- 755,758 ----
> { /* Add new patch number below this line */
> + /**/
> + 1779,
> /**/
strcharpart() with multibyte(more 3byte) does not work properly.
I wrote a patch.
Please check an attached patch. (file encoding is utf-8)
--
Best regards,
Hirohito Higashi (a.k.a. h_east)
--
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to the Google Groups
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.
diff --git a/src/eval.c b/src/eval.c
index a3fe2a2..8852787 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -19764,7 +19764,7 @@ f_strcharpart(typval_T *argvars, typval_T *rettv)
if (nchar > 0)
while (nchar > 0 && nbyte < slen)
{
- nbyte += mb_char2len(p[nbyte]);
+ nbyte += mb_cptr2len(p + nbyte);
--nchar;
}
else
@@ -19779,7 +19779,7 @@ f_strcharpart(typval_T *argvars, typval_T *rettv)
if (off < 0)
len += 1;
else
- len += mb_char2len(p[off]);
+ len += mb_cptr2len(p + off);
--charlen;
}
}
diff --git a/src/testdir/test_expr_utf8.vim b/src/testdir/test_expr_utf8.vim
index c512ddf..e8246ef 100644
--- a/src/testdir/test_expr_utf8.vim
+++ b/src/testdir/test_expr_utf8.vim
@@ -23,6 +23,16 @@ func Test_strcharpart()
call assert_equal('á', strcharpart('áxb', 0, 1))
call assert_equal('x', strcharpart('áxb', 1, 1))
+ call assert_equal('いうeお', strcharpart('あいうeお', 1))
+ call assert_equal('い', strcharpart('あいうeお', 1, 1))
+ call assert_equal('いう', strcharpart('あいうeお', 1, 2))
+ call assert_equal('いうe', strcharpart('あいうeお', 1, 3))
+ call assert_equal('いうeお', strcharpart('あいうeお', 1, 4))
+ call assert_equal('eお', strcharpart('あいうeお', 3))
+ call assert_equal('e', strcharpart('あいうeお', 3, 1))
+
+ call assert_equal('あ', strcharpart('あいうeお', -3, 4))
+
call assert_equal('a', strcharpart('àxb', 0, 1))
call assert_equal('̀', strcharpart('àxb', 1, 1))
call assert_equal('x', strcharpart('àxb', 2, 1))