gears.string doc revamp

This commit is contained in:
Seth Barberee 2019-12-13 14:19:38 -06:00
parent 9e3c418a03
commit d05b7d80f1
11 changed files with 72 additions and 8 deletions

View File

@ -11,7 +11,7 @@ local xml_entity_names = { ["'"] = "&apos;", ["\""] = "&quot;", ["<"] = "&lt;",
--- Escape a string from XML char. --- Escape a string from XML char.
-- Useful to set raw text in textbox. -- Useful to set raw text in textbox.
-- @param text Text to escape. -- @param text Text to escape.
-- @return Escape text. -- @treturn string Escaped text.
-- @staticfct gears.string.xml_escape -- @staticfct gears.string.xml_escape
function gstring.xml_escape(text) function gstring.xml_escape(text)
return text and text:gsub("['&<>\"]", xml_entity_names) or nil return text and text:gsub("['&<>\"]", xml_entity_names) or nil
@ -22,13 +22,14 @@ local xml_entity_chars = { lt = "<", gt = ">", nbsp = " ", quot = "\"", apos = "
--- Unescape a string from entities. --- Unescape a string from entities.
-- @param text Text to unescape. -- @param text Text to unescape.
-- @return Unescaped text. -- @treturn string Unescaped text.
-- @staticfct gears.string.xml_unescape -- @staticfct gears.string.xml_unescape
function gstring.xml_unescape(text) function gstring.xml_unescape(text)
return text and text:gsub("&(%a+);", xml_entity_chars) or nil return text and text:gsub("&(%a+);", xml_entity_chars) or nil
end end
--- Count number of lines in a string --- Count number of lines in a string.
-- @DOC_text_gears_string_linecount_EXAMPLE@
-- @tparam string text Input string. -- @tparam string text Input string.
-- @treturn int Number of lines. -- @treturn int Number of lines.
-- @staticfct gears.string.linecount -- @staticfct gears.string.linecount
@ -37,10 +38,11 @@ function gstring.linecount(text)
end end
--- Split a string into multiple lines. --- Split a string into multiple lines.
-- @param text String to wrap. -- @DOC_text_gears_string_linewrap_EXAMPLE@
-- @param width Maximum length of each line. Default: 72. -- @tparam string text String to wrap.
-- @param indent Number of spaces added before each wrapped line. Default: 0. -- @tparam number width Maximum length of each line. Default: 72.
-- @return The string with lines wrapped to width. -- @tparam number indent Number of spaces added before each wrapped line. Default: 0.
-- @treturn string The string with lines wrapped to width.
-- @staticfct gears.string.linewrap -- @staticfct gears.string.linewrap
function gstring.linewrap(text, width, indent) function gstring.linewrap(text, width, indent)
text = text or "" text = text or ""
@ -60,6 +62,9 @@ end
--- Escape all special pattern-matching characters so that lua interprets them --- Escape all special pattern-matching characters so that lua interprets them
-- literally instead of as a character class. -- literally instead of as a character class.
-- Source: http://stackoverflow.com/a/20778724/15690 -- Source: http://stackoverflow.com/a/20778724/15690
-- @DOC_text_gears_string_quote_pattern_EXAMPLE@
-- @tparam string s String to generate pattern for
-- @treturn string string with escaped characters
-- @staticfct gears.string.quote_pattern -- @staticfct gears.string.quote_pattern
function gstring.quote_pattern(s) function gstring.quote_pattern(s)
-- All special characters escaped in a string: %%, %^, %$, ... -- All special characters escaped in a string: %%, %^, %$, ...
@ -68,7 +73,7 @@ function gstring.quote_pattern(s)
end end
--- Generate a pattern matching expression that ignores case. --- Generate a pattern matching expression that ignores case.
-- @param s Original pattern matching expression. -- @tparam string q Original pattern matching expression.
-- @staticfct gears.string.query_to_pattern -- @staticfct gears.string.query_to_pattern
function gstring.query_to_pattern(q) function gstring.query_to_pattern(q)
local s = gstring.quote_pattern(q) local s = gstring.quote_pattern(q)
@ -102,7 +107,9 @@ function gstring.split(str, delimiter)
end end
--- Check if a string starts with another string. --- Check if a string starts with another string.
-- @DOC_text_gears_string_startswith_EXAMPLE@
-- @tparam string str String to search -- @tparam string str String to search
-- @treturn boolean `true` if string starts with specified string
-- @tparam string sub String to check for. -- @tparam string sub String to check for.
-- @staticfct gears.string.startswith -- @staticfct gears.string.startswith
function gstring.startswith(str, sub) function gstring.startswith(str, sub)
@ -110,8 +117,10 @@ function gstring.startswith(str, sub)
end end
--- Check if a string ends with another string. --- Check if a string ends with another string.
-- @DOC_text_gears_string_endswith_EXAMPLE@
-- @tparam string str String to search -- @tparam string str String to search
-- @tparam string sub String to check for. -- @tparam string sub String to check for.
-- @treturn boolean `true` if string ends with specified string
-- @staticfct gears.string.endswith -- @staticfct gears.string.endswith
function gstring.endswith(str, sub) function gstring.endswith(str, sub)
return sub == "" or string.sub(str,-string.len(sub)) == sub return sub == "" or string.sub(str,-string.len(sub)) == sub

View File

@ -0,0 +1,12 @@
--DOC_GEN_OUTPUT --DOC_HIDE
local gears = require("gears") --DOC_HIDE
local test = "do.it"
local res = gears.string.endswith(test,"it")
print(tostring(res))
assert(res == true) --DOC_HIDE
res = gears.string.endswith(test,"do")
print(tostring(res))
assert(res == false) --DOC_HIDE

View File

@ -0,0 +1,2 @@
true
false

View File

@ -0,0 +1,11 @@
--DOC_GEN_OUTPUT --DOC_HIDE
local gears = require("gears") --DOC_HIDE
local test = "do.t"
local res = gears.string.linecount(test)
print("Count is: " .. res)
local test2 = "do\nit\nnow"
local res2 = gears.string.linecount(test2)
print("Count is: " .. res2)

View File

@ -0,0 +1,2 @@
Count is: 1
Count is: 3

View File

@ -0,0 +1,6 @@
--DOC_GEN_OUTPUT --DOC_HIDE
local gears = require("gears") --DOC_HIDE
local test = "do it"
local res = gears.string.linewrap(test, 2, 0)
print(res)

View File

@ -0,0 +1,2 @@
do
it

View File

@ -0,0 +1,6 @@
--DOC_GEN_OUTPUT --DOC_HIDE
local gears = require("gears") --DOC_HIDE
local test = "do.it"
local res = gears.string.quote_pattern(test)
print(res)

View File

@ -0,0 +1 @@
do%.it

View File

@ -0,0 +1,11 @@
--DOC_GEN_OUTPUT --DOC_HIDE
local gears = require("gears") --DOC_HIDE
local test = "do.it"
local res = gears.string.startswith(test,"do")
print(tostring(res)) -- Print boolean value
assert(res == true) --DOC_HIDE
res = gears.string.startswith(test,"it")
print(tostring(res)) -- print boolean value
assert(res == false) --DOC_HIDE

View File

@ -0,0 +1,2 @@
true
false