views:

123

answers:

5

A simple find/replace will not work for code commented out like this:

ex:

/* templine1
   templine2
   templine3 */

Got any ideas?

A: 

Step by step:

  1. put your cursor on the right side of the first *.
  2. Hit backspace
  3. Hit backspace
  4. type '//'
  5. hit down arrow
  6. repeat steps 2-5
  7. repeat steps 2-4
  8. Hit end
  9. hit backspace
  10. hit backspace.

Steps 8-10 are not strictly necessary.

Noah Roberts
Why the downvote? With no script language specified this is a simple bit of pseudocode that will do what was requested. It is pretty trivial to take this and make a script that performs this behavior.
tloach
Trivial? Hardly! You're assuming that the the first star is actually a comment sentry. And that it's not already in a single-line comment. And that it's not in quotes. Etc. Etc.
John at CashCommons
Perfectly reasonable assumption given the specified input. But yeah, I did get my hands mixed up.
Noah Roberts
@Noah: yeah, this is not a good way to approach the issue. I suggest a regex. :)
Paul Nathan
@Paul: Now you've got two problems.
greyfade
@greyfade: well, that's what jwz said. Possibly both are solved now!
Paul Nathan
+1  A: 

A carefully-constructed regular expression might get you where you want to go.

(OK, it's not homework ... try here ;) )

John at CashCommons
I've edited my perl script to use that regex.
Paul Nathan
+1  A: 

Be careful... the following is legal:

if (x > /*let's think...
          I think comparing it to 3 will be a good idea! */ 3) {
     peanut();
}

So any script will have to make sure to put the single-line comment at the end of the line. This loses the precise location of the comment, plus you need to think what to do with things like this:

if (/*hmm...
      x?*/ x /* yes,
      x! */ > 3) {
    butter();
}

So you probably want to restrict yourself to comment blocks not appearing on the same line with other code. In that case, be aware block comments can be captured by a single regular expression, so a small Perl script could probably do the job.

EDIT: actually, "code not appearing in the same line" is not enough:

char* s = "hello \
           /* this is not a comment";
           /* this is */

EDIT2: If you want to cover all corner cases, I think a better solution is to tokenize the entire file. Since you can ignore many things, it wouldn't be too difficult - I've done so myself, in the past, for a C-like language. Once you get a token stream you can go over it, holding a "status" mode to keep track of strings / single-line comments / multiline comments.

Oak
+1  A: 

Perl hack, not tested or proved; will break some code that has f(blah /*, bar*/) in it

#open the $file and read it in
my ($fh, $file);
open $fh, "<", $ARGV[0] or die($!);

{ 
 local $/ = undef; 
 $file = <$fh>; 
}
close $fh;

#process it. does some assumptions about aliasing here, may not be valid.
#used the link from elsewhere for the regex
foreach my $comment ($file =~ m//\*(?:.|[\r\n])*?\*///g)
{
  my @lines = split(/\n/, $comment);
  s/^/\/\/ for @lines;
  $comment = join("\n", @lines);
}

open ">", $ARGV[0] or die($!);
print $fh $file;
close $fh;
Paul Nathan
This is not string-proof, and I think it's also not "\" proof (see the edit to my answer).
Oak
@Oak - added the regex from above
Paul Nathan
+1  A: 

If you use vim, you can use the Nerd Commenter plugin.

Nathan Fellman