unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: lloda <lloda@sarc.name>
To: guile-devel <guile-devel@gnu.org>
Subject: [PATCH] Add string-replace-substring to (ice-9 string-fun)
Date: Fri, 20 Dec 2019 18:32:22 +0100	[thread overview]
Message-ID: <27656CBA-297B-4EE3-93D6-FD56BFBDDC44@sarc.name> (raw)

[-- Attachment #1: Type: text/plain, Size: 214 bytes --]


This patch adds string-replace-substring that wingo posted to the mailing list to (ice-9 strings). This is a commonly used function and a good implementation isn't trivial, so I think it deserves inclusion.



[-- Attachment #2: 0001-Add-string-replace-substring-to-ice-9-string-fun.patch --]
[-- Type: application/octet-stream, Size: 1790 bytes --]

From cf5b4a1db8ad4c1060f08f07113791dd34374266 Mon Sep 17 00:00:00 2001
From: Daniel Llorens <lloda@sarc.name>
Date: Fri, 20 Dec 2019 12:53:34 +0100
Subject: [PATCH 1/3] Add string-replace-substring to (ice-9 string-fun)

---
 module/ice-9/string-fun.scm | 25 ++++++++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/module/ice-9/string-fun.scm b/module/ice-9/string-fun.scm
index c27ff847f..bef841db5 100644
--- a/module/ice-9/string-fun.scm
+++ b/module/ice-9/string-fun.scm
@@ -25,7 +25,8 @@
 	   separate-fields-discarding-char separate-fields-after-char
 	   separate-fields-before-char string-prefix-predicate string-prefix=?
 	   sans-surrounding-whitespace sans-trailing-whitespace
-	   sans-leading-whitespace sans-final-newline has-trailing-newline?))
+	   sans-leading-whitespace sans-final-newline has-trailing-newline?
+           string-replace-substring))
 
 ;;;;
 ;;;
@@ -278,3 +279,25 @@
 ;;;         (fail parts)
 ;;;         (apply return parts))))
 
+
+\f
+;;; {String Fun: string-replace-substring}
+;;;
+
+;; By A. Wingo in https://lists.gnu.org/archive/html/guile-devel/2014-03/msg00058.html
+
+(define (string-replace-substring s substring replacement)
+  "Replace every instance of @var{substring} in string @var{s} by @var{replacement}."
+  (let ((sublen (string-length substring)))
+    (with-output-to-string
+      (lambda ()
+        (let lp ((start 0))
+          (cond
+           ((string-contains s substring start)
+            => (lambda (end)
+                 (display (substring/shared s start end))
+                 (display replacement)
+                 (lp (+ end sublen))))
+           (else
+            (display (substring/shared s start)))))))))
+
-- 
2.20.1


             reply	other threads:[~2019-12-20 17:32 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-20 17:32 lloda [this message]
2020-01-05 11:34 ` [PATCH] Add string-replace-substring to (ice-9 string-fun) Andy Wingo
2020-01-05 12:12   ` Jan Nieuwenhuizen
2020-01-05 13:51     ` Christopher Lam
2020-01-05 15:49       ` Arne Babenhauserheide
2020-01-05 14:01     ` lloda
2020-01-05 23:40       ` David Pirotte
2020-01-06  9:08 ` Linus Björnstam

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/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=27656CBA-297B-4EE3-93D6-FD56BFBDDC44@sarc.name \
    --to=lloda@sarc.name \
    --cc=guile-devel@gnu.org \
    /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.
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).