Rebol Programming/replace

USAGE: edit

REPLACE target search replace /all /case /tail 

DESCRIPTION: edit

Replaces the search value with the replace value within the target series.

REPLACE is a function value.

ARGUMENTS edit

  • target -- Series that is being modified (Type: series)
  • search -- Value to be replaced (Type: any)
  • replace -- Value to replace with (will be called each time if a function) (Type: any)

REFINEMENTS edit

  • /all -- Replace all occurrences
  • /case -- Case-sensitive replacement
  • /tail -- Return target after the last replacement position

SOURCE CODE edit

replace: func [
    {Replaces the search value with the replace value within the target series.} 
    target [series!] "Series that is being modified" 
    search "Value to be replaced" 
    replace {Value to replace with (will be 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: system/words/case [
        bitset? :search 1 
        any-string? target [
            if any [not any-string? :search tag? :search] [search: form :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]
]