On Tue, 2004-05-25 at 07:59, Steve Wampler wrote:
>
> (1) ...(A separate challenge
> would be to handle a set of tags instead of just one, but
> please do that separately if you're interested in it!)
And here's another solution that solves this separate challenge
as well (any solution the 2nd should also handle the first, as
William Mitchell's solution also demostrates.
The procedure flatten() from the previous solution has been
made into a class - there's really very little need to do
so, but it does avoid adding several global variables and
encapsulates the initialization code.
The definition of just what a tag is (and how to make it into
a starttag and endtag for matching) is contained entirely
within that initialization code - making it fairly easy to
adapt that definition to other situations. Otherwise, it
lacks the simple elegance of William's approach.
----------------------------------------------------------
#<p>
# Flatten tags by removing nested instances. All tags are independent.
#</p>
import Utils # for StringUtils and ScanUtils
procedure main(args)
local tagSet
every insert(tagSet := set(), zapPrefix(!args, "--tag="))
if *tagSet = 0 then {
stop("Usage: flatten [--tag=TAG]+\n"||
" e.g. flatten --tag=x --tag=y")
}
Flatten(tagSet).flatten()
end
class Flatten : Class (tags, tagCnt, tagStart, tagEnd, ff)
method flatten(f)
local bRead, inside, tag
bRead := BlockRead(f)
inside := 0
while bRead.readBlock() ? {
while writes(tab(ff.locate())) do {
tag := ff.moveMatch()
if ((tagCnt[\tagStart[tag]] +:= 1) = 1) |
((tagCnt[\tagEnd[tag]] -:= 1) = 0) then writes(tag)
# malformed input? reset and continue
tagCnt[\((tagStart|tagEnd)[tag])] <:= 0
}
writes(tab(0))
}
# repair any remaining malformed input
every tagCnt[eTag := !tagEnd] > 0 do write("</",eTag,">")
return
end
initially (tSet)
tagCnt := table(0)
tags := ::set()
tagStart := table()
tagEnd := table()
every tag := !tSet do {
insert(tags, sTag := "<"||tag||">")
insert(tags, eTag := "</"||tag||">")
tagStart[sTag] := tag
tagEnd[eTag] := tag
}
ff := FindFirst(tags)
end
------------------------------------------------------------
--
Steve Wampler -- [EMAIL PROTECTED]
The gods that smiled on your birth are now laughing out loud.
-------------------------------------------------------
This SF.Net email is sponsored by: Oracle 10g
Get certified on the hottest thing ever to hit the market... Oracle 10g.
Take an Oracle 10g class now, and we'll give you the exam FREE.
http://ads.osdn.com/?ad_id=3149&alloc_id=8166&op=click
_______________________________________________
Unicon-group mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/unicon-group