Merge pull request #3249 from Grumph/gears_improvements
Make gears.string.*with functions more permissive
This commit is contained in:
commit
4319b16110
|
@ -116,7 +116,7 @@ end
|
||||||
-- @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)
|
||||||
return string.sub(str, 1, string.len(sub)) == sub
|
return str and (string.sub(str, 1, string.len(sub)) == sub) or false
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Check if a string ends with another string.
|
--- Check if a string ends with another string.
|
||||||
|
@ -126,7 +126,7 @@ end
|
||||||
-- @treturn boolean `true` if string ends with specified string
|
-- @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 str and (sub == "" or string.sub(str,-string.len(sub)) == sub) or false
|
||||||
end
|
end
|
||||||
|
|
||||||
return gstring
|
return gstring
|
||||||
|
|
|
@ -34,12 +34,14 @@ describe("gears.string", function()
|
||||||
assert.is_true(gstring.startswith("something", ""))
|
assert.is_true(gstring.startswith("something", ""))
|
||||||
assert.is_true(gstring.startswith("something", "some"))
|
assert.is_true(gstring.startswith("something", "some"))
|
||||||
assert.is_false(gstring.startswith("something", "none"))
|
assert.is_false(gstring.startswith("something", "none"))
|
||||||
|
assert.is_false(gstring.startswith(nil, "anything"))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe("endswith", function()
|
describe("endswith", function()
|
||||||
assert.is_true(gstring.endswith("something", ""))
|
assert.is_true(gstring.endswith("something", ""))
|
||||||
assert.is_true(gstring.endswith("something", "thing"))
|
assert.is_true(gstring.endswith("something", "thing"))
|
||||||
assert.is_false(gstring.endswith("something", "that"))
|
assert.is_false(gstring.endswith("something", "that"))
|
||||||
|
assert.is_false(gstring.endswith(nil, "anything"))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe("split", function()
|
describe("split", function()
|
||||||
|
|
|
@ -10,3 +10,6 @@ res = gears.string.endswith(test,"do")
|
||||||
print(tostring(res))
|
print(tostring(res))
|
||||||
assert(res == false) --DOC_HIDE
|
assert(res == false) --DOC_HIDE
|
||||||
|
|
||||||
|
res = gears.string.endswith(nil,"it")
|
||||||
|
print(tostring(res))
|
||||||
|
assert(res == false) --DOC_HIDE
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
true
|
true
|
||||||
false
|
false
|
||||||
|
false
|
||||||
|
|
|
@ -9,3 +9,7 @@ assert(res == true) --DOC_HIDE
|
||||||
res = gears.string.startswith(test,"it")
|
res = gears.string.startswith(test,"it")
|
||||||
print(tostring(res)) -- print boolean value
|
print(tostring(res)) -- print boolean value
|
||||||
assert(res == false) --DOC_HIDE
|
assert(res == false) --DOC_HIDE
|
||||||
|
|
||||||
|
res = gears.string.startswith(nil,"do")
|
||||||
|
print(tostring(res)) -- print boolean value
|
||||||
|
assert(res == false) --DOC_HIDE
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
true
|
true
|
||||||
false
|
false
|
||||||
|
false
|
||||||
|
|
Loading…
Reference in New Issue