1use crate::object::*;
2use crate::pyport::Py_ssize_t;
3use libc::wchar_t;
4use std::os::raw::{c_char, c_int, c_void};
5#[cfg(not(PyPy))]
6use std::ptr::addr_of_mut;
7
8#[cfg(not(Py_LIMITED_API))]
9pub type Py_UNICODE = wchar_t;
10
11pub type Py_UCS4 = u32;
12pub type Py_UCS2 = u16;
13pub type Py_UCS1 = u8;
14
15#[cfg_attr(windows, link(name = "pythonXY"))]
16extern "C" {
17 #[cfg_attr(PyPy, link_name = "PyPyUnicode_Type")]
18 pub static mut PyUnicode_Type: PyTypeObject;
19 pub static mut PyUnicodeIter_Type: PyTypeObject;
20
21 #[cfg(PyPy)]
22 #[link_name = "PyPyUnicode_Check"]
23 pub fn PyUnicode_Check(op: *mut PyObject) -> c_int;
24
25 #[cfg(PyPy)]
26 #[link_name = "PyPyUnicode_CheckExact"]
27 pub fn PyUnicode_CheckExact(op: *mut PyObject) -> c_int;
28}
29
30#[inline]
31#[cfg(not(PyPy))]
32pub unsafe fn PyUnicode_Check(op: *mut PyObject) -> c_int {
33 PyType_FastSubclass(t:Py_TYPE(op), Py_TPFLAGS_UNICODE_SUBCLASS)
34}
35
36#[inline]
37#[cfg(not(PyPy))]
38pub unsafe fn PyUnicode_CheckExact(op: *mut PyObject) -> c_int {
39 (Py_TYPE(ob:op) == addr_of_mut!(PyUnicode_Type)) as c_int
40}
41
42pub const Py_UNICODE_REPLACEMENT_CHARACTER: Py_UCS4 = 0xFFFD;
43
44extern "C" {
45
46 #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromStringAndSize")]
47 pub fn PyUnicode_FromStringAndSize(u: *const c_char, size: Py_ssize_t) -> *mut PyObject;
48 pub fn PyUnicode_FromString(u: *const c_char) -> *mut PyObject;
49
50 pub fn PyUnicode_Substring(
51 str: *mut PyObject,
52 start: Py_ssize_t,
53 end: Py_ssize_t,
54 ) -> *mut PyObject;
55 pub fn PyUnicode_AsUCS4(
56 unicode: *mut PyObject,
57 buffer: *mut Py_UCS4,
58 buflen: Py_ssize_t,
59 copy_null: c_int,
60 ) -> *mut Py_UCS4;
61 pub fn PyUnicode_AsUCS4Copy(unicode: *mut PyObject) -> *mut Py_UCS4;
62 #[cfg_attr(PyPy, link_name = "PyPyUnicode_GetLength")]
63 pub fn PyUnicode_GetLength(unicode: *mut PyObject) -> Py_ssize_t;
64 #[cfg(not(Py_3_12))]
65 #[deprecated(note = "Removed in Python 3.12")]
66 #[cfg_attr(PyPy, link_name = "PyPyUnicode_GetSize")]
67 pub fn PyUnicode_GetSize(unicode: *mut PyObject) -> Py_ssize_t;
68 pub fn PyUnicode_ReadChar(unicode: *mut PyObject, index: Py_ssize_t) -> Py_UCS4;
69 pub fn PyUnicode_WriteChar(
70 unicode: *mut PyObject,
71 index: Py_ssize_t,
72 character: Py_UCS4,
73 ) -> c_int;
74 #[cfg_attr(PyPy, link_name = "PyPyUnicode_Resize")]
75 pub fn PyUnicode_Resize(unicode: *mut *mut PyObject, length: Py_ssize_t) -> c_int;
76 #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromEncodedObject")]
77 pub fn PyUnicode_FromEncodedObject(
78 obj: *mut PyObject,
79 encoding: *const c_char,
80 errors: *const c_char,
81 ) -> *mut PyObject;
82 #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromObject")]
83 pub fn PyUnicode_FromObject(obj: *mut PyObject) -> *mut PyObject;
84 // #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromFormatV")]
85 // pub fn PyUnicode_FromFormatV(format: *const c_char, vargs: va_list) -> *mut PyObject;
86 #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromFormat")]
87 pub fn PyUnicode_FromFormat(format: *const c_char, ...) -> *mut PyObject;
88 #[cfg_attr(PyPy, link_name = "PyPyUnicode_InternInPlace")]
89 pub fn PyUnicode_InternInPlace(arg1: *mut *mut PyObject);
90 #[cfg(not(Py_3_12))]
91 #[cfg_attr(Py_3_10, deprecated(note = "Python 3.10"))]
92 pub fn PyUnicode_InternImmortal(arg1: *mut *mut PyObject);
93 #[cfg_attr(PyPy, link_name = "PyPyUnicode_InternFromString")]
94 pub fn PyUnicode_InternFromString(u: *const c_char) -> *mut PyObject;
95 #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromWideChar")]
96 pub fn PyUnicode_FromWideChar(w: *const wchar_t, size: Py_ssize_t) -> *mut PyObject;
97 #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsWideChar")]
98 pub fn PyUnicode_AsWideChar(
99 unicode: *mut PyObject,
100 w: *mut wchar_t,
101 size: Py_ssize_t,
102 ) -> Py_ssize_t;
103 #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsWideCharString")]
104 pub fn PyUnicode_AsWideCharString(
105 unicode: *mut PyObject,
106 size: *mut Py_ssize_t,
107 ) -> *mut wchar_t;
108 #[cfg_attr(PyPy, link_name = "PyPyUnicode_FromOrdinal")]
109 pub fn PyUnicode_FromOrdinal(ordinal: c_int) -> *mut PyObject;
110 pub fn PyUnicode_ClearFreeList() -> c_int;
111 #[cfg_attr(PyPy, link_name = "PyPyUnicode_GetDefaultEncoding")]
112 pub fn PyUnicode_GetDefaultEncoding() -> *const c_char;
113 #[cfg_attr(PyPy, link_name = "PyPyUnicode_Decode")]
114 pub fn PyUnicode_Decode(
115 s: *const c_char,
116 size: Py_ssize_t,
117 encoding: *const c_char,
118 errors: *const c_char,
119 ) -> *mut PyObject;
120 pub fn PyUnicode_AsDecodedObject(
121 unicode: *mut PyObject,
122 encoding: *const c_char,
123 errors: *const c_char,
124 ) -> *mut PyObject;
125 pub fn PyUnicode_AsDecodedUnicode(
126 unicode: *mut PyObject,
127 encoding: *const c_char,
128 errors: *const c_char,
129 ) -> *mut PyObject;
130 #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsEncodedObject")]
131 pub fn PyUnicode_AsEncodedObject(
132 unicode: *mut PyObject,
133 encoding: *const c_char,
134 errors: *const c_char,
135 ) -> *mut PyObject;
136 #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsEncodedString")]
137 pub fn PyUnicode_AsEncodedString(
138 unicode: *mut PyObject,
139 encoding: *const c_char,
140 errors: *const c_char,
141 ) -> *mut PyObject;
142 pub fn PyUnicode_AsEncodedUnicode(
143 unicode: *mut PyObject,
144 encoding: *const c_char,
145 errors: *const c_char,
146 ) -> *mut PyObject;
147 pub fn PyUnicode_BuildEncodingMap(string: *mut PyObject) -> *mut PyObject;
148 pub fn PyUnicode_DecodeUTF7(
149 string: *const c_char,
150 length: Py_ssize_t,
151 errors: *const c_char,
152 ) -> *mut PyObject;
153 pub fn PyUnicode_DecodeUTF7Stateful(
154 string: *const c_char,
155 length: Py_ssize_t,
156 errors: *const c_char,
157 consumed: *mut Py_ssize_t,
158 ) -> *mut PyObject;
159 #[cfg_attr(PyPy, link_name = "PyPyUnicode_DecodeUTF8")]
160 pub fn PyUnicode_DecodeUTF8(
161 string: *const c_char,
162 length: Py_ssize_t,
163 errors: *const c_char,
164 ) -> *mut PyObject;
165 pub fn PyUnicode_DecodeUTF8Stateful(
166 string: *const c_char,
167 length: Py_ssize_t,
168 errors: *const c_char,
169 consumed: *mut Py_ssize_t,
170 ) -> *mut PyObject;
171 #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsUTF8String")]
172 pub fn PyUnicode_AsUTF8String(unicode: *mut PyObject) -> *mut PyObject;
173 #[cfg(any(Py_3_10, not(Py_LIMITED_API)))]
174 #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsUTF8AndSize")]
175 pub fn PyUnicode_AsUTF8AndSize(unicode: *mut PyObject, size: *mut Py_ssize_t) -> *const c_char;
176 #[cfg_attr(PyPy, link_name = "PyPyUnicode_DecodeUTF32")]
177 pub fn PyUnicode_DecodeUTF32(
178 string: *const c_char,
179 length: Py_ssize_t,
180 errors: *const c_char,
181 byteorder: *mut c_int,
182 ) -> *mut PyObject;
183 pub fn PyUnicode_DecodeUTF32Stateful(
184 string: *const c_char,
185 length: Py_ssize_t,
186 errors: *const c_char,
187 byteorder: *mut c_int,
188 consumed: *mut Py_ssize_t,
189 ) -> *mut PyObject;
190 #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsUTF32String")]
191 pub fn PyUnicode_AsUTF32String(unicode: *mut PyObject) -> *mut PyObject;
192 #[cfg_attr(PyPy, link_name = "PyPyUnicode_DecodeUTF16")]
193 pub fn PyUnicode_DecodeUTF16(
194 string: *const c_char,
195 length: Py_ssize_t,
196 errors: *const c_char,
197 byteorder: *mut c_int,
198 ) -> *mut PyObject;
199 pub fn PyUnicode_DecodeUTF16Stateful(
200 string: *const c_char,
201 length: Py_ssize_t,
202 errors: *const c_char,
203 byteorder: *mut c_int,
204 consumed: *mut Py_ssize_t,
205 ) -> *mut PyObject;
206 #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsUTF16String")]
207 pub fn PyUnicode_AsUTF16String(unicode: *mut PyObject) -> *mut PyObject;
208 pub fn PyUnicode_DecodeUnicodeEscape(
209 string: *const c_char,
210 length: Py_ssize_t,
211 errors: *const c_char,
212 ) -> *mut PyObject;
213 #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsUnicodeEscapeString")]
214 pub fn PyUnicode_AsUnicodeEscapeString(unicode: *mut PyObject) -> *mut PyObject;
215 pub fn PyUnicode_DecodeRawUnicodeEscape(
216 string: *const c_char,
217 length: Py_ssize_t,
218 errors: *const c_char,
219 ) -> *mut PyObject;
220 pub fn PyUnicode_AsRawUnicodeEscapeString(unicode: *mut PyObject) -> *mut PyObject;
221 #[cfg_attr(PyPy, link_name = "PyPyUnicode_DecodeLatin1")]
222 pub fn PyUnicode_DecodeLatin1(
223 string: *const c_char,
224 length: Py_ssize_t,
225 errors: *const c_char,
226 ) -> *mut PyObject;
227 #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsLatin1String")]
228 pub fn PyUnicode_AsLatin1String(unicode: *mut PyObject) -> *mut PyObject;
229 #[cfg_attr(PyPy, link_name = "PyPyUnicode_DecodeASCII")]
230 pub fn PyUnicode_DecodeASCII(
231 string: *const c_char,
232 length: Py_ssize_t,
233 errors: *const c_char,
234 ) -> *mut PyObject;
235 #[cfg_attr(PyPy, link_name = "PyPyUnicode_AsASCIIString")]
236 pub fn PyUnicode_AsASCIIString(unicode: *mut PyObject) -> *mut PyObject;
237 pub fn PyUnicode_DecodeCharmap(
238 string: *const c_char,
239 length: Py_ssize_t,
240 mapping: *mut PyObject,
241 errors: *const c_char,
242 ) -> *mut PyObject;
243 pub fn PyUnicode_AsCharmapString(
244 unicode: *mut PyObject,
245 mapping: *mut PyObject,
246 ) -> *mut PyObject;
247 pub fn PyUnicode_DecodeLocaleAndSize(
248 str: *const c_char,
249 len: Py_ssize_t,
250 errors: *const c_char,
251 ) -> *mut PyObject;
252 pub fn PyUnicode_DecodeLocale(str: *const c_char, errors: *const c_char) -> *mut PyObject;
253 pub fn PyUnicode_EncodeLocale(unicode: *mut PyObject, errors: *const c_char) -> *mut PyObject;
254 #[cfg_attr(PyPy, link_name = "PyPyUnicode_FSConverter")]
255 pub fn PyUnicode_FSConverter(arg1: *mut PyObject, arg2: *mut c_void) -> c_int;
256 #[cfg_attr(PyPy, link_name = "PyPyUnicode_FSDecoder")]
257 pub fn PyUnicode_FSDecoder(arg1: *mut PyObject, arg2: *mut c_void) -> c_int;
258 #[cfg_attr(PyPy, link_name = "PyPyUnicode_DecodeFSDefault")]
259 pub fn PyUnicode_DecodeFSDefault(s: *const c_char) -> *mut PyObject;
260 #[cfg_attr(PyPy, link_name = "PyPyUnicode_DecodeFSDefaultAndSize")]
261 pub fn PyUnicode_DecodeFSDefaultAndSize(s: *const c_char, size: Py_ssize_t) -> *mut PyObject;
262 #[cfg_attr(PyPy, link_name = "PyPyUnicode_EncodeFSDefault")]
263 pub fn PyUnicode_EncodeFSDefault(unicode: *mut PyObject) -> *mut PyObject;
264 #[cfg_attr(PyPy, link_name = "PyPyUnicode_Concat")]
265 pub fn PyUnicode_Concat(left: *mut PyObject, right: *mut PyObject) -> *mut PyObject;
266 pub fn PyUnicode_Append(pleft: *mut *mut PyObject, right: *mut PyObject);
267 pub fn PyUnicode_AppendAndDel(pleft: *mut *mut PyObject, right: *mut PyObject);
268 #[cfg_attr(PyPy, link_name = "PyPyUnicode_Split")]
269 pub fn PyUnicode_Split(
270 s: *mut PyObject,
271 sep: *mut PyObject,
272 maxsplit: Py_ssize_t,
273 ) -> *mut PyObject;
274 #[cfg_attr(PyPy, link_name = "PyPyUnicode_Splitlines")]
275 pub fn PyUnicode_Splitlines(s: *mut PyObject, keepends: c_int) -> *mut PyObject;
276 pub fn PyUnicode_Partition(s: *mut PyObject, sep: *mut PyObject) -> *mut PyObject;
277 pub fn PyUnicode_RPartition(s: *mut PyObject, sep: *mut PyObject) -> *mut PyObject;
278 pub fn PyUnicode_RSplit(
279 s: *mut PyObject,
280 sep: *mut PyObject,
281 maxsplit: Py_ssize_t,
282 ) -> *mut PyObject;
283 pub fn PyUnicode_Translate(
284 str: *mut PyObject,
285 table: *mut PyObject,
286 errors: *const c_char,
287 ) -> *mut PyObject;
288 #[cfg_attr(PyPy, link_name = "PyPyUnicode_Join")]
289 pub fn PyUnicode_Join(separator: *mut PyObject, seq: *mut PyObject) -> *mut PyObject;
290 #[cfg_attr(PyPy, link_name = "PyPyUnicode_Tailmatch")]
291 pub fn PyUnicode_Tailmatch(
292 str: *mut PyObject,
293 substr: *mut PyObject,
294 start: Py_ssize_t,
295 end: Py_ssize_t,
296 direction: c_int,
297 ) -> Py_ssize_t;
298 #[cfg_attr(PyPy, link_name = "PyPyUnicode_Find")]
299 pub fn PyUnicode_Find(
300 str: *mut PyObject,
301 substr: *mut PyObject,
302 start: Py_ssize_t,
303 end: Py_ssize_t,
304 direction: c_int,
305 ) -> Py_ssize_t;
306 pub fn PyUnicode_FindChar(
307 str: *mut PyObject,
308 ch: Py_UCS4,
309 start: Py_ssize_t,
310 end: Py_ssize_t,
311 direction: c_int,
312 ) -> Py_ssize_t;
313 #[cfg_attr(PyPy, link_name = "PyPyUnicode_Count")]
314 pub fn PyUnicode_Count(
315 str: *mut PyObject,
316 substr: *mut PyObject,
317 start: Py_ssize_t,
318 end: Py_ssize_t,
319 ) -> Py_ssize_t;
320 #[cfg_attr(PyPy, link_name = "PyPyUnicode_Replace")]
321 pub fn PyUnicode_Replace(
322 str: *mut PyObject,
323 substr: *mut PyObject,
324 replstr: *mut PyObject,
325 maxcount: Py_ssize_t,
326 ) -> *mut PyObject;
327 #[cfg_attr(PyPy, link_name = "PyPyUnicode_Compare")]
328 pub fn PyUnicode_Compare(left: *mut PyObject, right: *mut PyObject) -> c_int;
329 #[cfg_attr(PyPy, link_name = "PyPyUnicode_CompareWithASCIIString")]
330 pub fn PyUnicode_CompareWithASCIIString(left: *mut PyObject, right: *const c_char) -> c_int;
331 pub fn PyUnicode_RichCompare(
332 left: *mut PyObject,
333 right: *mut PyObject,
334 op: c_int,
335 ) -> *mut PyObject;
336 #[cfg_attr(PyPy, link_name = "PyPyUnicode_Format")]
337 pub fn PyUnicode_Format(format: *mut PyObject, args: *mut PyObject) -> *mut PyObject;
338 pub fn PyUnicode_Contains(container: *mut PyObject, element: *mut PyObject) -> c_int;
339 pub fn PyUnicode_IsIdentifier(s: *mut PyObject) -> c_int;
340}
341