From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: "Dr. Arne Babenhauserheide" Newsgroups: gmane.lisp.guile.bugs Subject: bug#59203: [PATCH] add function string-split-substring Date: Fri, 11 Nov 2022 22:02:32 +0100 Message-ID: <87k041nqv4.fsf@web.de> Mime-Version: 1.0 Content-Type: multipart/signed; boundary="==-=-="; micalg=pgp-sha256; protocol="application/pgp-signature" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="10146"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.8.11; emacs 28.1 To: 59203@debbugs.gnu.org Original-X-From: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Fri Nov 11 22:07:54 2022 Return-path: Envelope-to: guile-bugs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1otbFf-0002C5-9k for guile-bugs@m.gmane-mx.org; Fri, 11 Nov 2022 22:07:51 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1otbEv-0001eB-7V; Fri, 11 Nov 2022 16:07:05 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1otbEt-0001do-Dh for bug-guile@gnu.org; Fri, 11 Nov 2022 16:07:03 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1otbEt-0006gW-3A for bug-guile@gnu.org; Fri, 11 Nov 2022 16:07:03 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1otbEs-00021S-Jg for bug-guile@gnu.org; Fri, 11 Nov 2022 16:07:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: "Dr. Arne Babenhauserheide" Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Fri, 11 Nov 2022 21:07:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 59203 X-GNU-PR-Package: guile X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-guile@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.16682007817724 (code B ref -1); Fri, 11 Nov 2022 21:07:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 11 Nov 2022 21:06:21 +0000 Original-Received: from localhost ([127.0.0.1]:46848 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1otbEC-00020W-T7 for submit@debbugs.gnu.org; Fri, 11 Nov 2022 16:06:21 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:38086) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1otbEB-00020N-8g for submit@debbugs.gnu.org; Fri, 11 Nov 2022 16:06:20 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1otbEB-0001cS-3t for bug-guile@gnu.org; Fri, 11 Nov 2022 16:06:19 -0500 Original-Received: from mout.web.de ([212.227.15.4]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1otbE9-0006cc-8w for bug-guile@gnu.org; Fri, 11 Nov 2022 16:06:18 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=web.de; s=s29768273; t=1668200773; bh=uSGIDoragn8gXYNo0OvG1kTGMy83GVbn59vTwq9Dodg=; h=X-UI-Sender-Class:From:To:Subject:Date; b=P02dPuzgFq5zQy/PcOTkWPPLt8v3exH/90vaBKZYObxSET10poRZ+XDU+rkZBAWTQ 33VVIaNgDVQ9r26YbngrIqaGiEXoc56k4gUriCoMAdjE4OeV8si9PkZUgMrIY4pylP N9laXql71mbp/ebQ4sDl4oTxpZ7w96o2e6s4F+9jz9TarI+ljJt1Um1117W0v7vj2E rIXX+ONTEE+NgDtB83sEYIYW8clmqV9SO3Sx1U+qGj1Ckup8cLMxK4WLtf3IsrjUy2 fR3Hm5BdJIFxmaW+PeUu6mEQgm1/tp343LiGpAvemA0qUXX9w8JDtyRf1oAxCoH+jB 31tZdXPcPPUQw== X-UI-Sender-Class: 814a7b36-bfc1-4dae-8640-3722d8ec6cd6 Original-Received: from fluss ([84.165.20.127]) by smtp.web.de (mrweb005 [213.165.67.108]) with ESMTPSA (Nemesis) id 1MxpiU-1pBf113E34-00zFTT; Fri, 11 Nov 2022 22:06:13 +0100 X-Provags-ID: V03:K1:oIMBRuw7B2OIvWd8L7imr7VNvBgW4ZIuAwL0IqspliX0dTTjJf7 GYrS0Xiun6T4qiAVcVHaZKg4/+RbI5t6x1iYeQm9Du3BpGEcBwyqe+sIfMa357RS+goxlz0 UY6+Vk2Yc6Z5T7T/hgDb8FLWGpfyY/TI/a+2shMOsODle4R5hngo5bZIczbpbHKPOabfz7d Y6Q27TCbTuCfEE8SAQ5Qg== UI-OutboundReport: notjunk:1;M01:P0:ry2Il9WlYbA=;6hDTMyxbDgLiAY0WSILKx6+EJ5r WFUE/CZeZ9XIy5cppLHHGhV7N15sFmOWVIBX652y9E9b5IoTte4lzwZd+2jhzo58waZvAk1Od ny30i2rqiFfGEstbfl3mjre2+kaCqSM794eixCdT6Hxd3O/CKUMDjKB2AoRw5bqZr/VhGbhNN T5kAp+y9c8cpkXz2ADLIyHfyFYI4gutw3kcq9NqBHPTorgdj/Prt0cT9p+taWFr1tismwFgpp KoRJqJ8htuymoOlSTvS2T6Cj5U/qmsp0mCWN4rUu8g+uLa+gnw5xRAgg71EYrJhWbPdzGwcID oB9pct37deblSULJq2x6rsj26px02FXdgBX6Yg3Ev32CLMiledbTGqtwAqyri9qwCCQKFG+/F ibdtDZCT63rmte65vUPaktJsQU/8Wnr0F75qizgeVCunZ0FeXXimCccO0DHPV6dfqaQ79cNIn WoWXvivtubaGdv3wyBYAlGDJD7YQdZH4qjzw1wiLm9bFOgxC7d++pMCR7PMfdawT018aUOhP7 hJEm0nVOfoJCx4KVu+C42OKDPL3SRyp7uDaco8Y+acBT6yjcyArA0OtmxLddPNLUU1uPPISse tvGe7IUpSlj5hXRwll5kcmwNMb/QlqWRSfUrbYvvJLSOZ5cf8aec4BFBRqVaO+bGB2yNkM0o5 m/NMXfH+SX+panI5nhNc7Aelwi+R8fCo0ENRNHdyAkub69Zbekt2wCdynLScKSB8QQoqRGgd0 hRkTuTHA95wI9llvb0TFSAb1VlL/1KSUeMNJlJlcwx+f6dFD2xkn+aceHZwuEpdAEWX0WjLQ Received-SPF: pass client-ip=212.227.15.4; envelope-from=arne_bab@web.de; helo=mout.web.de X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-guile@gnu.org List-Id: "Bug reports for GUILE, GNU's Ubiquitous Extension Language" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Original-Sender: bug-guile-bounces+guile-bugs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.lisp.guile.bugs:10423 Archived-At: --==-=-= Content-Type: multipart/mixed; boundary="=-=-=" --=-=-= Content-Type: text/plain 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") --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-New-function-string-split-substring-in-ice-9-string-.patch Content-Transfer-Encoding: quoted-printable =46rom d2901b5b8d91c9466fd37fc9d094fb6f0d4ea044 Mon Sep 17 00:00:00 2001 From: Arne Babenhauserheide 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. =2D-- 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 =2D-- 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{su= bstring} 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 =2D-- 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=3D? 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))))))))) + +=0C +;;; {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) + =3D> (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 =2D-- a/test-suite/tests/strings.test +++ b/test-suite/tests/strings.test @@ -699,4 +699,7 @@ (pass-if "string-replace-substring" (string=3D? (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")))) =2D- 2.38.0 --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Best wishes, Arne =2D-=20 Unpolitisch sein hei=C3=9Ft politisch sein, ohne es zu merken. draketo.de --=-=-=-- --==-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQJEBAEBCAAuFiEE801qEjXQSQPNItXAE++NRSQDw+sFAmNuuUAQHGFybmVfYmFi QHdlYi5kZQAKCRAT741FJAPD6w+2EACjETInD2pUhTDIXkmczLtrbJviA7K6zFNR ob6LHBOqrAZGUGXx/JdiMBAi4m+NamLA2NUsxKVmH3Cr+hWLEI7Qdc3klkvJbEa9 hR4M8Tq7nWhZwR/r5shiEL/FZ0pHeka+MeL6/ktO5He5a0wgrwZYkRrn7p+ruri2 2VOQwNosP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