1
1
local M = {}
2
2
3
+ local string_len , string_byte , string_sub , string_gsub =
4
+ string.len , string.byte , string.sub , string.gsub
5
+
3
6
--- @param s any
4
7
--- @return boolean
5
8
M .empty = function (s )
6
- return type (s ) ~= " string" or string.len (s ) == 0
9
+ return type (s ) ~= " string" or string_len (s ) == 0
7
10
end
8
11
9
12
--- @param s any
10
13
--- @return boolean
11
14
M .not_empty = function (s )
12
- return type (s ) == " string" and string.len (s ) > 0
15
+ return type (s ) == " string" and string_len (s ) > 0
13
16
end
14
17
15
18
--- @param s any
16
19
--- @return boolean
17
20
M .blank = function (s )
18
- return type (s ) ~= " string" or string.len (vim .trim (s )) == 0
21
+ return type (s ) ~= " string" or string_len (vim .trim (s )) == 0
19
22
end
20
23
21
24
--- @param s any
22
25
--- @return boolean
23
26
M .not_blank = function (s )
24
- return type (s ) == " string" and string.len (vim .trim (s )) > 0
27
+ return type (s ) == " string" and string_len (vim .trim (s )) > 0
25
28
end
26
29
27
30
--- @param s string
@@ -40,8 +43,8 @@ M.find = function(s, t, start)
40
43
match = false
41
44
break
42
45
end
43
- local a = string.byte (s , i + j - 1 )
44
- local b = string.byte (t , j )
46
+ local a = string_byte (s , i + j - 1 )
47
+ local b = string_byte (t , j )
45
48
if a ~= b then
46
49
match = false
47
50
break
@@ -70,8 +73,8 @@ M.rfind = function(s, t, rstart)
70
73
match = false
71
74
break
72
75
end
73
- local a = string.byte (s , i + j - 1 )
74
- local b = string.byte (t , j )
76
+ local a = string_byte (s , i + j - 1 )
77
+ local b = string_byte (t , j )
75
78
if a ~= b then
76
79
match = false
77
80
break
@@ -93,7 +96,7 @@ M.ltrim = function(s, t)
93
96
94
97
t = t or " %s+"
95
98
--- @diagnostic disable-next-line : redundant-return-value
96
- return string.gsub (s , " ^" .. t , " " )
99
+ return string_gsub (s , " ^" .. t , " " )
97
100
end
98
101
99
102
--- @param s string
@@ -105,7 +108,7 @@ M.rtrim = function(s, t)
105
108
106
109
t = t or " %s+"
107
110
--- @diagnostic disable-next-line : redundant-return-value
108
- return string.gsub (s , t .. " $" , " " )
111
+ return string_gsub (s , t .. " $" , " " )
109
112
end
110
113
111
114
--- @param s string
@@ -145,9 +148,9 @@ M.startswith = function(s, t, opts)
145
148
opts .ignorecase = type (opts .ignorecase ) == " boolean" and opts .ignorecase or false
146
149
147
150
if opts .ignorecase then
148
- return string.len (s ) >= string.len (t ) and s :sub (1 , # t ):lower () == t :lower ()
151
+ return string_len (s ) >= string_len (t ) and s :sub (1 , # t ):lower () == t :lower ()
149
152
else
150
- return string.len (s ) >= string.len (t ) and s :sub (1 , # t ) == t
153
+ return string_len (s ) >= string_len (t ) and s :sub (1 , # t ) == t
151
154
end
152
155
end
153
156
@@ -163,9 +166,9 @@ M.endswith = function(s, t, opts)
163
166
opts .ignorecase = type (opts .ignorecase ) == " boolean" and opts .ignorecase or false
164
167
165
168
if opts .ignorecase then
166
- return string.len (s ) >= string.len (t ) and s :sub (# s - # t + 1 ):lower () == t :lower ()
169
+ return string_len (s ) >= string_len (t ) and s :sub (# s - # t + 1 ):lower () == t :lower ()
167
170
else
168
- return string.len (s ) >= string.len (t ) and s :sub (# s - # t + 1 ) == t
171
+ return string_len (s ) >= string_len (t ) and s :sub (# s - # t + 1 ) == t
169
172
end
170
173
end
171
174
@@ -178,8 +181,8 @@ M.replace = function(s, p, r)
178
181
assert (type (p ) == " string" )
179
182
assert (type (r ) == " string" )
180
183
181
- local sn = string.len (s )
182
- local pn = string.len (p )
184
+ local sn = string_len (s )
185
+ local pn = string_len (p )
183
186
local pos = 1
184
187
local matched = 0
185
188
local result = s
@@ -189,7 +192,7 @@ M.replace = function(s, p, r)
189
192
if type (pos ) ~= " number" then
190
193
break
191
194
end
192
- result = string.sub (result , 1 , pos - 1 ) .. r .. string.sub (result , pos + pn )
195
+ result = string_sub (result , 1 , pos - 1 ) .. r .. string_sub (result , pos + pn )
193
196
pos = pos + pn
194
197
matched = matched + 1
195
198
end
@@ -201,55 +204,55 @@ end
201
204
--- @return boolean
202
205
M .isspace = function (c )
203
206
assert (type (c ) == " string" )
204
- assert (string.len (c ) == 1 )
207
+ assert (string_len (c ) == 1 )
205
208
return c :match (" %s" ) ~= nil
206
209
end
207
210
208
211
--- @param c string
209
212
--- @return boolean
210
213
M .isalnum = function (c )
211
214
assert (type (c ) == " string" )
212
- assert (string.len (c ) == 1 )
215
+ assert (string_len (c ) == 1 )
213
216
return c :match (" %w" ) ~= nil
214
217
end
215
218
216
219
--- @param c string
217
220
--- @return boolean
218
221
M .isdigit = function (c )
219
222
assert (type (c ) == " string" )
220
- assert (string.len (c ) == 1 )
223
+ assert (string_len (c ) == 1 )
221
224
return c :match (" %d" ) ~= nil
222
225
end
223
226
224
227
--- @param c string
225
228
--- @return boolean
226
229
M .isxdigit = function (c )
227
230
assert (type (c ) == " string" )
228
- assert (string.len (c ) == 1 )
231
+ assert (string_len (c ) == 1 )
229
232
return c :match (" %x" ) ~= nil
230
233
end
231
234
232
235
--- @param c string
233
236
--- @return boolean
234
237
M .isalpha = function (c )
235
238
assert (type (c ) == " string" )
236
- assert (string.len (c ) == 1 )
239
+ assert (string_len (c ) == 1 )
237
240
return c :match (" %a" ) ~= nil
238
241
end
239
242
240
243
--- @param c string
241
244
--- @return boolean
242
245
M .islower = function (c )
243
246
assert (type (c ) == " string" )
244
- assert (string.len (c ) == 1 )
247
+ assert (string_len (c ) == 1 )
245
248
return c :match (" %l" ) ~= nil
246
249
end
247
250
248
251
--- @param c string
249
252
--- @return boolean
250
253
M .isupper = function (c )
251
254
assert (type (c ) == " string" )
252
- assert (string.len (c ) == 1 )
255
+ assert (string_len (c ) == 1 )
253
256
return c :match (" %u" ) ~= nil
254
257
end
255
258
@@ -261,18 +264,18 @@ M.setchar = function(s, pos, ch)
261
264
assert (type (s ) == " string" )
262
265
assert (type (pos ) == " number" )
263
266
assert (type (ch ) == " string" )
264
- assert (string.len (ch ) == 1 )
267
+ assert (string_len (ch ) == 1 )
265
268
266
- local n = string.len (s )
269
+ local n = string_len (s )
267
270
pos = require (" gitlinker.commons.tbl" ).list_index (pos , n )
268
271
269
272
local buffer = " "
270
273
if pos > 1 then
271
- buffer = string.sub (s , 1 , pos - 1 )
274
+ buffer = string_sub (s , 1 , pos - 1 )
272
275
end
273
276
buffer = buffer .. ch
274
277
if pos < n then
275
- buffer = buffer .. string.sub (s , pos + 1 )
278
+ buffer = buffer .. string_sub (s , pos + 1 )
276
279
end
277
280
278
281
return buffer
283
286
M .tochars = function (s )
284
287
assert (type (s ) == " string" )
285
288
local l = {}
286
- local n = string.len (s )
289
+ local n = string_len (s )
287
290
for i = 1 , n do
288
- table.insert (l , string.sub (s , i , i ))
291
+ table.insert (l , string_sub (s , i , i ))
289
292
end
290
293
return l
291
294
end
0 commit comments