lines

function lines( str ) --> iter

Description

Returns an interator over all lines in a string.

Parameters

str

Multi-line string.

Return Values

iter

Iterator over all lines in a string that returns a line.

Code

--ZFUNC-lines-v1
local function lines( str ) --> iter
   if not str:find( "\n$" ) then str = str.."\n" end
   return str:gmatch( "([^\n]*)\n" )
end

return lines

Examples

local t = require( "tapered" )
local lines = require( "lines" )

str = "a\nbcdef\ngh\n\n123"

tab = {}
for l in lines( str ) do
   table.insert( tab, l )
end

t.same( { "a", "bcdef", "gh", "", "123" }, tab )

t.done()