Merge pull request #5527 from brcolow/vim-7.4.1730

vim-patch:7.4.[1730,1734,1741,1742,1779,1782]
This commit is contained in:
James McCoy 2016-10-31 10:13:41 -04:00 committed by GitHub
commit 859e9fa65f
6 changed files with 164 additions and 6 deletions

View File

@ -15612,6 +15612,39 @@ static void f_strftime(typval_T *argvars, typval_T *rettv, FunPtr fptr)
}
}
// "strgetchar()" function
static void f_strgetchar(typval_T *argvars, typval_T *rettv, FunPtr fptr)
{
char_u *str;
int len;
int error = false;
int charidx;
rettv->vval.v_number = -1;
str = get_tv_string_chk(&argvars[0]);
if (str == NULL) {
return;
}
len = (int)STRLEN(str);
charidx = get_tv_number_chk(&argvars[1], &error);
if (error) {
return;
}
{
int byteidx = 0;
while (charidx >= 0 && byteidx < len) {
if (charidx == 0) {
rettv->vval.v_number = mb_ptr2char(str + byteidx);
break;
}
charidx--;
byteidx += mb_cptr2len(str + byteidx);
}
}
}
/*
* "stridx()" function
*/
@ -15712,6 +15745,64 @@ static void f_strwidth(typval_T *argvars, typval_T *rettv, FunPtr fptr)
rettv->vval.v_number = (varnumber_T) mb_string2cells(s);
}
// "strcharpart()" function
static void f_strcharpart(typval_T *argvars, typval_T *rettv, FunPtr fptr) {
char_u *p;
int nchar;
int nbyte = 0;
int charlen;
int len = 0;
int slen;
int error = false;
p = get_tv_string(&argvars[0]);
slen = (int)STRLEN(p);
nchar = get_tv_number_chk(&argvars[1], &error);
if (!error) {
if (nchar > 0) {
while (nchar > 0 && nbyte < slen) {
nbyte += mb_cptr2len(p + nbyte);
nchar--;
}
} else {
nbyte = nchar;
}
}
if (argvars[2].v_type != VAR_UNKNOWN) {
charlen = get_tv_number(&argvars[2]);
while (charlen > 0 && nbyte + len < slen) {
int off = nbyte + len;
if (off < 0) {
len += 1;
} else {
len += mb_cptr2len(p + off);
}
charlen--;
}
} else {
len = slen - nbyte; // default: all bytes that are available.
}
// Only return the overlap between the specified part and the actual
// string.
if (nbyte < 0) {
len += nbyte;
nbyte = 0;
} else if (nbyte > slen) {
nbyte = slen;
}
if (len < 0) {
len = 0;
} else if (nbyte + len > slen) {
len = slen - nbyte;
}
rettv->v_type = VAR_STRING;
rettv->vval.v_string = vim_strnsave(p + nbyte, len);
}
/*
* "strpart()" function
*/

View File

@ -268,9 +268,11 @@ return {
sqrt={args=1, func="float_op_wrapper", data="&sqrt"},
str2float={args=1},
str2nr={args={1, 2}},
strcharpart={args={2, 3}},
strchars={args={1,2}},
strdisplaywidth={args={1, 2}},
strftime={args={1, 2}},
strgetchar={args={2, 2}},
stridx={args={2, 3}},
string={args=1},
strlen={args=1},

View File

@ -4,6 +4,8 @@
source test_assign.vim
source test_cursor_func.vim
source test_ex_undo.vim
source test_expr.vim
source test_expr_utf8.vim
source test_feedkeys.vim
source test_cmdline.vim
source test_menu.vim

View File

@ -0,0 +1,25 @@
" Tests for expressions.
func Test_strgetchar()
call assert_equal(char2nr('a'), strgetchar('axb', 0))
call assert_equal(char2nr('x'), strgetchar('axb', 1))
call assert_equal(char2nr('b'), strgetchar('axb', 2))
call assert_equal(-1, strgetchar('axb', -1))
call assert_equal(-1, strgetchar('axb', 3))
call assert_equal(-1, strgetchar('', 0))
endfunc
func Test_strcharpart()
call assert_equal('a', strcharpart('axb', 0, 1))
call assert_equal('x', strcharpart('axb', 1, 1))
call assert_equal('b', strcharpart('axb', 2, 1))
call assert_equal('xb', strcharpart('axb', 1))
call assert_equal('', strcharpart('axb', 1, 0))
call assert_equal('', strcharpart('axb', 1, -1))
call assert_equal('', strcharpart('axb', -1, 1))
call assert_equal('', strcharpart('axb', -2, 2))
call assert_equal('a', strcharpart('axb', -1, 2))
endfunc

View File

@ -0,0 +1,38 @@
" Tests for expressions using utf-8.
if !has('multi_byte')
finish
endif
scriptencoding utf-8
func Test_strgetchar_utf8()
call assert_equal(char2nr('á'), strgetchar('áxb', 0))
call assert_equal(char2nr('x'), strgetchar('áxb', 1))
call assert_equal(char2nr('a'), strgetchar('àxb', 0))
call assert_equal(char2nr('̀'), strgetchar('àxb', 1))
call assert_equal(char2nr('x'), strgetchar('àxb', 2))
call assert_equal(char2nr('あ'), strgetchar('あaい', 0))
call assert_equal(char2nr('a'), strgetchar('あaい', 1))
call assert_equal(char2nr('い'), strgetchar('あaい', 2))
endfunc
func Test_strcharpart_utf8()
call assert_equal('áxb', strcharpart('áxb', 0))
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))
endfunc

View File

@ -661,10 +661,10 @@ static int included_patches[] = {
// 1785,
// 1784 NA
// 1783,
// 1782,
1782,
// 1781,
// 1780,
// 1779,
1779,
// 1778 NA
// 1777 NA
// 1776 NA
@ -702,19 +702,19 @@ static int included_patches[] = {
// 1745 NA
// 1744 NA
// 1743 NA
// 1742,
// 1741,
1742,
1741,
1740,
// 1739,
// 1738,
// 1737 NA
// 1736 NA
// 1735,
// 1734,
1734,
// 1733 NA
1732,
// 1731,
// 1730,
1730,
// 1729 NA
1728,
// 1727,