From 894c254c42817c9b9431f8861a087f240bfaf5f4 Mon Sep 17 00:00:00 2001 From: romildo Date: Fri, 17 Mar 2017 21:13:11 -0300 Subject: [PATCH] Add string split function to gears.string --- lib/gears/string.lua | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/lib/gears/string.lua b/lib/gears/string.lua index 00b181eb..b11da817 100644 --- a/lib/gears/string.lua +++ b/lib/gears/string.lua @@ -84,4 +84,26 @@ function gstring.query_to_pattern(q) end) return s end + +--- Split separates a string containing a delimiter into the list of +-- substrings between that delimiter. +-- @class function +-- @name split +-- @tparam string str String to be splitted +-- @tparam string delimiter Character where the string will be splitted +-- @treturn table list of the substrings +function gstring.split(str, delimiter) + local pattern = "(.-)" .. delimiter .. "()" + local result = {} + local n = 0 + local lastPos = 0 + for part, pos in string.gmatch(str, pattern) do + n = n + 1 + result[n] = part + lastPos = pos + end + result[n + 1] = string.sub(str, lastPos) + return result +end + return gstring