wishful/mods/wislib/string.lua
2025-02-07 14:39:37 -05:00

59 lines
1.5 KiB
Lua

--setup lib env
local print = minetest and minetest.debug or print
--context is always -eq to the highest possible subfolder in the mods dir, this can be ether the mod or modpack root folder.
local context = debug and debug.getinfo(1, "S").source:match("mods[/\\][/\\]?([^/]*)") or "pack"
function string:basename ()
return self:match("(.+)%..+")
end
function string:trim()
return self:match("^%s*(.-)%s*$")
end
--[[
function string:split (p2Sep, p3Patern)
if p2Sep == nil then
p2Sep = "%s"
end
if p3Patern == nil then
p3Patern = table.concat({"([^", p2Sep, "]+)"}, "")
end
local returns = {}
for _ in string.gmatch(self, p3Patern) do
table.insert(returns, _)
end
return returns
end
--]]
function string:startsWith(p2Needle) -- self == haystack
return self:sub(1, #p2Needle) == p2Needle
end
function string:endsWith(p2Needle) -- self == haystack
return p2Needle == "" or self:sub(-#p2Needle) == p2Needle
end
function string:charCount(p2Char)
local count = 0
for _ in string.gmatch(self, p2Char) do
count = count + 1
end
return count
end
_G[context]=_G[context] or {}
_G[context].colors=_G[context].colors or {}
_G[context].colors.linux={
reset = "\27[0m",
red = "\27[31m",
green = "\27[32m",
yellow = "\27[33m",
blue = "\27[34m",
magenta = "\27[35m",
cyan = "\27[36m",
white= "\27[37m"
}
--some string function required by other modues and so is always loaded first, as load order is random.
--should not effect anything that depends on this library.