views:

51

answers:

3

I have large C++ code base. It is quite inconsistent where uses underscore_identifiers and where CamelCaseIdentifiers, but is consistent within groups of files. Think about it as a smaller library merged into bigger one.

I want to standardize it within this project to Google C++ Style Guide. How can I semi-automatize it ? Preferably with Linux tools.

A: 

A simple perl script would do it. You then run it with the -i flag ("replace inplace") for all your files.

Vladimir Dyuzhev
A: 

To go from underscores to camel case (using gnu sed):

sed 's/([a-zA-Z])_([a-zA-Z])/\1\U\2/g'

And the reverse:

sed 's/([a-z])([A-Z])/\1_\L\2/g'

If you are feeling brave you can use the -i flag to work in-place.

[n.b. not tested I'm on a mac and the sed shipped with it does not support \U or \L - on gnu Linux you should be fine]

Ben
+1  A: 

Try this command on the root of the source.

find . -name "*.cpp" -exec sed 's/_\([a-zA-Z]\)\([a-zA-Z]*\)/\U\1\E\2/g' {} \;

The output goes to STDOUT just to test it.

If you want to change the files directly you can add the -i option to sed.

WARNING: This will change the file in place. Make a backup first!

find . -name "*.cpp" -exec sed -i 's/_\([a-zA-Z]\)\([a-zA-Z]*\)/\U\1\E\2/g' {} \;

Of course you have to check all the automatic changes. See the comment from Jim Lewis.

michael.kebe