1
2 //////////////////// IncludeStringH.proto ////////////////////
3
4 #include <string.h>
5
6 //////////////////// IncludeCppStringH.proto ////////////////////
7
8 #include <string>
9
10 //////////////////// InitStrings.proto ////////////////////
11
12 static int __Pyx_InitStrings(__Pyx_StringTabEntry *t); /*proto*/
13
14 //////////////////// InitStrings ////////////////////
15
__Pyx_InitStrings(__Pyx_StringTabEntry * t)16 static int __Pyx_InitStrings(__Pyx_StringTabEntry *t) {
17 while (t->p) {
18 #if PY_MAJOR_VERSION < 3
19 if (t->is_unicode) {
20 *t->p = PyUnicode_DecodeUTF8(t->s, t->n - 1, NULL);
21 } else if (t->intern) {
22 *t->p = PyString_InternFromString(t->s);
23 } else {
24 *t->p = PyString_FromStringAndSize(t->s, t->n - 1);
25 }
26 #else /* Python 3+ has unicode identifiers */
27 if (t->is_unicode | t->is_str) {
28 if (t->intern) {
29 *t->p = PyUnicode_InternFromString(t->s);
30 } else if (t->encoding) {
31 *t->p = PyUnicode_Decode(t->s, t->n - 1, t->encoding, NULL);
32 } else {
33 *t->p = PyUnicode_FromStringAndSize(t->s, t->n - 1);
34 }
35 } else {
36 *t->p = PyBytes_FromStringAndSize(t->s, t->n - 1);
37 }
38 #endif
39 if (!*t->p)
40 return -1;
41 // initialise cached hash value
42 if (PyObject_Hash(*t->p) == -1)
43 return -1;
44 ++t;
45 }
46 return 0;
47 }
48
49 //////////////////// BytesContains.proto ////////////////////
50
51 static CYTHON_INLINE int __Pyx_BytesContains(PyObject* bytes, char character); /*proto*/
52
53 //////////////////// BytesContains ////////////////////
54 //@requires: IncludeStringH
55
__Pyx_BytesContains(PyObject * bytes,char character)56 static CYTHON_INLINE int __Pyx_BytesContains(PyObject* bytes, char character) {
57 const Py_ssize_t length = PyBytes_GET_SIZE(bytes);
58 char* char_start = PyBytes_AS_STRING(bytes);
59 return memchr(char_start, (unsigned char)character, (size_t)length) != NULL;
60 }
61
62
63 //////////////////// PyUCS4InUnicode.proto ////////////////////
64
65 static CYTHON_INLINE int __Pyx_UnicodeContainsUCS4(PyObject* unicode, Py_UCS4 character); /*proto*/
66
67 //////////////////// PyUCS4InUnicode ////////////////////
68
69 #if PY_VERSION_HEX < 0x03090000 || (defined(PyUnicode_WCHAR_KIND) && defined(PyUnicode_AS_UNICODE))
70
71 #if PY_VERSION_HEX < 0x03090000
72 #define __Pyx_PyUnicode_AS_UNICODE(op) PyUnicode_AS_UNICODE(op)
73 #define __Pyx_PyUnicode_GET_SIZE(op) PyUnicode_GET_SIZE(op)
74 #else
75 // Avoid calling deprecated C-API functions in Py3.9+ that PEP-623 schedules for removal in Py3.12.
76 // https://www.python.org/dev/peps/pep-0623/
77 #define __Pyx_PyUnicode_AS_UNICODE(op) (((PyASCIIObject *)(op))->wstr)
78 #define __Pyx_PyUnicode_GET_SIZE(op) ((PyCompactUnicodeObject *)(op))->wstr_length
79 #endif
80
81 #if !defined(Py_UNICODE_SIZE) || Py_UNICODE_SIZE == 2
__Pyx_PyUnicodeBufferContainsUCS4_SP(Py_UNICODE * buffer,Py_ssize_t length,Py_UCS4 character)82 static int __Pyx_PyUnicodeBufferContainsUCS4_SP(Py_UNICODE* buffer, Py_ssize_t length, Py_UCS4 character) {
83 /* handle surrogate pairs for Py_UNICODE buffers in 16bit Unicode builds */
84 Py_UNICODE high_val, low_val;
85 Py_UNICODE* pos;
86 high_val = (Py_UNICODE) (0xD800 | (((character - 0x10000) >> 10) & ((1<<10)-1)));
87 low_val = (Py_UNICODE) (0xDC00 | ( (character - 0x10000) & ((1<<10)-1)));
88 for (pos=buffer; pos < buffer+length-1; pos++) {
89 if (unlikely((high_val == pos[0]) & (low_val == pos[1]))) return 1;
90 }
91 return 0;
92 }
93 #endif
94
__Pyx_PyUnicodeBufferContainsUCS4_BMP(Py_UNICODE * buffer,Py_ssize_t length,Py_UCS4 character)95 static int __Pyx_PyUnicodeBufferContainsUCS4_BMP(Py_UNICODE* buffer, Py_ssize_t length, Py_UCS4 character) {
96 Py_UNICODE uchar;
97 Py_UNICODE* pos;
98 uchar = (Py_UNICODE) character;
99 for (pos=buffer; pos < buffer+length; pos++) {
100 if (unlikely(uchar == pos[0])) return 1;
101 }
102 return 0;
103 }
104 #endif
105
__Pyx_UnicodeContainsUCS4(PyObject * unicode,Py_UCS4 character)106 static CYTHON_INLINE int __Pyx_UnicodeContainsUCS4(PyObject* unicode, Py_UCS4 character) {
107 #if CYTHON_PEP393_ENABLED
108 const int kind = PyUnicode_KIND(unicode);
109 #ifdef PyUnicode_WCHAR_KIND
110 if (likely(kind != PyUnicode_WCHAR_KIND))
111 #endif
112 {
113 Py_ssize_t i;
114 const void* udata = PyUnicode_DATA(unicode);
115 const Py_ssize_t length = PyUnicode_GET_LENGTH(unicode);
116 for (i=0; i < length; i++) {
117 if (unlikely(character == PyUnicode_READ(kind, udata, i))) return 1;
118 }
119 return 0;
120 }
121 #elif PY_VERSION_HEX >= 0x03090000
122 #error Cannot use "UChar in Unicode" in Python 3.9 without PEP-393 unicode strings.
123 #elif !defined(PyUnicode_AS_UNICODE)
124 #error Cannot use "UChar in Unicode" in Python < 3.9 without Py_UNICODE support.
125 #endif
126
127 #if PY_VERSION_HEX < 0x03090000 || (defined(PyUnicode_WCHAR_KIND) && defined(PyUnicode_AS_UNICODE))
128 #if !defined(Py_UNICODE_SIZE) || Py_UNICODE_SIZE == 2
129 if ((sizeof(Py_UNICODE) == 2) && unlikely(character > 65535)) {
130 return __Pyx_PyUnicodeBufferContainsUCS4_SP(
131 __Pyx_PyUnicode_AS_UNICODE(unicode),
132 __Pyx_PyUnicode_GET_SIZE(unicode),
133 character);
134 } else
135 #endif
136 {
137 return __Pyx_PyUnicodeBufferContainsUCS4_BMP(
138 __Pyx_PyUnicode_AS_UNICODE(unicode),
139 __Pyx_PyUnicode_GET_SIZE(unicode),
140 character);
141
142 }
143 #endif
144 }
145
146
147 //////////////////// PyUnicodeContains.proto ////////////////////
148
__Pyx_PyUnicode_ContainsTF(PyObject * substring,PyObject * text,int eq)149 static CYTHON_INLINE int __Pyx_PyUnicode_ContainsTF(PyObject* substring, PyObject* text, int eq) {
150 int result = PyUnicode_Contains(text, substring);
151 return unlikely(result < 0) ? result : (result == (eq == Py_EQ));
152 }
153
154
155 //////////////////// CStringEquals.proto ////////////////////
156
157 static CYTHON_INLINE int __Pyx_StrEq(const char *, const char *); /*proto*/
158
159 //////////////////// CStringEquals ////////////////////
160
__Pyx_StrEq(const char * s1,const char * s2)161 static CYTHON_INLINE int __Pyx_StrEq(const char *s1, const char *s2) {
162 while (*s1 != '\0' && *s1 == *s2) { s1++; s2++; }
163 return *s1 == *s2;
164 }
165
166
167 //////////////////// StrEquals.proto ////////////////////
168 //@requires: BytesEquals
169 //@requires: UnicodeEquals
170
171 #if PY_MAJOR_VERSION >= 3
172 #define __Pyx_PyString_Equals __Pyx_PyUnicode_Equals
173 #else
174 #define __Pyx_PyString_Equals __Pyx_PyBytes_Equals
175 #endif
176
177
178 //////////////////// UnicodeEquals.proto ////////////////////
179
180 static CYTHON_INLINE int __Pyx_PyUnicode_Equals(PyObject* s1, PyObject* s2, int equals); /*proto*/
181
182 //////////////////// UnicodeEquals ////////////////////
183 //@requires: BytesEquals
184
__Pyx_PyUnicode_Equals(PyObject * s1,PyObject * s2,int equals)185 static CYTHON_INLINE int __Pyx_PyUnicode_Equals(PyObject* s1, PyObject* s2, int equals) {
186 #if CYTHON_COMPILING_IN_PYPY
187 return PyObject_RichCompareBool(s1, s2, equals);
188 #else
189 #if PY_MAJOR_VERSION < 3
190 PyObject* owned_ref = NULL;
191 #endif
192 int s1_is_unicode, s2_is_unicode;
193 if (s1 == s2) {
194 /* as done by PyObject_RichCompareBool(); also catches the (interned) empty string */
195 goto return_eq;
196 }
197 s1_is_unicode = PyUnicode_CheckExact(s1);
198 s2_is_unicode = PyUnicode_CheckExact(s2);
199 #if PY_MAJOR_VERSION < 3
200 if ((s1_is_unicode & (!s2_is_unicode)) && PyString_CheckExact(s2)) {
201 owned_ref = PyUnicode_FromObject(s2);
202 if (unlikely(!owned_ref))
203 return -1;
204 s2 = owned_ref;
205 s2_is_unicode = 1;
206 } else if ((s2_is_unicode & (!s1_is_unicode)) && PyString_CheckExact(s1)) {
207 owned_ref = PyUnicode_FromObject(s1);
208 if (unlikely(!owned_ref))
209 return -1;
210 s1 = owned_ref;
211 s1_is_unicode = 1;
212 } else if (((!s2_is_unicode) & (!s1_is_unicode))) {
213 return __Pyx_PyBytes_Equals(s1, s2, equals);
214 }
215 #endif
216 if (s1_is_unicode & s2_is_unicode) {
217 Py_ssize_t length;
218 int kind;
219 void *data1, *data2;
220 if (unlikely(__Pyx_PyUnicode_READY(s1) < 0) || unlikely(__Pyx_PyUnicode_READY(s2) < 0))
221 return -1;
222 length = __Pyx_PyUnicode_GET_LENGTH(s1);
223 if (length != __Pyx_PyUnicode_GET_LENGTH(s2)) {
224 goto return_ne;
225 }
226 #if CYTHON_USE_UNICODE_INTERNALS
227 {
228 Py_hash_t hash1, hash2;
229 #if CYTHON_PEP393_ENABLED
230 hash1 = ((PyASCIIObject*)s1)->hash;
231 hash2 = ((PyASCIIObject*)s2)->hash;
232 #else
233 hash1 = ((PyUnicodeObject*)s1)->hash;
234 hash2 = ((PyUnicodeObject*)s2)->hash;
235 #endif
236 if (hash1 != hash2 && hash1 != -1 && hash2 != -1) {
237 goto return_ne;
238 }
239 }
240 #endif
241 // len(s1) == len(s2) >= 1 (empty string is interned, and "s1 is not s2")
242 kind = __Pyx_PyUnicode_KIND(s1);
243 if (kind != __Pyx_PyUnicode_KIND(s2)) {
244 goto return_ne;
245 }
246 data1 = __Pyx_PyUnicode_DATA(s1);
247 data2 = __Pyx_PyUnicode_DATA(s2);
248 if (__Pyx_PyUnicode_READ(kind, data1, 0) != __Pyx_PyUnicode_READ(kind, data2, 0)) {
249 goto return_ne;
250 } else if (length == 1) {
251 goto return_eq;
252 } else {
253 int result = memcmp(data1, data2, (size_t)(length * kind));
254 #if PY_MAJOR_VERSION < 3
255 Py_XDECREF(owned_ref);
256 #endif
257 return (equals == Py_EQ) ? (result == 0) : (result != 0);
258 }
259 } else if ((s1 == Py_None) & s2_is_unicode) {
260 goto return_ne;
261 } else if ((s2 == Py_None) & s1_is_unicode) {
262 goto return_ne;
263 } else {
264 int result;
265 PyObject* py_result = PyObject_RichCompare(s1, s2, equals);
266 #if PY_MAJOR_VERSION < 3
267 Py_XDECREF(owned_ref);
268 #endif
269 if (!py_result)
270 return -1;
271 result = __Pyx_PyObject_IsTrue(py_result);
272 Py_DECREF(py_result);
273 return result;
274 }
275 return_eq:
276 #if PY_MAJOR_VERSION < 3
277 Py_XDECREF(owned_ref);
278 #endif
279 return (equals == Py_EQ);
280 return_ne:
281 #if PY_MAJOR_VERSION < 3
282 Py_XDECREF(owned_ref);
283 #endif
284 return (equals == Py_NE);
285 #endif
286 }
287
288
289 //////////////////// BytesEquals.proto ////////////////////
290
291 static CYTHON_INLINE int __Pyx_PyBytes_Equals(PyObject* s1, PyObject* s2, int equals); /*proto*/
292
293 //////////////////// BytesEquals ////////////////////
294 //@requires: IncludeStringH
295
__Pyx_PyBytes_Equals(PyObject * s1,PyObject * s2,int equals)296 static CYTHON_INLINE int __Pyx_PyBytes_Equals(PyObject* s1, PyObject* s2, int equals) {
297 #if CYTHON_COMPILING_IN_PYPY
298 return PyObject_RichCompareBool(s1, s2, equals);
299 #else
300 if (s1 == s2) {
301 /* as done by PyObject_RichCompareBool(); also catches the (interned) empty string */
302 return (equals == Py_EQ);
303 } else if (PyBytes_CheckExact(s1) & PyBytes_CheckExact(s2)) {
304 const char *ps1, *ps2;
305 Py_ssize_t length = PyBytes_GET_SIZE(s1);
306 if (length != PyBytes_GET_SIZE(s2))
307 return (equals == Py_NE);
308 // len(s1) == len(s2) >= 1 (empty string is interned, and "s1 is not s2")
309 ps1 = PyBytes_AS_STRING(s1);
310 ps2 = PyBytes_AS_STRING(s2);
311 if (ps1[0] != ps2[0]) {
312 return (equals == Py_NE);
313 } else if (length == 1) {
314 return (equals == Py_EQ);
315 } else {
316 int result;
317 #if CYTHON_USE_UNICODE_INTERNALS
318 Py_hash_t hash1, hash2;
319 hash1 = ((PyBytesObject*)s1)->ob_shash;
320 hash2 = ((PyBytesObject*)s2)->ob_shash;
321 if (hash1 != hash2 && hash1 != -1 && hash2 != -1) {
322 return (equals == Py_NE);
323 }
324 #endif
325 result = memcmp(ps1, ps2, (size_t)length);
326 return (equals == Py_EQ) ? (result == 0) : (result != 0);
327 }
328 } else if ((s1 == Py_None) & PyBytes_CheckExact(s2)) {
329 return (equals == Py_NE);
330 } else if ((s2 == Py_None) & PyBytes_CheckExact(s1)) {
331 return (equals == Py_NE);
332 } else {
333 int result;
334 PyObject* py_result = PyObject_RichCompare(s1, s2, equals);
335 if (!py_result)
336 return -1;
337 result = __Pyx_PyObject_IsTrue(py_result);
338 Py_DECREF(py_result);
339 return result;
340 }
341 #endif
342 }
343
344 //////////////////// GetItemIntByteArray.proto ////////////////////
345
346 #define __Pyx_GetItemInt_ByteArray(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck) \
347 (__Pyx_fits_Py_ssize_t(i, type, is_signed) ? \
348 __Pyx_GetItemInt_ByteArray_Fast(o, (Py_ssize_t)i, wraparound, boundscheck) : \
349 (PyErr_SetString(PyExc_IndexError, "bytearray index out of range"), -1))
350
351 static CYTHON_INLINE int __Pyx_GetItemInt_ByteArray_Fast(PyObject* string, Py_ssize_t i,
352 int wraparound, int boundscheck);
353
354 //////////////////// GetItemIntByteArray ////////////////////
355
__Pyx_GetItemInt_ByteArray_Fast(PyObject * string,Py_ssize_t i,int wraparound,int boundscheck)356 static CYTHON_INLINE int __Pyx_GetItemInt_ByteArray_Fast(PyObject* string, Py_ssize_t i,
357 int wraparound, int boundscheck) {
358 Py_ssize_t length;
359 if (wraparound | boundscheck) {
360 length = PyByteArray_GET_SIZE(string);
361 if (wraparound & unlikely(i < 0)) i += length;
362 if ((!boundscheck) || likely(__Pyx_is_valid_index(i, length))) {
363 return (unsigned char) (PyByteArray_AS_STRING(string)[i]);
364 } else {
365 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
366 return -1;
367 }
368 } else {
369 return (unsigned char) (PyByteArray_AS_STRING(string)[i]);
370 }
371 }
372
373
374 //////////////////// SetItemIntByteArray.proto ////////////////////
375
376 #define __Pyx_SetItemInt_ByteArray(o, i, v, type, is_signed, to_py_func, is_list, wraparound, boundscheck) \
377 (__Pyx_fits_Py_ssize_t(i, type, is_signed) ? \
378 __Pyx_SetItemInt_ByteArray_Fast(o, (Py_ssize_t)i, v, wraparound, boundscheck) : \
379 (PyErr_SetString(PyExc_IndexError, "bytearray index out of range"), -1))
380
381 static CYTHON_INLINE int __Pyx_SetItemInt_ByteArray_Fast(PyObject* string, Py_ssize_t i, unsigned char v,
382 int wraparound, int boundscheck);
383
384 //////////////////// SetItemIntByteArray ////////////////////
385
__Pyx_SetItemInt_ByteArray_Fast(PyObject * string,Py_ssize_t i,unsigned char v,int wraparound,int boundscheck)386 static CYTHON_INLINE int __Pyx_SetItemInt_ByteArray_Fast(PyObject* string, Py_ssize_t i, unsigned char v,
387 int wraparound, int boundscheck) {
388 Py_ssize_t length;
389 if (wraparound | boundscheck) {
390 length = PyByteArray_GET_SIZE(string);
391 if (wraparound & unlikely(i < 0)) i += length;
392 if ((!boundscheck) || likely(__Pyx_is_valid_index(i, length))) {
393 PyByteArray_AS_STRING(string)[i] = (char) v;
394 return 0;
395 } else {
396 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
397 return -1;
398 }
399 } else {
400 PyByteArray_AS_STRING(string)[i] = (char) v;
401 return 0;
402 }
403 }
404
405
406 //////////////////// GetItemIntUnicode.proto ////////////////////
407
408 #define __Pyx_GetItemInt_Unicode(o, i, type, is_signed, to_py_func, is_list, wraparound, boundscheck) \
409 (__Pyx_fits_Py_ssize_t(i, type, is_signed) ? \
410 __Pyx_GetItemInt_Unicode_Fast(o, (Py_ssize_t)i, wraparound, boundscheck) : \
411 (PyErr_SetString(PyExc_IndexError, "string index out of range"), (Py_UCS4)-1))
412
413 static CYTHON_INLINE Py_UCS4 __Pyx_GetItemInt_Unicode_Fast(PyObject* ustring, Py_ssize_t i,
414 int wraparound, int boundscheck);
415
416 //////////////////// GetItemIntUnicode ////////////////////
417
__Pyx_GetItemInt_Unicode_Fast(PyObject * ustring,Py_ssize_t i,int wraparound,int boundscheck)418 static CYTHON_INLINE Py_UCS4 __Pyx_GetItemInt_Unicode_Fast(PyObject* ustring, Py_ssize_t i,
419 int wraparound, int boundscheck) {
420 Py_ssize_t length;
421 if (unlikely(__Pyx_PyUnicode_READY(ustring) < 0)) return (Py_UCS4)-1;
422 if (wraparound | boundscheck) {
423 length = __Pyx_PyUnicode_GET_LENGTH(ustring);
424 if (wraparound & unlikely(i < 0)) i += length;
425 if ((!boundscheck) || likely(__Pyx_is_valid_index(i, length))) {
426 return __Pyx_PyUnicode_READ_CHAR(ustring, i);
427 } else {
428 PyErr_SetString(PyExc_IndexError, "string index out of range");
429 return (Py_UCS4)-1;
430 }
431 } else {
432 return __Pyx_PyUnicode_READ_CHAR(ustring, i);
433 }
434 }
435
436
437 /////////////// decode_c_string_utf16.proto ///////////////
438
__Pyx_PyUnicode_DecodeUTF16(const char * s,Py_ssize_t size,const char * errors)439 static CYTHON_INLINE PyObject *__Pyx_PyUnicode_DecodeUTF16(const char *s, Py_ssize_t size, const char *errors) {
440 int byteorder = 0;
441 return PyUnicode_DecodeUTF16(s, size, errors, &byteorder);
442 }
__Pyx_PyUnicode_DecodeUTF16LE(const char * s,Py_ssize_t size,const char * errors)443 static CYTHON_INLINE PyObject *__Pyx_PyUnicode_DecodeUTF16LE(const char *s, Py_ssize_t size, const char *errors) {
444 int byteorder = -1;
445 return PyUnicode_DecodeUTF16(s, size, errors, &byteorder);
446 }
__Pyx_PyUnicode_DecodeUTF16BE(const char * s,Py_ssize_t size,const char * errors)447 static CYTHON_INLINE PyObject *__Pyx_PyUnicode_DecodeUTF16BE(const char *s, Py_ssize_t size, const char *errors) {
448 int byteorder = 1;
449 return PyUnicode_DecodeUTF16(s, size, errors, &byteorder);
450 }
451
452 /////////////// decode_cpp_string.proto ///////////////
453 //@requires: IncludeCppStringH
454 //@requires: decode_c_bytes
455
__Pyx_decode_cpp_string(std::string cppstring,Py_ssize_t start,Py_ssize_t stop,const char * encoding,const char * errors,PyObject * (* decode_func)(const char * s,Py_ssize_t size,const char * errors))456 static CYTHON_INLINE PyObject* __Pyx_decode_cpp_string(
457 std::string cppstring, Py_ssize_t start, Py_ssize_t stop,
458 const char* encoding, const char* errors,
459 PyObject* (*decode_func)(const char *s, Py_ssize_t size, const char *errors)) {
460 return __Pyx_decode_c_bytes(
461 cppstring.data(), cppstring.size(), start, stop, encoding, errors, decode_func);
462 }
463
464 /////////////// decode_c_string.proto ///////////////
465
466 static CYTHON_INLINE PyObject* __Pyx_decode_c_string(
467 const char* cstring, Py_ssize_t start, Py_ssize_t stop,
468 const char* encoding, const char* errors,
469 PyObject* (*decode_func)(const char *s, Py_ssize_t size, const char *errors));
470
471 /////////////// decode_c_string ///////////////
472 //@requires: IncludeStringH
473 //@requires: decode_c_string_utf16
474 //@substitute: naming
475
476 /* duplicate code to avoid calling strlen() if start >= 0 and stop >= 0 */
__Pyx_decode_c_string(const char * cstring,Py_ssize_t start,Py_ssize_t stop,const char * encoding,const char * errors,PyObject * (* decode_func)(const char * s,Py_ssize_t size,const char * errors))477 static CYTHON_INLINE PyObject* __Pyx_decode_c_string(
478 const char* cstring, Py_ssize_t start, Py_ssize_t stop,
479 const char* encoding, const char* errors,
480 PyObject* (*decode_func)(const char *s, Py_ssize_t size, const char *errors)) {
481 Py_ssize_t length;
482 if (unlikely((start < 0) | (stop < 0))) {
483 size_t slen = strlen(cstring);
484 if (unlikely(slen > (size_t) PY_SSIZE_T_MAX)) {
485 PyErr_SetString(PyExc_OverflowError,
486 "c-string too long to convert to Python");
487 return NULL;
488 }
489 length = (Py_ssize_t) slen;
490 if (start < 0) {
491 start += length;
492 if (start < 0)
493 start = 0;
494 }
495 if (stop < 0)
496 stop += length;
497 }
498 if (unlikely(stop <= start))
499 return __Pyx_NewRef($empty_unicode);
500 length = stop - start;
501 cstring += start;
502 if (decode_func) {
503 return decode_func(cstring, length, errors);
504 } else {
505 return PyUnicode_Decode(cstring, length, encoding, errors);
506 }
507 }
508
509 /////////////// decode_c_bytes.proto ///////////////
510
511 static CYTHON_INLINE PyObject* __Pyx_decode_c_bytes(
512 const char* cstring, Py_ssize_t length, Py_ssize_t start, Py_ssize_t stop,
513 const char* encoding, const char* errors,
514 PyObject* (*decode_func)(const char *s, Py_ssize_t size, const char *errors));
515
516 /////////////// decode_c_bytes ///////////////
517 //@requires: decode_c_string_utf16
518 //@substitute: naming
519
__Pyx_decode_c_bytes(const char * cstring,Py_ssize_t length,Py_ssize_t start,Py_ssize_t stop,const char * encoding,const char * errors,PyObject * (* decode_func)(const char * s,Py_ssize_t size,const char * errors))520 static CYTHON_INLINE PyObject* __Pyx_decode_c_bytes(
521 const char* cstring, Py_ssize_t length, Py_ssize_t start, Py_ssize_t stop,
522 const char* encoding, const char* errors,
523 PyObject* (*decode_func)(const char *s, Py_ssize_t size, const char *errors)) {
524 if (unlikely((start < 0) | (stop < 0))) {
525 if (start < 0) {
526 start += length;
527 if (start < 0)
528 start = 0;
529 }
530 if (stop < 0)
531 stop += length;
532 }
533 if (stop > length)
534 stop = length;
535 if (unlikely(stop <= start))
536 return __Pyx_NewRef($empty_unicode);
537 length = stop - start;
538 cstring += start;
539 if (decode_func) {
540 return decode_func(cstring, length, errors);
541 } else {
542 return PyUnicode_Decode(cstring, length, encoding, errors);
543 }
544 }
545
546 /////////////// decode_bytes.proto ///////////////
547 //@requires: decode_c_bytes
548
__Pyx_decode_bytes(PyObject * string,Py_ssize_t start,Py_ssize_t stop,const char * encoding,const char * errors,PyObject * (* decode_func)(const char * s,Py_ssize_t size,const char * errors))549 static CYTHON_INLINE PyObject* __Pyx_decode_bytes(
550 PyObject* string, Py_ssize_t start, Py_ssize_t stop,
551 const char* encoding, const char* errors,
552 PyObject* (*decode_func)(const char *s, Py_ssize_t size, const char *errors)) {
553 return __Pyx_decode_c_bytes(
554 PyBytes_AS_STRING(string), PyBytes_GET_SIZE(string),
555 start, stop, encoding, errors, decode_func);
556 }
557
558 /////////////// decode_bytearray.proto ///////////////
559 //@requires: decode_c_bytes
560
__Pyx_decode_bytearray(PyObject * string,Py_ssize_t start,Py_ssize_t stop,const char * encoding,const char * errors,PyObject * (* decode_func)(const char * s,Py_ssize_t size,const char * errors))561 static CYTHON_INLINE PyObject* __Pyx_decode_bytearray(
562 PyObject* string, Py_ssize_t start, Py_ssize_t stop,
563 const char* encoding, const char* errors,
564 PyObject* (*decode_func)(const char *s, Py_ssize_t size, const char *errors)) {
565 return __Pyx_decode_c_bytes(
566 PyByteArray_AS_STRING(string), PyByteArray_GET_SIZE(string),
567 start, stop, encoding, errors, decode_func);
568 }
569
570 /////////////// PyUnicode_Substring.proto ///////////////
571
572 static CYTHON_INLINE PyObject* __Pyx_PyUnicode_Substring(
573 PyObject* text, Py_ssize_t start, Py_ssize_t stop);
574
575 /////////////// PyUnicode_Substring ///////////////
576 //@substitute: naming
577
__Pyx_PyUnicode_Substring(PyObject * text,Py_ssize_t start,Py_ssize_t stop)578 static CYTHON_INLINE PyObject* __Pyx_PyUnicode_Substring(
579 PyObject* text, Py_ssize_t start, Py_ssize_t stop) {
580 Py_ssize_t length;
581 if (unlikely(__Pyx_PyUnicode_READY(text) == -1)) return NULL;
582 length = __Pyx_PyUnicode_GET_LENGTH(text);
583 if (start < 0) {
584 start += length;
585 if (start < 0)
586 start = 0;
587 }
588 if (stop < 0)
589 stop += length;
590 else if (stop > length)
591 stop = length;
592 if (stop <= start)
593 return __Pyx_NewRef($empty_unicode);
594 #if CYTHON_PEP393_ENABLED
595 return PyUnicode_FromKindAndData(PyUnicode_KIND(text),
596 PyUnicode_1BYTE_DATA(text) + start*PyUnicode_KIND(text), stop-start);
597 #else
598 return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(text)+start, stop-start);
599 #endif
600 }
601
602
603 /////////////// py_unicode_istitle.proto ///////////////
604
605 // Py_UNICODE_ISTITLE() doesn't match unicode.istitle() as the latter
606 // additionally allows character that comply with Py_UNICODE_ISUPPER()
607
608 #if PY_VERSION_HEX < 0x030200A2
__Pyx_Py_UNICODE_ISTITLE(Py_UNICODE uchar)609 static CYTHON_INLINE int __Pyx_Py_UNICODE_ISTITLE(Py_UNICODE uchar)
610 #else
611 static CYTHON_INLINE int __Pyx_Py_UNICODE_ISTITLE(Py_UCS4 uchar)
612 #endif
613 {
614 return Py_UNICODE_ISTITLE(uchar) || Py_UNICODE_ISUPPER(uchar);
615 }
616
617
618 /////////////// unicode_tailmatch.proto ///////////////
619
620 static int __Pyx_PyUnicode_Tailmatch(
621 PyObject* s, PyObject* substr, Py_ssize_t start, Py_ssize_t end, int direction); /*proto*/
622
623 /////////////// unicode_tailmatch ///////////////
624
625 // Python's unicode.startswith() and unicode.endswith() support a
626 // tuple of prefixes/suffixes, whereas it's much more common to
627 // test for a single unicode string.
628
__Pyx_PyUnicode_TailmatchTuple(PyObject * s,PyObject * substrings,Py_ssize_t start,Py_ssize_t end,int direction)629 static int __Pyx_PyUnicode_TailmatchTuple(PyObject* s, PyObject* substrings,
630 Py_ssize_t start, Py_ssize_t end, int direction) {
631 Py_ssize_t i, count = PyTuple_GET_SIZE(substrings);
632 for (i = 0; i < count; i++) {
633 Py_ssize_t result;
634 #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
635 result = PyUnicode_Tailmatch(s, PyTuple_GET_ITEM(substrings, i),
636 start, end, direction);
637 #else
638 PyObject* sub = PySequence_ITEM(substrings, i);
639 if (unlikely(!sub)) return -1;
640 result = PyUnicode_Tailmatch(s, sub, start, end, direction);
641 Py_DECREF(sub);
642 #endif
643 if (result) {
644 return (int) result;
645 }
646 }
647 return 0;
648 }
649
__Pyx_PyUnicode_Tailmatch(PyObject * s,PyObject * substr,Py_ssize_t start,Py_ssize_t end,int direction)650 static int __Pyx_PyUnicode_Tailmatch(PyObject* s, PyObject* substr,
651 Py_ssize_t start, Py_ssize_t end, int direction) {
652 if (unlikely(PyTuple_Check(substr))) {
653 return __Pyx_PyUnicode_TailmatchTuple(s, substr, start, end, direction);
654 }
655 return (int) PyUnicode_Tailmatch(s, substr, start, end, direction);
656 }
657
658
659 /////////////// bytes_tailmatch.proto ///////////////
660
661 static int __Pyx_PyBytes_SingleTailmatch(PyObject* self, PyObject* arg,
662 Py_ssize_t start, Py_ssize_t end, int direction); /*proto*/
663 static int __Pyx_PyBytes_Tailmatch(PyObject* self, PyObject* substr,
664 Py_ssize_t start, Py_ssize_t end, int direction); /*proto*/
665
666 /////////////// bytes_tailmatch ///////////////
667
__Pyx_PyBytes_SingleTailmatch(PyObject * self,PyObject * arg,Py_ssize_t start,Py_ssize_t end,int direction)668 static int __Pyx_PyBytes_SingleTailmatch(PyObject* self, PyObject* arg,
669 Py_ssize_t start, Py_ssize_t end, int direction) {
670 const char* self_ptr = PyBytes_AS_STRING(self);
671 Py_ssize_t self_len = PyBytes_GET_SIZE(self);
672 const char* sub_ptr;
673 Py_ssize_t sub_len;
674 int retval;
675
676 Py_buffer view;
677 view.obj = NULL;
678
679 if ( PyBytes_Check(arg) ) {
680 sub_ptr = PyBytes_AS_STRING(arg);
681 sub_len = PyBytes_GET_SIZE(arg);
682 }
683 #if PY_MAJOR_VERSION < 3
684 // Python 2.x allows mixing unicode and str
685 else if ( PyUnicode_Check(arg) ) {
686 return (int) PyUnicode_Tailmatch(self, arg, start, end, direction);
687 }
688 #endif
689 else {
690 if (unlikely(PyObject_GetBuffer(self, &view, PyBUF_SIMPLE) == -1))
691 return -1;
692 sub_ptr = (const char*) view.buf;
693 sub_len = view.len;
694 }
695
696 if (end > self_len)
697 end = self_len;
698 else if (end < 0)
699 end += self_len;
700 if (end < 0)
701 end = 0;
702 if (start < 0)
703 start += self_len;
704 if (start < 0)
705 start = 0;
706
707 if (direction > 0) {
708 /* endswith */
709 if (end-sub_len > start)
710 start = end - sub_len;
711 }
712
713 if (start + sub_len <= end)
714 retval = !memcmp(self_ptr+start, sub_ptr, (size_t)sub_len);
715 else
716 retval = 0;
717
718 if (view.obj)
719 PyBuffer_Release(&view);
720
721 return retval;
722 }
723
__Pyx_PyBytes_TailmatchTuple(PyObject * self,PyObject * substrings,Py_ssize_t start,Py_ssize_t end,int direction)724 static int __Pyx_PyBytes_TailmatchTuple(PyObject* self, PyObject* substrings,
725 Py_ssize_t start, Py_ssize_t end, int direction) {
726 Py_ssize_t i, count = PyTuple_GET_SIZE(substrings);
727 for (i = 0; i < count; i++) {
728 int result;
729 #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
730 result = __Pyx_PyBytes_SingleTailmatch(self, PyTuple_GET_ITEM(substrings, i),
731 start, end, direction);
732 #else
733 PyObject* sub = PySequence_ITEM(substrings, i);
734 if (unlikely(!sub)) return -1;
735 result = __Pyx_PyBytes_SingleTailmatch(self, sub, start, end, direction);
736 Py_DECREF(sub);
737 #endif
738 if (result) {
739 return result;
740 }
741 }
742 return 0;
743 }
744
__Pyx_PyBytes_Tailmatch(PyObject * self,PyObject * substr,Py_ssize_t start,Py_ssize_t end,int direction)745 static int __Pyx_PyBytes_Tailmatch(PyObject* self, PyObject* substr,
746 Py_ssize_t start, Py_ssize_t end, int direction) {
747 if (unlikely(PyTuple_Check(substr))) {
748 return __Pyx_PyBytes_TailmatchTuple(self, substr, start, end, direction);
749 }
750
751 return __Pyx_PyBytes_SingleTailmatch(self, substr, start, end, direction);
752 }
753
754
755 /////////////// str_tailmatch.proto ///////////////
756
757 static CYTHON_INLINE int __Pyx_PyStr_Tailmatch(PyObject* self, PyObject* arg, Py_ssize_t start,
758 Py_ssize_t end, int direction); /*proto*/
759
760 /////////////// str_tailmatch ///////////////
761 //@requires: bytes_tailmatch
762 //@requires: unicode_tailmatch
763
__Pyx_PyStr_Tailmatch(PyObject * self,PyObject * arg,Py_ssize_t start,Py_ssize_t end,int direction)764 static CYTHON_INLINE int __Pyx_PyStr_Tailmatch(PyObject* self, PyObject* arg, Py_ssize_t start,
765 Py_ssize_t end, int direction)
766 {
767 // We do not use a C compiler macro here to avoid "unused function"
768 // warnings for the *_Tailmatch() function that is not being used in
769 // the specific CPython version. The C compiler will generate the same
770 // code anyway, and will usually just remove the unused function.
771 if (PY_MAJOR_VERSION < 3)
772 return __Pyx_PyBytes_Tailmatch(self, arg, start, end, direction);
773 else
774 return __Pyx_PyUnicode_Tailmatch(self, arg, start, end, direction);
775 }
776
777
778 /////////////// bytes_index.proto ///////////////
779
780 static CYTHON_INLINE char __Pyx_PyBytes_GetItemInt(PyObject* bytes, Py_ssize_t index, int check_bounds); /*proto*/
781
782 /////////////// bytes_index ///////////////
783
__Pyx_PyBytes_GetItemInt(PyObject * bytes,Py_ssize_t index,int check_bounds)784 static CYTHON_INLINE char __Pyx_PyBytes_GetItemInt(PyObject* bytes, Py_ssize_t index, int check_bounds) {
785 if (index < 0)
786 index += PyBytes_GET_SIZE(bytes);
787 if (check_bounds) {
788 Py_ssize_t size = PyBytes_GET_SIZE(bytes);
789 if (unlikely(!__Pyx_is_valid_index(index, size))) {
790 PyErr_SetString(PyExc_IndexError, "string index out of range");
791 return (char) -1;
792 }
793 }
794 return PyBytes_AS_STRING(bytes)[index];
795 }
796
797
798 //////////////////// StringJoin.proto ////////////////////
799
800 #if PY_MAJOR_VERSION < 3
801 #define __Pyx_PyString_Join __Pyx_PyBytes_Join
802 #define __Pyx_PyBaseString_Join(s, v) (PyUnicode_CheckExact(s) ? PyUnicode_Join(s, v) : __Pyx_PyBytes_Join(s, v))
803 #else
804 #define __Pyx_PyString_Join PyUnicode_Join
805 #define __Pyx_PyBaseString_Join PyUnicode_Join
806 #endif
807
808 #if CYTHON_COMPILING_IN_CPYTHON
809 #if PY_MAJOR_VERSION < 3
810 #define __Pyx_PyBytes_Join _PyString_Join
811 #else
812 #define __Pyx_PyBytes_Join _PyBytes_Join
813 #endif
814 #else
815 static CYTHON_INLINE PyObject* __Pyx_PyBytes_Join(PyObject* sep, PyObject* values); /*proto*/
816 #endif
817
818
819 //////////////////// StringJoin ////////////////////
820
821 #if !CYTHON_COMPILING_IN_CPYTHON
__Pyx_PyBytes_Join(PyObject * sep,PyObject * values)822 static CYTHON_INLINE PyObject* __Pyx_PyBytes_Join(PyObject* sep, PyObject* values) {
823 return PyObject_CallMethodObjArgs(sep, PYIDENT("join"), values, NULL);
824 }
825 #endif
826
827
828 /////////////// JoinPyUnicode.proto ///////////////
829
830 static PyObject* __Pyx_PyUnicode_Join(PyObject* value_tuple, Py_ssize_t value_count, Py_ssize_t result_ulength,
831 Py_UCS4 max_char);
832
833 /////////////// JoinPyUnicode ///////////////
834 //@requires: IncludeStringH
835 //@substitute: naming
836
__Pyx_PyUnicode_Join(PyObject * value_tuple,Py_ssize_t value_count,Py_ssize_t result_ulength,CYTHON_UNUSED Py_UCS4 max_char)837 static PyObject* __Pyx_PyUnicode_Join(PyObject* value_tuple, Py_ssize_t value_count, Py_ssize_t result_ulength,
838 CYTHON_UNUSED Py_UCS4 max_char) {
839 #if CYTHON_USE_UNICODE_INTERNALS && CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS
840 PyObject *result_uval;
841 int result_ukind;
842 Py_ssize_t i, char_pos;
843 void *result_udata;
844 #if CYTHON_PEP393_ENABLED
845 // Py 3.3+ (post PEP-393)
846 result_uval = PyUnicode_New(result_ulength, max_char);
847 if (unlikely(!result_uval)) return NULL;
848 result_ukind = (max_char <= 255) ? PyUnicode_1BYTE_KIND : (max_char <= 65535) ? PyUnicode_2BYTE_KIND : PyUnicode_4BYTE_KIND;
849 result_udata = PyUnicode_DATA(result_uval);
850 #else
851 // Py 2.x/3.2 (pre PEP-393)
852 result_uval = PyUnicode_FromUnicode(NULL, result_ulength);
853 if (unlikely(!result_uval)) return NULL;
854 result_ukind = sizeof(Py_UNICODE);
855 result_udata = PyUnicode_AS_UNICODE(result_uval);
856 #endif
857
858 char_pos = 0;
859 for (i=0; i < value_count; i++) {
860 int ukind;
861 Py_ssize_t ulength;
862 void *udata;
863 PyObject *uval = PyTuple_GET_ITEM(value_tuple, i);
864 if (unlikely(__Pyx_PyUnicode_READY(uval)))
865 goto bad;
866 ulength = __Pyx_PyUnicode_GET_LENGTH(uval);
867 if (unlikely(!ulength))
868 continue;
869 if (unlikely(char_pos + ulength < 0))
870 goto overflow;
871 ukind = __Pyx_PyUnicode_KIND(uval);
872 udata = __Pyx_PyUnicode_DATA(uval);
873 if (!CYTHON_PEP393_ENABLED || ukind == result_ukind) {
874 memcpy((char *)result_udata + char_pos * result_ukind, udata, (size_t) (ulength * result_ukind));
875 } else {
876 #if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX >= 0x030300F0 || defined(_PyUnicode_FastCopyCharacters)
877 _PyUnicode_FastCopyCharacters(result_uval, char_pos, uval, 0, ulength);
878 #else
879 Py_ssize_t j;
880 for (j=0; j < ulength; j++) {
881 Py_UCS4 uchar = __Pyx_PyUnicode_READ(ukind, udata, j);
882 __Pyx_PyUnicode_WRITE(result_ukind, result_udata, char_pos+j, uchar);
883 }
884 #endif
885 }
886 char_pos += ulength;
887 }
888 return result_uval;
889 overflow:
890 PyErr_SetString(PyExc_OverflowError, "join() result is too long for a Python string");
891 bad:
892 Py_DECREF(result_uval);
893 return NULL;
894 #else
895 // non-CPython fallback
896 result_ulength++;
897 value_count++;
898 return PyUnicode_Join($empty_unicode, value_tuple);
899 #endif
900 }
901
902
903 /////////////// BuildPyUnicode.proto ///////////////
904
905 static PyObject* __Pyx_PyUnicode_BuildFromAscii(Py_ssize_t ulength, char* chars, int clength,
906 int prepend_sign, char padding_char);
907
908 /////////////// BuildPyUnicode ///////////////
909
910 // Create a PyUnicode object from an ASCII char*, e.g. a formatted number.
911
__Pyx_PyUnicode_BuildFromAscii(Py_ssize_t ulength,char * chars,int clength,int prepend_sign,char padding_char)912 static PyObject* __Pyx_PyUnicode_BuildFromAscii(Py_ssize_t ulength, char* chars, int clength,
913 int prepend_sign, char padding_char) {
914 PyObject *uval;
915 Py_ssize_t uoffset = ulength - clength;
916 #if CYTHON_USE_UNICODE_INTERNALS
917 Py_ssize_t i;
918 #if CYTHON_PEP393_ENABLED
919 // Py 3.3+ (post PEP-393)
920 void *udata;
921 uval = PyUnicode_New(ulength, 127);
922 if (unlikely(!uval)) return NULL;
923 udata = PyUnicode_DATA(uval);
924 #else
925 // Py 2.x/3.2 (pre PEP-393)
926 Py_UNICODE *udata;
927 uval = PyUnicode_FromUnicode(NULL, ulength);
928 if (unlikely(!uval)) return NULL;
929 udata = PyUnicode_AS_UNICODE(uval);
930 #endif
931 if (uoffset > 0) {
932 i = 0;
933 if (prepend_sign) {
934 __Pyx_PyUnicode_WRITE(PyUnicode_1BYTE_KIND, udata, 0, '-');
935 i++;
936 }
937 for (; i < uoffset; i++) {
938 __Pyx_PyUnicode_WRITE(PyUnicode_1BYTE_KIND, udata, i, padding_char);
939 }
940 }
941 for (i=0; i < clength; i++) {
942 __Pyx_PyUnicode_WRITE(PyUnicode_1BYTE_KIND, udata, uoffset+i, chars[i]);
943 }
944
945 #else
946 // non-CPython
947 {
948 PyObject *sign = NULL, *padding = NULL;
949 uval = NULL;
950 if (uoffset > 0) {
951 prepend_sign = !!prepend_sign;
952 if (uoffset > prepend_sign) {
953 padding = PyUnicode_FromOrdinal(padding_char);
954 if (likely(padding) && uoffset > prepend_sign + 1) {
955 PyObject *tmp;
956 PyObject *repeat = PyInt_FromSize_t(uoffset - prepend_sign);
957 if (unlikely(!repeat)) goto done_or_error;
958 tmp = PyNumber_Multiply(padding, repeat);
959 Py_DECREF(repeat);
960 Py_DECREF(padding);
961 padding = tmp;
962 }
963 if (unlikely(!padding)) goto done_or_error;
964 }
965 if (prepend_sign) {
966 sign = PyUnicode_FromOrdinal('-');
967 if (unlikely(!sign)) goto done_or_error;
968 }
969 }
970
971 uval = PyUnicode_DecodeASCII(chars, clength, NULL);
972 if (likely(uval) && padding) {
973 PyObject *tmp = PyNumber_Add(padding, uval);
974 Py_DECREF(uval);
975 uval = tmp;
976 }
977 if (likely(uval) && sign) {
978 PyObject *tmp = PyNumber_Add(sign, uval);
979 Py_DECREF(uval);
980 uval = tmp;
981 }
982 done_or_error:
983 Py_XDECREF(padding);
984 Py_XDECREF(sign);
985 }
986 #endif
987
988 return uval;
989 }
990
991
992 //////////////////// ByteArrayAppendObject.proto ////////////////////
993
994 static CYTHON_INLINE int __Pyx_PyByteArray_AppendObject(PyObject* bytearray, PyObject* value);
995
996 //////////////////// ByteArrayAppendObject ////////////////////
997 //@requires: ByteArrayAppend
998
__Pyx_PyByteArray_AppendObject(PyObject * bytearray,PyObject * value)999 static CYTHON_INLINE int __Pyx_PyByteArray_AppendObject(PyObject* bytearray, PyObject* value) {
1000 Py_ssize_t ival;
1001 #if PY_MAJOR_VERSION < 3
1002 if (unlikely(PyString_Check(value))) {
1003 if (unlikely(PyString_GET_SIZE(value) != 1)) {
1004 PyErr_SetString(PyExc_ValueError, "string must be of size 1");
1005 return -1;
1006 }
1007 ival = (unsigned char) (PyString_AS_STRING(value)[0]);
1008 } else
1009 #endif
1010 #if CYTHON_USE_PYLONG_INTERNALS
1011 if (likely(PyLong_CheckExact(value)) && likely(Py_SIZE(value) == 1 || Py_SIZE(value) == 0)) {
1012 if (Py_SIZE(value) == 0) {
1013 ival = 0;
1014 } else {
1015 ival = ((PyLongObject*)value)->ob_digit[0];
1016 if (unlikely(ival > 255)) goto bad_range;
1017 }
1018 } else
1019 #endif
1020 {
1021 // CPython calls PyNumber_Index() internally
1022 ival = __Pyx_PyIndex_AsSsize_t(value);
1023 if (unlikely(!__Pyx_is_valid_index(ival, 256))) {
1024 if (ival == -1 && PyErr_Occurred())
1025 return -1;
1026 goto bad_range;
1027 }
1028 }
1029 return __Pyx_PyByteArray_Append(bytearray, ival);
1030 bad_range:
1031 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
1032 return -1;
1033 }
1034
1035 //////////////////// ByteArrayAppend.proto ////////////////////
1036
1037 static CYTHON_INLINE int __Pyx_PyByteArray_Append(PyObject* bytearray, int value);
1038
1039 //////////////////// ByteArrayAppend ////////////////////
1040 //@requires: ObjectHandling.c::PyObjectCallMethod1
1041
__Pyx_PyByteArray_Append(PyObject * bytearray,int value)1042 static CYTHON_INLINE int __Pyx_PyByteArray_Append(PyObject* bytearray, int value) {
1043 PyObject *pyval, *retval;
1044 #if CYTHON_COMPILING_IN_CPYTHON
1045 if (likely(__Pyx_is_valid_index(value, 256))) {
1046 Py_ssize_t n = Py_SIZE(bytearray);
1047 if (likely(n != PY_SSIZE_T_MAX)) {
1048 if (unlikely(PyByteArray_Resize(bytearray, n + 1) < 0))
1049 return -1;
1050 PyByteArray_AS_STRING(bytearray)[n] = value;
1051 return 0;
1052 }
1053 } else {
1054 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
1055 return -1;
1056 }
1057 #endif
1058 pyval = PyInt_FromLong(value);
1059 if (unlikely(!pyval))
1060 return -1;
1061 retval = __Pyx_PyObject_CallMethod1(bytearray, PYIDENT("append"), pyval);
1062 Py_DECREF(pyval);
1063 if (unlikely(!retval))
1064 return -1;
1065 Py_DECREF(retval);
1066 return 0;
1067 }
1068
1069
1070 //////////////////// PyObjectFormat.proto ////////////////////
1071
1072 #if CYTHON_USE_UNICODE_WRITER
1073 static PyObject* __Pyx_PyObject_Format(PyObject* s, PyObject* f);
1074 #else
1075 #define __Pyx_PyObject_Format(s, f) PyObject_Format(s, f)
1076 #endif
1077
1078 //////////////////// PyObjectFormat ////////////////////
1079
1080 #if CYTHON_USE_UNICODE_WRITER
__Pyx_PyObject_Format(PyObject * obj,PyObject * format_spec)1081 static PyObject* __Pyx_PyObject_Format(PyObject* obj, PyObject* format_spec) {
1082 int ret;
1083 _PyUnicodeWriter writer;
1084
1085 if (likely(PyFloat_CheckExact(obj))) {
1086 // copied from CPython 3.5 "float__format__()" in floatobject.c
1087 #if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX < 0x03040000
1088 _PyUnicodeWriter_Init(&writer, 0);
1089 #else
1090 _PyUnicodeWriter_Init(&writer);
1091 #endif
1092 ret = _PyFloat_FormatAdvancedWriter(
1093 &writer,
1094 obj,
1095 format_spec, 0, PyUnicode_GET_LENGTH(format_spec));
1096 } else if (likely(PyLong_CheckExact(obj))) {
1097 // copied from CPython 3.5 "long__format__()" in longobject.c
1098 #if CYTHON_COMPILING_IN_CPYTHON && PY_VERSION_HEX < 0x03040000
1099 _PyUnicodeWriter_Init(&writer, 0);
1100 #else
1101 _PyUnicodeWriter_Init(&writer);
1102 #endif
1103 ret = _PyLong_FormatAdvancedWriter(
1104 &writer,
1105 obj,
1106 format_spec, 0, PyUnicode_GET_LENGTH(format_spec));
1107 } else {
1108 return PyObject_Format(obj, format_spec);
1109 }
1110
1111 if (unlikely(ret == -1)) {
1112 _PyUnicodeWriter_Dealloc(&writer);
1113 return NULL;
1114 }
1115 return _PyUnicodeWriter_Finish(&writer);
1116 }
1117 #endif
1118
1119
1120 //////////////////// PyObjectFormatSimple.proto ////////////////////
1121
1122 #if CYTHON_COMPILING_IN_PYPY
1123 #define __Pyx_PyObject_FormatSimple(s, f) ( \
1124 likely(PyUnicode_CheckExact(s)) ? (Py_INCREF(s), s) : \
1125 PyObject_Format(s, f))
1126 #elif PY_MAJOR_VERSION < 3
1127 // str is common in Py2, but formatting must return a Unicode string
1128 #define __Pyx_PyObject_FormatSimple(s, f) ( \
1129 likely(PyUnicode_CheckExact(s)) ? (Py_INCREF(s), s) : \
1130 likely(PyString_CheckExact(s)) ? PyUnicode_FromEncodedObject(s, NULL, "strict") : \
1131 PyObject_Format(s, f))
1132 #elif CYTHON_USE_TYPE_SLOTS
1133 // Py3 nicely returns unicode strings from str() which makes this quite efficient for builtin types
1134 #define __Pyx_PyObject_FormatSimple(s, f) ( \
1135 likely(PyUnicode_CheckExact(s)) ? (Py_INCREF(s), s) : \
1136 likely(PyLong_CheckExact(s)) ? PyLong_Type.tp_str(s) : \
1137 likely(PyFloat_CheckExact(s)) ? PyFloat_Type.tp_str(s) : \
1138 PyObject_Format(s, f))
1139 #else
1140 #define __Pyx_PyObject_FormatSimple(s, f) ( \
1141 likely(PyUnicode_CheckExact(s)) ? (Py_INCREF(s), s) : \
1142 PyObject_Format(s, f))
1143 #endif
1144
1145
1146 //////////////////// PyObjectFormatAndDecref.proto ////////////////////
1147
1148 static CYTHON_INLINE PyObject* __Pyx_PyObject_FormatSimpleAndDecref(PyObject* s, PyObject* f);
1149 static CYTHON_INLINE PyObject* __Pyx_PyObject_FormatAndDecref(PyObject* s, PyObject* f);
1150
1151 //////////////////// PyObjectFormatAndDecref ////////////////////
1152
__Pyx_PyObject_FormatSimpleAndDecref(PyObject * s,PyObject * f)1153 static CYTHON_INLINE PyObject* __Pyx_PyObject_FormatSimpleAndDecref(PyObject* s, PyObject* f) {
1154 if (unlikely(!s)) return NULL;
1155 if (likely(PyUnicode_CheckExact(s))) return s;
1156 #if PY_MAJOR_VERSION < 3
1157 // str is common in Py2, but formatting must return a Unicode string
1158 if (likely(PyString_CheckExact(s))) {
1159 PyObject *result = PyUnicode_FromEncodedObject(s, NULL, "strict");
1160 Py_DECREF(s);
1161 return result;
1162 }
1163 #endif
1164 return __Pyx_PyObject_FormatAndDecref(s, f);
1165 }
1166
__Pyx_PyObject_FormatAndDecref(PyObject * s,PyObject * f)1167 static CYTHON_INLINE PyObject* __Pyx_PyObject_FormatAndDecref(PyObject* s, PyObject* f) {
1168 PyObject *result = PyObject_Format(s, f);
1169 Py_DECREF(s);
1170 return result;
1171 }
1172
1173
1174 //////////////////// PyUnicode_Unicode.proto ////////////////////
1175
1176 static CYTHON_INLINE PyObject* __Pyx_PyUnicode_Unicode(PyObject *obj);/*proto*/
1177
1178 //////////////////// PyUnicode_Unicode ////////////////////
1179
__Pyx_PyUnicode_Unicode(PyObject * obj)1180 static CYTHON_INLINE PyObject* __Pyx_PyUnicode_Unicode(PyObject *obj) {
1181 if (unlikely(obj == Py_None))
1182 obj = PYUNICODE("None");
1183 return __Pyx_NewRef(obj);
1184 }
1185
1186
1187 //////////////////// PyObject_Unicode.proto ////////////////////
1188
1189 #if PY_MAJOR_VERSION >= 3
1190 #define __Pyx_PyObject_Unicode(obj) \
1191 (likely(PyUnicode_CheckExact(obj)) ? __Pyx_NewRef(obj) : PyObject_Str(obj))
1192 #else
1193 #define __Pyx_PyObject_Unicode(obj) \
1194 (likely(PyUnicode_CheckExact(obj)) ? __Pyx_NewRef(obj) : PyObject_Unicode(obj))
1195 #endif
1196