Editable strings anyone?

Paul Rubin phr-n2002a at nightsong.com
Sun Feb 17 08:46:17 EST 2002


martin at v.loewis.de (Martin v. Loewis) writes:
> > 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.
> 
> There is already an efficient mutable vector type, the array type,
> which can be used to implement both mutable byte strings and mutable
> character strings.

Very interesting.  I was about to answer that:

  1) There are functions to insert and delete single values into the array
     but not multi-element slices.  

  2) I don't think the existing string functions work on byte arrays.
     In particular, I don't think you can search for a regular expression in one.

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.

> > 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.
> 
> The Python counterpart of the Java StringBuffer is the StringIO module.

OK, though it looks like the C version needs to be extended to handle Unicode.

Anyway, it looks like the array module may do what I need.  So I guess
I'll submit a doc bug saying the above items should be described.

Thanks!!

Paul



More information about the Python-list mailing list