From 868baff3981da75811d25861ee306f07202046a0 Mon Sep 17 00:00:00 2001 From: Toni Gundogdu Date: Tue, 30 Apr 2013 17:33:17 +0300 Subject: [PATCH] quvi/util: Add the `tokenize' function Signed-off-by: Toni Gundogdu --- share/common/quvi/util.lua | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/share/common/quvi/util.lua b/share/common/quvi/util.lua index 177d638..4601fde 100644 --- a/share/common/quvi/util.lua +++ b/share/common/quvi/util.lua @@ -142,6 +142,28 @@ function M.trim(s) return s:gsub('(.)%s+$', '%1') end +--[[ +Tokenize a string. +Parameters: + s .. String to tokenize + sep .. Separator +Returns: + Tokenized items in a table. +]]-- +function M.tokenize(s,sep) -- Based on http://lua-users.org/wiki/SplitJoin + local sep, fields = sep or ':', {} + local pattern = string.format('([^%s]+)', sep) + s:gsub(pattern, function(c) fields[#fields+1] = c + end) + return fields +end + +-- Uncomment to test +--[[ +package.path = package.path .. ';../?.lua' +for _,v in pairs(M.tokenize('a,b,c,d,e',',')) do print(v) end +]]-- + return M -- vim: set ts=2 sw=2 tw=72 expandtab: -- 2.11.4.GIT