GNU bug report logs -
#59203
[PATCH] add function string-split-substring
Previous Next
To reply to this bug, email your comments to 59203 AT debbugs.gnu.org.
Toggle the display of automated, internal messages from the tracker.
Report forwarded
to
bug-guile <at> gnu.org
:
bug#59203
; Package
guile
.
(Fri, 11 Nov 2022 21:07:02 GMT)
Full text and
rfc822 format available.
Acknowledgement sent
to
"Dr. Arne Babenhauserheide" <arne_bab <at> web.de>
:
New bug report received and forwarded. Copy sent to
bug-guile <at> gnu.org
.
(Fri, 11 Nov 2022 21:07:02 GMT)
Full text and
rfc822 format available.
Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):
[Message part 1 (text/plain, inline)]
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")
[0001-New-function-string-split-substring-in-ice-9-string-.patch (text/x-patch, inline)]
From d2901b5b8d91c9466fd37fc9d094fb6f0d4ea044 Mon Sep 17 00:00:00 2001
From: Arne Babenhauserheide <arne_bab <at> web.de>
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
[Message part 3 (text/plain, inline)]
Best wishes,
Arne
--
Unpolitisch sein
heißt politisch sein,
ohne es zu merken.
draketo.de
[signature.asc (application/pgp-signature, inline)]
This bug report was last modified 2 years and 47 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.