Just have string-replace' now, with an
#:all?' keyword (defaulting to #t).
This commit is contained in:
parent
37a1c0af91
commit
f55eba70c0
|
@ -5,8 +5,7 @@
|
|||
string-trim
|
||||
string-normalize-spaces
|
||||
string-split
|
||||
string-replace
|
||||
string-replace*)
|
||||
string-replace)
|
||||
|
||||
(define string-append*
|
||||
(case-lambda [(strs) (apply string-append strs)] ; optimize common case
|
||||
|
@ -94,22 +93,17 @@
|
|||
space))
|
||||
|
||||
(define replace-cache (make-weak-hasheq))
|
||||
(define (replace-internal who str from to all?)
|
||||
(unless (string? str) (raise-type-error who "string" str))
|
||||
(unless (string? to) (raise-type-error who "string" to))
|
||||
(define (string-replace str from to #:all? [all? #t])
|
||||
(unless (string? str) (raise-type-error 'string-replace "string" str))
|
||||
(unless (string? to) (raise-type-error 'string-replace "string" to))
|
||||
(define from*
|
||||
(if (regexp? from)
|
||||
from
|
||||
(hash-ref! replace-cache from
|
||||
(λ() (if (string? from)
|
||||
(regexp (regexp-quote from))
|
||||
(raise-type-error who "string" from))))))
|
||||
(raise-type-error 'string-replace "string" from))))))
|
||||
(define to* (regexp-replace-quote to))
|
||||
(if all?
|
||||
(regexp-replace* from* str to*)
|
||||
(regexp-replace from* str to*)))
|
||||
|
||||
(define (string-replace str from to)
|
||||
(replace-internal 'string-replace str from to #f))
|
||||
(define (string-replace* str from to)
|
||||
(replace-internal 'string-replace* str from to #t))
|
||||
|
|
|
@ -472,5 +472,23 @@ You can specify @racket[space] for an alternate space replacement.
|
|||
Note that this is the same as
|
||||
@racket[(string-join (string-split str sep ....) space)]}
|
||||
|
||||
@defproc[(string-replace [str string?]
|
||||
[from (or/c string? regexp?)]
|
||||
[to string?]
|
||||
[#:all all? any/c #t])
|
||||
string?]{
|
||||
|
||||
Returns a copy of @racket[str] where all occurrences of @racket[from]
|
||||
are replaced with with @racket[to].
|
||||
|
||||
When @racket[from] is a string it is matched literally. The replacement
|
||||
@racket[to] argument must be a string and is always inserted as-is. All
|
||||
occurrences are replaced by default, pass @racket[#f] for @racket[all?]
|
||||
to replace only the first match.
|
||||
|
||||
@mz-examples[#:eval string-eval
|
||||
(string-replace "foo bar baz" "bar" "blah")
|
||||
]}
|
||||
|
||||
|
||||
@close-eval[string-eval]
|
||||
|
|
|
@ -446,8 +446,8 @@
|
|||
(test "" string-replace "" "|" "?")
|
||||
(test "foo" string-replace "foo" "|" "?")
|
||||
(test "foo\\bar" string-replace "foo\\bar" "\\" "\\")
|
||||
(test "foo[bar]" string-replace "foo]bar]" "]" "[")
|
||||
(test "foo[bar[" string-replace* "foo]bar]" "]" "[")
|
||||
(test "foo[bar]" string-replace "foo]bar]" "]" "[" #:all? #f)
|
||||
(test "foo[bar[" string-replace "foo]bar]" "]" "[")
|
||||
(test "foo\\1bar" string-replace "foo===bar" #rx"(=+)" "\\1")
|
||||
(test "foo\\1bar" string-replace "foo===bar" #px"={3}" "\\1"))
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user