Debian Codegraph @

strings/librt_strings.h

raw
1 #ifndef LIBRT_STRINGS_H
2 #define LIBRT_STRINGS_H
3
4 #include <Python.h>
5 #include <stdbool.h>
6 #include <stdint.h>
7 #include "CPy.h"
8 #include "librt_strings_common.h"
9
10 // ABI version -- only an exact match is compatible. This will only be changed in
11 // very exceptional cases (likely never) due to strict backward compatibility
12 // requirements.
13 #define LIBRT_STRINGS_ABI_VERSION 1
14
15 // API version -- more recent versions must maintain backward compatibility, i.e.
16 // we can add new features but not remove or change existing features (unless
17 // ABI version is changed, but see the comment above).
18 #define LIBRT_STRINGS_API_VERSION 4
19
20 // Number of functions in the capsule API. If you add a new function, also increase
21 // LIBRT_STRINGS_API_VERSION.
22 #define LIBRT_STRINGS_API_LEN 14
23
24 typedef struct {
25 PyObject_HEAD
26 char *buf; // Beginning of the buffer
27 char kind; // Bytes per code point (1, 2 or 4)
28 Py_ssize_t len; // Current length (number of code points written)
29 Py_ssize_t capacity; // Total capacity of the buffer (number of code points)
30 char data[WRITER_EMBEDDED_BUF_LEN]; // Default buffer
31 } StringWriterObject;
32
33 // Codepoint classification helpers. Inputs are signed i32 for compatibility
34 // with mypyc's int32_rprimitive; out-of-range values (negative, or past the
35 // maximum Unicode code point 0x10FFFF) are non-codepoints and return false.
36 // Defined `static inline` so they compile statically into
37 // both the librt.strings module and any mypyc-compiled extension that
38 // includes this header, avoiding the capsule indirection that would dwarf
39 // the work of a single Py_UNICODE_IS* macro call.
40
41 static inline bool LibRTStrings_IsSpace(int32_t c) {
42 return c >= 0 && Py_UNICODE_ISSPACE((Py_UCS4)c);
43 }
44
45 static inline bool LibRTStrings_IsDigit(int32_t c) {
46 return c >= 0 && Py_UNICODE_ISDIGIT((Py_UCS4)c);
47 }
48
49 static inline bool LibRTStrings_IsAlnum(int32_t c) {
50 return c >= 0 && Py_UNICODE_ISALNUM((Py_UCS4)c);
51 }
52
53 static inline bool LibRTStrings_IsAlpha(int32_t c) {
54 return c >= 0 && Py_UNICODE_ISALPHA((Py_UCS4)c);
55 }
56
57 // True if c could start a valid identifier (XID_Start, per PEP 3131).
58 // ASCII fast path covers `[A-Za-z_]`; non-ASCII delegates to CPython's
59 // PyUnicode_IsIdentifier on a 1-character string. Aborts via
60 // CPyError_OutOfMemory on allocation failure to keep this ERR_NEVER.
61 static inline bool LibRTStrings_IsIdentifier(int32_t c) {
62 // Unsigned compare: negatives wrap to large values and skip the fast path.
63 if ((uint32_t)c < 128) {
64 return (c >= 'a' && c <= 'z')
65 || (c >= 'A' && c <= 'Z')
66 || c == '_';
67 }
68 // Reject negatives and code points past the Unicode maximum.
69 if ((uint32_t)c > 0x10FFFF) return false;
70 PyObject *s = PyUnicode_FromOrdinal((int)c);
71 if (s == NULL) {
72 CPyError_OutOfMemory();
73 }
74 int r = PyUnicode_IsIdentifier(s);
75 Py_DECREF(s);
76 return r == 1;
77 }
78
79 // Shared slow path for LibRTStrings_ToUpper / _ToLower. Round-trips the
80 // codepoint through CPython's str.upper / str.lower on a 1-character
81 // string. When the conversion expands to multiple codepoints (e.g.
82 // 'ß'.upper() == 'SS') we return the input unchanged so the public
83 // helpers stay i32 -> i32. Aborts via CPyError_OutOfMemory on allocation
84 // failure.
85 static inline int32_t LibRTStrings_ChangeCase_slow(int32_t c, const char *method) {
86 PyObject *s = PyUnicode_FromOrdinal((int)c);
87 if (s == NULL) {
88 CPyError_OutOfMemory();
89 }
90 PyObject *u = PyObject_CallMethod(s, method, NULL);
91 Py_DECREF(s);
92 if (u == NULL) {
93 CPyError_OutOfMemory();
94 }
95 int32_t result = c;
96 if (PyUnicode_GET_LENGTH(u) == 1) {
97 result = (int32_t)PyUnicode_READ_CHAR(u, 0);
98 }
99 Py_DECREF(u);
100 return result;
101 }
102
103 // Uppercase a codepoint. ASCII fast path is `a..z -> A..Z` (subtract 32);
104 // non-ASCII delegates to str.upper on a 1-character string. Returns the
105 // input unchanged when uppercasing expands to multiple codepoints.
106 static inline int32_t LibRTStrings_ToUpper(int32_t c) {
107 // Unsigned compare: negatives wrap to large values and skip the fast path.
108 if ((uint32_t)c < 128) {
109 if (c >= 'a' && c <= 'z') return c - 32;
110 return c;
111 }
112 // Negatives and code points past the Unicode maximum are returned unchanged.
113 if ((uint32_t)c > 0x10FFFF) return c;
114 return LibRTStrings_ChangeCase_slow(c, "upper");
115 }
116
117 // Lowercase a codepoint. ASCII fast path is `A..Z -> a..z` (add 32);
118 // non-ASCII delegates to str.lower on a 1-character string. Returns the
119 // input unchanged when lowercasing expands to multiple codepoints.
120 static inline int32_t LibRTStrings_ToLower(int32_t c) {
121 // Unsigned compare: negatives wrap to large values and skip the fast path.
122 if ((uint32_t)c < 128) {
123 if (c >= 'A' && c <= 'Z') return c + 32;
124 return c;
125 }
126 // Negatives and code points past the Unicode maximum are returned unchanged.
127 if ((uint32_t)c > 0x10FFFF) return c;
128 return LibRTStrings_ChangeCase_slow(c, "lower");
129 }
130
131 #endif // LIBRT_STRINGS_H