Skip to content

Commit 7fed5c1

Browse files
committed
patch 7.4.1685
Problem: There is no easy way to get all the information about a match. Solution: Add matchstrpos(). (Ozaki Kiichi)
1 parent d18cfb7 commit 7fed5c1

File tree

6 files changed

+88
-4
lines changed

6 files changed

+88
-4
lines changed

runtime/doc/eval.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,6 +2020,8 @@ matchlist( {expr}, {pat}[, {start}[, {count}]])
20202020
List match and submatches of {pat} in {expr}
20212021
matchstr( {expr}, {pat}[, {start}[, {count}]])
20222022
String {count}'th match of {pat} in {expr}
2023+
matchstrpos( {expr}, {pat}[, {start}[, {count}]])
2024+
List {count}'th match of {pat} in {expr}
20232025
max( {list}) Number maximum value of items in {list}
20242026
min( {list}) Number minimum value of items in {list}
20252027
mkdir( {name} [, {path} [, {prot}]])
@@ -5204,6 +5206,24 @@ matchstr({expr}, {pat}[, {start}[, {count}]]) *matchstr()*
52045206
:echo matchstr("testing", "ing", 5)
52055207
< result is "".
52065208
When {expr} is a |List| then the matching item is returned.
5209+
The type isn't changed, it's not necessarily a String.
5210+
5211+
matchstrpos({expr}, {pat}[, {start}[, {count}]]) *matchstrpos()*
5212+
Same as |matchstr()|, but return the matched string, the start
5213+
position and the end position of the match. Example: >
5214+
:echo matchstrpos("testing", "ing")
5215+
< results in ["ing", 4, 7].
5216+
When there is no match ["", -1, -1] is returned.
5217+
The {start}, if given, has the same meaning as for |match()|. >
5218+
:echo matchstrpos("testing", "ing", 2)
5219+
< results in ["ing", 4, 7]. >
5220+
:echo matchstrpos("testing", "ing", 5)
5221+
< result is ["", -1, -1].
5222+
When {expr} is a |List| then the matching item, the index
5223+
of first item where {pat} matches, the start position and the
5224+
end position of the match are returned. >
5225+
:echo matchstrpos([1, '__x'], '\a')
5226+
< result is ["x", 1, 2, 3].
52075227
The type isn't changed, it's not necessarily a String.
52085228

52095229
*max()*

runtime/doc/usr_41.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ String manipulation: *string-functions*
592592
match() position where a pattern matches in a string
593593
matchend() position where a pattern match ends in a string
594594
matchstr() match of a pattern in a string
595+
matchstrpos() match and postions of a pattern in a string
595596
matchlist() like matchstr() and also return submatches
596597
stridx() first index of a short string in a long string
597598
strridx() last index of a short string in a long string

src/eval.c

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,7 @@ static void f_matchdelete(typval_T *argvars, typval_T *rettv);
673673
static void f_matchend(typval_T *argvars, typval_T *rettv);
674674
static void f_matchlist(typval_T *argvars, typval_T *rettv);
675675
static void f_matchstr(typval_T *argvars, typval_T *rettv);
676+
static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
676677
static void f_max(typval_T *argvars, typval_T *rettv);
677678
static void f_min(typval_T *argvars, typval_T *rettv);
678679
#ifdef vim_mkdir
@@ -8383,6 +8384,7 @@ static struct fst
83838384
{"matchend", 2, 4, f_matchend},
83848385
{"matchlist", 2, 4, f_matchlist},
83858386
{"matchstr", 2, 4, f_matchstr},
8387+
{"matchstrpos", 2, 4, f_matchstrpos},
83868388
{"max", 1, 1, f_max},
83878389
{"min", 1, 1, f_min},
83888390
#ifdef vim_mkdir
@@ -15302,11 +15304,26 @@ find_some_match(typval_T *argvars, typval_T *rettv, int type)
1530215304
p_cpo = (char_u *)"";
1530315305

1530415306
rettv->vval.v_number = -1;
15305-
if (type == 3)
15307+
if (type == 3 || type == 4)
1530615308
{
15307-
/* return empty list when there are no matches */
15309+
/* type 3: return empty list when there are no matches.
15310+
* type 4: return ["", -1, -1, -1] */
1530815311
if (rettv_list_alloc(rettv) == FAIL)
1530915312
goto theend;
15313+
if (type == 4
15314+
&& (list_append_string(rettv->vval.v_list,
15315+
(char_u *)"", 0) == FAIL
15316+
|| list_append_number(rettv->vval.v_list,
15317+
(varnumber_T)-1) == FAIL
15318+
|| list_append_number(rettv->vval.v_list,
15319+
(varnumber_T)-1) == FAIL
15320+
|| list_append_number(rettv->vval.v_list,
15321+
(varnumber_T)-1) == FAIL))
15322+
{
15323+
list_free(rettv->vval.v_list, TRUE);
15324+
rettv->vval.v_list = NULL;
15325+
goto theend;
15326+
}
1531015327
}
1531115328
else if (type == 2)
1531215329
{
@@ -15383,7 +15400,7 @@ find_some_match(typval_T *argvars, typval_T *rettv, int type)
1538315400
break;
1538415401
}
1538515402
vim_free(tofree);
15386-
str = echo_string(&li->li_tv, &tofree, strbuf, 0);
15403+
expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
1538715404
if (str == NULL)
1538815405
break;
1538915406
}
@@ -15420,7 +15437,23 @@ find_some_match(typval_T *argvars, typval_T *rettv, int type)
1542015437

1542115438
if (match)
1542215439
{
15423-
if (type == 3)
15440+
if (type == 4)
15441+
{
15442+
listitem_T *li1 = rettv->vval.v_list->lv_first;
15443+
listitem_T *li2 = li1->li_next;
15444+
listitem_T *li3 = li2->li_next;
15445+
listitem_T *li4 = li3->li_next;
15446+
15447+
li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15448+
(int)(regmatch.endp[0] - regmatch.startp[0]));
15449+
li3->li_tv.vval.v_number =
15450+
(varnumber_T)(regmatch.startp[0] - expr);
15451+
li4->li_tv.vval.v_number =
15452+
(varnumber_T)(regmatch.endp[0] - expr);
15453+
if (l != NULL)
15454+
li2->li_tv.vval.v_number = (varnumber_T)idx;
15455+
}
15456+
else if (type == 3)
1542415457
{
1542515458
int i;
1542615459

@@ -15465,6 +15498,11 @@ find_some_match(typval_T *argvars, typval_T *rettv, int type)
1546515498
vim_regfree(regmatch.regprog);
1546615499
}
1546715500

15501+
if (type == 4 && l == NULL)
15502+
/* matchstrpos() without a list: drop the second item. */
15503+
listitem_remove(rettv->vval.v_list,
15504+
rettv->vval.v_list->lv_first->li_next);
15505+
1546815506
theend:
1546915507
vim_free(tofree);
1547015508
p_cpo = save_cpo;
@@ -15665,6 +15703,15 @@ f_matchstr(typval_T *argvars, typval_T *rettv)
1566515703
find_some_match(argvars, rettv, 2);
1566615704
}
1566715705

15706+
/*
15707+
* "matchstrpos()" function
15708+
*/
15709+
static void
15710+
f_matchstrpos(typval_T *argvars, typval_T *rettv)
15711+
{
15712+
find_some_match(argvars, rettv, 4);
15713+
}
15714+
1566815715
static void max_min(typval_T *argvars, typval_T *rettv, int domax);
1566915716

1567015717
static void

src/testdir/test_alot.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ source test_glob2regpat.vim
1515
source test_help_tagjump.vim
1616
source test_join.vim
1717
source test_lispwords.vim
18+
source test_matchstrpos.vim
1819
source test_menu.vim
1920
source test_partial.vim
2021
source test_reltime.vim

src/testdir/test_matchstrpos.vim

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
" Test matchstrpos
2+
3+
func Test_matchstrpos()
4+
call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing'))
5+
6+
call assert_equal(['ing', 4, 7], matchstrpos('testing', 'ing', 2))
7+
8+
call assert_equal(['', -1, -1], matchstrpos('testing', 'ing', 5))
9+
10+
call assert_equal(['ing', 1, 4, 7], matchstrpos(['vim', 'testing', 'execute'], 'ing'))
11+
12+
call assert_equal(['', -1, -1, -1], matchstrpos(['vim', 'testing', 'execute'], 'img'))
13+
endfunc

src/version.c

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

749749
static int included_patches[] =
750750
{ /* Add new patch number below this line */
751+
/**/
752+
1685,
751753
/**/
752754
1684,
753755
/**/

0 commit comments

Comments
 (0)