submatch() function returns NULL when used outside of substitute

14 views
Skip to first unread message

Yegappan Lakshmanan

unread,
Apr 9, 2020, 11:32:21 AM4/9/20
to vim_dev
Hi all,

The submatch() function with a second argument set to 1, returns NULL
when used outside of a :substitute command or the substitute() function.
All the other functions that return a List, return an empty List on failure.
But the submatch() function returns NULL on failure. e.g.

      call assert_equal([], submatch(0, 1))
      echo v:errors

The returned value cannot be compared against NULL. I think the submatch()
function should be changed to return an empty List.

- Yegappan

Tony Mechelynck

unread,
Apr 9, 2020, 12:03:23 PM4/9/20
to vim_dev
submatch() is not meant to be used outside of a substitute, it's documented:

submatch({nr} [, {list}]) *submatch()* *E935*
Only for an expression in a |:substitute| command or
substitute() function.

IMHO when used outside if a :substitute command or of a substitute()
function, submatch() should generate an error. (What is the en_US text
of error E935?)

Best regards,
Tony.

Yegappan Lakshmanan

unread,
Apr 9, 2020, 12:18:16 PM4/9/20
to vim_dev
Hi Tony,

On Thu, Apr 9, 2020 at 9:03 AM Tony Mechelynck <antoine.m...@gmail.com> wrote:
On Thu, Apr 9, 2020 at 5:32 PM Yegappan Lakshmanan <yega...@gmail.com> wrote:
>
> Hi all,
>
> The submatch() function with a second argument set to 1, returns NULL
> when used outside of a :substitute command or the substitute() function.
> All the other functions that return a List, return an empty List on failure.
> But the submatch() function returns NULL on failure. e.g.
>
>       call assert_equal([], submatch(0, 1))
>       echo v:errors
>
> The returned value cannot be compared against NULL. I think the submatch()
> function should be changed to return an empty List.
>
> - Yegappan

submatch() is not meant to be used outside of a substitute, it's documented:


Yes. I am developing a test case to verify that submatch() fails when called
outside of a substitute. To do this, usually the returned value is compared
against an empty string or an empty list or an error code.
In this case, the submatch() function is returning NULL. So it is not possible
to check the returned value.

Regards,
Yegappan

Bram Moolenaar

unread,
Apr 9, 2020, 3:33:57 PM4/9/20
to vim...@googlegroups.com, Yegappan Lakshmanan

> > > The submatch() function with a second argument set to 1, returns NULL
> > > when used outside of a :substitute command or the substitute() function.
> > > All the other functions that return a List, return an empty List on
> > failure.
> > > But the submatch() function returns NULL on failure. e.g.
> > >
> > > call assert_equal([], submatch(0, 1))
> > > echo v:errors
> > >
> > > The returned value cannot be compared against NULL. I think the
> > submatch()
> > > function should be changed to return an empty List.
> > >
> > > - Yegappan
> >
> > submatch() is not meant to be used outside of a substitute, it's
> > documented:
> >
>
>
> Yes. I am developing a test case to verify that submatch() fails when called
> outside of a substitute. To do this, usually the returned value is compared
> against an empty string or an empty list or an error code.
> In this case, the submatch() function is returning NULL. So it is not
> possible to check the returned value.

A NULL list means the same as an empty list, but assert_equal() is
picky. You can compare with test_null_list().

Hmm, it then says that NULL is not equal NULL. That looks like a bug.
I think we should consider a NULL list equal to another NULL list.
And most likely also equal to an empty list, since the user can't tell
the difference.

--
LETTERS TO THE EDITOR (The Times of London)

Dear Sir,

I am firmly opposed to the spread of microchips either to the home or
to the office.  We have more than enough of them foisted upon us in
public places.  They are a disgusting Americanism, and can only result
in the farmers being forced to grow smaller potatoes, which in turn
will cause massive unemployment in the already severely depressed
agricultural industry.

Yours faithfully,
        Capt. Quinton D'Arcy, J. P.
        Sevenoaks

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///
Reply all
Reply to author
Forward
0 new messages