Protect endswith and startswith from nil str

A nil parameter can mean an empty string (e.g. client.name), so we
just return false.
This commit is contained in:
Grumph 2021-01-26 17:58:25 +01:00
parent 66b1780d85
commit f6a2306d1a
1 changed files with 2 additions and 2 deletions

View File

@ -116,7 +116,7 @@ end
-- @tparam string sub String to check for.
-- @staticfct gears.string.startswith
function gstring.startswith(str, sub)
return string.sub(str, 1, string.len(sub)) == sub
return str and (string.sub(str, 1, string.len(sub)) == sub) or false
end
--- Check if a string ends with another string.
@ -126,7 +126,7 @@ end
-- @treturn boolean `true` if string ends with specified string
-- @staticfct gears.string.endswith
function gstring.endswith(str, sub)
return sub == "" or string.sub(str,-string.len(sub)) == sub
return str and (sub == "" or string.sub(str,-string.len(sub)) == sub) or false
end
return gstring