When someone says "I want a programming language in which I need only say what I wish done," give him a lollipop.

the brown-dragon blog

Converting Comment Styles with Sed

2008-12-20

When I program in C, I like keeping my comment in the old C-style (/* */). Unfortunately, I'm so used to the C++ comment style (//), that my code invariably gets peppered with it.

While it doesn't make any difference in the compilation I think it's worth doing something just for the aesthetic value. So I wrote the following Sed script to convert the comment style to C.

c++2c.sed

# Convert C++ line-style comments (//)
# to old C-style comments (/* */)
#######################################
# Replace comment (note end marker \n)
s_//\(.*\)$_/*\1 */\n_
# First line kept in the hold buffer
1h;1d
# Is this a pure comment line?
\_^[ \t]*/\*_{
x
# Is it is a multi-line comment?
/ \*\/\n/{
# Ok - change accordingly
s_ \*\/\n__
x
s_/_ _
}
# No? Then we're fine as is
/ \*\/\n/!x
}
# Display previous line (without end marker)
x;s/\n//
# Last line. Have to handle current and previous.
${p;x;s/\n//}

Other Posts

(ordered by Tags then Date)