From 56ef2f3f2d78ffe8ec04346014320756938744c4 Mon Sep 17 00:00:00 2001 From: Jan Nieuwenhuizen Date: Sun, 29 Apr 2018 18:27:29 +0200 Subject: [PATCH] mes: Add string-replace. * module/srfi/srfi-13.mes (string-replace): New function. * tests/srfi-13.test ("string-replace"): Test it. --- module/srfi/srfi-13.mes | 12 ++++++++++++ tests/srfi-13.test | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/module/srfi/srfi-13.mes b/module/srfi/srfi-13.mes index 196e8f39..4e3ff7ef 100644 --- a/module/srfi/srfi-13.mes +++ b/module/srfi/srfi-13.mes @@ -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)))))))) diff --git a/tests/srfi-13.test b/tests/srfi-13.test index 29035ccd..afa75f54 100755 --- a/tests/srfi-13.test +++ b/tests/srfi-13.test @@ -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)