Geo*_*Geo 0 c++ groovy comments header
我写了一个方法来从C++源文件中删除单行注释:
def stripRegularComments(text) {
def builder = new StringBuilder()
text.eachLine {
def singleCommentPos = it.indexOf("//")
def process = true
if(singleCommentPos > -1)
{
def counter = 0
it.eachWithIndex
{ obj,i ->
if((obj == '\'') || (obj == '"'))
counter++
if(i == singleCommentPos)
{
process = ((counter % 2) == 1)
if(!process)
return
}
}
if(!process)
{
def line = it.substring(0,singleCommentPos)
builder << line << "\n"
}
else
{
builder << it << "\n"
}
Run Code Online (Sandbox Code Playgroud)
} else {builder << it <<"\n"}}返回builder.toString()}
我测试了它:
println a.stripRegularComments("""
this is a test inside double quotes "//inside double quotes"
this is a test inside single quotes '//inside single quotes'
two// a comment?//other
single //comment
""")
Run Code Online (Sandbox Code Playgroud)
它产生这个输出:
this is a test inside double quotes "//inside double quotes"
this is a test inside single quotes '//inside single quotes'
two
single
有些情况我不见了吗?
D.S*_*ley 11
有趣的是由三字母和线条延续形成的.我个人最喜欢的是:
/??/
* this is a comment *??/
/
Run Code Online (Sandbox Code Playgroud)
Pum*_*uli 10
// Single line comments can\
actually be multi line.
Run Code Online (Sandbox Code Playgroud)
我想你无法处理
puts("Test \
// not a comment");
Run Code Online (Sandbox Code Playgroud)
这也可能会产生问题:
puts("'"); // this is a comment
Run Code Online (Sandbox Code Playgroud)
您似乎没有处理转义引号,例如:
"Comment\"//also inside string"
Run Code Online (Sandbox Code Playgroud)
与
"Comment"//not inside string"
Run Code Online (Sandbox Code Playgroud)