lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On 02/20/2017 07:38 AM, Dirk Laurie wrote:
> Martin should
> be pleased to see there is (briefly) a license and usage example in
> the inlined comments, which are formatted to be visible to LDoc
> (thus creating external docs) and IHelp. This email serves as the
> readme. Self-tests via standalone test system, training course videos
> and product site will have to wait for Reader 2.0.

Yes Dirk, I'm pleased! (Although lack of training video course settles
a deep sorrow in my soul.)

I've tested it a bit and it have different behavior for files,
strings and tables.

For files it natively wraps io.lines(). For strings it skips empty
lines. For tables I think some agreement should be defined whether
sequences in table can contain nils or only string values allowed.

Personally when working with strings I prefer function to return empty
string in case of empty "line" in current postion (as io.lines() do).

BTW I have similar impelmentation for string iterator written
for personal usage and amusement.

--[[ test_reader.lua ]]--
local print_results =
  function(reader, header)
    header = header or ''
    print(('--[ %s'):format(header))
    for line, pos in reader do
      print(('[%d][%q]'):format(pos, line))

local c_reader = require('reader')

local test_str = '1\n\n3'
local reader = c_reader(test_str)
print_results(reader, 'string')

local test_file ='test.txt', 'wb')
local test_file ='test.txt', 'rb')
local reader = c_reader(test_file)
print_results(reader, 'file')

local test_tbl = {'1', nil, '3'}
local reader = c_reader(test_tbl)
print_results(reader, 'table')

--[[ output ]]--
--[ string
--[ file
--[ table