StringTools.c 40 KB

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