how to replace c++ style comment with c style

484 views
Skip to first unread message

Alick.Guo

unread,
Jan 9, 2008, 10:44:16 PM1/9/08
to vim_use
hello all vimmers.
i wanna replace all c++ style comment with c sytle comment , so i use
":%s!//\(.*\)!/*\1*/!g" command. But when change lines that c
comment inside c++ comment will make big trouble.

etc:
// xxxx /* yyy */
---------->after command will change to
/* xxxx /* yyy */*/
it will cause compile error.

how can i change above mix sytle comment to /* xxxx yyy */

Yongwei Wu

unread,
Jan 10, 2008, 1:16:09 AM1/10/08
to vim...@googlegroups.com

The requirements are vague to me. What do you want to change for
these kind of comments:

// xxx /* yyy */ /* zzz */
// xxx /* yyy */ bla /* zzz */

And your rationale for the change?

Best regards,

Yongwei

--
Wu Yongwei
URL: http://wyw.dcweb.cn/

AlicK

unread,
Jan 10, 2008, 4:50:41 AM1/10/08
to vim...@googlegroups.com

>> hello all vimmers.
>> i wanna replace all c++ style comment with c sytle comment , so i use
>> ":%s!//\(.*\)!/*\1*/!g" command. But when change lines that c
>> comment inside c++ comment will make big trouble.
>>
>> etc:
>> // xxxx /* yyy */
>> ---------->after command will change to
>> /* xxxx /* yyy */*/
>> it will cause compile error.
>>
>> how can i change above mix sytle comment to /* xxxx yyy */
>
> The requirements are vague to me. What do you want to change for
> these kind of comments:
>
> // xxx /* yyy */ /* zzz */
> // xxx /* yyy */ bla /* zzz */
>
> And your rationale for the change?
>
> Best regards,
>
> Yongwei
>

// xxx /* yyy */ /* zzz */

------->
/* xxx yyy zzz */ or any other format even like this /* xxx { yyy } {
zzz } */


According to our company coding standard ,c++ style comment is forbidden
so we have to make this change.

wow , u have just awoke me , i will try to replace these comment in two
step :
1st form /* xxx /* yyy */ /* zzz */*/
2nd form /* xxx { yyy } { zzz }*/

ths~ :)

Maybe some one who has the solution which can do this in one step plz
tell me , thanks~~

Alick


John Beckett

unread,
Jan 10, 2008, 5:49:31 AM1/10/08
to vim...@googlegroups.com
AlicK wrote:
> According to our company coding standard ,c++ style comment
> is forbidden so we have to make this change.

Amazing! I was thinking of deleting this silly tip, and now you are asking
for it!

http://vim.wikia.com/wiki/VimTip1010

I did quite a lot of quick cleaning up on the tip. If you want to be super
cautious, you could click the "imported from" link and check that I didn't
leave anything useful out.

John

Frans Grotepass

unread,
Jan 10, 2008, 6:22:54 AM1/10/08
to vim...@googlegroups.com

That silly tip was mine ~(_8(I) . I still the replace per line instead of
the :%s solution. If I can remember correctly, the %s solution had problems
with indented comments. Indented comments are handy if you use folding.
(Although the folding might be tuned to handle folding better, but hey, it
works).

The tip options is solution for the mentioned problem, though.

Yongwei Wu

unread,
Jan 10, 2008, 10:18:47 AM1/10/08
to vim...@googlegroups.com

I think you should first replace the /* and */ after //, and then
replace //. Something like:

%s!//.*\zs/\*!{!
%s!//.*\zs\*/!}!
%s!//\(.*\)!/*\1 */!

The problem of these substitutions is that one needs to repeat the
first two steps until all instances replaced. Maybe there are better
ways.

John Beckett

unread,
Jan 10, 2008, 9:22:27 PM1/10/08
to vim...@googlegroups.com
Frans Grotepass wrote:
>> http://vim.wikia.com/wiki/VimTip1010

>
> That silly tip was mine ~(_8(I) . I still the replace per
> line instead of the :%s solution. If I can remember
> correctly, the %s solution had problems with indented
> comments. Indented comments are handy if you use folding.
> (Although the folding might be tuned to handle folding
> better, but hey, it works).

Hi Frans! This thread proves I was wrong - there are still people needing to
convert from C++ to C style comments.

I hope you have a look at the tip in its new home (URL above) and see if you
can improve it.

John

Frans Grotepass

unread,
Jan 11, 2008, 2:01:02 AM1/11/08
to vim...@googlegroups.com

The desire to do the conversion is not silly at all. If you compile
with -ansi -pedantic then the c++ style comments do a headloop. It is however
a far easier form of commenting, using only one initialisation with no shift
required. For the motor industry, MISRA compliant C programming is required.
That requires -ansi -pedantic. Importing generic code blocks with c++ style
commenting then requires a conversion.

Frans

Yongwei Wu

unread,
Jan 11, 2008, 9:39:11 AM1/11/08
to vim...@googlegroups.com
On 11/01/2008, Frans Grotepass <fmgro...@yahoo.co.uk> wrote:
>
> On Friday 11 January 2008, John Beckett wrote:
> > Frans Grotepass wrote:
> > >> http://vim.wikia.com/wiki/VimTip1010
> > >
> > > That silly tip was mine ~(_8(I) . I still the replace per
> > > line instead of the :%s solution. If I can remember
> > > correctly, the %s solution had problems with indented
> > > comments. Indented comments are handy if you use folding.
> > > (Although the folding might be tuned to handle folding
> > > better, but hey, it works).
> >
> > Hi Frans! This thread proves I was wrong - there are still people needing
> > to convert from C++ to C style comments.
> >
> > I hope you have a look at the tip in its new home (URL above) and see if
> > you can improve it.
> >
> > John
>
> The desire to do the conversion is not silly at all. If you compile
> with -ansi -pedantic then the c++ style comments do a headloop.

This is GCC specific.

> It is however a far easier form of commenting, using only one
> initialisation with no shift required. For the motor industry, MISRA
> compliant C programming is required. That requires -ansi -pedantic.
> Importing generic code blocks with c++ style commenting then
> requires a conversion.

Let me be pedantic too :-). "//" is not only valid in C++, but also
valid in C99. GCC won't complain if you specify std=c99.

> Frans

John Beckett

unread,
Jan 11, 2008, 6:39:12 PM1/11/08
to vim...@googlegroups.com
Frans Grotepass wrote:
> The desire to do the conversion is not silly at all. If you
> compile with -ansi -pedantic then the c++ style comments do a
> headloop. It is however a far easier form of commenting,
> using only one initialisation with no shift required. For the
> motor industry, MISRA compliant C programming is required.
> That requires -ansi -pedantic. Importing generic code blocks
> with c++ style commenting then requires a conversion.

OK. Sorry I was flippant in my remark - I was really referring to the whole
page, including the bunch of comments arguing about //... and /*...*/.
Moreover, I think the fact that I cleaned the tip, and quoted it to someone
wanting the info shows that I don't really think it was silly, and was just
being flippant (to those of us who don't need to deal with MISRA or
whatever, it does sound a little strange that someone would want to automate
the conversion of C++ to C comments).

BTW if I had to do that, I would take a copy of the files, then run the
regex from the comment in the tip, then carefully check a diff with the
originals.

John

Frans Grotepass

unread,
Jan 14, 2008, 2:31:47 AM1/14/08
to vim...@googlegroups.com
On Saturday 12 January 2008, John Beckett wrote:
> OK. Sorry I was flippant in my remark - I was really referring to the whole
> page, including the bunch of comments arguing about //... and /*...*/.
> Moreover, I think the fact that I cleaned the tip, and quoted it to someone
> wanting the info shows that I don't really think it was silly, and was just
> being flippant (to those of us who don't need to deal with MISRA or
> whatever, it does sound a little strange that someone would want to
> automate the conversion of C++ to C comments).

No worries mate, we all start programming and think: "What's the big deal with
global variables?" (as an example that we don't always understand the full
reason why). Outlookmonkeys love to write their emails in a word document and
then post a mail saying: "see attachment" They cannot grasp that it is
irritating and cumbersome. What I'm saying is that it happens soooo often
that people voice their opionon without full comprehension of the reasons why
people do it so, that it not even remotely angers me. Hell, 3 years ago I
would've thought it anal to go on a tangent about the c/c++ style commenting.


> BTW if I had to do that, I would take a copy of the files, then run the
> regex from the comment in the tip, then carefully check a diff with the
> originals.

If the files are small enough, then it using :%s/..../..../c is also an
option.

Greets,

Frans

Gene Kwiecinski

unread,
Jan 23, 2008, 12:00:41 PM1/23/08
to vim...@googlegroups.com
>>Moreover, I think the fact that I cleaned the tip, and quoted it to
someone
>>wanting the info shows that I don't really think it was silly, and was
just
>>being flippant (to those of us who don't need to deal with MISRA or
>>whatever, it does sound a little strange that someone would want to
>>automate the conversion of C++ to C comments).

>No worries mate, we all start programming and think: "What's the big
deal with
>global variables?" (as an example that we don't always understand the
full

I actually liked most of the ideas MISRA promotes (mandates?), because I
tend to be a rather anal-retentive programmer myself. I like modules
and routines that're wholly self-contained, and don't rely on global
variables at all. Makes them that much easier to transplant into
different routines...


>reason why). Outlookmonkeys love to write their emails in a word
document and
>then post a mail saying: "see attachment" They cannot grasp that it is
>irritating and cumbersome. What I'm saying is that it happens soooo
often

"Gee, your attachment was corrupted. Seems to happen a *lot* for some
reason. Send it as normal mail next time..."

I don't do proprietary formats at all. Someone sends you a file in M$W9
and if you only got M$W8 or -7, you're hosed. I don't care it
OpenOffice or StarOffice can read M$ files, send normal f'n mail. How
hard is *that*?

What you describe is easy for the *sender*, but a royal pain in the ass
for *me*. Hell, maybe I'll just send 'em an ODF file then let *them*
worry about it. How's *that*?

"Outlookmonkeys" is right...


>that people voice their opionon without full comprehension of the
reasons why
>people do it so, that it not even remotely angers me. Hell, 3 years ago
I
>would've thought it anal to go on a tangent about the c/c++ style
commenting.

Hey, we *all* need a hobby...

"Therapy helps, but screaming obscenities is cheaper..."

Reply all
Reply to author
Forward
0 new messages