Final works with string api
This commit is contained in:
@@ -1,29 +1,31 @@
|
||||
local pairs, string_gsub
|
||||
= pairs, string.gsub
|
||||
|
||||
string.replace = string.gsub
|
||||
local sub, upper, ensureArgType
|
||||
= string_utf8.sub, string_utf8.upper, Ensure.argType
|
||||
|
||||
--- Writes a string with a capital letter.
|
||||
--- @param str string Input string.
|
||||
--- @return string
|
||||
function string.capitalize(str)
|
||||
Ensure.argType(str, "string", 1, "string.capitalize")
|
||||
Ensure.argNotNil(str, 1,"string.capitalize")
|
||||
ensureArgType(str, "string", 1, "string.capitalize")
|
||||
|
||||
local first_char = string_utf8.sub(str,1, 1)
|
||||
first_char = string_utf8.upper(first_char)
|
||||
local first_char = sub(str,1, 1)
|
||||
|
||||
return first_char..string_utf8.sub(str,2, -1)
|
||||
if not first_char then
|
||||
return
|
||||
end
|
||||
|
||||
first_char = upper(first_char)
|
||||
|
||||
return first_char..sub(str,2, -1)
|
||||
end
|
||||
|
||||
--- Capitalizes each word in a line.
|
||||
--- @param str string Input string.
|
||||
--- @return string
|
||||
function string.to_title(str)
|
||||
Ensure.argType(str, "string", 1, "string.to_title")
|
||||
Ensure.argNotNil(str, 1,"string.to_title")
|
||||
ensureArgType(str, "string", 1, "string.to_title")
|
||||
|
||||
local result = ""
|
||||
|
||||
for _, word in ipairs(str:split(" ")) do
|
||||
result = string.format("%s%s ", result, word:capitalize())
|
||||
end
|
||||
@@ -35,8 +37,7 @@ end
|
||||
--- @param str string Input string.
|
||||
--- @return string
|
||||
function string.remove_underscores(str)
|
||||
Ensure.argType(str, "string", 1, "string.remove_underscores")
|
||||
Ensure.argNotNil(str, 1,"string.remove_underscores")
|
||||
ensureArgType(str, "string", 1, "string.remove_underscores")
|
||||
|
||||
return str:gsub("_", " ")
|
||||
end
|
||||
@@ -46,11 +47,8 @@ end
|
||||
--- @param prefix string The string the `str` should start with.
|
||||
--- @return boolean
|
||||
function string.starts_with(str, prefix)
|
||||
Ensure.argType(str, "string", 1, "string.starts_with")
|
||||
Ensure.argType(prefix, "string", 2, "string.starts_with")
|
||||
|
||||
Ensure.argNotNil(str, 1,"string.starts_with")
|
||||
Ensure.argNotNil(prefix, 2,"string.starts_with")
|
||||
ensureArgType(str, "string", 1, "string.starts_with")
|
||||
ensureArgType(prefix, "string", 2, "string.starts_with")
|
||||
|
||||
return str:sub(1, #prefix) == prefix
|
||||
end
|
||||
@@ -61,11 +59,8 @@ end
|
||||
--- @param suffix string The string the `str` should end with.
|
||||
--- @return boolean
|
||||
function string.ends_with(str, suffix)
|
||||
Ensure.argType(str, "string", 1, "string.ends_with")
|
||||
Ensure.argType(suffix, "string", 2, "string.ends_with")
|
||||
|
||||
Ensure.argNotNil(str, 1,"string.ends_with")
|
||||
Ensure.argNotNil(suffix, 2,"string.ends_with")
|
||||
ensureArgType(str, "string", 1, "string.ends_with")
|
||||
ensureArgType(suffix, "string", 2, "string.ends_with")
|
||||
|
||||
return str:sub(-#suffix, -1) == suffix
|
||||
end
|
||||
@@ -75,11 +70,62 @@ end
|
||||
--- @param sub_string string The text segment to search for within the string.
|
||||
--- @return boolean
|
||||
function string.contains(str, sub_string)
|
||||
Ensure.argType(str, "string", 1, "string.contains")
|
||||
Ensure.argType(sub_string, "string", 2, "string.contains")
|
||||
|
||||
Ensure.argNotNil(str, 1,"string.contains")
|
||||
Ensure.argNotNil(sub_string, 2,"string.contains")
|
||||
ensureArgType(str, "string", 1, "string.contains")
|
||||
ensureArgType(sub_string, "string", 2, "string.contains")
|
||||
|
||||
return str:find(sub_string, 1, true) ~= nil
|
||||
end
|
||||
|
||||
|
||||
|
||||
-- EMMYLUA ANNOTATIONS OVERWRITES --
|
||||
|
||||
--- Returns a copy of `s` in which all (or the first `n`, if given)
|
||||
--- occurrences of the `pattern` have been replaced by a replacement string
|
||||
--- specified by `repl`, which can be a string, a table, or a function. `gsub`
|
||||
--- also returns, as its second value, the total number of matches that
|
||||
--- occurred.
|
||||
---
|
||||
--- If `repl` is a string, then its value is used for replacement. The character
|
||||
--- `%` works as an escape character: any sequence in `repl` of the form `%n`,
|
||||
--- with *n* between 1 and 9, stands for the value of the *n*-th captured
|
||||
--- substring (see below). The sequence `%0` stands for the whole match. The
|
||||
--- sequence `%%` stands for a single `%`.
|
||||
---
|
||||
--- If `repl` is a table, then the table is queried for every match, using
|
||||
--- the first capture as the key; if the pattern specifies no captures, then
|
||||
--- the whole match is used as the key.
|
||||
---
|
||||
--- If `repl` is a function, then this function is called every time a match
|
||||
--- occurs, with all captured substrings passed as arguments, in order; if
|
||||
--- the pattern specifies no captures, then the whole match is passed as a
|
||||
--- sole argument.
|
||||
---
|
||||
--- If the value returned by the table query or by the function call is a
|
||||
--- string or a number, then it is used as the replacement string; otherwise,
|
||||
--- if it is false or nil, then there is no replacement (that is, the original
|
||||
--- match is kept in the string).
|
||||
---
|
||||
--- Here are some examples:
|
||||
--- `x = string.gsub("hello world", "(%w+)", "%1 %1")`
|
||||
--- `-- > x="hello hello world world"`
|
||||
--- `x = string.gsub("hello world", "%w+", "%0 %0", 1)`
|
||||
--- `-- > x="hello hello world"`
|
||||
--- `x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1")`
|
||||
--- `-- > x="world hello Lua from"`
|
||||
--- `x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv)`
|
||||
--- `-- > x="home = /home/roberto, user = roberto"`
|
||||
--- `x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s)`
|
||||
--- >> return loadstring(s)()
|
||||
--- > end)
|
||||
--- `-- > x="4+5 = 9"`
|
||||
--- `local t = {name="lua", version="5.3"}`
|
||||
--- `x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t)`
|
||||
--- > x="lua-5.3.tar.gz"
|
||||
---@overload fun(s:string, pattern:string, repl:string|table|fun()):string, number
|
||||
---@param s string
|
||||
---@param pattern string
|
||||
---@param repl string|table|fun(param:string)
|
||||
---@param n? number
|
||||
---@return string, number
|
||||
function string.gsub(s, pattern, repl, n) end
|
||||
Reference in New Issue
Block a user