Skip to content

Commit 89deb7d

Browse files
authored
[mypyc] Document recent additions to librt.strings, such as ispace (#21696)
Document `is*` functions, `toupper` and `tolower`.
1 parent f5163c0 commit 89deb7d

1 file changed

Lines changed: 61 additions & 1 deletion

File tree

mypyc/doc/librt_strings.rst

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Thread safety
1515
``BytesWriter`` and ``StringWriter`` objects are unsafe to access from another
1616
thread if they are concurrently modified (on free-threaded Python builds). They are optimized
1717
for maximal performance, and they aren't fully synchronized. Read-only access from multiple
18-
threads is safe.
18+
threads is safe, as always.
1919

2020
BytesWriter
2121
^^^^^^^^^^^
@@ -101,6 +101,9 @@ StringWriter
101101
Functions
102102
---------
103103

104+
Reading and writing binary data
105+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
106+
104107
The ``write_*`` and ``read_*`` functions allow interpreting bytes as packed binary
105108
data. They can be used as (much) more efficient but lower-level alternatives to the
106109
stdlib :mod:`struct` module in compiled code.
@@ -210,3 +213,60 @@ This example writes two binary values and reads them afterwards::
210213

211214
Read a 64-bit floating-point value starting at the given index as a big-endian binary value
212215
(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 ```` 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.

0 commit comments

Comments
 (0)