0

I'm trying to convert strings in Python such as:

string = 'void demofun(double* output, double db4nsfy[], double VdSGV[], int length)'

into

wrapper = 'void demofun(ref double output, double[] db4nsfy, double[] VdSGV, int length)'

Now for most cases, I'm able to use a trivial combination of while, string.find() and string.replace() to do this because I don't need to meddle with the variable names (such as output or length), but what I can't figure out is replacing these strings:

double db4nsfy[] --> double[] db4nsfy

double[] VdSGV --> double[] VdSGV

How should I do this? I know I will find my answer with some RTFM of regex in Python, but I'm hoping to start with a practical example.

2
  • I'm probably wrong - but aren't their better ways instead of manipulating the source code of co-oping between C/C++ and C# ? Commented Mar 16, 2013 at 5:45
  • Hm, it'd be annoying if a tool like this already existed but I'd be even happier if it did. I have a lot of Fortran/C numerical routines that I need to interop and they get thrown into many separate .cs source files because of the various things I need done (e.g. testing their speeds against my pure C# implementations). I figured the usage was too specialized but simple enough that a proper tool didn't exist. Had to come up with my own pariah method of indicating which arguments to marshal with the [In] and [Out] attributes. Commented Mar 16, 2013 at 6:07

3 Answers 3

2

You could use re.sub:

>>> import re
>>> re.sub(r'(\w+) (\w+)\[\]', r'\1[] \2', string)
    'void demofun(double* output, double[] db4nsfy, double[] VdSGV, int length)'
  • (\w+) (\w+)\[\] matches two "words" wrapped in capturing groups and brackets.
  • \1 and \2 refer to the stuff captured by those groups.
Sign up to request clarification or add additional context in comments.

3 Comments

Awesome! Explains the solution I needed. Could you suggest the benefits that I'd gain from using something like AStyle in this particular instance? Be brutally honest ;)
@ephedyn: Probably ease of use and versatility. Regex is clunky and wasn't made specifically to beautify source files.
I see. I'm actually using Python to generate C# wrappers for a bunch of Fortran/C subroutines. I'll pick up a code formatter and try it out on the routines themselves, thanks a lot!
1

verbose, but without regex and handles both pointer and arrays(and also without regex):

def new_arguments(func_string):
    def argument_replace(arguments):
        new_arguments = []
        for argument in arguments.split(', '):
            typ, var = argument.split()
            if typ.endswith('*'):
                typ = 'ref ' + typ.replace('*', '')
            if var.endswith('[]'):
                var = var.replace('[]', '')
                typ += '[]'
            new_arguments.append(' '.join([typ, var]))
        return ', '.join(new_arguments)

    func_name = func_string[:func_string.index('(')]
    arguments = func_string[func_string.index('(')+1:func_string.index(')')]

    return ''.join((func_name, '(', argument_replace(arguments), ')'))

string = 'void demofun(double* output, double db4nsfy[], double VdSGV[], int length)'
print new_arguments(string)
#void demofun(ref double output, double[] db4nsfy, double[] VdSGV, int length)

Comments

1

This is an intuitive approach without regex.

s = 'void demofun(double* output, double db4nsfy[], double VdSGV[], int length)'
s = s.split()
for i in range(len(s)):
    if s[i][-3:] == '[],':
        s[i] = s[i][:-3] + ','
        s[i-1] = s[i-1] + '[]'
    elif s[i][-3:] == '[])':
        s[i] = s[i][:-3] + ')'
        s[i-1] = s[i-1] + '[]'
s = ' '.join(s)
print s
# void demofun(double* output, double[] db4nsfy, double[] VdSGV, int length)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.