46 lines
1.4 KiB
Plaintext
46 lines
1.4 KiB
Plaintext
From: tim_one at email.msn.com (Tim Peters)
|
|
Date: Mon, 26 Apr 1999 00:45:32 GMT
|
|
Subject: Handling backspace chars in a string...
|
|
In-Reply-To: <37239c48.594910176@news2.bga.com>
|
|
References: <37239c48.594910176@news2.bga.com>
|
|
Message-ID: <000301be8f7e$16b89780$669e2299@tim>
|
|
Content-Length: 1038
|
|
X-UID: 1104
|
|
|
|
[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>?
|
|
|
|
> 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
|
|
|
|
def stripbs(sin):
|
|
import string
|
|
sout = []
|
|
for ch in sin:
|
|
if ch == '\b':
|
|
del sout[-1:] # a nop if len(sout) == 0
|
|
else:
|
|
sout.append(ch)
|
|
return string.join(sout, '')
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|