Skip to content

Commit d0129f6

Browse files
authored
[librt] Add minimal librt.strings.StringWriter class (experimental) (#20588)
It can be used to build a string from code points (via `append` method) and strings (via `write`). It's similar to `BytesWriter`, but it has to keep track of the kind of string being created (1/2/4 bytes per code point). I've optimized `append` but there are no mypyc primitives yet, so performance is still bad. I'll add mypyc primitives in a follow-up PR and do further optimizations as needed. There is no `truncate` or `__setitem__`, unlike `BytesWriter`, since these could require the recomputation of kind, and this would be inefficient. My current thinking is that we won't support these. I used LLM assist, especially for tests, but manually reviewed all changes.
1 parent c0b89be commit d0129f6

File tree

4 files changed

+792
-8
lines changed

4 files changed

+792
-8
lines changed
Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,21 @@
11
from typing import final
22

3-
from mypy_extensions import i64, u8
3+
from mypy_extensions import i64, i32, u8
44

55
@final
66
class BytesWriter:
77
def append(self, /, x: int) -> None: ...
8-
def write(self, /, b: bytes | bytearray) -> None: ...
8+
def write(self, b: bytes | bytearray, /) -> None: ...
99
def getvalue(self) -> bytes: ...
10-
def truncate(self, /, size: i64) -> None: ...
10+
def truncate(self, size: i64, /) -> None: ...
1111
def __len__(self) -> i64: ...
12-
def __getitem__(self, /, i: i64) -> u8: ...
13-
def __setitem__(self, /, i: i64, x: u8) -> None: ...
12+
def __getitem__(self, i: i64, /) -> u8: ...
13+
def __setitem__(self, i: i64, x: u8, /) -> None: ...
14+
15+
@final
16+
class StringWriter:
17+
def append(self, x: int, /) -> None: ...
18+
def write(self, s: str, /) -> None: ...
19+
def getvalue(self) -> str: ...
20+
def __len__(self) -> i64: ...
21+
def __getitem__(self, i: i64, /) -> i32: ...

0 commit comments

Comments
 (0)