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.
  (
https://github.com/martin-eden/workshop/blob/master/string/iterate_by_lines.lua#L11
)

--[[ 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))
    end
  end

local c_reader = require('reader')

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

local test_file = io.open('test.txt', 'wb')
test_file:write(test_str)
test_file:close()
local test_file = io.open('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
[1]["1"]
[4]["3"]
--[ file
[0]["1"]
[2][""]
[3]["3"]
--[ table
[1]["1"]