A simple find/replace will not work for code commented out like this:
ex:
/* templine1
templine2
templine3 */
Got any ideas?
A simple find/replace will not work for code commented out like this:
ex:
/* templine1
templine2
templine3 */
Got any ideas?
Step by step:
Steps 8-10 are not strictly necessary.
A carefully-constructed regular expression might get you where you want to go.
(OK, it's not homework ... try here ;) )
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.
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;