diff --git a/lib/chibi/regexp-test.sld b/lib/chibi/regexp-test.sld index 4cec9297..303b7c9e 100644 --- a/lib/chibi/regexp-test.sld +++ b/lib/chibi/regexp-test.sld @@ -208,11 +208,19 @@ (test '("123" "456" "789") (regexp-extract '(+ digit) "abc123def456ghi789")) (test '("123" "456" "789") (regexp-extract '(* digit) "abc123def456ghi789")) (test '("abc" "def" "ghi" "") (regexp-split '(+ digit) "abc123def456ghi789")) - ;; (test '("a" "b" "c" "d" "e" "f" "g" "h" "i") - ;; (regexp-split '(* digit) "abc123def456ghi789")) + (test '("abc" "def" "ghi" "") + (regexp-split '(* digit) "abc123def456ghi789")) (test '("a" "b") (regexp-split '(+ whitespace) "a b")) + (test '("a" "" "b") + (regexp-split '(",;") "a,,b")) (test '("a" "" "b" "") (regexp-split '(",;") "a,,b,")) + (test '("") + (regexp-partition '(* digit) "")) + (test '("abc" "123" "def" "456" "ghi") + (regexp-partition '(* digit) "abc123def456ghi")) + (test '("abc" "123" "def" "456" "ghi" "789") + (regexp-partition '(* digit) "abc123def456ghi789")) (test '("한" "글") (regexp-extract diff --git a/lib/chibi/regexp.scm b/lib/chibi/regexp.scm index 3d56955e..24ad516a 100644 --- a/lib/chibi/regexp.scm +++ b/lib/chibi/regexp.scm @@ -1033,12 +1033,16 @@ (regexp-fold rx (lambda (from md str a) - (let ((i (regexp-match-submatch-start md 0))) - (if (eqv? i 0) a (cons (substring str from i) a)))) - '() + (let ((i (regexp-match-submatch-start md 0)) + (j (regexp-match-submatch-end md 0))) + (if (eqv? i j) + a + (cons j + (cons (substring str (car a) i) (cdr a)))))) + (cons start '()) str (lambda (from md str a) - (reverse (cons (substring str from end) a))) + (reverse (cons (substring str (car a) end) (cdr a)))) start end))) @@ -1057,13 +1061,19 @@ (let ((start (if (pair? o) (car o) 0)) (end (if (and (pair? o) (pair? (cdr o))) (cadr o) (string-length str)))) (define (kons from md str a) - (let ((left (substring str from (regexp-match-submatch-start md 0)))) - (cons (regexp-match-submatch md 0) (cons left a)))) + (let ((i (regexp-match-submatch-start md 0)) + (j (regexp-match-submatch-end md 0))) + (if (eqv? i j) + a + (let ((left (substring str (car a) i))) + (cons j + (cons (regexp-match-submatch md 0) + (cons left (cdr a)))))))) (define (final from md str a) - (if (or (< from end) (null? a)) - (cons (substring str from end) a) - a)) - (reverse (regexp-fold rx kons '() str final start end)))) + (if (or (< from end) (null? (cdr a))) + (cons (substring str (car a) end) (cdr a)) + (cdr a))) + (reverse (regexp-fold rx kons (cons start '()) str final start end)))) ;;> Returns a new string replacing the \var{count}th match of \var{re} ;;> in \var{str} the \var{subst}, where the zero-indexed \var{count}