-2

I'm working on making a change to a large C++ codebase where I need to change a method call to an equivalent function call, e.g. someExpression.foo() becomes bar(someExpression).

A few more examples of the sort of transformation I'm looking for:

  • (itemA + itemB).foo() becomes bar(itemA + itemB)
  • outerFunction(object->method(), arg2 + val, innerFunction(arg3)).foo() becomes bar(outerFunction(object->method(), arg2 + val, innerFunction(arg3)))

Basically, the expression proceeding the method call to foo() could be fairly complex. But foo() comes up a lot of times in my codebase, so I'm really hoping there's some way I can automate this change rather than having to laboriously edit everything manually.

Can this be done with a regex? Or is there another tool I can use to make this change?

Bri Bri
  • 2,169
  • 3
  • 19
  • 44

1 Answers1

0

I've slapped together a regex that seems to work so far, based on the non-recursive parenthesis-balancing regex from this answer.

([^\s\(]*(?:\((?:[^)(]|\((?:[^)(]|\((?:[^)(]|\([^)(]*\))*\))*\))*\))?)\.foo\(\)

That won't match any valid expression proceeding .foo() but will match most of them. There may be bugs with this regex, but I haven't found any or hit any mismatches so far. And it contains a capture group that can be used for replacement, i.e.: bar(\1)

I'm going to leave this question open though because there are almost certainly better methods than this.

Bri Bri
  • 2,169
  • 3
  • 19
  • 44