Łukasz Lew
Łukasz Lew

Reputation: 50298

How to automatically convert underscore_identifiers into CamelCaseIdentifiers?

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.

Upvotes: 0

Views: 979

Answers (3)

michael.kebe
michael.kebe

Reputation: 11085

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.

Upvotes: 1

Ben
Ben

Reputation: 1106

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]

Upvotes: 0

Vladimir Dyuzhev
Vladimir Dyuzhev

Reputation: 18336

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

Upvotes: 0

Related Questions