- 论坛徽章:
- 84
|
1、strip_c_comment1.awk 正则方法
BEGIN { ORS = "" }
{ code = code $0 "\n" }
END {
while ( length(code) )
code = process( code )
}
function process( text )
{
if ( c99 ) {
if ( match( text, /"|'|\/\*|\/\// ) )
return span( text )
} else {
if ( match( text, /"|'|\/\*/ ) )
return span( text )
}
print text
return ""
}
function span( text , starter )
{
print substr( text, 1, RSTART - 1 )
starter = substr( text, RSTART, RLENGTH )
text = substr( text, RSTART + RLENGTH )
if ( "\"" == starter || "'" == starter )
return quoted( text, starter )
if ( "//" == starter )
return remove( text, "\n", "\n" )
## Allow for
## /* foo *\
## /
return remove( text, "\\*(\\\\\n)?/", " " )
}
function remove( text, ender, replacement )
{
print replacement
return substr( text, match(text, ender) + RLENGTH )
}
function quoted( text, starter )
{
if ( "'" == starter )
match( text, /^(\\.|[^'])*'/ )
else
match( text, /^(\\.|\?\?\/.|[^"])*"/ )
print starter substr( text, 1, RLENGTH )
return substr( text, RSTART + RLENGTH )
}
---------------------------------------------------------------------------------------------------------------
2、strip_c_comment2.awk 状态机,
---------------------------------------------------------------------------------------------------------------
3、借用C预处理器,,
#!/bin/sh
sed 's/#/-_-!/g' "$1" |
cpp - | sed '/#/d' |
sed 's/-_-!/#/g'
----------------------------------------------------------------------------------------
对不住了,暂时先公开第一、三种方法,和第二种方法的思路,想看就先顶一下吧
-------------------------------------------------------------
md竟然被人投诉恶意灌水,原来隐藏是要受道德谴责的,,
[ 本帖最后由 yjh777 于 2009-3-27 19:38 编辑 ] |
评分
-
查看全部评分
|