lua-users home
lua-l archive

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

Just letting you know..

Doing a custom iterator is the Right Thing in the case I showed below. I feel embarrased, having shown a 'neat trick' just to realize how overly complicated it was.. So sorry. <:$


16.9.2004 kello 15:38, Asko Kauppi kirjoitti:

Linewise splitting of string data is quite trivial with string.gfind() but handling paragraphs (split at 2 or more newlines, with spaces ignored) is tougher.

Here's a solution I crafted, and which seems to work. I was especially delighted by the usability of 'capture position' to get info where the iterator is going. I believe this trick can be usable elsewhere, too. Without it, you won't know if your patterns jump over stuff or not.

Here's the code fragment:

	local ret= {}
    local last_pos

    -- note: '()' captures the position index
for s, pos in string.gfind( str, "(.-\n%s*\n)()" ) do -- hey, this works even with >2 linefeeds! (also linefeed is %s :)
        --io.stderr:write( "<<<<<<\n"..s.."\n>>>>>>\n" )

        last_pos= pos   -- remember last position (for tail handling)

        Loc_AddParagraph( ret, s )

    -- The left-overs..
    Loc_AddParagraph( ret, string.sub( str, last_pos ) )