Reformat c ++ brackets without changing indentation?

We would like to make our C ++ fit style more consistent. Right now, our code contains a combination of:

if (cond) { // ... } else { // ... } 

... and:

 if (cond) { // ... } else { // ... } 

We want to use only the latest style.

However, we do not want to indent our code. I tried using astyle, bcpp, GNU indent and Uncrustify (and I examined command line options for GreatCode). Unfortunately, each of these tools insists on re-encoding our code, and most of them strongly block the C ++ constructor constructor initializer lists and preprocessor macros.

Are there any C ++ code constructors that can fix curly braces, leaving only indentation? It doesn't have to be an existing tool - if you know how to do it using some crazy single-line Perl, that's good too. Thanks!

Update: Yes, we know that this will make it difficult to read diff against old code. This is a long-term code cleanup, and we decided that the day-to-day benefits of consistent formatting outweigh any version control difficulties.

+6
c ++ coding-style formatting indentation curly-braces
source share
6 answers

Here is a single-line Perl that should do what you want.

 perl -pi.bak -e 'BEGIN { undef $/; } s/\s*?(\s?\/\/.*)?\r?\n\s*{/ {\1/g; s/}(\s?\/\/.*)?\r?\n\s*else\b(.*)/} else\2\1/g;' 

He turns this:

 int main(int argc, char *argv[]) { int something = 0; if (something) // 5-12-2007 { printf("Hi!\n"); } else // 5-13-2007 { printf("Bye\n"); } return 0; } 

in it:

 int main(int argc, char *argv[]) { int something = 0; if (something) { // 5-12-2007 printf("Hi!\n"); } else { // 5-13-2007 printf("Bye\n"); } return 0; } 
+7
source share

You really need to think twice and probably three times before doing this. It will completely destroy the change history of the source code management system at the shift point. You use a source control system, right?

+3
source share

The UNIX Indent command ( http://en.wikipedia.org/wiki/Indent_(Unix) ) (available for GNU PCs) has a million options for setting formatting just like you do.

+2
source share
 perl -ei '$/=undef;while(<>){s/}\s*?(\s*\/\/^[\r\n]*)?\r?\n\s*else/} else$1/gm;s/(\s*\/\/[^\r\n]*)?\r?\n\s*{/ {$1/gm;print;}' yoursourcefile.cpp 

This first changes } <eol> <whitespace> else to } else , then removes the end of the line from <eol> <whitespace> { .

To apply this to the source tree, use find and xargs :

 find . -name \*.cpp -print0 | xargs -0 perl -ei '$/=undef;while(<>){s/}\s*?(\s*\/\/^[\r\n]*)?\r?\n\s*else/} else$1/gm;s/(\s*\/\/[^\r\n]*)?\r?\n\s*{/ {$1/gm;print;}' 
+2
source share

Could a hand full of simple regular expressions do the trick? Like (\).? \ N.? \ {) → (\) \ {) to remove the space between the closing bracket and the opening curly bracket.

+1
source share

In the past, I dealt with such problems, first using some tool like astyle to do 95% of what we wanted, and then I wrote a Perl or Python script based on the result to finish the remaining 5%. Try it, it's always a good practice to know your regular expressions. :)

0
source share

All Articles