mirror of
https://github.com/Bluewhale1337/ElvUIModernized.git
synced 2026-07-27 16:34:45 +00:00
added 2nd argument support for string.trim
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
local assert = assert
|
local assert = assert
|
||||||
local error = error
|
local error = error
|
||||||
local geterrorhandler = geterrorhandler
|
local geterrorhandler = geterrorhandler
|
||||||
|
local loadstring = loadstring
|
||||||
local pairs = pairs
|
local pairs = pairs
|
||||||
local pcall = pcall
|
local pcall = pcall
|
||||||
local tostring = tostring
|
local tostring = tostring
|
||||||
@@ -11,6 +12,36 @@ local ceil, floor = math.ceil, math.floor
|
|||||||
local find, format, gfind, gsub, sub = string.find, string.format, string.gfind, string.gsub, string.sub
|
local find, format, gfind, gsub, sub = string.find, string.format, string.gfind, string.gsub, string.sub
|
||||||
local getn, setn, tinsert = table.getn, table.setn, table.insert
|
local getn, setn, tinsert = table.getn, table.setn, table.insert
|
||||||
|
|
||||||
|
local escapeSequences = {
|
||||||
|
["\a"] = "\\a", -- Bell
|
||||||
|
["\b"] = "\\b", -- Backspace
|
||||||
|
["\t"] = "\\t", -- Horizontal tab
|
||||||
|
["\n"] = "\\n", -- Newline
|
||||||
|
["\v"] = "\\v", -- Vertical tab
|
||||||
|
["\f"] = "\\f", -- Form feed
|
||||||
|
["\r"] = "\\r", -- Carriage return
|
||||||
|
["\\"] = "\\\\", -- Backslash
|
||||||
|
["\""] = "\\\"", -- Quotation mark
|
||||||
|
["|"] = "||",
|
||||||
|
[" "] = "%s",
|
||||||
|
|
||||||
|
[" "] = "%s",
|
||||||
|
["!"] = "\\!",
|
||||||
|
["#"] = "\\#",
|
||||||
|
["$"] = "\\$",
|
||||||
|
["%"] = "\\%",
|
||||||
|
["&"] = "\\&",
|
||||||
|
["'"] = "\\'",
|
||||||
|
["("] = "\\(",
|
||||||
|
[")"] = "\\)",
|
||||||
|
["*"] = "\\*",
|
||||||
|
["+"] = "\\+",
|
||||||
|
[","] = "\\,",
|
||||||
|
["-"] = "\\-",
|
||||||
|
["."] = "\\.",
|
||||||
|
["/"] = "\\/"
|
||||||
|
}
|
||||||
|
|
||||||
math.huge = 1/0
|
math.huge = 1/0
|
||||||
string.gmatch = gfind
|
string.gmatch = gfind
|
||||||
|
|
||||||
@@ -92,37 +123,48 @@ end
|
|||||||
strsplit = string.split
|
strsplit = string.split
|
||||||
|
|
||||||
function string.trim(str, chars)
|
function string.trim(str, chars)
|
||||||
assert(type(str) == "string" or type(str) == "number", format("bad argument #1 to 'trim' (string expected, got %s)", delimiter and type(delimiter) or "no value"))
|
assert(type(str) == "string" or type(str) == "number", format("bad argument #1 to 'trim' (string expected, got %s)", str and type(str) or "no value"))
|
||||||
|
assert(not type(chars) == "string" or type(chars) == "number", format("bad argument #2 to 'trim' (string expected, got %s)", chars and type(chars) or "no value"))
|
||||||
|
|
||||||
str = type(str) == "number" and tostring(str) or str
|
str = type(str) == "number" and tostring(str) or str
|
||||||
--[[
|
|
||||||
if chars then
|
if chars then
|
||||||
local size = string.len(chars)
|
chars = type(chars) == "number" and tostring(chars) or chars
|
||||||
local token = ""
|
|
||||||
|
local tokens = {}
|
||||||
|
|
||||||
|
for token in gfind(chars, "[%z\1-\255\"\\]") do
|
||||||
|
tinsert(tokens, token)
|
||||||
|
end
|
||||||
|
|
||||||
|
local pattern = ""
|
||||||
|
local size = getn(tokens)
|
||||||
|
|
||||||
for i = 1, size do
|
for i = 1, size do
|
||||||
token = token .. string.sub(chars, i, i + 1) .. "*"
|
pattern = pattern..(escapeSequences[tokens[i]] or tokens[i]).."+"
|
||||||
|
|
||||||
if size > 1 and i < size then
|
if size > 1 and i < size then
|
||||||
token = token.."|"
|
pattern = pattern.."|"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
token = "["..token.."]"
|
patternStart = "^["..pattern.."](.-)$"
|
||||||
|
patternEnd = "^(.-)["..pattern.."]$"
|
||||||
|
patternStart = loadstring("return \""..patternStart.."\"")()
|
||||||
|
patternEnd = loadstring("return \""..patternEnd.."\"")()
|
||||||
|
|
||||||
local trimed = 1
|
local trimed, x, y = 1
|
||||||
while trimed == 1 do
|
while trimed >= 1 do
|
||||||
str, trimed = string.gsub(str, "^"..token.."(._)"..token.."$", "")
|
str, x = gsub(str, patternStart, "%1")
|
||||||
end
|
str, y = gsub(str, patternEnd, "%1")
|
||||||
else
|
trimed = x + y
|
||||||
-- remove leading/trailing [space][tab][return][newline]
|
|
||||||
str = string.gsub(str, "^%s*(.-)%s*$", "%1")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return str
|
return str
|
||||||
]]
|
else
|
||||||
|
-- remove leading/trailing [space][tab][return][newline]
|
||||||
return string.gsub(str, "^%s*(.-)%s*$", "%1")
|
return string.gsub(str, "^%s*(.-)%s*$", "%1")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
strtrim = string.trim
|
strtrim = string.trim
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user