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:
Jan Nieuwenhuizen 2018-04-29 18:27:29 +02:00
parent 563d1d92f9
commit 56ef2f3f2d
No known key found for this signature in database
GPG Key ID: F3C1A0D9C1D65273
2 changed files with 16 additions and 0 deletions

View File

@ -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))))))))

View File

@ -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)