Merge pull request #1992 from Veratil/string-funcs
Add two new gears.string functions: startswith, endswith.
This commit is contained in:
commit
aa1ff23b19
|
@ -21,6 +21,7 @@ local pairs = pairs
|
|||
local string = string
|
||||
|
||||
local gears_debug = require("gears.debug")
|
||||
local gstring = require("gears.string")
|
||||
|
||||
local completion = {}
|
||||
|
||||
|
@ -79,10 +80,6 @@ function completion.shell(command, cur_pos, ncomp, shell)
|
|||
local i = 1
|
||||
local comptype = "file"
|
||||
|
||||
local function str_starts(str, start)
|
||||
return string.sub(str, 1, string.len(start)) == start
|
||||
end
|
||||
|
||||
-- do nothing if we are on a letter, i.e. not at len + 1 or on a space
|
||||
if cur_pos ~= #command + 1 and command:sub(cur_pos, cur_pos) ~= " " then
|
||||
return command, cur_pos
|
||||
|
@ -160,7 +157,7 @@ function completion.shell(command, cur_pos, ncomp, shell)
|
|||
while true do
|
||||
local line = c:read("*line")
|
||||
if not line then break end
|
||||
if str_starts(line, "./") and gfs.is_dir(line) then
|
||||
if gstring.startswith(line, "./") and gfs.is_dir(line) then
|
||||
line = line .. "/"
|
||||
end
|
||||
table.insert(output, bash_escape(line))
|
||||
|
|
|
@ -106,4 +106,22 @@ function gstring.split(str, delimiter)
|
|||
return result
|
||||
end
|
||||
|
||||
--- Check if a string starts with another string
|
||||
-- @class function
|
||||
-- @name startswith
|
||||
-- @tparam string str String to search
|
||||
-- @tparam string sub String to check for
|
||||
function gstring.startswith(str, sub)
|
||||
return string.sub(str, 1, string.len(sub)) == sub
|
||||
end
|
||||
|
||||
--- Check if a string ends with another string
|
||||
-- @class function
|
||||
-- @name endswith
|
||||
-- @tparam string str String to search
|
||||
-- @tparam string sub String to check for
|
||||
function gstring.endswith(str, sub)
|
||||
return sub == "" or string.sub(str,-string.len(sub)) == sub
|
||||
end
|
||||
|
||||
return gstring
|
||||
|
|
|
@ -29,4 +29,18 @@ describe("gears.string", function()
|
|||
assert.is.equal(string.match("DownLow", gstring.query_to_pattern("ownl")), "ownL")
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("startswith", function()
|
||||
assert.is_true(gstring.startswith("something", ""))
|
||||
assert.is_true(gstring.startswith("something", "some"))
|
||||
assert.is_false(gstring.startswith("something", "none"))
|
||||
end)
|
||||
|
||||
describe("endswith", function()
|
||||
assert.is_true(gstring.endswith("something", ""))
|
||||
assert.is_true(gstring.endswith("something", "thing"))
|
||||
assert.is_false(gstring.endswith("something", "that"))
|
||||
end)
|
||||
end)
|
||||
|
||||
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|
||||
|
|
Loading…
Reference in New Issue