| 1 |
|
| 2 |
|
| 3 |
import sys |
| 4 |
from typing import overload |
| 5 |
|
| 6 |
if sys.version_info >= (3, 12): |
| 7 |
from collections.abc import Buffer |
| 8 |
else: |
| 9 |
from typing_extensions import Buffer |
| 10 |
|
| 11 |
class Bytes(Buffer): |
| 12 |
"""A `bytes`-like buffer. |
| 13 |
|
| 14 |
This implements the Python buffer protocol, allowing zero-copy access |
| 15 |
to underlying Rust memory. |
| 16 |
|
| 17 |
You can pass this to `memoryview` for a zero-copy view into the underlying |
| 18 |
data or to `bytes` to copy the underlying data into a Python `bytes`. |
| 19 |
|
| 20 |
Many methods from the Python `bytes` class are implemented on this, |
| 21 |
""" |
| 22 |
|
| 23 |
def __init__(self, buf: Buffer = b"") -> None: |
| 24 |
"""Construct a new Bytes object. |
| 25 |
|
| 26 |
This will be a zero-copy view on the Python byte slice. |
| 27 |
""" |
| 28 |
def __add__(self, other: Buffer) -> Bytes: ... |
| 29 |
def __buffer__(self, flags: int) -> memoryview[int]: ... |
| 30 |
def __contains__(self, other: Buffer) -> bool: ... |
| 31 |
def __eq__(self, other: object) -> bool: ... |
| 32 |
@overload |
| 33 |
def __getitem__(self, key: int, /) -> int: ... |
| 34 |
@overload |
| 35 |
def __getitem__(self, key: slice[int, int, int], /) -> Bytes: ... |
| 36 |
def __getitem__(self, key: int | slice, /) -> int | Bytes: ... |
| 37 |
def __mul__(self, other: Buffer) -> int: ... |
| 38 |
def __len__(self) -> int: ... |
| 39 |
def removeprefix(self, prefix: Buffer, /) -> Bytes: |
| 40 |
"""If the binary data starts with the prefix string, return `bytes[len(prefix):]`. |
| 41 |
Otherwise, return the original binary data. |
| 42 |
""" |
| 43 |
def removesuffix(self, suffix: Buffer, /) -> Bytes: |
| 44 |
"""If the binary data ends with the suffix string and that suffix is not empty, |
| 45 |
return `bytes[:-len(suffix)]`. Otherwise, return the original binary data. |
| 46 |
""" |
| 47 |
def isalnum(self) -> bool: |
| 48 |
"""Return `True` if all bytes in the sequence are alphabetical ASCII characters or |
| 49 |
ASCII decimal digits and the sequence is not empty, `False` otherwise. |
| 50 |
|
| 51 |
Alphabetic ASCII characters are those byte values in the sequence |
| 52 |
`b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'`. ASCII decimal digits |
| 53 |
are those byte values in the sequence `b'0123456789'`. |
| 54 |
""" |
| 55 |
def isalpha(self) -> bool: |
| 56 |
"""Return `True` if all bytes in the sequence are alphabetic ASCII characters and |
| 57 |
the sequence is not empty, `False` otherwise. |
| 58 |
|
| 59 |
Alphabetic ASCII characters are those byte values in the sequence |
| 60 |
`b'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'`. |
| 61 |
""" |
| 62 |
def isascii(self) -> bool: |
| 63 |
"""Return `True` if the sequence is empty or all bytes in the sequence are ASCII, |
| 64 |
`False` otherwise. |
| 65 |
|
| 66 |
ASCII bytes are in the range `0-0x7F`. |
| 67 |
""" |
| 68 |
def isdigit(self) -> bool: |
| 69 |
"""Return `True` if all bytes in the sequence are ASCII decimal digits and the |
| 70 |
sequence is not empty, `False` otherwise. |
| 71 |
|
| 72 |
ASCII decimal digits are those byte values in the sequence `b'0123456789'`. |
| 73 |
""" |
| 74 |
def islower(self) -> bool: |
| 75 |
"""Return `True` if there is at least one lowercase ASCII character in the sequence |
| 76 |
and no uppercase ASCII characters, `False` otherwise. |
| 77 |
""" |
| 78 |
def isspace(self) -> bool: |
| 79 |
r"""Return `True` if all bytes in the sequence are ASCII whitespace and the sequence |
| 80 |
is not empty, `False` otherwise. |
| 81 |
|
| 82 |
ASCII whitespace characters are those byte values |
| 83 |
in the sequence `b' \t\n\r\x0b\f'` (space, tab, newline, carriage return, |
| 84 |
vertical tab, form feed). |
| 85 |
""" |
| 86 |
def isupper(self) -> bool: |
| 87 |
"""Return `True` if there is at least one uppercase alphabetic ASCII character in |
| 88 |
the sequence and no lowercase ASCII characters, `False` otherwise. |
| 89 |
""" |
| 90 |
|
| 91 |
def lower(self) -> Bytes: |
| 92 |
"""Return a copy of the sequence with all the uppercase ASCII characters converted |
| 93 |
to their corresponding lowercase counterpart. |
| 94 |
""" |
| 95 |
|
| 96 |
def upper(self) -> Bytes: |
| 97 |
"""Return a copy of the sequence with all the lowercase ASCII characters converted |
| 98 |
to their corresponding uppercase counterpart. |
| 99 |
""" |
| 100 |
|
| 101 |
def to_bytes(self) -> bytes: |
| 102 |
"""Copy this buffer's contents into a Python `bytes` object.""" |