c++ - 你能给我一些 C++ 中奇怪的单行注释的例子吗?

标签 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
     } 
    }<p></p>

<pre><code>if(!process)
{
 def line = it.substring(0,singleCommentPos)
 builder &lt;&lt; line &lt;&lt; "\n"
}
else
{
 builder &lt;&lt; it &lt;&lt; "\n" 
}
</code></pre>

<p>}
   else
   {
    builder << it << "\n"
   }
  }
  return builder.toString()
 }
</p>

我测试了它:

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

我是否遗漏了一些案例?

最佳答案

有趣的是由trigraphs组成的和续行。我个人最喜欢的是:

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

关于c++ - 你能给我一些 C++ 中奇怪的单行注释的例子吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1618419/

相关文章:

java - 如何测试事务数据库代码?

Python:任何用于在行连接中间注释掉一行的变通方法

java - Matlab中多行注释的正则表达式

comments - PyCharm:自动对齐行内注释

c++ - 需要帮助理解 MFC 项目

c++ - 3D 模型格式之间的转换如何工作? (例如 Collada *.dae 到 ACIS *.sat)

c++ - 为什么在预处理中使用负像?

gradle - Gradle-uploadArchives生成的快照与createJar不同

groovy - 如何使用 Groovy Set 来获取独特元素?

c++ - 数独递归,接受零为合法