goer%sophist@GARGOYLE.UCHICAGO.EDU (Richard Goerwitz) (09/27/90)
Thanks everyone for the help with stripping out comments. I
kinda synthesized all the postings and came up with the procedure
appended below. Note that it now runs faster than any of the
previous postings people (including me) have made:
procedure strip_comments(s)
#######
#
# Commented-out portions of Icon code - strip 'em. Fails on lines
# which, either stripped or otherwise, come out as an empty string.
#
# I'd expect strip_comments to be used typically as follows:
#
# every write(strip_comments(!&input))
#
#######
local i, j, c, c2
s ? {
tab(many(' \t'))
match("#") & fail
(s2 <- tab(find("#"))) ? {
c2 := &null
while tab(upto('\\"\'')) do {
case c := move(1) of {
"\\" : {
if match("^")
then move(2)
else move(1)
}
default: {
if \c2
then (c == c2, c2 := &null)
else c2 := c
}
}
}
/c2
}
return "" ~== trim((\s2 | tab(0)) \ 1, ' \t')
}
end
-Richard L. Goerwitz goer%sophist@uchicago.bitnet
goer@sophist.uchicago.edu rutgers!oddjob!gide!sophist!goer