Add Racket-like string-replace function
This commit is contained in:
parent
c3fd80de71
commit
23d2d07b1a
@ -16,6 +16,7 @@
|
|||||||
string-c-radix->number
|
string-c-radix->number
|
||||||
has-duplicates? find-duplicates
|
has-duplicates? find-duplicates
|
||||||
insert-between
|
insert-between
|
||||||
|
string-replace-text
|
||||||
|
|
||||||
make-mux-selectors))
|
make-mux-selectors))
|
||||||
(else
|
(else
|
||||||
@ -174,3 +175,18 @@
|
|||||||
lst
|
lst
|
||||||
(cons* (car lst) x
|
(cons* (car lst) x
|
||||||
(insert-between (cdr lst) x))))
|
(insert-between (cdr lst) x))))
|
||||||
|
|
||||||
|
;;; Racket-like string-replace
|
||||||
|
(define* (string-replace-text str from to #:key (all #t))
|
||||||
|
(let ((flen (string-length from))
|
||||||
|
(tlen (string-length to)))
|
||||||
|
(let replace ((str str) (idx 0))
|
||||||
|
(if (>= idx (string-length str))
|
||||||
|
str
|
||||||
|
(let ((occ (string-contains str from idx)))
|
||||||
|
(if occ
|
||||||
|
(let ((str (string-replace str to occ (+ occ flen))))
|
||||||
|
(if all
|
||||||
|
(replace str (+ occ tlen 1))
|
||||||
|
str))
|
||||||
|
str))))))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user