2021-03-20 02:48:56 +01:00
|
|
|
--[[
|
|
|
|
Functions that operate on strings that *really* should be present in the Lua
|
|
|
|
standard library, but somehow aren't.
|
|
|
|
A good rule of thumb is to ask "does Javascript have a built-in function for
|
|
|
|
this?". If yes, then your implementation probably belongs here.
|
|
|
|
]]--
|
|
|
|
|
|
|
|
--- Pads str to length len with char from right
|
|
|
|
-- @source https://snipplr.com/view/13092/strlpad--pad-string-to-the-left
|
2021-06-26 03:05:01 +02:00
|
|
|
local function str_padend(str, len, char)
|
2021-03-20 02:48:56 +01:00
|
|
|
if char == nil then char = ' ' end
|
|
|
|
return str .. string.rep(char, len - #str)
|
|
|
|
end
|
|
|
|
--- Pads str to length len with char from left
|
|
|
|
-- Adapted from the above
|
2021-06-26 03:05:01 +02:00
|
|
|
local function str_padstart(str, len, char)
|
2021-03-20 02:48:56 +01:00
|
|
|
if char == nil then char = ' ' end
|
|
|
|
return string.rep(char, len - #str) .. str
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Equivalent to string.startsWith in JS
|
|
|
|
-- @param str string The string to operate on
|
|
|
|
-- @param start number The start string to look for
|
|
|
|
-- @returns bool Whether start is present at the beginning of str
|
2021-06-26 03:05:01 +02:00
|
|
|
local function str_starts(str, start)
|
2021-05-30 14:06:47 +02:00
|
|
|
return string.sub(str, 1, string.len(start)) == start
|
2021-03-20 02:48:56 +01:00
|
|
|
end
|
|
|
|
|
2021-12-31 01:55:46 +01:00
|
|
|
--- Equivalent to string.endsWith in JS
|
|
|
|
-- @param str string The string to operate on
|
|
|
|
-- @param substr_end number The ending string to look for
|
|
|
|
-- @returns bool Whether substr_end is present at the end of str
|
|
|
|
local function str_ends(str, substr_end)
|
|
|
|
return string.sub(str, -string.len(substr_end)) == substr_end
|
|
|
|
end
|
|
|
|
|
2021-03-20 02:48:56 +01:00
|
|
|
--- Trims whitespace from a string from the beginning and the end.
|
|
|
|
-- From http://lua-users.org/wiki/StringTrim
|
|
|
|
-- @param str string The string to trim the whitespace from.
|
|
|
|
-- @returns string A copy of the original string with the whitespace trimmed.
|
2021-06-26 03:05:01 +02:00
|
|
|
local function trim(str)
|
2021-03-20 02:48:56 +01:00
|
|
|
return (str:gsub("^%s*(.-)%s*$", "%1"))
|
|
|
|
end
|
2021-06-26 03:05:01 +02:00
|
|
|
|
|
|
|
|
|
|
|
if worldeditadditions then
|
|
|
|
worldeditadditions.str_padend = str_padend
|
|
|
|
worldeditadditions.str_padstart = str_padstart
|
|
|
|
worldeditadditions.str_starts = str_starts
|
2021-12-31 01:55:46 +01:00
|
|
|
worldeditadditions.str_ends = str_starts
|
2021-06-26 03:05:01 +02:00
|
|
|
worldeditadditions.trim = trim
|
|
|
|
else
|
|
|
|
return {
|
2021-06-26 03:17:49 +02:00
|
|
|
str_padend = str_padend,
|
|
|
|
str_padstart = str_padstart,
|
|
|
|
str_starts = str_starts,
|
2021-12-31 01:55:46 +01:00
|
|
|
str_ends = str_starts,
|
2021-06-26 03:17:49 +02:00
|
|
|
trim = trim
|
2021-06-26 03:05:01 +02:00
|
|
|
}
|
|
|
|
end
|