| 1 |
.. _librt-strings: |
| 2 |
|
| 3 |
librt.strings |
| 4 |
============= |
| 5 |
|
| 6 |
The ``librt.strings`` module is part of the ``librt`` package on PyPI, and it includes |
| 7 |
low-level string and bytes utilities. |
| 8 |
|
| 9 |
Classes |
| 10 |
------- |
| 11 |
|
| 12 |
Thread safety |
| 13 |
^^^^^^^^^^^^^ |
| 14 |
|
| 15 |
``BytesWriter`` and ``StringWriter`` objects are unsafe to access from another |
| 16 |
thread if they are concurrently modified (on free-threaded Python builds). They are optimized |
| 17 |
for maximal performance, and they aren't fully synchronized. Read-only access from multiple |
| 18 |
threads is safe, as always. |
| 19 |
|
| 20 |
BytesWriter |
| 21 |
^^^^^^^^^^^ |
| 22 |
|
| 23 |
.. class:: BytesWriter |
| 24 |
|
| 25 |
This class can be used to efficiently construct a bytes object from individual byte values |
| 26 |
and from bytes or bytearray objects. It also provides some operations for accessing |
| 27 |
and modifying items, but it doesn't support the full sequence interface. |
| 28 |
|
| 29 |
This can be used as a faster replacement for :py:class:`io.BytesIO` or :py:class:`bytearray` in |
| 30 |
compiled code. This is also usually faster than constructing a list of bytes objects and using |
| 31 |
the :meth:`bytes.join` method to concatenate them. |
| 32 |
|
| 33 |
.. method:: append(x: int, /) -> None |
| 34 |
|
| 35 |
Append a byte to contents. |
| 36 |
|
| 37 |
.. method:: write(b: bytes | bytearray, /) -> None |
| 38 |
|
| 39 |
Append a bytes or bytearray object to contents. |
| 40 |
|
| 41 |
.. method:: getvalue() -> bytes |
| 42 |
|
| 43 |
Return the contents as a bytes object. |
| 44 |
|
| 45 |
.. method:: truncate(size: i64, /) -> None |
| 46 |
|
| 47 |
Truncate the length of the contents to the given size. |
| 48 |
|
| 49 |
.. describe:: len(writer) → i64 |
| 50 |
|
| 51 |
Return the length of the contents. |
| 52 |
|
| 53 |
.. describe:: writer[i] → u8 |
| 54 |
|
| 55 |
Return the byte at a specific index. The index can be negative. |
| 56 |
|
| 57 |
.. describe:: writer[i] = x |
| 58 |
|
| 59 |
Set a byte at a specific index. The index can be negative. |
| 60 |
|
| 61 |
.. _librt-string-writer: |
| 62 |
|
| 63 |
StringWriter |
| 64 |
^^^^^^^^^^^^ |
| 65 |
|
| 66 |
.. class:: StringWriter |
| 67 |
|
| 68 |
This class can be used to efficiently construct a string object from individual Unicode code |
| 69 |
point integer values and from string objects. It also provides some operations for accessing |
| 70 |
items, but it doesn't support the full sequence interface. |
| 71 |
|
| 72 |
``StringWriter`` can be used as a faster replacement for :py:class:`io.StringIO` in |
| 73 |
compiled code. This is also usually faster than constructing a list of str objects and using |
| 74 |
the :meth:`str.join` method to concatenate them. |
| 75 |
|
| 76 |
If you construct a string from individual characters or code points, using integer values |
| 77 |
can be much faster than using 1-length strings. You can rely on expressions like ``ord("x")`` |
| 78 |
being treated as compile-time integer constants in compiled code. Also ``ord(s[i])`` is |
| 79 |
guaranteed to be a very quick operation in compiled code, if ``s`` has type :py:class:`str`. |
| 80 |
|
| 81 |
.. method:: append(x: int, /) -> None |
| 82 |
|
| 83 |
Append a Unicode code point (often representing a character) to the contents. |
| 84 |
|
| 85 |
.. method:: write(s: str, /) -> None |
| 86 |
|
| 87 |
Append a string to contents. |
| 88 |
|
| 89 |
.. method:: getvalue() -> str |
| 90 |
|
| 91 |
Return the contents as a string. |
| 92 |
|
| 93 |
.. describe:: len(writer) → i64 |
| 94 |
|
| 95 |
Return the length of the contents (number of code points). |
| 96 |
|
| 97 |
.. describe:: writer[i] → i32 |
| 98 |
|
| 99 |
Return the Unicode code point at a specific index as an integer. The index can be negative. |
| 100 |
|
| 101 |
Functions |
| 102 |
--------- |
| 103 |
|
| 104 |
Reading and writing binary data |
| 105 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 106 |
|
| 107 |
The ``write_*`` and ``read_*`` functions allow interpreting bytes as packed binary |
| 108 |
data. They can be used as (much) more efficient but lower-level alternatives to the |
| 109 |
stdlib :mod:`struct` module in compiled code. |
| 110 |
|
| 111 |
There are no functions for reading or writing individual bytes. ``BytesWriter.append`` can |
| 112 |
be used to insert a byte value, and ``b[n]`` can be used to read a byte value. Both |
| 113 |
are fast operations in compiled code. |
| 114 |
|
| 115 |
This example writes two binary values and reads them afterwards:: |
| 116 |
|
| 117 |
def example() -> None: |
| 118 |
b = BytesWriter() |
| 119 |
write_i32_le(b, 123) |
| 120 |
write_f64_le(b, 4.5) |
| 121 |
data = b.getvalue() |
| 122 |
|
| 123 |
x = read_i32_le(data, 0) |
| 124 |
y = read_f64_le(data, 4) |
| 125 |
... |
| 126 |
|
| 127 |
.. function:: write_i16_le(b: BytesWriter, n: i16, /) -> None |
| 128 |
|
| 129 |
Append a 16-bit integer as a little-endian binary value. |
| 130 |
|
| 131 |
.. function:: write_i16_be(b: BytesWriter, n: i16, /) -> None |
| 132 |
|
| 133 |
Append a 16-bit integer as a big-endian binary value. |
| 134 |
|
| 135 |
.. function:: read_i16_le(b: bytes, index: i64, /) -> i16 |
| 136 |
|
| 137 |
Read a 16-bit integer value starting at the given index as a little-endian binary value |
| 138 |
(2 bytes). |
| 139 |
|
| 140 |
.. function:: read_i16_be(b: bytes, index: i64, /) -> i16 |
| 141 |
|
| 142 |
Read a 16-bit integer value starting at the given index as a big-endian binary value |
| 143 |
(2 bytes). |
| 144 |
|
| 145 |
.. function:: write_i32_le(b: BytesWriter, n: i32, /) -> None |
| 146 |
|
| 147 |
Append a 32-bit integer as a little-endian binary value. |
| 148 |
|
| 149 |
.. function:: write_i32_be(b: BytesWriter, n: i32, /) -> None |
| 150 |
|
| 151 |
Append a 32-bit integer as a big-endian binary value. |
| 152 |
|
| 153 |
.. function:: read_i32_le(b: bytes, index: i64, /) -> i32 |
| 154 |
|
| 155 |
Read a 32-bit integer value starting at the given index as a little-endian binary value |
| 156 |
(4 bytes). |
| 157 |
|
| 158 |
.. function:: read_i32_be(b: bytes, index: i64, /) -> i32 |
| 159 |
|
| 160 |
Read a 32-bit integer value starting at the given index as a big-endian binary value |
| 161 |
(4 bytes). |
| 162 |
|
| 163 |
.. function:: write_i64_le(b: BytesWriter, n: i64, /) -> None |
| 164 |
|
| 165 |
Append a 64-bit integer as a little-endian binary value. |
| 166 |
|
| 167 |
.. function:: write_i64_be(b: BytesWriter, n: i64, /) -> None |
| 168 |
|
| 169 |
Append a 64-bit integer as a big-endian binary value. |
| 170 |
|
| 171 |
.. function:: read_i64_le(b: bytes, index: i64, /) -> i64 |
| 172 |
|
| 173 |
Read a 64-bit integer value starting at the given index as a little-endian binary value |
| 174 |
(8 bytes). |
| 175 |
|
| 176 |
.. function:: read_i64_be(b: bytes, index: i64, /) -> i64 |
| 177 |
|
| 178 |
Read a 64-bit integer value starting at the given index as a big-endian binary value |
| 179 |
(8 bytes). |
| 180 |
|
| 181 |
.. function:: write_f32_le(b: BytesWriter, n: float, /) -> None |
| 182 |
|
| 183 |
Append a 32-bit floating-point value as a little-endian binary value. |
| 184 |
|
| 185 |
.. function:: write_f32_be(b: BytesWriter, n: float, /) -> None |
| 186 |
|
| 187 |
Append a 32-bit floating-point value as a big-endian binary value. |
| 188 |
|
| 189 |
.. function:: read_f32_le(b: bytes, index: i64, /) -> float |
| 190 |
|
| 191 |
Read a 32-bit floating-point value starting at the given index as a little-endian binary value |
| 192 |
(4 bytes). |
| 193 |
|
| 194 |
.. function:: read_f32_be(b: bytes, index: i64, /) -> float |
| 195 |
|
| 196 |
Read a 32-bit floating-point value starting at the given index as a big-endian binary value |
| 197 |
(4 bytes). |
| 198 |
|
| 199 |
.. function:: write_f64_le(b: BytesWriter, n: float, /) -> None |
| 200 |
|
| 201 |
Append a 64-bit floating-point value as a little-endian binary value. |
| 202 |
|
| 203 |
.. function:: write_f64_be(b: BytesWriter, n: float, /) -> None |
| 204 |
|
| 205 |
Append a 64-bit floating-point value as a big-endian binary value. |
| 206 |
|
| 207 |
.. function:: read_f64_le(b: bytes, index: i64, /) -> float |
| 208 |
|
| 209 |
Read a 64-bit floating-point value starting at the given index as a little-endian binary value |
| 210 |
(8 bytes). |
| 211 |
|
| 212 |
.. function:: read_f64_be(b: bytes, index: i64, /) -> float |
| 213 |
|
| 214 |
Read a 64-bit floating-point value starting at the given index as a big-endian binary value |
| 215 |
(8 bytes). |
| 216 |
|
| 217 |
Code point classification |
| 218 |
^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 219 |
|
| 220 |
These functions classify a single Unicode code point, passed as an ``i32`` integer. They are |
| 221 |
faster alternatives to calling the corresponding :py:class:`str` methods on a one-character |
| 222 |
string in compiled code. A code point is often obtained via ``ord(s[i])``, which is a |
| 223 |
fast operation in compiled code when ``s`` has type :py:class:`str`. |
| 224 |
|
| 225 |
Each function agrees with the matching :py:class:`str` method applied to the one-character |
| 226 |
string ``chr(c)``. Out-of-range inputs (negative values, or values past the maximum Unicode |
| 227 |
code point ``0x10FFFF``) return ``False``. |
| 228 |
|
| 229 |
.. function:: isspace(c: i32, /) -> bool |
| 230 |
|
| 231 |
Return whether the code point is whitespace. Equivalent to ``chr(c).isspace()``. |
| 232 |
|
| 233 |
.. function:: isalpha(c: i32, /) -> bool |
| 234 |
|
| 235 |
Return whether the code point is alphabetic. Equivalent to ``chr(c).isalpha()``. |
| 236 |
|
| 237 |
.. function:: isdigit(c: i32, /) -> bool |
| 238 |
|
| 239 |
Return whether the code point is a digit. Equivalent to ``chr(c).isdigit()``. |
| 240 |
|
| 241 |
.. function:: isalnum(c: i32, /) -> bool |
| 242 |
|
| 243 |
Return whether the code point is alphanumeric. Equivalent to ``chr(c).isalnum()``. |
| 244 |
|
| 245 |
.. function:: isidentifier(c: i32, /) -> bool |
| 246 |
|
| 247 |
Return whether the code point is valid as the first character of a Python identifier. |
| 248 |
Equivalent to ``chr(c).isidentifier()``. |
| 249 |
|
| 250 |
Code point case conversion |
| 251 |
^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 252 |
|
| 253 |
These functions convert the case of a single Unicode code point, passed as an ``i32`` integer, |
| 254 |
and return the converted code point as an ``i32``. They are faster alternatives to |
| 255 |
:py:meth:`str.upper` / :py:meth:`str.lower` on a one-character string in compiled code. |
| 256 |
|
| 257 |
For the rare code points whose Unicode uppercase or lowercase form has multiple code points |
| 258 |
(e.g. U+00DF ``ß`` has the upper case form ``"SS"``, and U+FB01 ``fi`` maps to ``"FI"``), the |
| 259 |
input is returned unchanged, so the signature can stay ``i32 -> i32``. Use :py:meth:`str.upper` |
| 260 |
/ :py:meth:`str.lower` when full Unicode case conversion matters, or implement the logic to |
| 261 |
handle the special cases explicitly. Out-of-range inputs (negative values, or values past the |
| 262 |
maximum Unicode code point ``0x10FFFF``) are returned unchanged. |
| 263 |
|
| 264 |
.. function:: toupper(c: i32, /) -> i32 |
| 265 |
|
| 266 |
Return the uppercase of the code point, or the input unchanged if the uppercase does not |
| 267 |
consist of exactly one code point. |
| 268 |
|
| 269 |
.. function:: tolower(c: i32, /) -> i32 |
| 270 |
|
| 271 |
Return the lowercase of the code point, or the input unchanged if the lowercase does not |
| 272 |
consist of exactly one code point. |