I’m cleaning up a large C++ code base where I need all variables of type “vector” need to be changed to “std::vector”. Skipping over #include and comments in the code. And most importantly, if the expression is already written as “std::vector”, don’t convert it to “std::std::vector”
That is:
#include <vector>
vector<Foo> foolist;
typedef vector<Foo> FooListType;
std::vector<Foo> otherfoolist;
int main()
{
// this vector is for iteration
for (vector <Foo>::iterator itor = foo.begin...)
Converts to
#include <vector>
std::vector<Foo> foolist;
typedef std::vector<Foo> FooListType;
std::vector<Foo> otherfoolist;
int main()
{
// this vector is for iteration
for (std::vector<Foo>::iterator itor = foo.begin...)
So far, I have this narrowed down to two sed commands
sed -r 's/vector\s{0,1}</std::vector</g' < codefile > tmpfile
sed 's/std::std/std/' < tmpfile > codefile
The first sed matches “vector< and “vector <” and converts either to “std::vector<“.
The second sed fixes the side effect of converting “std::vector<” into “std::std::vector<“.
How can merge the two different regex expressions above so I can have a single sed command that fixes the code correctly.
I’ve tried reading online about lookahead and lookbehind, but my eyes are starting to burn out.
You can make the first regexp also match a possible
std::by usingbtw: you can make the changes in place by adding
-iand just passing the file as command line parameter: