From 5d0e9fd9e300a95119264419b00c388bd9f91475 Mon Sep 17 00:00:00 2001 From: Kevin Zander Date: Mon, 14 Aug 2017 10:48:12 -0500 Subject: [PATCH 1/2] Add startswith and endswith function to gears.string, change awful.completion to use startswith over local function --- lib/awful/completion.lua | 7 ++----- lib/gears/string.lua | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/lib/awful/completion.lua b/lib/awful/completion.lua index c62f22c0..4fd14840 100644 --- a/lib/awful/completion.lua +++ b/lib/awful/completion.lua @@ -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)) diff --git a/lib/gears/string.lua b/lib/gears/string.lua index b11da817..186082ff 100644 --- a/lib/gears/string.lua +++ b/lib/gears/string.lua @@ -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 From e7bf8de081948fb1a3e8bd6087bce3795b4c5752 Mon Sep 17 00:00:00 2001 From: Uli Schlachter Date: Mon, 21 Aug 2017 09:05:37 +0200 Subject: [PATCH 2/2] Add tests for gears.string.startswith and endswith Signed-off-by: Uli Schlachter --- spec/gears/string_spec.lua | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spec/gears/string_spec.lua b/spec/gears/string_spec.lua index 817bdbed..11f7e6e3 100644 --- a/spec/gears/string_spec.lua +++ b/spec/gears/string_spec.lua @@ -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