跳转到内容

Rebol 编程/替换

来自维基教科书,开放世界中的开放书籍
REPLACE target search replace /all /case /tail 

在目标序列中用替换值替换搜索值。

REPLACE 是一个函数值。

  • 目标 -- 被修改的序列(类型:序列)
  • 搜索 -- 要替换的值(类型:任何)
  • 替换 -- 用它替换的值(如果是一个函数,每次都会被调用)(类型:任何)
  • /all -- 替换所有出现
  • /case -- 区分大小写的替换
  • /tail -- 返回最后一个替换位置后的目标

源代码

[编辑 | 编辑源代码]
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]
]
华夏公益教科书