Automatically change pass-by-value to pass-by-reference
I have many hundreds of functions from a manual java to c++ port. In the result c++ code, I wish to change parameters passed by value to passed by reference:
from:
void funcName1( Type1 t1, Type2 t2, int i);
to:
void funcName2( Type1& t1, Type2& t2, int i);
Preferably leave the primitive types such as int, float unchanged.
Any refactoring tools to automate this process? Some regular expression tricks?
Or any tool that converts portable java code to c+开发者_高级运维+?
Try creating 2 regular expressions:
void\s+\w+\(([^\)]+))
([A-Z]\w+)\s+\w+
Matching group 1 from the first will give you:
Type1 t1, Type2 t2, int i
Run the second against this output and group 1 will be:
Type
If you're using java, you can convert quickly with:
Pattern p = Pattern.compile("(void\\s+\\w+\\s+\)(([^\\)]+))");
Pattern p2 = Pattern.compile("([A-Z]\\w+)(\\s+\\w+)");
Matcher m = p.matcher("input.....");
StringBuffer sb = new StringBuffer();
while(m.find()) {
Matcher m2 = p2.matcher(m.group(0));
StringBuffer sb2 = new StringBuffer();
while(m2.find()) {
m2.appendReplacement(sb2, "$1&$2")
}
m2.appendTail(sb2);
m.appendReplacement(sb, "$1("+sb2.toString()+")")
}
m.appendTail(sb);
精彩评论