Debian Codegraph @

mypyc/doc/librt_strings.rst

raw
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.
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 The ``write_*`` and ``read_*`` functions allow interpreting bytes as packed binary
105 data. They can be used as (much) more efficient but lower-level alternatives to the
106 stdlib :mod:`struct` module in compiled code.
107
108 There are no functions for reading or writing individual bytes. ``BytesWriter.append`` can
109 be used to insert a byte value, and ``b[n]`` can be used to read a byte value. Both
110 are fast operations in compiled code.
111
112 This example writes two binary values and reads them afterwards::
113
114 def example() -> None:
115 b = BytesWriter()
116 write_i32_le(b, 123)
117 write_f64_le(b, 4.5)
118 data = b.getvalue()
119
120 x = read_i32_le(data, 0)
121 y = read_f64_le(data, 4)
122 ...
123
124 .. function:: write_i16_le(b: BytesWriter, n: i16, /) -> None
125
126 Append a 16-bit integer as a little-endian binary value.
127
128 .. function:: write_i16_be(b: BytesWriter, n: i16, /) -> None
129
130 Append a 16-bit integer as a big-endian binary value.
131
132 .. function:: read_i16_le(b: bytes, index: i64, /) -> i16
133
134 Read a 16-bit integer value starting at the given index as a little-endian binary value
135 (2 bytes).
136
137 .. function:: read_i16_be(b: bytes, index: i64, /) -> i16
138
139 Read a 16-bit integer value starting at the given index as a big-endian binary value
140 (2 bytes).
141
142 .. function:: write_i32_le(b: BytesWriter, n: i32, /) -> None
143
144 Append a 32-bit integer as a little-endian binary value.
145
146 .. function:: write_i32_be(b: BytesWriter, n: i32, /) -> None
147
148 Append a 32-bit integer as a big-endian binary value.
149
150 .. function:: read_i32_le(b: bytes, index: i64, /) -> i32
151
152 Read a 32-bit integer value starting at the given index as a little-endian binary value
153 (4 bytes).
154
155 .. function:: read_i32_be(b: bytes, index: i64, /) -> i32
156
157 Read a 32-bit integer value starting at the given index as a big-endian binary value
158 (4 bytes).
159
160 .. function:: write_i64_le(b: BytesWriter, n: i64, /) -> None
161
162 Append a 64-bit integer as a little-endian binary value.
163
164 .. function:: write_i64_be(b: BytesWriter, n: i64, /) -> None
165
166 Append a 64-bit integer as a big-endian binary value.
167
168 .. function:: read_i64_le(b: bytes, index: i64, /) -> i64
169
170 Read a 64-bit integer value starting at the given index as a little-endian binary value
171 (8 bytes).
172
173 .. function:: read_i64_be(b: bytes, index: i64, /) -> i64
174
175 Read a 64-bit integer value starting at the given index as a big-endian binary value
176 (8 bytes).
177
178 .. function:: write_f32_le(b: BytesWriter, n: float, /) -> None
179
180 Append a 32-bit floating-point value as a little-endian binary value.
181
182 .. function:: write_f32_be(b: BytesWriter, n: float, /) -> None
183
184 Append a 32-bit floating-point value as a big-endian binary value.
185
186 .. function:: read_f32_le(b: bytes, index: i64, /) -> float
187
188 Read a 32-bit floating-point value starting at the given index as a little-endian binary value
189 (4 bytes).
190
191 .. function:: read_f32_be(b: bytes, index: i64, /) -> float
192
193 Read a 32-bit floating-point value starting at the given index as a big-endian binary value
194 (4 bytes).
195
196 .. function:: write_f64_le(b: BytesWriter, n: float, /) -> None
197
198 Append a 64-bit floating-point value as a little-endian binary value.
199
200 .. function:: write_f64_be(b: BytesWriter, n: float, /) -> None
201
202 Append a 64-bit floating-point value as a big-endian binary value.
203
204 .. function:: read_f64_le(b: bytes, index: i64, /) -> float
205
206 Read a 64-bit floating-point value starting at the given index as a little-endian binary value
207 (8 bytes).
208
209 .. function:: read_f64_be(b: bytes, index: i64, /) -> float
210
211 Read a 64-bit floating-point value starting at the given index as a big-endian binary value
212 (8 bytes).