I need a sed script to automatically convert C functions to lower snake case.
What I have so far is the following which will separate camel case words with underscores, but it doesn't lower case them and it affects everything.
sed -i -e 's/\([a-z0-9]\)\([A-Z]\)/\1_\L\2/g' `find source/ -type f`
How do I make it only apply on functions? I.e. only on strings followed by the character '('.
Also, what do I need to make the strings go lower case?
For example, If I have this code:
void destroyPoolLender(PoolLender *lender)
{
while (!isListEmpty(&lender->pools)) {
MemoryPool *myPool = listPop(&this->pool);
if (pool->inUse) {
logError("%s memory pool still in use. Pool not released.", pool->lenderName);
} else {
free(pool);
}
}
listDestroy(&this->pool);
}
It should look like this once converted:
void destroy_pool_lender(PoolLender *lender)
{
while (!is_list_empty(&lender->pools)) {
MemoryPool *myPool = list_pop(&this->pool);
if (pool->inUse) {
log_error("%s memory pool still in use. Pool not released.", pool->lenderName);
} else {
free(pool);
}
}
list_destroy(&lender->pools);
}
Notice how myPool is untouched because it isn't a function name.
We can do this with sed. The trick is to match everything up to and including the
(
as capture group 2, and use\l
rather than\L
, to downcase only the first matched character:We can't just use the
/g
modifier, because subsequent replacements may overlap, so use it in a loop:(I used
-r
for GNU sed to reduce the number of backslashes I needed).A further simplification is to match a non-word-boundary; this removes the need for two capture groups:
Demo:
Note that this only modifies function calls and definitions - it can be hard to identify which names are functions, if you're storing or passing function pointers. You might choose to fix those up manually (reacting to compilation errors) if you only have 70k lines to deal with. If you're working with 1M+, you might want a proper refactoring tool.