unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* [PATCH] Add string-replace-substring to (ice-9 string-fun)
@ 2019-12-20 17:32 lloda
  2020-01-05 11:34 ` Andy Wingo
  2020-01-06  9:08 ` Linus Björnstam
  0 siblings, 2 replies; 8+ messages in thread
From: lloda @ 2019-12-20 17:32 UTC (permalink / raw)
  To: guile-devel

[-- 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


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2020-01-06  9:08 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-12-20 17:32 [PATCH] Add string-replace-substring to (ice-9 string-fun) lloda
2020-01-05 11:34 ` 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

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).