1
votes

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

4
is it allways the same name/struct ? the same inscruction ? does your IDE's regex implementation support captured reference (or "submatch recur", like \1 in the search expression) ?instanceof me

4 Answers

2
votes

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.

0
votes

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]
0
votes

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

0
votes

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.