StringTools.c 41 KB

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