-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstringpp.lua
More file actions
244 lines (207 loc) · 7.22 KB
/
stringpp.lua
File metadata and controls
244 lines (207 loc) · 7.22 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
-- MIT License
--
-- Copyright (c) 2023 LuaUtils
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.
-- stringpp (string plus plus)
-- expansion of string lib
local ok, aux = pcall(require, "luautils.auxiliar")
if not ok then
ok, aux = pcall(require, "auxiliar")
end
local expected_args = aux.expected_args
---@class stringpplib: stringlib
local stringpp = {}
--- it returns a version of the string where each word is titlecased.
---```
---stringpp.title("exAMPLe") --> Example
---stringpp.title("anotheR EXAmPLE") --> Another Example
---```
---@param s string
---@return string
function stringpp.title(s)
expected_args("title", {s}, {"string"})
local spl = stringpp.split(s, " ")
for i = 1, #spl do
spl[i] = stringpp.captalize(spl[i])
end
return table.concat(spl, " ")
end
--- it returns a capitalized version of the string.
---```
---stringpp.capitalize("fiRST eXAMPLe") --> First example
---stringpp.capitalize("sECOND example") --> Second example
---```
---@param s string
---@return string
function stringpp.capitalize(s)
expected_args("capitalize", {s}, {"string"})
return string.upper(string.sub(s, 1)) .. string.lower(string.sub(s, 2))
end
--- Return a version of the string suitable for caseless comparisons.
---@param s string
---@return string
function stringpp.casefold(s)
expected_args("casefold", {s}, {"string"})
return string.lower(s)
end
-- adjust `s` to center
---@param s string
---@param width integer
---@param fillchar string
---@return string
function stringpp.center(s, width, fillchar)
expected_args("center", {s, width, fillchar}, {"string", "integer", "string"})
fillchar = fillchar or " "
local s_len = #s
local fillchar_repeat = math.floor((width - s_len) / 2)
local fillchar_rep_res = string.rep(fillchar, fillchar_repeat)
local res = fillchar_rep_res .. s .. fillchar_rep_res
if(#res ~= width) then res = res .. fillchar end
return res
end
-- adjust `s` to left
---@param s string
---@param width integer
---@param fillchar string
---@return unknown
function stringpp.left(s, width, fillchar)
expected_args("left", {s, width, fillchar}, {"string", "integer", "string"})
local s_len = #s
local fillchar_repeat = width - s_len
local fillchar_rep_res = string.rep(fillchar, fillchar_repeat)
return s .. fillchar_rep_res
end
-- adjust `s` to right
---@param s string
---@param width integer
---@param fillchar string
---@return unknown
function stringpp.right(s, width, fillchar)
expected_args("right", {s, width, fillchar}, {"string", "integer", "string"})
local s_len = #s
local fillchar_repeat = width - s_len
local fillchar_rep_res = string.rep(fillchar, fillchar_repeat)
return fillchar_rep_res .. s
end
--- returns how many times `x` appeared in `s` starting from the `start` position to the `stop` position
---@param s string
---@param x string
---@param start? integer
---@param stop? integer
---@return integer
function stringpp.count(s, x, start, stop)
start = start or 1
stop = stop or #s
expected_args("count", {s, x, start, stop}, {"string", "string", "integer", "integer"})
local x_len = #x
local res = 0
for i = start, stop do
local next_chars = string.sub(s, i, i+x_len-1)
if(next_chars == x) then
res = res + 1
end
end
return res
end
--- Return True if the string is an alphabetic string, False otherwise.
--- A string is alphabetic if all characters in the string are alphabetic and there
--- is at least one character in the string.
---@param s string
---@return boolean
function stringpp.isalpha(s)
expected_args("isalpha", {s}, {"string"})
return string.match(s, " ") == nil and
string.match(s, "%d") == nil
end
--- Return True if the string is a decimal string, False otherwise.
--- A string is a decimal string if all characters in the string are decimal and
--- there is at least one character in the string.
---@param s string
---@return boolean
function stringpp.isdecimal(s)
expected_args("isdecimal", {s}, {"string"})
return tonumber(s) ~= nil
end
--- Return True if the string is a lowercase string, False otherwise.
--- A string is lowercase if all cased characters in the string are lowercase and
--- there is at least one cased character in the string.
---@param s string
---@return boolean
function stringpp.islower(s)
expected_args("islower", {s}, {"string"})
return s == string.lower(s)
end
--- Return True if the string is an uppercase string, False otherwise.
--- A string is uppercase if all cased characters in the string are uppercase and
--- there is at least one cased character in the string.
---@param s string
---@return boolean
function stringpp.isupper(s)
expected_args("isupper", {s}, {"string"})
return s == string.upper(s)
end
--- Return True if the string is a whitespace string, False otherwise.
--- A string is whitespace if all characters in the string are whitespace and there
--- is at least one character in the string.
---@param s string
---@return boolean
function stringpp.isspace(s)
expected_args("isspace", {s}, {"string"})
if s == "" then return false end
return string.gsub(s, " ", "") == ""
end
--- Return True if the string is a title-cased string, False otherwise.
--- In a title-cased string, upper- and title-case characters may only
--- follow uncased characters and lowercase characters only cased ones.
---@param s string
---@return boolean
function stringpp.istitle(s)
expected_args("istitle", {s}, {"string"})
return s == stringpp.title(s)
end
--- splits `s` with `sep` separator
---```
---stringpp.split("Hello World!") --> {"Hello", "World!"}
---stringpp.split("Hello,World!,") --> {"Hello", "World!", ""}
---```
---@param s string
---@param sep string
---@return table
function stringpp.split(s, sep)
sep = sep or " "
expected_args("split", {s, sep}, {"string", "string"})
s = s .. sep
local str = ""
local res = {}
local i = 1
while(i < #s+1) do
local next_chars = string.sub(s, i, i+#sep-1)
if(next_chars == sep) then
i = i + #sep-1
table.insert(res, str)
str = ""
else
str = str .. string.sub(s, i, i)
end
i = i + 1
end
return res
end
return stringpp