[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: A function to take the regexp-matched subsring directly
From: |
daanturo |
Subject: |
Re: A function to take the regexp-matched subsring directly |
Date: |
Mon, 31 Oct 2022 00:16:03 +0700 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.1 |
> 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).
Got it. By using `substring` directly, it looks like this:
```emacs-lisp
(when (string-match regexp string)
(let ((matched-data (match-data))
matches beg end)
(while matched-data
(setq beg (pop matched-data))
(setq end (pop matched-data))
(push (and beg end
(substring string beg end))
matches))
(nreverse matches)))
```
> `save-match-data` is costly and extremely rarely needed.
I committed a change that now makes inhibit-modify optional (though `(declare
(pure t) (side-effect-free t))` is lost in the process).
Although I think intuitively, when running those kind of functions, we naturally
expect them not to cause any side-effects from a high-level perspective so
`save-match-data` should be the default.
--
Daanturo.
regexp-match.patch
Description: Text Data