2020-04-29 18:57:18 +00:00
|
|
|
local M = {}
|
|
|
|
|
|
|
|
function M.starts_with(str, start)
|
|
|
|
return str:sub(1, #start) == start
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.ends_with(str, ending)
|
|
|
|
return ending == "" or str:sub(-#ending) == ending
|
|
|
|
end
|
|
|
|
|
2020-09-28 00:25:47 +00:00
|
|
|
--- Makes a seed from a string.
|
|
|
|
-- If the input is a number, it is returned as-is.
|
|
|
|
-- If the input is a string and can be converted to a number with tonumber(),
|
|
|
|
-- the output of tonumber() is returned.
|
|
|
|
-- Otherwise, the string is converted to a number via a simple hashing algorithm
|
|
|
|
-- (caution: certainlly NOT crypto-secure!).
|
2020-09-28 00:41:49 +00:00
|
|
|
-- @param {string} str The string to convert.
|
|
|
|
-- @source https://stackoverflow.com/a/2624210/1460422 The idea came from here
|
2020-09-28 00:25:47 +00:00
|
|
|
function M.makeseed(str)
|
|
|
|
if type(str) == "number" then return str end
|
|
|
|
if tonumber(str) ~= nil then return tonumber(str) end
|
|
|
|
local result = 0
|
|
|
|
for i = 1, #str do
|
|
|
|
result = (result*91) + (string.byte(str:sub(i, i)) * 31)
|
|
|
|
end
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2020-04-29 18:57:18 +00:00
|
|
|
return M
|