unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Tino Calancha <tino.calancha@gmail.com>
To: Oleh Krehel <oleh@oremacs.com>
Cc: tino.calancha@gmail.com, Emacs developers <emacs-devel@gnu.org>
Subject: Re: Latest commit to dired-aux; maybe add string-multi-replace?
Date: Mon, 29 Aug 2016 21:16:49 +0900 (JST)	[thread overview]
Message-ID: <alpine.DEB.2.20.1608292112190.8833@calancha-pc> (raw)
In-Reply-To: <87poosj75w.fsf@oremacs.com>



On Mon, 29 Aug 2016, Oleh Krehel wrote:

> In which file would the new function belong?
I guess this should go to subr.el (or subr-x.el).
In that case you should use:
(car (cddr pat))
instead of
(caddr pat)
> Any further comments?
I would mention in the doc string that the returned string is
actually a copy of STR.
I would also mention the order of the replacements: only in case this
doesn't result obvious, but i guess it should be obvious.

How about passing the patterns just as a list of strings as follows?:

(defun string-multi-replace (str &rest patterns)
   "Replace PATTERNS in STR.
Return a new string containing the replacements.
PATTERNS is a list of patterns (FROM TO LITERAL ...).
Replace patterns calling `replace-regexp-in-string'
sequencially starting from (car PATTERNS)."
   (let (pat from to literal)
     (unless (zerop (% (length patterns) 3))
       (error "Length of patterns should be a multiple of 3"))
       (while (setq pat patterns)
         (setq from     (car pat)
               to       (cadr pat)
               literal  (car (cddr pat))
               str      (replace-regexp-in-string from to str nil literal)
               patterns (nthcdr 3 patterns)))
       str))

  (string-multi-replace
   "tar -c %i | xz -c9 > %o"
   "%o" (shell-quote-argument "foo bar.tar.gz") t
   "%i" (shell-quote-argument "foo bar") t)
;; => "tar -c foo\\ bar | xz -c9 > foo\\ bar.tar.gz"



      parent reply	other threads:[~2016-08-29 12:16 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-29  8:44 Latest commit to dired-aux; maybe add string-multi-replace? Oleh Krehel
2016-08-29 11:52 ` Noam Postavsky
2016-08-29 12:04   ` Oleh Krehel
2016-08-29 12:16 ` Tino Calancha [this message]

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=alpine.DEB.2.20.1608292112190.8833@calancha-pc \
    --to=tino.calancha@gmail.com \
    --cc=emacs-devel@gnu.org \
    --cc=oleh@oremacs.com \
    /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).