|
From: | Stefan Monnier |
Subject: | Re: A function to take the regexp-matched subsring directly |
Date: | Sun, 30 Oct 2022 11:52:19 -0400 |
User-agent: | Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) |
> +;;;###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. So I'd much rather not save it here. > + (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
[Prev in Thread] | Current Thread | [Next in Thread] |