Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstring.lua
More file actions
Latest commit
215 lines (195 loc) · 7.11 KB
/
Copy pathstring.lua
File metadata and controls
215 lines (195 loc) · 7.11 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
localtype=type
localselect=select
localassert=assert
localtonumber=tonumber
localstring=string
localssub=string.sub
localsrep=string.rep
localsfind=string.find
localsbyte=string.byte
localsgsub=string.gsub
localsgmatch=string.gmatch
localtconcat=table.concat
---comment 计算`pattern`在`text`中pos位置开始出现的总次数.
---@paramtextstring @实际内容
---@parampatternstring @匹配内容
---@paramposinteger @字符串
---@returnstring @返回拼接好的字符串
functionstring.count (text, pattern, pos)
localcount=0
ifnotposthen
pos=1
end
whiletruedo
locals, e=sfind(text, pattern, pos)
ifnotsornotethen
break
end
count=count+1
pos=e+1
end
returncount
end
---comment 以`text`为中心拼接`count`个`fill`在两侧.
---@paramtextstring @实际内容
---@paramfillstring @填充字符
---@paramcountinteger @填充数量
---@returnstring @返回拼接好的字符串
functionstring.center (text, fill, count)
assert(type(text) =='string' andtype(fill) =='string' , "Invalid string `text` or `fill`.")
assert(type(count) =='number' andtonumber(count) orcount>0, "Invalid fill `count`")
localfill_text=srep(fill, count)
returntconcat{ fill_text, text, fill_text }
end
---comment 判断指定字符串内容是否全部空格
---@paramtextstring @判断内容
---@returnboolean @如果`text`全是空格返回`true`, 否则返回`false`.
functionstring.allspace (text)
returnsfind(textor'', "^[ ]+$") andtrueorfalse
end
---comment 连接`1`个或`N`个字符串, 非可转换字符串的对象会抛出异常
---@returnstring
functionstring.join (...)
ifselect("#", ...) <=1then
return (...) or''
end
returntconcat{...}
end
---comment 根据`sep`分割`text`字符串.
---@paramtextstring @待分割的字符串
---@paramsepstring @分割用的分隔符
---@returntable @分割后的数组
functionstring.split(text, sep)
assert(type(text) =='string', "Invalid string `text`.")
ifnotseportype(sep) ~='string' orsep=='' then
sep='%,'
end
localindex=1
locallist= {}
forsubinsgmatch(text, "([^" ..sep.."]*)") do
list[index] =sub
index=index+1
end
ifindex==1then
list[index] =text
end
returnlist
end
localfunctionstrip(text, sep, left, right)
ifleftthen
text=sgsub(text, "^[" ..sep.."]+", "", 1)
end
ifrightthen
text=sgsub(text, "[" ..sep.."]+$", "", 1)
end
returntext
end
---comment 移除字符串头、尾指定的字符
---@paramtextstring @待分割的字符串
---@paramsepstring @移除用的分隔符
functionstring.strip (text, sep)
assert(type(text) =='string', "Invalid string `text`.")
ifnotseportype(sep) ~='string' orsep=='' then
sep=''
end
returnstrip(text, sep, true, true)
end
---comment 移除字符串头部指定的字符
---@paramtextstring @待分割的字符串
---@paramsepstring @移除用的分隔符
functionstring.lstrip (text, sep)
assert(type(text) =='string', "Invalid string `text`.")
ifnotseportype(sep) ~='string' orsep=='' then
sep=''
end
returnstrip(text, sep, true, false)
end
---comment 移除字符串尾部指定的字符
---@paramtextstring @待分割的字符串
---@paramsepstring @移除用的分隔符
functionstring.rstrip (text, sep)
assert(type(text) =='string', "Invalid string `text`.")
ifnotseportype(sep) ~='string' orsep=='' then
sep=''
end
returnstrip(text, sep, false, true)
end
---comment 判断起始位置是否指定内容
---@paramtextstring @待匹配内容
---@paramsstringstring @其实内容
---@paramstartinteger @起始位置
functionstring.startswith(text, sstring, start)
assert(type(text) =='string', "Invalid string `text`.")
assert(type(sstring) =='string', "Invalid start string.")
returnsfind(text, '^' ..sstring, start) andtrueorfalse
end
---comment 判断结束位置是否指定内容
---@paramtextstring @待匹配内容
---@paramestringstring @结束内容
---@paramoverinteger @结束位置
functionstring.endswith(text, estring, over)
assert(type(text) =='string', "Invalid string `text`.")
assert(type(estring) =='string', "Invalid end string.")
returnsfind(text, estring..'$', over) andtrueorfalse
end
---comment 将`text`里的`s1`替换为`s2`(最多`count`此)
---@paramtextstring @原始内容
---@params1string @匹配字符串
---@params2string @替换字符串
---@paramcountnumber @替换次数
---@returnstring @替换后内容
functionstring.replace (text, s1, s2, count)
assert(type(text) =='string' andtext~='', "Invalid text string.")
assert(type(s1) =='string' ands1~='', "Invalid s1 string.")
assert(type(s2) =='string' ands2~='', "Invalid s2 string.")
locals=sgsub(text, s1, s2, count)
returns
end
---comment 字符串转换为字节数组
---@paramtextstring @待转换的字符串
---@returntable @转换后的字节数组
functionstring.tobytes(text)
assert(type(text) =='string' andtext~='', "Invalid text string.")
locallist= {}
foridx=1, #textdo
list[#list+1] =sbyte(text, idx)
end
returnlist
end
---comment 向指定位置的字符串后插入字符串.
---@paramtextstring @原始字符串
---@paramposinteger @待插入的位置
---@paramstrstring @待插入的字符串
---@returnstring @返回插入后的字符串内容
functionstring.insert(text, pos, str)
assert(type(text) =='string' andtext~='', "Invalid text string.")
assert(type(pos) =='number', "Invalid pos integer.")
assert(type(str) =='string' andstr~='', "Invalid text string.")
returntconcat{ssub(text, 1, pos), str, ssub(text, pos+1, -1)}
end
local_, liconv=pcall(require, "liconv")
---comment 替换iconv函数, 错误的实现会出现运行时错误.
---@parammoduletable
---@paramencodestring @需保证`module[encode]`行为与`liconv.to`行为一致
---@paramdecodestring @需保证`module[decode]`行为与`liconv.from`行为一致
functionstring.iconv(module, encode, decode)
liconv= { to=module[encode], from=module[decode] }
end
---comment 使用iconv进行编码转换
---@paramtextstring @文本内容
---@paramencodingstring @目标编码
---@returnstring @转换后的文本
functionstring.encode (text, encoding)
assert(type(text) =='string', "Invalid string `text`.")
assert(type(encoding) =='string', "Invalid string encoding.")
returnassert(type(liconv) =='table' andliconv, "Lua iconv is not supported.").to(encoding, text)
end
---comment 使用iconv进行编码转换
---@paramtextstring @文本内容
---@paramdecodingstring @原始编码
---@returnstring @转换后的文本
functionstring.decode (text, decoding)
assert(type(text) =='string', "Invalid string `text`.")
assert(type(decoding) =='string', "Invalid string encoding.")
returnassert(type(liconv) =='table' andliconv, "Lua iconv is not supported.").from(decoding, text)
end