Editable strings anyone?
phr-n2002a at nightsong.com
Sun Feb 17 14:01:47 CET 2002
I'm wondering how difficult it would be to add an editable (mutable)
string class to Python 2.2 implemented in C under the new type/class
unification scheme. Right now, writing a text editor in Python seems
difficult to do in any natural way. I'm messing with Amit Patel's very
cool Proxy3 web proxy (which does HTML editing on the fly) and find
myself really wanting something like this.
I guess the editable string class it would support the current string
operations and list operations. The underlying implementation should
support fast editing operations (insertions and deletions of slices)
even for very large (multi-megabyte) strings, at least in the usual
case where consecutive editing operations happen in the same region of
the string. The usual method of representing the editable string as
two contiguous regions of a chunk of memory separated by a gap, so you
can add or delete characters by growing or shrinking the gap, and
moving the gap as necessary, would be fine.
e = editable_string('potato') => potato
e.insert(0,'baked ') => baked potato
x,y = re.search('ak',e).span() => 1,3
e[x:y] = 'oil' => boiled potato
Does this sound like a reasonable design? Does it fit in reasonably
with the CPython implementation? Would it require a lot of hacking to
existing string functions like the re module?
Java has a StringBuffer class which Java programmers use idiomatically
whenever they want to build a big string from smaller ones. Python
surprisingly lacks a counterpart to StringBuffer. Python programmers
tend to say either a=b+c+d+e+f (potentially taking quadratic time in the
total number of characters) or some kludge like a=''.join(b,c,d,e,f).
Editable strings would be even more general than StringBuffer.
More information about the Python-list