mes: Add string-replace.
* module/srfi/srfi-13.mes (string-replace): New function. * tests/srfi-13.test ("string-replace"): Test it.
This commit is contained in:
parent
563d1d92f9
commit
56ef2f3f2d
|
@ -157,3 +157,15 @@
|
|||
(define (string-map f string)
|
||||
(list->string (map f (string->list string))))
|
||||
|
||||
(define (string-replace string replace . rest)
|
||||
(let* ((start1 (and (pair? rest) (car rest)))
|
||||
(end1 (and start1 (pair? (cdr rest)) (cadr rest)))
|
||||
(start2 (and end1 (pair? (cddr rest)) (caddr rest)))
|
||||
(end2 (and start2 (pair? (cdddr rest)) (cadddr rest))))
|
||||
(if start2 (error "string-replace: not supported: START2=" start2))
|
||||
(if end2 (error "string-replace: not supported: END2=" end2))
|
||||
(list->string
|
||||
(append
|
||||
(string->list (string-take string (or start1 0)))
|
||||
(string->list replace)
|
||||
(string->list (string-drop string (or end1 (string-length string))))))))
|
||||
|
|
|
@ -80,4 +80,8 @@ exit $?
|
|||
(pass-if-equal "string-map" "fuubar"
|
||||
(string-map (lambda (c) (if (eq? c #\o) #\u c)) "foobar"))
|
||||
|
||||
(pass-if-equal "string-replace" "fubar"
|
||||
(string-replace "foobar" "u" 1 3))
|
||||
|
||||
|
||||
(result 'report)
|
||||
|
|
Loading…
Reference in a new issue