-
Notifications
You must be signed in to change notification settings - Fork 15
/
replace.rsc
128 lines (125 loc) · 4.7 KB
/
replace.rsc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# 关键字替换
## 例如 "abc%%% test %%%ghi" 替换为 abcdefghi
## $replace string=$string keyWord="%%% test %%%" value=def
## $replace string=$string startKey="%%%" endKey="%%%" value=def
## $replace string=$string key="%%%" value=def
## 必须传入参数:
## - string: 需要处理的字符串
## - keyWord: 全字匹配,如输入 {{{ test }}}
## - key: start 和 end 都是一样的话,就传入这个参数即可如: %%
## - startKey: 包裹替换字符的开始关键字,如:{{{ ,如果 key 配置了,此项不生效
## - endKey: 包裹替换字符的结束关键字,如:}}} ,如果 key 配置了,此项不生效
## - value: 替换的内容
:local replace do={
:local result
:local char
:local keywordRanges
:local pos 0
:local maxPos [:len $string]
if ( ! any $string ) do={ :error ("string is not defined!") }
if ( (! any $key) && ((! any $startKey) || (! any $endKey)) && (! any $keyWord ) ) do={
:error ("error [replace] key or startKey or endKey or keyWord is not defined!")
}
if ( ! any $value ) do={ :error ("error [replace] value is not defined!") }
# 查找关键字范围
## 必须传入参数:
## - string: 需要处理的字符串
## - keyWord: 全字匹配,如输入 {{{ test }}}
## - key: start 和 end 都是一样的话,就传入这个参数即可如: %%
## - startKey: 包裹替换字符的开始关键字,如:{{{
## - endKey: 包裹替换字符的结束关键字,如:}}}
## 可选参数:
## - startPos: 从哪里开始查找,如未定义默认为 0
:local findKeywordRanges do={
:local start 0
:local end
:local length
:local type
:local ranges {
"startPos"=-1;
"endPos"=-1;
"length"=0;
"found"=false;
}
if ( any $startPos ) do={ :set start $startPos }
if ( any $keyWord ) do={ :set type "keyWord" }
if ( any $key ) do={ :set type "key" }
if ( (any $startKey) && (any $endKey) ) do={ :set type "startAndEndKey" }
do {
if ( $type = "keyWord" ) do={
:set length [ :len $keyWord ]
:set start [ find $string $keyWord $start ]
:set end ( $start + $length )
}
if ( $type = "key" ) do={
:set start [ find $string $key $start ]
:set end [ find $string $key $start ]
:set length ( [:len $key] + ($end - $start) )
:set end ( $start + $length )
}
if ( $type = "startAndEndKey" ) do={
:set start [ find $string $startKey $start ]
:set end [ find $string $endKey $start ]
:set length ( [:len $endKey] + ($end - $start) )
:set end ( $start + $length )
}
} on-error={
:put ("[error] [findKeywordRanges] An error occurred while searching for a keyword.")
return $ranges
}
if ( any $start and any $end ) do={
if ( ( $start >= 0 ) && ( $end > 0 ) ) do={
:set ranges {
"startPos"=$start;
"endPos"=$end;
"length"=$length
"found"=true;
}
}
}
return $ranges
}
# 查找第一个关键字
:set keywordRanges [
$findKeywordRanges \
string=$string \
keyWord=$keyWord \
key=$key \
startKey=$startKey \
endKey=$endKey
]
# 找不到关键字,返回原字符串
if ( ($keywordRanges->"found") = false ) do={
if ( $debug = true) do={ :put ("[debug] [replace] No matching string found.") }
return $string
}
# 循环查找字符串
while ( ($keywordRanges->"found") = true ) do={
if ( $debug = true) do={ :put ("[debug] [replace] keywordRanges:" . [:tostr $keywordRanges;]) }
# 从开始到关键字位置的字符存入 result
:set result ($result . [:pick $string $pos ($keywordRanges->"startPos")])
# 插入目标字符
:set result ($result . $value)
# 跳过原关键字
:set pos ($keywordRanges->"endPos")
# 继续查找下一个关键字
:set keywordRanges [
$findKeywordRanges \
string=$string \
keyWord=$keyWord \
key=$key \
startKey=$startKey \
endKey=$endKey \
startPos=$pos
]
}
# 将结尾的数据存入 result
if ( $pos < $maxPos ) do={
:set result ($result . [:pick $string $pos $maxPos])
}
return $result
}
return {
"moduleName"="replace";
"replace"=$replace;
}