forked from rebol/rebol
-
Notifications
You must be signed in to change notification settings - Fork 2
Replace
angerangel edited this page Mar 19, 2013
·
1 revision
REPLACE target search replace /all /case /tail
Replaces a search value with the replace value within the target series.
REPLACE is a function value.
- target -- Series to replace within (modified) (series!)
- search -- Value to be replaced (converted if necessary)
- replace -- Value to replace with (called each time if a function)
- /all -- Replace all occurrences
- /case -- Case-sensitive replacement
- /tail -- Return target after the last replacement position
#SOURCE
replace: make function! [ [
{Replaces a search value with the replace value within the target series.}
target [series!] "Series to replace within (modified)"
search "Value to be replaced (converted if necessary)"
replace {Value to replace with (called each time if a function)}
/all "Replace all occurrences"
/case "Case-sensitive replacement"
/tail "Return target after the last replacement position"
/local save-target len value pos do-break
][
save-target: target
len: lib/case [
bitset? :search 1
any-string? target [
if any [not any-string? :search tag? :search] [search: form :search]
length? :search
]
binary? target [
unless binary? :search [search: to-binary :search]
length? :search
]
any-block? :search [length? :search]
true 1
]
do-break: unless all [:break]
while pick [
[pos: find target :search]
[pos: find/case target :search]
] not case [
(value: replace pos)
target: change/part pos :value len
do-break
]
either tail [target] [save-target]
] ]