[Python-Dev] RFD: how to build strings from lots of slices?
Greg Stein
gstein@lyra.org
Sun, 27 Feb 2000 10:42:08 -0800 (PST)
On Sun, 27 Feb 2000, 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.
This is exactly what the PyBufferObject does. I just documented the thing
in api.tex a week ago or so. Regardless, the thing can operate exactly
like a lightweight slice object. It it very similar at the Python level to
a string, but it doesn't have the new string methods (yet) :-(
If you want a temporary object for your slices (before recomposition with
a "".join), then you should be able to use the buffer objects.
[ unfortunately, the "".join method is nowhere near as optimal as it could
be... it converts elems to string objects during the concatenation; it
should have a variant that uses the buffer interface to precalculate the
joined size, then use the interface to fetch the data ]
Cheers,
-g
--
Greg Stein, http://www.lyra.org/