From unknown Mon Aug 18 09:02:10 2025 X-Loop: help-debbugs@gnu.org Subject: bug#54143: [PATCH] Add string-split-substring to ice-9 string-fun Resent-From: Vijay Marupudi Original-Sender: "Debbugs-submit" Resent-CC: bug-guile@gnu.org Resent-Date: Thu, 24 Feb 2022 15:09:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 54143 X-GNU-PR-Package: guile X-GNU-PR-Keywords: patch To: 54143@debbugs.gnu.org X-Debbugs-Original-To: bug-guile@gnu.org Received: via spool by submit@debbugs.gnu.org id=B.16457152931475 (code B ref -1); Thu, 24 Feb 2022 15:09:03 +0000 Received: (at submit) by debbugs.gnu.org; 24 Feb 2022 15:08:13 +0000 Received: from localhost ([127.0.0.1]:50114 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nNFj1-0000Na-2v for submit@debbugs.gnu.org; Thu, 24 Feb 2022 10:08:12 -0500 Received: from lists.gnu.org ([209.51.188.17]:53954) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nNEQW-0003sG-HU for submit@debbugs.gnu.org; Thu, 24 Feb 2022 08:45:00 -0500 Received: from eggs.gnu.org ([209.51.188.92]:36100) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nNEQV-0005xO-TQ for bug-guile@gnu.org; Thu, 24 Feb 2022 08:45:00 -0500 Received: from [2a0c:5a00:149::25] (port=54142 helo=mailtransmit04.runbox.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nNEQT-0002nb-Aa for bug-guile@gnu.org; Thu, 24 Feb 2022 08:44:59 -0500 Received: from mailtransmit02.runbox ([10.9.9.162] helo=aibo.runbox.com) by mailtransmit04.runbox.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.93) (envelope-from ) id 1nNEQN-00EXAx-C0 for bug-guile@gnu.org; Thu, 24 Feb 2022 14:44:51 +0100 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=vijaymarupudi.com; s=selector1; h=Content-Type:MIME-Version:Message-ID:Date :Subject:To:From; bh=LkUuO4TlSvHoh8lZmPsxT2S8Lu3j4zHK8GRlK3zzXVU=; b=JIJchVTj QXvYI4BH5sYoOWprG1nmZ4JY8xLAl49+/k8m5fA1tMwWCRL0RaQyrKrGxeApDNAy5ywPzj6v4i6iH oQ3jL6aH7I5mGghopMUOfOgzbXfQ/9nWvM2NILeQ/aeyJR6wb2HLLn/em+1sZnHrwJlYMicP7F6c8 qcVJXfv4StztJM9YtLh7F+S79VogbaRix0X2wQgfZv2Ef7fRcShtiIz76UwHWuiZSy+uHwgVUTceO k2f0Jv+iaeCulplPHQVOQWuGodZLymnkFrJoXaVlst/Myh09o4xU0Quke7dfDNRhxAEZ7kG8YuY7N QCfS4PTPCQAoktTcvcUJYiUqZw==; Received: from [10.9.9.72] (helo=submission01.runbox) by mailtransmit02.runbox with esmtp (Exim 4.86_2) (envelope-from ) id 1nNEQM-0006W1-UZ for bug-guile@gnu.org; Thu, 24 Feb 2022 14:44:51 +0100 Received: by submission01.runbox with esmtpsa [Authenticated ID (1028486)] (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) id 1nNEQ3-0003cy-ED for bug-guile@gnu.org; Thu, 24 Feb 2022 14:44:31 +0100 From: Vijay Marupudi Date: Thu, 24 Feb 2022 08:44:29 -0500 Message-ID: <87zgmg4bwi.fsf@vijaymarupudi.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a0c:5a00:149::25 (failed) Received-SPF: pass client-ip=2a0c:5a00:149::25; envelope-from=vijay@vijaymarupudi.com; helo=mailtransmit04.runbox.com X-Spam_score_int: -19 X-Spam_score: -2.0 X-Spam_bar: -- X-Spam_report: (-2.0 / 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, RCVD_IN_DNSWL_LOW=-0.7, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no autolearn_force=no X-Spam_action: no action X-Spam-Score: -1.4 (-) X-Mailman-Approved-At: Thu, 24 Feb 2022 10:08:05 -0500 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -2.4 (--) --=-=-= Content-Type: text/plain Hello, I've attached a patch adding a new function, (string-split-substring str substr), that splits a string into a list of multiple strings delimited by a substring. The behavior matches (string-split str char/pred) exactly. ~ Vijay --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Add-string-split-substring.patch >From 3c0940082ff49695ac9c2147c900b959be5f8e70 Mon Sep 17 00:00:00 2001 From: Vijay Marupudi Date: Sat, 12 Feb 2022 22:00:57 -0500 Subject: [PATCH] Add string-split-substring * /ref/api-data.texi: Added documentation * module/ice-9/string-fun.scm: Added implementation * test-suite/tests/strings.test: Added tests --- doc/ref/api-data.texi | 10 ++++++++++ module/ice-9/string-fun.scm | 23 ++++++++++++++++++++++- test-suite/tests/strings.test | 22 +++++++++++++++++++++- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/doc/ref/api-data.texi b/doc/ref/api-data.texi index 8658b9785..a5fcc47b1 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 +Split the string @var{str} into a list of substrings delimited by the +appearance of substring @var{substring}. For example: + +@lisp +(string-replace-substring "item-1::item-2::item-3::item-4" "::") +@result{} ("item-1" "item-2" "item-3" "item-4") +@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 03e0238fa..a1d4c0366 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,24 @@ (else (display (substring/shared str start))))))))) +(define (string-split-substring str substr) + "Split the string @var{str} into a list of substrings delimited by the +substring @var{substr}." + + (define substrlen (string-length substr)) + (define strlen (string-length str)) + + (define (loop index start) + (cond + ((>= start strlen) (list "")) + ((not index) (list (substring str start))) + (else + (cons (substring str start index) + (let ((new-start (+ index substrlen))) + (loop (string-contains str substr new-start) + new-start)))))) + + (cond + ((string-contains str substr) => (lambda (idx) (loop idx 0))) + (else (list str)))) + diff --git a/test-suite/tests/strings.test b/test-suite/tests/strings.test index 7393bc8ec..8bc26e3e3 100644 --- a/test-suite/tests/strings.test +++ b/test-suite/tests/strings.test @@ -699,4 +699,24 @@ (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 - empty string" + (equal? (string-split-substring "" "foo") + '(""))) + + (pass-if "string-split-substring - non-empty, no delimiters" + (equal? (string-split-substring "testing" "foo") + '("testing"))) + + (pass-if "string-split-substring - non-empty, delimiters" + (equal? (string-split-substring "testingfoobar" "foo") + '("testing" "bar"))) + + (pass-if "string-split-substring - non-empty, leading delimiters" + (equal? (string-split-substring "foobar" "foo") + '("" "bar"))) + + (pass-if "string-split-substring - non-empty, trailing delimiters" + (equal? (string-split-substring "barfoo" "foo") + (list "bar" "")))) -- 2.35.1 --=-=-=--