emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Make regexp handling more regular


From: Lars Ingebrigtsen
Subject: Re: Make regexp handling more regular
Date: Thu, 03 Dec 2020 09:38:21 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> Naming is, of course, the most difficult problem here.
>
> I agree that it might be worth looking at what other languages do.
> But we could also just follow "traditional regexp" libraries's
> suggestions for naming and go with something like:
>
>     (re-match  REGEXP &optional OBJECT START END)
>     (re-search REGEXP &optional OBJECT START END)
>
> [ the first being like `looking-at` (i.e. an "anchored" match).  ]

I like it.

Off-list, it's been pointed out that the current implementation of
functions like re-search-forward would be faster than these interfaces
because they produce less garbage -- since there's just one global match
object, it's static, while

(while (setq match (re-search "[a-z]+"))
  (bar (re-data match 0)))

would create a whole lot of garbage to be collected.  Now, that's not
really that much of an issue if you're just saying (when (re-match
"foo") ...) here and there, but searching through a buffer for matches
is a very common use case, and we wouldn't want that to be slower than
now.

So the suggestion is to be able to pass in an (optional) match object.
However, we don't really need to create that explicitly everywhere.  The
idiom could be just:

(while (setq match (re-search "[a-z]+" match))
  (bar (re-data match 0)))

In the first iteration, it's nil, which means that `re-search' will
allocate one, but in subsequent iterations, it'll be reused.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no



reply via email to

[Prev in Thread] Current Thread [Next in Thread]