changhong/lua_probject/base_project/Core/string.lua

305 lines
7.6 KiB
Lua
Raw Permalink Normal View History

2025-05-24 14:29:14 +08:00
string._htmlspecialchars_set = {}
string._htmlspecialchars_set["&"] = "&"
string._htmlspecialchars_set["\""] = """
string._htmlspecialchars_set["'"] = "'"
string._htmlspecialchars_set["<"] = "&lt;"
string._htmlspecialchars_set[">"] = "&gt;"
--[[--
HTML
~~~ lua
print(string.htmlspecialchars("<ABC>"))
-- 输出 &lt;ABC&gt;
~~~
@param string input
@return string
]]
function string.htmlspecialchars(input)
for k, v in pairs(string._htmlspecialchars_set) do
input = string.gsub(input, k, v)
end
return input
end
--[[--
HTML string.htmlspecialchars()
~~~ lua
print(string.restorehtmlspecialchars("&lt;ABC&gt;"))
-- 输出 <ABC>
~~~
@param string input
@return string
]]
function string.restorehtmlspecialchars(input)
for k, v in pairs(string._htmlspecialchars_set) do
input = string.gsub(input, v, k)
end
return input
end
--[[--
\n HTML
~~~ lua
print(string.nl2br("Hello\nWorld"))
-- 输出
-- Hello<br />World
~~~
@param string input
@return string
]]
function string.nl2br(input)
return string.gsub(input, "\n", "<br />")
end
--[[--
\n HTML
~~~ lua
print(string.nl2br("<Hello>\nWorld"))
-- 输出
-- &lt;Hello&gt;<br />World
~~~
@param string input
@return string
]]
function string.text2html(input)
input = string.gsub(input, "\t", " ")
input = string.htmlspecialchars(input)
input = string.gsub(input, " ", "&nbsp;")
input = string.nl2br(input)
return input
end
--[[--
~~~ lua
local input = "Hello,World"
local res = string.split(input, ",")
-- res = {"Hello", "World"}
local input = "Hello-+-World-+-Quick"
local res = string.split(input, "-+-")
-- res = {"Hello", "World", "Quick"}
~~~
@param string input
@param string delimiter
@return array
]]
function string.split(input, delimiter)
input = tostring(input)
delimiter = tostring(delimiter)
if (delimiter=='') then return false end
local pos,arr = 0, {}
-- for each divider found
for st,sp in function() return string.find(input, delimiter, pos, true) end do
table.insert(arr, string.sub(input, pos, st - 1))
pos = sp + 1
end
table.insert(arr, string.sub(input, pos))
return arr
end
--[[--
~~~ lua
local input = " ABC"
print(string.ltrim(input))
-- 输出 ABC输入字符串前面的两个空格被去掉了
~~~
-
- \t
- \n
- \r
@param string input
@return string
@see string.rtrim, string.trim
]]
function string.ltrim(input)
return string.gsub(input, "^[ \t\n\r]+", "")
end
--[[--
~~~ lua
local input = "ABC "
print(string.ltrim(input))
-- 输出 ABC输入字符串最后的两个空格被去掉了
~~~
@param string input
@return string
@see string.ltrim, string.trim
]]
function string.rtrim(input)
return string.gsub(input, "[ \t\n\r]+$", "")
end
--[[--
@param string input
@return string
@see string.ltrim, string.rtrim
]]
function string.trim(input)
input = string.gsub(input, "^[ \t\n\r]+", "")
return string.gsub(input, "[ \t\n\r]+$", "")
end
--[[--
~~~ lua
local input = "hello"
print(string.ucfirst(input))
-- 输出 Hello
~~~
@param string input
@return string
]]
function string.ucfirst(input)
return string.upper(string.sub(input, 1, 1)) .. string.sub(input, 2)
end
local function urlencodechar(char)
return "%" .. string.format("%02X", string.byte(char))
end
--[[--
URL
~~~ lua
local input = "hello world"
print(string.urlencode(input))
-- 输出
-- hello%20world
~~~
@param string input
@return string
@see string.urldecode
]]
function string.urlencode(input)
-- convert line endings
input = string.gsub(tostring(input), "\n", "\r\n")
-- escape all characters but alphanumeric, '.' and '-'
input = string.gsub(input, "([^%w%.%- ])", urlencodechar)
-- convert spaces to "+" symbols
return string.gsub(input, " ", "+")
end
--[[--
URL
~~~ lua
local input = "hello%20world"
print(string.urldecode(input))
-- 输出
-- hello world
~~~
@param string input
@return string
@see string.urlencode
]]
function string.urldecode(input)
input = string.gsub (input, "+", " ")
input = string.gsub (input, "%%(%x%x)", function(h) return string.char(checknumber(h,16)) end)
input = string.gsub (input, "\r\n", "\n")
return input
end
--[[--
UTF8
~~~ lua
local input = "你好World"
print(string.utf8len(input))
-- 输出 7
~~~
@param string input
@return integer
]]
function string.utf8len(input)
if input == nil then return 0 end
if type(input) ~= "string" then return 0 end
local len = string.len(input)
local left = len
local cnt = 0
local arr = {0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc}
while left ~= 0 do
local tmp = string.byte(input, -left)
local i = #arr
while arr[i] do
if tmp >= arr[i] then
left = left - i
break
end
i = i - 1
end
cnt = cnt + 1
end
return cnt
end
--[[
utf8
@param string input
@param integer indexendIndex 1index
@return string
]]
function string.utf8sub(input, index, endIndex)
if input == nil or type(input) ~= "string" then return nil end
if not endIndex then
endIndex = index
index = 1
end
local len = string.len(input)
local left = len
local cnt = 0
local head, tail = 0, 0
local arr = {0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc}
while left ~= 0 do
if cnt + 1 == index then
head = len - left + 1
end
local tmp = string.byte(input, -left)
local i = #arr
while arr[i] do
if tmp >= arr[i] then
left = left - i
break
end
i = i - 1
end
cnt = cnt + 1
if cnt == endIndex or left == 0 then
tail = len - left
break
end
end
local rt = string.sub(input, head, tail)
return rt
end
--[[--
~~~ lua
print(string.formatnumberthousands(1924235))
-- 输出 1,924,235
~~~
@param number num
@return string
]]
function string.formatnumberthousands(num)
local formatted = tostring(checknumber(num))
local k
while true do
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1,%2')
if k == 0 then break end
end
return formatted
end
function string.concat( ... )
local str = {}
local tem = {...}
for _,v in ipairs(tem) do
str[#str + 1] = v
end
return table.concat(str , "")
end