Editable strings anyone?

Neil Hodgson nhodgson at bigpond.net.au
Sun Feb 17 23:09:12 CET 2002


Paul Rubin:

> 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.
> ...
> 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.

   I have considered creating a Python split buffer extension based upon the
classes I wrote for Scintilla, a GUI text editing component. On top of the
basic split buffer class, a text editor also often wants features for
tracking various things about the text such as line start positions,
styling, and undo. Performance is often more dependent upon these than on
the split buffer.
http://www.scintilla.org/

   There is a newer, better set of basic editing classes in SinkWorld, the
next version of Scintilla at:
http://scintilla.sourceforge.net/SinkWorld.html
   The major unresolved questions in providing a Python extension is what to
expose and how. It would be good to provide a basic split buffer, plus
allowing options to add undo, line position maintenance, styling and further
features. More contentious is whether to also provide callbacks from the
classes upon events such as insert text, as applications often use decoupled
code to mutate and display the text and so providing an observer interface
is a natural solution. There are then reentrance and performance issues,
especially with invoking Python code upon the lowest level actions.

>  1) There are functions to insert and delete single values into the array
>     but not multi-element slices.
> ...
> But it turns out that both operations are supported!  I discovered
> this by looking in the arraymodule.c source to see how the buffer
> interface was implemented, and then experimenting.  The docs for the
> array module gave no clue at all that these operations were supported.

   Damn! If I had known this then I would have developed at least an
experimental split buffer in Python.

   Neil





More information about the Python-list mailing list