[Python-Dev] RFD: how to build strings from lots of slices?

Jean-Claude Wippler jcw@equi4.com
Sun, 27 Feb 2000 13:23:41 +0100


Ka-Ping Yee wrote:
> 
> On Sun, 27 Feb 2000, Fredrik Lundh wrote:
> > here's one proposal, off the top of my head:
> >
> > 1. introduce a PySliceListObject, which behaves like a
> > simple sequence of strings, but stores them as slices.
> 
> It occurred to me when i read this that *all* slices
> could be references within the original string, since
> strings are immutable.  That is,
> 
>     s[x:y]
> 
> could return a PyStringRefObject that behaves just like
> a string, but contains a length y - x and a pointer to
> &(s->ob_sval) + x instead of the character data itself.
> The creation of this PyStringRefObject would increment
> the reference count of s by 1.

The general approach is "cords" (in Hans Boehm's GC, garbage-collected),
and "ropes" (in SGI's STL, http://www.sgi.com/Technology/STL/Rope.html,
reference-counted).

It's a great idea, IMO.  Why create and copy strings all the time?

-jcw