I think this is two questions:
Q1) What is the pattern I should use to capture all int
declarations in the source codes?
- The regex pattern I have come up so far is:
[\(<\s]?(int)[\s>\)]?
- @WiktorStribiżew suggested in the comments to match whole word
int
. His solution is simpler than my first approach and does not cause the problem I ask in Question 2.
Q2) How do I tell sed
to replace only the word inside the group capture? I.e.: replace int
to int32_t
but leave all other characters in the matching pattern untouched?
- If I use
sed -i -E 's/[\(<\s]?(int)[\s>\)]?/int32_t/g' file.cpp
it will cause the following undesired effect:func(int a)
->funcint32_ta)
Here are some examples of test cases:
int a;
->int32_t a;
func(int a);
->func(int32_t a)
template<int> a;
->template<int32_t> a;
EDIT 1: To simplify, the solution can ignore unsigned int
, i.e., if it leads to unsigned int32_t
it is okay for me.
EDIT 2: Some of you are asking why: this is a scientific computation application that will be distributed in some embedded processors. We want to guarantee our integer types are fixed to 32-bits.
EDIT 3: The source code is not large. In fact, I can (and probably will) verify each modification realized. I just took the opportunity to learn more about the regex.