[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: hint: paragraph parsing
- From: Asko Kauppi <asko.kauppi@...>
- Date: Thu, 16 Sep 2004 15:38:10 +0300
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 )
end
-- The left-overs..
--
Loc_AddParagraph( ret, string.sub( str, last_pos ) )
-ak