Skip to content

Commit 9966b21

Browse files
committed
patch 8.0.0794: checking translations fails with multiple NL
Problem: The script to check translations fails if there is more than one NL in one line. Solution: Count the number of NL characters. Make count() accept a string.
1 parent 93723a4 commit 9966b21

5 files changed

Lines changed: 57 additions & 15 deletions

File tree

runtime/doc/eval.txt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
*eval.txt* For Vim version 8.0. Last change: 2017 Jul 22
1+
*eval.txt* For Vim version 8.0. Last change: 2017 Jul 28
22

33

44
VIM REFERENCE MANUAL by Bram Moolenaar
@@ -3255,11 +3255,16 @@ cosh({expr}) *cosh()*
32553255

32563256
count({comp}, {expr} [, {ic} [, {start}]]) *count()*
32573257
Return the number of times an item with value {expr} appears
3258-
in |List| or |Dictionary| {comp}.
3258+
in |String|, |List| or |Dictionary| {comp}.
3259+
32593260
If {start} is given then start with the item with this index.
32603261
{start} can only be used with a |List|.
3262+
32613263
When {ic} is given and it's |TRUE| then case is ignored.
32623264

3265+
When {comp} is a string then the number of not overlapping
3266+
occurences of {expr} is returned.
3267+
32633268

32643269
*cscope_connection()*
32653270
cscope_connection([{num} , {dbpath} [, {prepend}]])

src/evalfunc.c

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2314,8 +2314,45 @@ f_count(typval_T *argvars, typval_T *rettv)
23142314
{
23152315
long n = 0;
23162316
int ic = FALSE;
2317+
int error = FALSE;
23172318

2318-
if (argvars[0].v_type == VAR_LIST)
2319+
if (argvars[2].v_type != VAR_UNKNOWN)
2320+
ic = (int)get_tv_number_chk(&argvars[2], &error);
2321+
2322+
if (argvars[0].v_type == VAR_STRING)
2323+
{
2324+
char_u *expr = get_tv_string_chk(&argvars[1]);
2325+
char_u *p = argvars[0].vval.v_string;
2326+
char_u *next;
2327+
2328+
if (!error && expr != NULL && p != NULL)
2329+
{
2330+
if (ic)
2331+
{
2332+
size_t len = STRLEN(expr);
2333+
2334+
while (*p != NUL)
2335+
{
2336+
if (MB_STRNICMP(p, expr, len) == 0)
2337+
{
2338+
++n;
2339+
p += len;
2340+
}
2341+
else
2342+
MB_PTR_ADV(p);
2343+
}
2344+
}
2345+
else
2346+
while ((next = (char_u *)strstr((char *)p, (char *)expr))
2347+
!= NULL)
2348+
{
2349+
++n;
2350+
p = next + STRLEN(expr);
2351+
}
2352+
}
2353+
2354+
}
2355+
else if (argvars[0].v_type == VAR_LIST)
23192356
{
23202357
listitem_T *li;
23212358
list_T *l;
@@ -2326,9 +2363,6 @@ f_count(typval_T *argvars, typval_T *rettv)
23262363
li = l->lv_first;
23272364
if (argvars[2].v_type != VAR_UNKNOWN)
23282365
{
2329-
int error = FALSE;
2330-
2331-
ic = (int)get_tv_number_chk(&argvars[2], &error);
23322366
if (argvars[3].v_type != VAR_UNKNOWN)
23332367
{
23342368
idx = (long)get_tv_number_chk(&argvars[3], &error);
@@ -2356,11 +2390,8 @@ f_count(typval_T *argvars, typval_T *rettv)
23562390

23572391
if ((d = argvars[0].vval.v_dict) != NULL)
23582392
{
2359-
int error = FALSE;
2360-
23612393
if (argvars[2].v_type != VAR_UNKNOWN)
23622394
{
2363-
ic = (int)get_tv_number_chk(&argvars[2], &error);
23642395
if (argvars[3].v_type != VAR_UNKNOWN)
23652396
EMSG(_(e_invarg));
23662397
}

src/po/check.vim

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,12 @@ endif
114114
func! CountNl(first, last)
115115
let nl = 0
116116
for lnum in range(a:first, a:last)
117-
if getline(lnum) =~ '\\n'
118-
let nl += 1
119-
endif
117+
let nl += count(getline(lnum), "\n")
120118
endfor
121119
return nl
122120
endfunc
123121

124-
" Check that the \n at the end of the msid line is also present in the msgstr
122+
" Check that the \n at the end of the msgid line is also present in the msgstr
125123
" line. Skip over the header.
126124
/^"MIME-Version:
127125
while 1
@@ -138,7 +136,7 @@ while 1
138136
let transcount = CountNl(strlnum, end - 1)
139137
" Allow for a few more or less line breaks when there are 2 or more
140138
if origcount != transcount && (origcount <= 2 || transcount <= 2)
141-
echomsg 'Mismatching "\\n" in line ' . line('.')
139+
echomsg 'Mismatching "\n" in line ' . line('.')
142140
if error == 0
143141
let error = lnum
144142
endif

src/testdir/test_functions.vim

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,13 @@ func Test_count()
635635
call assert_equal(0, count(d, 'c', 1))
636636

637637
call assert_fails('call count(d, "a", 0, 1)', 'E474:')
638-
call assert_fails('call count("a", "a")', 'E712:')
638+
639+
call assert_equal(0, count("foo", "bar"))
640+
call assert_equal(1, count("foo", "oo"))
641+
call assert_equal(2, count("foo", "o"))
642+
call assert_equal(0, count("foo", "O"))
643+
call assert_equal(2, count("foo", "O", 1))
644+
call assert_equal(2, count("fooooo", "oo"))
639645
endfunc
640646

641647
func Test_changenr()

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,8 @@ static char *(features[]) =
769769

770770
static int included_patches[] =
771771
{ /* Add new patch number below this line */
772+
/**/
773+
794,
772774
/**/
773775
793,
774776
/**/

0 commit comments

Comments
 (0)