From 27aa85d56766d152eced21cd0d2915c70a99dcc7 Mon Sep 17 00:00:00 2001 From: NalaGinrut Date: Fri, 30 Dec 2011 19:46:01 +0800 Subject: [PATCH] ADD regexp-split --- module/ice-9/regex.scm | 19 ++++++++++++++++++- 1 files changed, 18 insertions(+), 1 deletions(-) diff --git a/module/ice-9/regex.scm b/module/ice-9/regex.scm index f7b94b7..e9b01ea 100644 --- a/module/ice-9/regex.scm +++ b/module/ice-9/regex.scm @@ -38,10 +38,11 @@ ;;;; POSIX regex support functions. (define-module (ice-9 regex) + #:autoload (srfi srfi-1) (iota) #:export (match:count match:string match:prefix match:suffix regexp-match? regexp-quote match:start match:end match:substring string-match regexp-substitute fold-matches list-matches - regexp-substitute/global)) + regexp-substitute/global regexp-split)) ;; References: ;; @@ -226,3 +227,19 @@ (begin (do-item (car items)) ; This is not. (next-item (cdr items))))))))))) + +(define* (regexp-split regex str #:optional (flags 0)) + (let ((ret (fold-matches + regex str (list '() 0 '("")) + (lambda (m prev) + (let* ((ll (car prev)) + (start (cadr prev)) + (tail (match:suffix m)) + (end (match:start m)) + (s (substring/shared str start end)) + (groups (map (lambda (n) (match:substring m n)) + (iota (1- (match:count m)))))) + (list `(,@ll ,s ,@groups) (match:end m) tail))) + flags))) + `(,@(car ret) ,(caddr ret)))) + -- 1.7.0.4