Handling backspace chars in a string...

Purple bwizard at bga.com
Sun Apr 25 21:15:32 EDT 1999


On Mon, 26 Apr 1999 00:45:32 GMT, "Tim Peters" <tim_one at email.msn.com>
wrote:

>[Purple]
>> I'm in the poistion of having to process strings with arbitrary
>> numbers of backspace and newline characters in them.
>
>Your code doesn't appear to care about newlines one way or t'other.  Do you
><wink>?

I didn't post the code for that bit as it seems easy enough to take
care of processing those with something like
map(string.strip,string.split(stringWithNewlines,"\n")

Unless there's a better way to do that too? :)

>> The backspaces actually get put in the string, so I have to handle
>> removing the characters that are backspaced over.
>> ... [rather sprawling string + indexing code] ...
>> This just looked rather messy to me -- I was curious if anyone know a
>> better way?
>
>Assuming "better" means "less messy" here, lists support appending and
>deleting quite naturally and efficiently; like
>
[code using lists snipped]

>This essentially treats the input string as a sequence of opcodes for a
>stack machine, where "\b" means "pop" and anything else means "push me!".
>
>don't-use-any-indices-and-you-can't-screw-'em-up<wink>-ly y'rs  - tim

I may well go that route... Is it any slower or faster to do this
using lists rather than counting up the backspaces and slicing around
the bits that need to be snipped?





More information about the Python-list mailing list