本文介绍了你能给我在C ++中奇数单行注释的例子吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我写了一个方法来从C ++源文件中删除单行注释:
I wrote a method to remove single line comments from a C++ source file:
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"
}
}
else
{
builder << it << "\n"
}
}
return builder.toString()
}
我测试它:
And I tested it with :
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
""")
生成以下输出:
It produces this output:
this is a test inside double quotes "//inside double quotes"
this is a test inside single quotes '//inside single quotes'
two
single
有没有一些我失踪的情况?
Are there some cases I'm missing?
推荐答案
由和线延续形成。我个人最喜欢的是:
The fun ones are formed by trigraphs and line continuations. My personal favorite is:
/??/
* this is a comment *??/
/
这篇关于你能给我在C ++中奇数单行注释的例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!