views:

73

answers:

1

I have a massive string im trying to parse as series of tokens in string form, and i found a problem: because many of the strings are alike, sometimes doing string.replace()will cause previously replaced characters to be replaced again.

say i have the string being replaced is 'goto' and it gets replaced by '41' (hex) and gets converted into ASCII ('A'). later on, the string 'A' is also to be replaced, so that converted token gets replaced again, causing problems.

what would be the best way to get the strings to be replaced only once? breaking each token off the original string and searching for them one at a time takes very long

This is the code i have now. although it more or less works, its not very fast

# The largest token is 8 ASCII chars long
'out' is the string with the final outputs
while len(data) != 0:
    length = 8
    while reverse_search(data[:length]) == None:#sorry THC4k, i used your code 
                                                #at first, but it didnt work out 
                                                #for this and I was too lazy to
                                                #change it
        length -= 1
    out += reverse_search(data[:length])
    data = data[length:]
+1  A: 

If you're trying to substitute strings at once, you can use a dictionary:

translation = {'PRINT': '32', 'GOTO': '41'}
code = ' '.join(translation[i] if i in translation else i for i in code.split(' '))

which is basically O(2|S|+(n*|dict|)). Very fast. Although memory usage could be quite substantial. Keeping track of substitutions would allow you to solve the problem in linear time, but only if you exclude the cost of looking up previous substitution. Altogether, the problem seems to be polynomial by nature.

Unless there is a function in python to translate strings via dictionaries that i don't know about, this one seems to be the simplest way of putting it.

it turns

10 PRINT HELLO
20 GOTO 10

into

10 32 HELLO
20 41 10

I hope this has something to do with your problem.

stefano palazzo