Debian Codegraph @

mypyc/lib-rt/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; negative values are non-codepoints and
35 // return false. Defined `static inline` so they compile statically into
36 // both the librt.strings module and any mypyc-compiled extension that
37 // includes this header, avoiding the capsule indirection that would dwarf
38 // the work of a single Py_UNICODE_IS* macro call.
39
40 static inline bool LibRTStrings_IsSpace(int32_t c) {
41 return c >= 0 && Py_UNICODE_ISSPACE((Py_UCS4)c);
42 }
43
44 static inline bool LibRTStrings_IsDigit(int32_t c) {
45 return c >= 0 && Py_UNICODE_ISDIGIT((Py_UCS4)c);
46 }
47
48 static inline bool LibRTStrings_IsAlnum(int32_t c) {
49 return c >= 0 && Py_UNICODE_ISALNUM((Py_UCS4)c);
50 }
51
52 static inline bool LibRTStrings_IsAlpha(int32_t c) {
53 return c >= 0 && Py_UNICODE_ISALPHA((Py_UCS4)c);
54 }
55
56 // True if c could start a valid identifier (XID_Start, per PEP 3131).
57 // ASCII fast path covers `[A-Za-z_]`; non-ASCII delegates to CPython's
58 // PyUnicode_IsIdentifier on a 1-character string. Aborts via
59 // CPyError_OutOfMemory on allocation failure to keep this ERR_NEVER.
60 static inline bool LibRTStrings_IsIdentifier(int32_t c) {
61 if (c < 0) return false;
62 if (c < 128) {
63 return (c >= 'a' && c <= 'z')
64 || (c >= 'A' && c <= 'Z')
65 || c == '_';
66 }
67 PyObject *s = PyUnicode_FromOrdinal((int)c);
68 if (s == NULL) {
69 CPyError_OutOfMemory();
70 }
71 int r = PyUnicode_IsIdentifier(s);
72 Py_DECREF(s);
73 return r == 1;
74 }
75
76 // Shared slow path for LibRTStrings_ToUpper / _ToLower. Round-trips the
77 // codepoint through CPython's str.upper / str.lower on a 1-character
78 // string. When the conversion expands to multiple codepoints (e.g.
79 // 'ß'.upper() == 'SS') we return the input unchanged so the public
80 // helpers stay i32 -> i32. Aborts via CPyError_OutOfMemory on allocation
81 // failure.
82 static inline int32_t LibRTStrings_ChangeCase_slow(int32_t c, const char *method) {
83 PyObject *s = PyUnicode_FromOrdinal((int)c);
84 if (s == NULL) {
85 CPyError_OutOfMemory();
86 }
87 PyObject *u = PyObject_CallMethod(s, method, NULL);
88 Py_DECREF(s);
89 if (u == NULL) {
90 CPyError_OutOfMemory();
91 }
92 int32_t result = c;
93 if (PyUnicode_GET_LENGTH(u) == 1) {
94 result = (int32_t)PyUnicode_READ_CHAR(u, 0);
95 }
96 Py_DECREF(u);
97 return result;
98 }
99
100 // Uppercase a codepoint. ASCII fast path is `a..z -> A..Z` (subtract 32);
101 // non-ASCII delegates to str.upper on a 1-character string. Returns the
102 // input unchanged when uppercasing expands to multiple codepoints.
103 static inline int32_t LibRTStrings_ToUpper(int32_t c) {
104 if (c < 0) return c;
105 if (c >= 'a' && c <= 'z') return c - 32;
106 if (c < 128) return c;
107 return LibRTStrings_ChangeCase_slow(c, "upper");
108 }
109
110 // Lowercase a codepoint. ASCII fast path is `A..Z -> a..z` (add 32);
111 // non-ASCII delegates to str.lower on a 1-character string. Returns the
112 // input unchanged when lowercasing expands to multiple codepoints.
113 static inline int32_t LibRTStrings_ToLower(int32_t c) {
114 if (c < 0) return c;
115 if (c >= 'A' && c <= 'Z') return c + 32;
116 if (c < 128) return c;
117 return LibRTStrings_ChangeCase_slow(c, "lower");
118 }
119
120 #endif // LIBRT_STRINGS_H