Last modified on 13 November 2012, at 10:51

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.

ARGUMENTSEdit

  • 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)

REFINEMENTSEdit

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

SOURCE CODEEdit

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]
]