-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString.lua
154 lines (142 loc) · 3.98 KB
/
String.lua
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
--[[
* Copyright (c) 2011-2012 by Adam Hellberg.
*
* This file is part of Command.
*
* Command is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Command is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Command. If not, see <http://www.gnu.org/licenses/>.
--]]
-- Upvalues
local type = type
local C = Command
if type(C.Extensions) ~= "table" then
C.Extensions = {}
end
--- Table containing all String methods.
-- This is referenced "CES" in String.lua.
-- @name Command.Extentions.String
-- @class table
-- @field type No current use.
--
C.Extensions.String = {
type = "ext" -- For future use
}
local CES = C.Extensions.String
--- Check if a string starts with a specific string.
-- @param s String to be checked.
-- @param target String to search for at beginning of s.
--
function CES:StartsWith(s, target)
if not s then return false end
return s:sub(1, target:len()) == target
end
--- Check if a string ends with a specific string.
-- @param s String to be checked.
-- @param target String to search for at end of s.
--
function CES:EndsWith(s, target)
return target == '' or s:sub(-target:len()) == target
end
--- Trim a string, removing whitespace at the beginning of it.
-- @param s String to be trimmed.
-- @return The trimmed string.
--
function CES:Trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
--- Split a string with space as delimiter.
-- @param s String to be split.
-- @param d Delimiter
-- @return Table containing the individual words.
--
function CES:Split(s, d)
if not s then return nil end
local t = {}
if not d then
for token in s:gmatch("[^%s]+") do
table.insert(t, token)
end
else
if not s:find(d) then return {s} end
local pattern = "(.-)" .. d .. "()"
local num = 0
local lastPos
for part,pos in s:gmatch(pattern) do
num = num + 1
t[num] = part
lastPos = pos
end
t[num + 1] = s:sub(lastPos)
end
return t
end
--- Cut a string into parts.
-- @param s The String to cut.
-- @param l Length of each string part.
-- @return Table containing the different parts.
--
function CES:Cut(s, l)
if type(s) ~= "string" or type(l) ~= "number" then
error("Invalid parameters passed, expected [string, number], got: [" .. type(s) .. ", " .. type(l) .. "]!")
return
end
if s:len() <= l then return s end
local c = math.ceil(s:len() / l)
local pos = 1
local t = {}
for i = 1, c do
local part = s:sub(pos, l * i)
table.insert(t, part)
pos = l * i + 1
end
return t
end
--- Split a string into parts to fit the length specified.
-- Works like Cut() except keeps words together to make it more pretty.
-- @param s The string to fit.
-- @param l Max length of each part.
-- @param d Delimiter to separate each word with.
-- @return Table containing the parts.
--
function CES:Fit(s, l, d)
if (type(s) ~= "string" and type(s) ~= "table") or type(l) ~= "number" then
error("Invalid parameters passed, expected [string, number], got: [" .. type(s) .. ", " .. type(l) .. "]!")
return
end
d = d or " "
if type(s) == "table" then s = C.Extensions.Table:Join(s) end
if s:len() <= l then return s end
local words = self:Split(s)
local parts = {}
local part = ""
for i=1, #words do
part = part .. words[i]
if i >= #words then
table.insert(parts, part)
break
elseif (part .. words[i + 1]):len() >= l then
table.insert(parts, part)
part = ""
else
part = part .. d
end
end
return parts
end
--- Checks if a string is null (nil) or empty.
-- @param s The string to check
-- @return True if string is null (nil) or empty (""), false otherwise.
--
function CES:IsNullOrEmpty(s)
return s == nil or s == ""
end