Last active
July 18, 2026 23:52
-
-
Save saxbophone/e988cef9f351863f4312f2eef41a3a83 to your computer and use it in GitHub Desktop.
Python StringView implementation. Supports slicing, iteration and creating sub-views of existing StringViews. No copying, only reference semantics.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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. | |
| """ | |
| def __init__(self, source: str, start=0, stop=None): | |
| if isinstance(source, StringView): | |
| self.__source = source.__source | |
| self.__start = source.__start + start | |
| self.__stop = source.__start + (min(stop, len(source)) if stop is not None else 0) | |
| else: | |
| self.__source = source | |
| self.__start = start | |
| self.__stop = stop if stop is not None else len(source) | |
| assert self.__start <= self.__stop | |
| def __str__(self): | |
| return self.__source[self.__start:self.__stop] | |
| def __repr__(self): | |
| return 'StringView({}, {}, {})'.format( | |
| repr(self.__source), | |
| self.__start, | |
| self.__stop | |
| ) | |
| # these next two methods are provided so we can produce StringViews from StringViews | |
| def __len__(self): | |
| return self.__stop - self.__start | |
| def __getitem__(self, key): | |
| if isinstance(key, slice): | |
| if key.step is not None: | |
| raise TypeError('StringView does not support step when slicing') | |
| return StringView(self, key.start, key.stop) | |
| """ | |
| not only is there no point returning a StringView of length 1, it's also | |
| slightly less memory-intensive to just return a str of length 1... | |
| """ | |
| return self.__source[key] | |
| def contents(self): | |
| """ | |
| Returns Generator for efficient no-copy iteration over string contents | |
| """ | |
| return (self.__source[i] for i in range(self.__start, self.__stop)) |
Author
Thank you for your interest, and I commend your diligence in asking.
I hereby release this work into the Public Domain.
To cover the case where for any reason you are not able to use Public
Domain work (for example if your jurisdiction does not recognise the
validity of the Public Domain), I am also licensing it under the terms of
the "Do What The Fuck You Want To Public License", version 2 (SPDX ID:
WTFPL).
Thanks
…On Mon, 25 Aug 2025, 20:32 Balt, ***@***.***> wrote:
***@***.**** commented on this gist.
------------------------------
What would the license on this code be?
—
Reply to this email directly, view it on GitHub
<https://gist.github.com/saxbophone/e988cef9f351863f4312f2eef41a3a83#gistcomment-5734576>
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACCKNV7MH5H47IUANNQF4FT3PNQDJBFHORZGSZ3HMVZKMY3SMVQXIZNMON2WE2TFMN2F65DZOBS2WR3JON2EG33NNVSW45FGORXXA2LDOOIYFJDUPFYGLJDHNFZXJJLWMFWHKZNJGEZDGOBTHE4TONVKMF2HI4TJMJ2XIZLTSKBKK5TBNR2WLJDUOJ2WLJDOMFWWLO3UNBZGKYLEL5YGC4TUNFRWS4DBNZ2F6YLDORUXM2LUPGBKK5TBNR2WLJDHNFZXJJDOMFWWLK3UNBZGKYLEL52HS4DF>
.
You are receiving this email because you authored the thread.
Triage notifications on the go with GitHub Mobile for iOS
<https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675>
or Android
<https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub>
.
There is a bug in the constructor, when constructing from a StringView in the line
self.__stop = source.__start + (min(stop, len(source)) if stop is not None else 0)
the else 0 should be else len(source).
And another embarrassing bug is in the __getitem__ method, which ignores the start offset completely.
return self.__source[key]
should be
return self.__source[self.__start + key]
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What would the license on this code be?