Changing Comments from C++ style to C style
I have a C source file having comments in //
(C++) 开发者_开发问答style. I want to change all the comments to the old /* */
(C) style. Is there a way to do this using an existing script?
A substitution with you favorite editor and a regular expression like s#//(.*)#/*\1 */#
should do it...
If you are looking for something a little more generic, you could also use a source code formatting tool for this. For C, I've used uncrustify before and it worked reasonably well. There may be others as well, but I think uncrustify can change C++ style comments into C style comments with the cmt_cpp_to_c
parameter.
The configuration can be a little daunting, but if you just use the example config file and change only the stuff you are interested in, it might do what you want.
Unfortunately most scripts will only work the other way around. There is a decent one named "RECOMMENT" but it takes C and converts to the newer C++ style comments. I imagine your reason for wanting to do this is due to compiler errors with the C++ style comments. The usual cause of this is a line that uses a C-style comment with an C++ style comment. Perhaps looking for that particular scenario would eliminate your need to convert back to older style commenting. If not, sadly you might have to do it by hand. (I pray that you don't as I know how tedious that can be!)
Recomment Link: http://people.sc.fsu.edu/~jburkardt/cpp_src/recomment/recomment.html
You can do this with the Vim plugin Nerdcommenter.
This makes it easy to uncomment the text and then add a multi-line comment like you want.
Well, 1,$s#//\(.*\)#/*\1 */#
will work only if you have no instances of C++-style comments within (usually multi-line) C-style comments, since the substitution will prematurely end the C-style comment, leaving the remaining portion of the C-style comment without a starting /*
.
Any regular C++-style comment that has */
within it will also cause problems. This happens in code where a bad programmer changed a C-style comment to a C++-style comment without removing the ending */
.
This is a simple problem on the surface, but a very difficult problem to handle all the edge cases. The simple solution is easily implemented in sed:
sed -e 'sX// *\(.*[^ ]\) *$X/* \1 */X' < oldfile > newfile
You can adjust that as needed: I have it eat all the spaces at the start and end of the comment.
What this doesn't handle is new-style comments with embedded old-style comments (as others have noted). What this really messes up is strings with double-slashes in them--they're not comments, but without parsing the strings, they'll get modified as if they were. Check for those:
egrep '//.*/[*]|".*//' oldfile
If you hit any of those, they'll require manual correction. Any attempt to automate it without actually parsing the file will just create new and more convoluted edge conditions, though you may recognize a pattern for a hack that is good enough for your situation.
Vim Nerdcommenter alternate delims map
If you do: <leader>ca
some file types have alternate comment styles, and in particular for C / C++ it allows switching between //
and /*
, tested in 2.5.2.
You may then also be interested in the "sexy comments mode" used with <leader>cs
which does nice C multiline comments as mentioned at: NERD commenter : How to comment out a range
For all the coders out there! The below solution works with Eclipse, Sublime and other editors that support Regular expressions.
- Bring-up find and replace in sublime
- Make sure Regular Expression option is enabled (Alt+R)
- Enter in Find: //(.*)
- Enter in Replace /*\1 */
voilà!
精彩评论