From 7e8718eb28d89605f5ba0ffdbc2178d55b12c80b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jens=20Axel=20S=C3=B8gaard?= Date: Tue, 19 Aug 2014 23:52:18 +0200 Subject: [PATCH] Added string-replace and string-index implemented in #lang whalesong --- whalesong/selfhost/selfhost-strings.rkt | 51 +++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 whalesong/selfhost/selfhost-strings.rkt diff --git a/whalesong/selfhost/selfhost-strings.rkt b/whalesong/selfhost/selfhost-strings.rkt new file mode 100644 index 0000000..dfc7921 --- /dev/null +++ b/whalesong/selfhost/selfhost-strings.rkt @@ -0,0 +1,51 @@ +#lang whalesong (require "selfhost-lang.rkt" whalesong/lang/for) + +(provide string-replace) ; (string-replace k r s) replace all occurences of k in s with r + +; string-index : string string [integer] -> integer +; return the index of the first occurence of k in the string s +; whose index is from or greater +(define (string-index k s [from 0]) + (define kn (string-length k)) + (define sn (string-length s)) + (and (<= (+ from kn) sn) + (for/or ([i (in-range from (- sn kn -1))]) + (and (for/and ([j (in-range i (+ i kn))] [l (in-range kn)]) + (char=? (string-ref s j) (string-ref k l))) + i)))) + +; a new string is returned where occurences of the string k (the key) +; are replaced with the string r (the replacement) in the string s. +(define (string-replace k r s) + (define kn (string-length k)) + (define sn (string-length s)) + (let loop ([start 0] [from 0] [chunks '()]) + (define i (string-index k s from)) + (displayln (list 'loop start from chunks i)) + (cond + [i + (define new-start (+ i kn)) + (loop new-start + new-start + (cons r (cons (substring s start i) chunks)))] + [(empty? chunks) + (string-copy s)] + [else + (apply string-append + (reverse (cons (if (<= start sn) + (substring s start) + "") + chunks)))]))) + +; Test must evaluate to #t +#;(and (= (string-index "bar" "foobarbazbar") + (string-index "bar" "foobarbazbar" 1) + (string-index "bar" "foobarbazbar" 2) + (string-index "bar" "foobarbazbar" 3) + 3) + (= (string-index "bar" "foobarbazbar" 4) 9) + (equal? (string-index "bar" "foobarbazbar" 10) #f)) + +; (string-replace "foo" "_" "abfoocdfoooo") + +