unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: "Dr. Arne Babenhauserheide" <arne_bab@web.de>
To: 59203@debbugs.gnu.org
Subject: bug#59203: [PATCH] add function string-split-substring
Date: Fri, 11 Nov 2022 22:02:32 +0100	[thread overview]
Message-ID: <87k041nqv4.fsf@web.de> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 312 bytes --]

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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1.2: 0001-New-function-string-split-substring-in-ice-9-string-.patch --]
[-- Type: text/x-patch, Size: 3349 bytes --]

From d2901b5b8d91c9466fd37fc9d094fb6f0d4ea044 Mon Sep 17 00:00:00 2001
From: Arne Babenhauserheide <arne_bab@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)))))))))

+
+\f
+;;; {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


[-- Attachment #1.3: Type: text/plain, Size: 101 bytes --]


Best wishes,
Arne
-- 
Unpolitisch sein
heißt politisch sein,
ohne es zu merken.
draketo.de

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 1125 bytes --]

                 reply	other threads:[~2022-11-11 21:02 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=87k041nqv4.fsf@web.de \
    --to=arne_bab@web.de \
    --cc=59203@debbugs.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).