views:

81

answers:

4

I have a very big C programming project that uses thousands of struct variables with this naming convention:

specificstruct->x = specificstruct->y + specificstruct->z

I want to do some heavy refactoring, namely converting most of these struct members to arrays. The code above would look like this:

specificstruct->x[i] = specificstruct->y[i] + specificstruct->z[i]

... and I don't feel like wasting an entire day on doing all this manually. Does anyone have a suitable regex in store?

EDIT: It is always the same struct, but the equations vary.

Thanks in advance!

Best regards, P. Nilsson

A: 

For a generalized approach something like this ought to do - the assumption is that you've got consistent spacing between your expressions

(.*?->.) = (.*?->.) \+ (.*?->.)

You can then write your new array structure as:

\1[i] = \2[i] + \3[i]
Gavin Miller
A: 

s/\(specificstruct->x\) = \(specificstruct->y\ )\+ \(specificstruct->z\)/\1[i] = \2[i] + \3[i]/g

John Weldon
A: 

If you're just looking for the name followed by -> then a single character, you could try

(?<struct>\w+)\s?->\s?(?<var>\w{1}) //single char after ->
(?<struct>\w+)\s?->\s?(?<var>\w+) //multiple char after ->

That way you have groups so you can compare the names before you do any replacements. The \s? helps to match even if you added spacing between some but not others.

Hugoware
+1  A: 

I'm not sure about your particular case, but maybe Coccinelle can help you. It is a system for patching source code, based on some rules like "if x is an expression without function invocations, change x+x to 2*x" etc.

jpalecek
This sounds exactly like a case which coccinelle was made to handle.
hlovdal