vim-patch:7.4.755

Problem:    It is not easy to count the number of characters.
Solution:   Add the skipcc argument to strchars(). (Hirohito Higashi, Ken
            Takata)

641e48c224
This commit is contained in:
Jurica Bradaric 2016-01-30 17:42:22 +01:00 committed by Justin M. Keyes
parent 31aa060bca
commit a9a25fda42
4 changed files with 48 additions and 18 deletions

View File

@ -2028,7 +2028,7 @@ split( {expr} [, {pat} [, {keepempty}]])
sqrt( {expr}) Float square root of {expr} sqrt( {expr}) Float square root of {expr}
str2float( {expr}) Float convert String to Float str2float( {expr}) Float convert String to Float
str2nr( {expr} [, {base}]) Number convert String to Number str2nr( {expr} [, {base}]) Number convert String to Number
strchars( {expr}) Number character length of the String {expr} strchars( {expr} [, {skipcc}]) Number character length of the String {expr}
strdisplaywidth( {expr} [, {col}]) Number display length of the String {expr} strdisplaywidth( {expr} [, {col}]) Number display length of the String {expr}
strftime( {format}[, {time}]) String time in specified format strftime( {format}[, {time}]) String time in specified format
stridx( {haystack}, {needle}[, {start}]) stridx( {haystack}, {needle}[, {start}])
@ -6231,15 +6231,11 @@ string({expr}) Return {expr} converted to a String. If {expr} is a Number,
*strlen()* *strlen()*
strlen({expr}) The result is a Number, which is the length of the String strlen({expr}) The result is a Number, which is the length of the String
{expr} in bytes. {expr} in bytes.
If you want to count the number of multi-byte characters (not
counting composing characters) use something like this: >
:let len = strlen(substitute(str, ".", "x", "g"))
<
If the argument is a Number it is first converted to a String. If the argument is a Number it is first converted to a String.
For other types an error is given. For other types an error is given.
Also see |len()|, |strchars()|, |strdisplaywidth()| and If you want to count the number of multi-byte characters use
|strwidth()|. |strchars()|.
Also see |len()|, |strdisplaywidth()| and |strwidth()|.
strpart({src}, {start}[, {len}]) *strpart()* strpart({src}, {start}[, {len}]) *strpart()*
The result is a String, which is part of {src}, starting from The result is a String, which is part of {src}, starting from

View File

@ -3045,12 +3045,13 @@ static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock)
li = li->li_next; li = li->li_next;
++lp->ll_n1; ++lp->ll_n1;
} }
} else if (lp->ll_list != NULL) } else if (lp->ll_list != NULL) {
/* (un)lock a List item. */ // (un)lock a List item.
item_lock(&lp->ll_li->li_tv, deep, lock); item_lock(&lp->ll_li->li_tv, deep, lock);
else } else {
/* un(lock) a Dictionary item. */ // (un)lock a Dictionary item.
item_lock(&lp->ll_di->di_tv, deep, lock); item_lock(&lp->ll_di->di_tv, deep, lock);
}
return ret; return ret;
} }
@ -7337,7 +7338,7 @@ static struct fst {
{ "sqrt", 1, 1, f_sqrt }, { "sqrt", 1, 1, f_sqrt },
{ "str2float", 1, 1, f_str2float }, { "str2float", 1, 1, f_str2float },
{ "str2nr", 1, 2, f_str2nr }, { "str2nr", 1, 2, f_str2nr },
{ "strchars", 1, 1, f_strchars }, { "strchars", 1, 2, f_strchars },
{ "strdisplaywidth", 1, 2, f_strdisplaywidth }, { "strdisplaywidth", 1, 2, f_strdisplaywidth },
{ "strftime", 1, 2, f_strftime }, { "strftime", 1, 2, f_strftime },
{ "stridx", 2, 3, f_stridx }, { "stridx", 2, 3, f_stridx },
@ -16213,13 +16214,23 @@ static void f_strlen(typval_T *argvars, typval_T *rettv)
static void f_strchars(typval_T *argvars, typval_T *rettv) static void f_strchars(typval_T *argvars, typval_T *rettv)
{ {
char_u *s = get_tv_string(&argvars[0]); char_u *s = get_tv_string(&argvars[0]);
int skipcc = 0;
varnumber_T len = 0; varnumber_T len = 0;
int (*func_mb_ptr2char_adv)(char_u **pp);
while (*s != NUL) { if (argvars[1].v_type != VAR_UNKNOWN) {
mb_cptr2char_adv(&s); skipcc = get_tv_number_chk(&argvars[1], NULL);
++len; }
if (skipcc < 0 || skipcc > 1) {
EMSG(_(e_invarg));
} else {
func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
while (*s != NUL) {
func_mb_ptr2char_adv(&s);
++len;
}
rettv->vval.v_number = len;
} }
rettv->vval.v_number = len;
} }
/* /*

View File

@ -533,7 +533,7 @@ static int included_patches[] = {
// 758, // 758,
// 757 NA // 757 NA
// 756 NA // 756 NA
// 755, 755,
754, 754,
753, 753,
// 752, // 752,

View File

@ -3,6 +3,7 @@
local helpers = require('test.functional.helpers') local helpers = require('test.functional.helpers')
local clear, feed, insert = helpers.clear, helpers.feed, helpers.insert local clear, feed, insert = helpers.clear, helpers.feed, helpers.insert
local execute, expect = helpers.execute, helpers.expect local execute, expect = helpers.execute, helpers.expect
local eq, eval = helpers.eq, helpers.eval
describe('utf8', function() describe('utf8', function()
setup(clear) setup(clear)
@ -27,4 +28,26 @@ describe('utf8', function()
xあああ xあああ
bxbb]]) bxbb]])
end) end)
it('strchars()', function()
eq(1, eval('strchars("a")'))
eq(1, eval('strchars("a", 0)'))
eq(1, eval('strchars("a", 1)'))
eq(3, eval('strchars("あいa")'))
eq(3, eval('strchars("あいa", 0)'))
eq(3, eval('strchars("あいa", 1)'))
eq(2, eval('strchars("A\\u20dd")'))
eq(2, eval('strchars("A\\u20dd", 0)'))
eq(1, eval('strchars("A\\u20dd", 1)'))
eq(3, eval('strchars("A\\u20dd\\u20dd")'))
eq(3, eval('strchars("A\\u20dd\\u20dd", 0)'))
eq(1, eval('strchars("A\\u20dd\\u20dd", 1)'))
eq(1, eval('strchars("\\u20dd")'))
eq(1, eval('strchars("\\u20dd", 0)'))
eq(1, eval('strchars("\\u20dd", 1)'))
end)
end) end)