From: Philip Kaludercic <philipk@posteo.net>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: daanturo <daanturo@gmail.com>, emacs-devel@gnu.org
Subject: Re: A function to take the regexp-matched subsring directly
Date: Sun, 30 Oct 2022 17:29:21 +0000 [thread overview]
Message-ID: <87h6zl9q4u.fsf@posteo.net> (raw)
In-Reply-To: <jwvr0yp2tz5.fsf-monnier+emacs@gnu.org> (Stefan Monnier's message of "Sun, 30 Oct 2022 11:52:19 -0400")
Stefan Monnier <monnier@iro.umontreal.ca> writes:
>> +;;;###autoload
>> +(defun regexp-match (regexp string &optional n)
>> + "Return the N -th matched substring for REGEXP in STRING.
>> +N defaults to 0 (the whole match).
>> +
>> +This function does not change the match data."
>> + (declare (pure t) (side-effect-free t))
>> + (let ((n (or n 0)))
>> + (save-match-data
>> + (when (string-match regexp string)
>> + (match-string n string)))))
>
> `save-match-data` is costly and extremely rarely needed.
What makes it so expensive? The implementation appears to be trivial.
> So I'd much rather not save it here.
If the function is supposed to be side-effect-free, then it ought not to
sometimes replace the match data and not touch it when optimised away.
>> + (save-match-data
>> + (when (string-match regexp string)
>> + (let ((match-index (1- (/ (length (match-data)) 2)))
>> + matches)
>> + (while (<= 0 match-index)
>> + (push (match-string match-index string) matches)
>> + (setq match-index (1- match-index)))
>> + matches))))
>
> I suspect it'd be more efficient to iterate directly on the `match-data` rather
> than on an integer (which suffers from an O(N²) complexity).
>
>
> Stefan
next prev parent reply other threads:[~2022-10-30 17:29 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-30 15:17 A function to take the regexp-matched subsring directly daanturo
2022-10-30 15:45 ` Philip Kaludercic
2022-10-30 16:46 ` daanturo
2022-10-30 17:26 ` Philip Kaludercic
2022-10-30 15:52 ` Stefan Monnier
2022-10-30 17:16 ` daanturo
2022-10-30 22:01 ` Stefan Monnier
2022-10-31 3:47 ` daanturo
2022-10-30 17:29 ` Philip Kaludercic [this message]
2022-10-30 22:07 ` Stefan Monnier
2022-10-31 8:56 ` Mattias Engdegård
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87h6zl9q4u.fsf@posteo.net \
--to=philipk@posteo.net \
--cc=daanturo@gmail.com \
--cc=emacs-devel@gnu.org \
--cc=monnier@iro.umontreal.ca \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).