Skip to content

Instantly share code, notes, and snippets.

@jpalecek
jpalecek / stringview.py
Created July 18, 2026 23:52 — forked from saxbophone/stringview.py
Python StringView implementation. Supports slicing, iteration and creating sub-views of existing StringViews. No copying, only reference semantics.
class StringView:
"""
StringView implementation using minimal copying with maximum use of
reference semantics. Creating a sub-view of an existing StringView using
either object slicing or constructing one from another will reüse the same
source string object, using a reference rather than a copy.
The contents() method can similarly be used to get an iterator (Generator)
to access the view contents sequentially without putting it all in memory
at once.
A brand new string object is only created if the StringView is cast to str.