Fixed string splitting and added convenience funct to output tables to stdout

This commit is contained in:
sloumdrone 2019-01-04 17:55:53 -08:00
parent 379085f971
commit d90f8ff97d
1 changed files with 23 additions and 8 deletions

View File

@ -8,19 +8,34 @@ session = { history={ loc=0, items={} }, current_links = {}, favorites={} }
local save_file = string.format('%s/.stubbfaves', assert(os.getenv('HOME'), 'Unable to get home directory'))
string.split = function(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t={}; i=1
for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
t[i] = str
i = i + 1
function string.split(str, sep)
assert(str and sep, 'sting.split requires both an input string and a field separator string')
local start = 1
local t = {}
while true do
local out
local point = string.find(str, sep, start)
if point == start then
out = ''
elseif not point then
out = string.sub(str, start)
else
out = string.sub(str, start, point - 1)
end
table.insert(t, out)
start = point and point + 1 or break
end
return t
end
function table.out(t)
for i, v in ipairs(t) do
print(string.format('%q', v))
end
end
table.has = function(t, item)
for i, v in pairs(t) do
if v == item then