bug#59203: [PATCH] add function string-split-substring

"Dr. Arne Babenhauserheide" <[email protected]>
Newsgroups gmane.lisp.guile.bugs
Message-ID <[email protected]>
Hi,

this patch adds the function string-split-substring. The existing
string-split functions all only allow splitting by char or char_set, but
common file formats like graphviz need to be split by strings like " -> ".

Example usage:

(string-split-substring line "key -> value" " -> ")
;; => '("key" "value")


Best wishes,
Arne
-- 
Unpolitisch sein
heißt politisch sein,
ohne es zu merken.
draketo.de
0001-New-function-string-split-substring-in-ice-9-string-.patch (text/x-patch, 3.2 KB)
From d2901b5b8d91c9466fd37fc9d094fb6f0d4ea044 Mon Sep 17 00:00:00 2001
From: Arne Babenhauserheide <[email protected]>
Date: Fri, 11 Nov 2022 21:26:45 +0100
Subject: [PATCH] New function string-split-substring in (ice-9 string-fun)

* module/ice-9/string-fun.scm (string-split-substring): as stated.
* doc/ref/api-data.texi: Document the new function.
* test-suite/tests/strings.test: Test.
---
 doc/ref/api-data.texi         | 10 ++++++++++
 module/ice-9/string-fun.scm   | 29 ++++++++++++++++++++++++++++-
 test-suite/tests/strings.test |  5 ++++-
 3 files changed, 42 insertions(+), 2 deletions(-)

diff --git a/doc/ref/api-data.texi b/doc/ref/api-data.texi
index 8658b9785..fe93b2030 100644
--- a/doc/ref/api-data.texi
+++ b/doc/ref/api-data.texi
@@ -4245,6 +4245,16 @@ Return a new string where every instance of @var{substring} in string
 @end lisp
 @end deffn

+@deffn {Scheme Procedure} string-split-substring str substring
+Return a new list of strings after splitting the string @var{str}
+by @var{substring}. For example:
+
+@lisp
+(string-split-substring "a ring of strings" "ring")
+@result{} '("a " " of st" "s")
+@end lisp
+@end deffn
+
 @node Representing Strings as Bytes
 @subsubsection Representing Strings as Bytes

diff --git a/module/ice-9/string-fun.scm b/module/ice-9/string-fun.scm
index 592b49e20..049dc2394 100644
--- a/module/ice-9/string-fun.scm
+++ b/module/ice-9/string-fun.scm
@@ -26,7 +26,7 @@
 	   separate-fields-before-char string-prefix-predicate string-prefix=?
 	   sans-surrounding-whitespace sans-trailing-whitespace
 	   sans-leading-whitespace sans-final-newline has-trailing-newline?
-           string-replace-substring))
+           string-replace-substring string-split-substring))

 ;;;;
 ;;;
@@ -313,3 +313,30 @@
            (else
             (display (substring/shared str start)))))))))

+
+
+;;; {String Fun: string-split-substring}
+;;;
+
+;; string-split-substring By A. Babenhauserheide based on
+;; string-replace-substring
+
+(define (string-split-substring str substring)
+  "Return a new list of strings after splitting the string @var{str}
+   by @var{substring}. For example:
+
+   @lisp
+   (string-split-substring \"a ring of strings\" \"ring\")
+   @result{} '(\"a \" \" of st\" \"s\")
+   @end lisp
+   "
+  (if (equal? substring "")
+      (map string (string->list str)) ;; split each letter
+      (let ((sublen (string-length substring)))
+        (let lp ((start 0) (res '()))
+          (cond
+           ((string-contains str substring start)
+            => (lambda (end)
+                 (lp (+ end sublen) (cons (substring/shared str start end) res))))
+           (else
+            (reverse! (cons (substring/shared str start) res))))))))
diff --git a/test-suite/tests/strings.test b/test-suite/tests/strings.test
index 7393bc8ec..c73451efc 100644
--- a/test-suite/tests/strings.test
+++ b/test-suite/tests/strings.test
@@ -699,4 +699,7 @@

   (pass-if "string-replace-substring"
     (string=? (string-replace-substring "a ring of strings" "ring" "rut")
-              "a rut of struts")))
+              "a rut of struts"))
+  (pass-if "string-split-substring"
+    (equal? (string-split-substring "a ring of strings" "ring")
+            '("a " " of st" "s"))))
--
2.38.0
signature.asc (application/pgp-signature, 1.1 KB)
-----BEGIN PGP SIGNATURE-----

iQJEBAEBCAAuFiEE801qEjXQSQPNItXAE++NRSQDw+sFAmNuuUAQHGFybmVfYmFi
QHdlYi5kZQAKCRAT741FJAPD6w+2EACjETInD2pUhTDIXkmczLtrbJviA7K6zFNR
ob6LHBOqrAZGUGXx/JdiMBAi4m+NamLA2NUsxKVmH3Cr+hWLEI7Qdc3klkvJbEa9
hR4M8Tq7nWhZwR/r5shiEL/FZ0pHeka+MeL6/ktO5He5a0wgrwZYkRrn7p+ruri2
2VOQwNosPWKggPruFgrEw9IIQnHdZz1vlbLjicjEBrvBcquFMgzYnjOUwIRW449M
A/+Z4YFVwRWG1ZDbfhtKB3PMu3oE5dbmXrF7hu53kILW0S6lHd65NkogiTDZweYP
gSJ8mjI1vzGwCbc1qnP8WsMRw9gHxB1FqXC1ujLryNSZjbBTtwG+WtLUaZleCawu
qV3+gGIGZDPg8+g9nGVJyQDmlza/uAH7izESnXV+ZS9ArXNYLz5CBn9JXgB4LCqe
S6uCSn97aflW6KtbHiEZ3w6gd5wr9uu66hBRhvKYdDw6S4Xd7cVhG4YhSo8QmocN
KmwQIGRkOm2woQ7wLrs69oa4uwARMlyn2RAUUMzO3dtFwaIuoxriaLOj+KywTkMC
JcoNKufCGc8LhuEGGKos3ZWdW/rjfkR9NBPHGVHpkrh3x+Bksl5BP4JB/8IEKIfx
lM5XOXt6MpzcY0Mp9Stw2ZkBpsc4LOSiAHpnV/GZzvMsBLKIPmVRoJwPtnER7PBx
PdEkR+/X24jEBAEBCAAuFiEE3Si95tmHXKvOSosd3M8NswvBBUgFAmNuuUMQHGFy
bmVfYmFiQHdlYi5kZQAKCRDczw2zC8EFSMHjA/9nREtsWaoVArIO0rdO/AFxFV1u
rJgedEdxOA6o27n0AcNq9akpMwrNQdnigo1VKXPjy98MXRaNx7JftUMdtn3A8X1a
F2F6zAiA75m8Ov9tRkrbcudkPhgn5vV1gIdBKvpmf3PaViTGkIPdaVUVy/yYqCM2
+VgG3ZeohzYRAbDixg==
=sWeQ
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.