string-split on the empty string is null, on a single space is two empty strings

This commit is contained in:
Alex Shinn 2013-04-02 22:20:41 +09:00
parent 98bad7bc63
commit af8aed4c5a
2 changed files with 7 additions and 3 deletions

View file

@ -54,11 +54,13 @@
(define (string-split str . o)
(let ((pred (make-char-predicate (if (pair? o) (car o) #\space)))
(limit (if (and (pair? o) (pair? (cdr o))) (cadr o) (string-size str)))
(limit (if (and (pair? o) (pair? (cdr o)))
(cadr o)
(+ 1 (string-size str))))
(start (string-cursor-start str))
(end (string-cursor-end str)))
(if (string-cursor>=? start end)
(list "")
'()
(let lp ((i start) (n 1) (res '()))
(cond
((>= n limit)

View file

@ -41,8 +41,10 @@
(test "foobarbaz" (string-join '("foo" "bar" "baz")))
(test "foo bar baz" (string-join '("foo" "bar" "baz") " "))
(test '("") (string-split ""))
(test '() (string-split ""))
(test '("" "") (string-split " "))
(test '("foo" "bar" "baz") (string-split "foo bar baz"))
(test '("foo" "bar" "baz" "") (string-split "foo bar baz "))
(test '("foo" "bar" "baz") (string-split "foo:bar:baz" #\:))
(test '("" "foo" "bar" "baz") (string-split ":foo:bar:baz" #\:))
(test '("foo" "bar" "baz" "") (string-split "foo:bar:baz:" #\:))