* elisp find/replace efficiency question
@ 2010-10-08 19:58 Xah Lee
2010-10-09 0:22 ` Stefan Monnier
0 siblings, 1 reply; 2+ messages in thread
From: Xah Lee @ 2010-10-08 19:58 UTC (permalink / raw)
To: help-gnu-emacs
suppose i want write a command that do a find/replace on a region
which is more efficient?
(defun space2underscore-region (p1 p2)
"Replace space by underscore in region."
(interactive "r")
(save-restriction
(narrow-to-region p1 p2)
(goto-char (point-min))
(while (search-forward " " nil t) (replace-match "_")) ) )
vs
...
(setq meat (replace-regexp-in-string " " "_"
(buffer-substring-no-properties p1 p2)) )
(delete-region p1 p2)
(insert meat)
vs
(replace-string " " "_" nil p1 p2)
it appears to me, the first is most efficient but is most cumbersome.
The last is most lean, but least efficient.
(sorry if this is a duplicate post)
Xah ∑ xahlee.org ☄
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: elisp find/replace efficiency question
2010-10-08 19:58 elisp find/replace efficiency question Xah Lee
@ 2010-10-09 0:22 ` Stefan Monnier
0 siblings, 0 replies; 2+ messages in thread
From: Stefan Monnier @ 2010-10-09 0:22 UTC (permalink / raw)
To: help-gnu-emacs
>>>>> "Xah" == Xah Lee <xahlee@gmail.com> writes:
> suppose i want write a command that do a find/replace on a region
> which is more efficient?
Based on the suggestion of C-h f replace-regexp RET I'd say:
(defun space2underscore-region (p1 p2)
"Replace space by underscore in region."
(interactive "r")
(setq p2 (copy-marker p2 t))
(goto-char p1)
(while (search-forward " " p2 t) (replace-match "_")))
-- Stefan
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2010-10-09 0:22 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-10-08 19:58 elisp find/replace efficiency question Xah Lee
2010-10-09 0:22 ` Stefan Monnier
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).