[Python-Dev] String views (was: Re: Proof of the pudding: str.partition())

skip@pobox.com skip at pobox.com
Thu Sep 1 03:23:34 CEST 2005


    Tim> I'm actually starting to think that this may be a good use case for
    Tim> views of strings i.e. rather than create 3 new strings, each
    Tim> "string" is a view onto the string that was partitioned.

How would this work?  One of the advantages of the current string is that
the underlying data is NUL-terminated, so when passing strings to C routines
no copying is required.  Suppose I executed

    scheme, _, rest = "http://www.python.org/".partition(':')

As a Python programmer I'd get back what look like three strings: "http",
":", and "//www.python.org/".  If each of them was a view onto part of the
original string, only the last one would truly refer to a NUL-terminated
sequence of characters.  If I then wanted to see what scheme's value
compared to, the string's comparison method would have to recognize that it
wasn't truly NUL-terminated, copy it, call strncmp() or whatever underlying
routine is used for string comparisons.  (Maybe string comparisons are done
inline.  I'm sure there are some examples where the underlying C string
routines are called.)

OTOH, maybe that would work.  Perhaps we should try it.

Skip


More information about the Python-Dev mailing list