你能给我在C ++中奇数单行注释的例子吗? [英] Can you give me examples of odd single line comments in C++?

查看:182
本文介绍了你能给我在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()
}

} else { builder << it << "\n" } } return 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
""")


this is a test inside double quotes "//inside double quotes"
this is a test inside single quotes '//inside single quotes'
two
single

有些情况我缺少吗?

推荐答案

有趣的是由三字母和行连续。我个人最喜欢的是:

The fun ones are formed by trigraphs and line continuations. My personal favorite is:

/??/
* this is a comment *??/
/

这篇关于你能给我在C ++中奇数单行注释的例子吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆