Replace C style comments with C ++ style comments

How can I automatically replace all C style comments (/ * comment * /) with C ++ style comments (// comment)? This must be done automatically in several files. Any solution is ok if it works.

+6
c ++ comments
source share
12 answers

This tool does the job: http://people.sc.fsu.edu/~burkardt/cpp_src/recomment/recomment.html

RECOMMENT is a C ++ program that converts C style comments to C ++ style comments.

It also handles all non-trivial cases mentioned by other people:

This code contains suggestions and coding provided on April 28, 2005, by Stephen Martin of JDS Unifhase, Melbourne Florida. These suggestions allow the program to ignore the internal contents of the lines, otherwise comments may start or end), to process lines of code with comments and to process comments with trailing bits of code.

+17
source share

This is not a trivial problem.

int * /* foo /* this is not the beginning of a comment. int * */ var = NULL; 

What do you want to replace? Any real substitution sometimes requires dividing lines.

 int * // foo // this is not the beginning of a comment. // int * var = NULL; 
+13
source share

How are you going to handle such situations:

 void CreateExportableDataTable(/*[out, retval]*/ IDispatch **ppVal) { //blah } 

Pay attention to the comment inside parens ... this is a general way of documenting things in the generated code or mentioning the default parameter values ​​in the class implementation, etc. I'm usually not a fan of this use of comments, but they are general and require consideration. I don’t think you can convert them into comments to the C ++ style without doing much thinking.

+5
source share

I am with people who commented on your question. Why? Just leave it.

he spends time, adds useless commits to version control, runs the risk of twisting

EDIT: Adding Details from OP Comments

The main reason for the preferred comment on C ++ is that you can comment on a block of code that may contain comments in it. If this comment is C-style, this comment code from the code is not direct. - unknown (yahoo)

which may be fair / well, what needs to be done, but I have two comments about this:

  • I do not know anyone who would advocate changing all existing code - this is the preference for the new code. (IMO)
  • If you feel the need to “comment on the code” (another practice), you can do it as needed - not earlier

Also it seems that you want to use c-style comments to lock a section of code? Or are you going to use // to block many lines?

One option is the #ifdef preprocessor for this situation. I snuggle up to this, but it's as bad as commenting out lines / blocks. There should also be nothing in the production code.

+4
source share

Here is a Python script that will (mostly) do the job. It handles most cases of edges, but it does not handle comment characters inside lines, although this should be easy to fix.

 #!/usr/bin/python import sys out = '' in_comment = False file = open(sys.argv[1], 'r+') for line in file: if in_comment: end = line.find('*/') if end != -1: out += '//' + line[:end] + '\n' out += ' ' * (end + 2) + line[end+2:] in_comment = False else: out += '//' + line else: start = line.find('/*') cpp_start = line.find('//') if start != -1 and (cpp_start == -1 or cpp_start > start): out += line[:start] + '//' + line[start+2:] in_comment = True else: out += line file.seek(0) file.write(out) 
+2
source share

I recently converted all C-style comments to C ++ style for all files in our repository. Since I could not find a tool that would do this automatically, I wrote my own: c-comments-to-cpp

This is not perfect, but better than everything I've tried (including RECOMMENT). Among other things, it supports the conversion of Doxygen style comments, for example:

 /** * @brief My foo struct. */ struct foo { int bar; /*!< This is a member. It also has a meaning. */ }; 

Gets the conversion to:

 /// @brief My foo struct. struct foo { int bar; ///< This is a member. ///< It also has a meaning. }; 
+1
source share

Why don't you write a C application to parse its own source files? You can find / * comments * / sections with a relatively easy Regex request. You can then replace the new string characters with the new string character + /// ".

In any case, just a thought. Good luck with that.

0
source share

If you are writing a / script application to process C source files, pay attention to some things:

  • comment characters in lines
  • comment characters in the middle of the line (you may not want to split the line of code)

You might be better off trying to find an application that understands how to actually parse code as code.

0
source share

If there are only “a few files”, is it really necessary to write a program? Opening it in a text editor can make the trick faster in practice if there is not a lot of commentary. emacs has a comment-region command that (unsurprisingly) comments on a region, so this is just the case when you insult the abusive "/ *" and "* /".

0
source share

There are several suggestions you can try:

a) Write your own code (C / Python / whatever language you like) to replace the comments. Something like what the regular expression said, or this naive solution 'might work: [Prohibiting cases, as one rmeador, Darron published]

 
 for line in file:
     if line [0] == "\ *":
        buf = '//' + all charachters in the line except '\ *'
        flag = True
     if flag = True:
        if line ends with '* /':
           strip off '* /'
           flag = False
        add '//' + line to buf

b) Find a tool for this. (I will find some and publish if I find them.)

c) Almost all modern IDEs (if you use it) or text editors have an automatic comment feature. Then you can manually open each file, select comment lines, decide how to handle the situation, and comment on the C ++ style using the accelerator (say, Ctrl + M). Then you can simply "Find and replace" all "/ *" and "* /", again using your opinion. I have Gedit configured for this using the "Code Comment" plugin. I don’t remember how I did it in Vim. I am sure this can be easily found.

0
source share

A very old question, I know, but I just achieved this using "pure emacs". In short, the solution is as follows:

Run Mx query-replace-regexp . When prompted, enter

 /\*\(\(.\|^J\)*?\)*\*/ 

as a regular expression to search for. ^J is a new line that you can enter by pressing ^Q (Ctrl + Q on most keyboards) and then pressing the enter key. Then enter

 //\,(replace-regexp-in-string "[\n]\\([ ]*?\\) \\([^ ]\\)" "\n\\1// \\2" \1)) 

as a replacement expression.

Essentially, the idea is that you use two nested searches in a regular expression. The main one simply finds C-style comments (it’s very convenient to use *? Impatient repetition for this). Then, the elisp expression is used to perform the second replacement inside the comment text. In this case, I am looking for new lines followed by a space, and replacing the last three space characters with // , which is good for preserving the formatting of comments (only works as long as all comments are indented).

Changes in the secondary regular expression will make this approach work in other cases, for example

 //\,(replace-regexp-in-string "[\n]" " " \1)) 

just put the entire contents of the original comment in a single C ++ style comment.

0
source share

from a collection of PHP commands ... some reasons must exist if a question is asked. Just answer if you know.

Never use C ++ style comments (i.e., // comment). Always use C-style instead of comments. PHP is written in C and is intended to be compiled under any ANSI-C compatible compiler. Although many compilers accept comments in C ++ in C code, you must ensure that your code compiles with other compilers. The only exception to this rule is Win32-specific code, because the Win32 port is MS-Visual C ++-specific, and this compiler is known to accept comments in C ++ in C code.

-3
source share

All Articles