From 510a6056fa73b537481abca9b8027675b3d03f94 Mon Sep 17 00:00:00 2001 From: Munyoki Kilyungi Date: Mon, 27 Mar 2023 22:50:36 +0300 Subject: Split substring int a list of substrings delimited by substring * dump.scm (string-split-substring): New function. Signed-off-by: Munyoki Kilyungi --- dump.scm | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/dump.scm b/dump.scm index 1f0a262..9a8cc07 100755 --- a/dump.scm +++ b/dump.scm @@ -98,6 +98,27 @@ (string->symbol (format #f "~s~a" field schema)))) +(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)))) + (define (delete-substrings str . substrings) "Delete SUBSTRINGS, a list of strings, from STR." (fold (lambda (substring result) -- cgit v1.2.3