From dbddbd9b0cec1bfe8e92a39552b28ab2f11195d9 Mon Sep 17 00:00:00 2001 From: Mike Mason Date: Wed, 28 Nov 2007 15:26:13 +0000 Subject: added split and join to fs.lua and gave them function names so we don't have to strsplit = require split. Added a date string to table function in date.lua git-svn-id: svn://svn.alpinelinux.org/acf/core/trunk@378 ab2d0c66-481e-0410-8bed-d214d4d58bed --- lib/fs.lua | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'lib/fs.lua') diff --git a/lib/fs.lua b/lib/fs.lua index 88df9f1..79d0cdc 100644 --- a/lib/fs.lua +++ b/lib/fs.lua @@ -155,3 +155,39 @@ function find ( what, where ) end end +-- This code comes from http://lua-users.org/wiki/SplitJoin +-- -- example: strjoin(", ", {"Anna", "Bob", "Charlie", "Dolores"}) +function table_to_string (delimiter, list) + local len = getn(list) + if len == 0 then + return "" + end + local string = list[1] + for i = 2, len do + string = string .. delimiter .. list[i] + end + return string +end + +-- This code comes from http://lua-users.org/wiki/SplitJoin +-- example: strsplit(",%s*", "Anna, Bob, Charlie,Dolores") +function string_to_table (delimiter, text) + local list = {} + local pos = 1 + -- this would result in endless loops + if string.find("", delimiter, 1) then + error("delimiter matches empty string!") + end + while 1 do + local first, last = string.find(text, delimiter, pos) + if first then -- found? + table.insert(list, string.sub(text, pos, first-1)) + pos = last+1 + else + table.insert(list, string.sub(text, pos)) + break + end + end + return list +end + -- cgit v1.2.3