Replace

angerangel edited this page Jan 7, 2013 · 1 revision

USAGE

   REPLACE target search replace /all /case /tail

DESCRIPTION

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

REPLACE is a function value.

ARGUMENTS

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

REFINEMENTS

  • /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]
] ]
Clone this wiki locally
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.