mirror of
https://github.com/justinethier/cyclone.git
synced 2025-05-23 20:15:05 +02:00
Added string-replace-all
This commit is contained in:
parent
3beec26ba5
commit
50ed9b639f
1 changed files with 40 additions and 1 deletions
|
@ -40,8 +40,10 @@
|
|||
gensym
|
||||
delete
|
||||
delete-duplicates
|
||||
list-insert-at!
|
||||
list-index2
|
||||
list-insert-at!
|
||||
list-prefix?
|
||||
string-replace-all
|
||||
filter)
|
||||
(begin
|
||||
|
||||
|
@ -125,6 +127,43 @@
|
|||
-1
|
||||
(+ 1 (list-index2 e (cdr lst))))))))
|
||||
|
||||
;; Replace all instances of needle within haystack.
|
||||
;; Based on code from:
|
||||
;; http://stackoverflow.com/a/32320936/101258
|
||||
(define (string-replace-all haystack needle replacement)
|
||||
;; most of the processing works on lists
|
||||
;; of char, not strings.
|
||||
(let ((haystack (string->list haystack))
|
||||
(needle (string->list needle))
|
||||
(replacement (string->list replacement))
|
||||
(needle-len (string-length needle)))
|
||||
(let loop ((haystack haystack) (acc '()))
|
||||
(cond ((null? haystack)
|
||||
(list->string (reverse acc)))
|
||||
((list-prefix? haystack needle)
|
||||
(loop (list-tail haystack needle-len)
|
||||
(reverse-append replacement acc)))
|
||||
(else
|
||||
(loop (cdr haystack) (cons (car haystack) acc)))))))
|
||||
|
||||
(define (reverse-append pre lis)
|
||||
(append (reverse pre) lis))
|
||||
|
||||
(define (list-prefix? lis prefix)
|
||||
(call/cc
|
||||
(lambda (return)
|
||||
(for-each
|
||||
(lambda (x y)
|
||||
(if (not (equal? x y))
|
||||
(return #f)))
|
||||
lis
|
||||
prefix)
|
||||
(return #t))))
|
||||
; Tests -
|
||||
;(write (list-prefix? '(1 2 3 4 5) '(1 2)))
|
||||
;(write (list-prefix? '(1 2 3) '(a 1 2 3 4)))
|
||||
;(write (string-replace-all "The cat looks like a cat." "cat" "dog"))
|
||||
|
||||
; gensym-count : integer
|
||||
(define gensym-count 0)
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue