开发者

Can you give me examples of odd single line comments in 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 *??/
/


// Single line comments can\
actually be multi line.


I think you can't handle

  puts("Test \
    // not a comment");

and this is also likely to make problems:

  puts("'"); // this is a comment


You don't seem to handle escaped quotes, like:

"Comment\"//also inside string"

versus

"Comment"//not inside string"


I think you are missing the /* comment */ case.


The handling of \ character at the end of the line is performed at the earlier translation phase (phase 2) than replacement of comments (phase 3). For this reason, a // comment can actually occupy more than one line in the original source file

// This \
whole thing \
is actually \
a single comment

P.S. Oh... I see this is already posted. OK, I'll keep it alive just for mentioning phases of translation :)


This is always a favourite:

// Why doesn't this run?????????????????????/
foo(bar);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜