unicodeobject.h 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  1. #ifndef Py_UNICODEOBJECT_H
  2. #define Py_UNICODEOBJECT_H
  3. #include <stdarg.h>
  4. /*
  5. Unicode implementation based on original code by Fredrik Lundh,
  6. modified by Marc-Andre Lemburg (mal@lemburg.com) according to the
  7. Unicode Integration Proposal. (See
  8. http://www.egenix.com/files/python/unicode-proposal.txt).
  9. Copyright (c) Corporation for National Research Initiatives.
  10. Original header:
  11. --------------------------------------------------------------------
  12. * Yet another Unicode string type for Python. This type supports the
  13. * 16-bit Basic Multilingual Plane (BMP) only.
  14. *
  15. * Written by Fredrik Lundh, January 1999.
  16. *
  17. * Copyright (c) 1999 by Secret Labs AB.
  18. * Copyright (c) 1999 by Fredrik Lundh.
  19. *
  20. * fredrik@pythonware.com
  21. * http://www.pythonware.com
  22. *
  23. * --------------------------------------------------------------------
  24. * This Unicode String Type is
  25. *
  26. * Copyright (c) 1999 by Secret Labs AB
  27. * Copyright (c) 1999 by Fredrik Lundh
  28. *
  29. * By obtaining, using, and/or copying this software and/or its
  30. * associated documentation, you agree that you have read, understood,
  31. * and will comply with the following terms and conditions:
  32. *
  33. * Permission to use, copy, modify, and distribute this software and its
  34. * associated documentation for any purpose and without fee is hereby
  35. * granted, provided that the above copyright notice appears in all
  36. * copies, and that both that copyright notice and this permission notice
  37. * appear in supporting documentation, and that the name of Secret Labs
  38. * AB or the author not be used in advertising or publicity pertaining to
  39. * distribution of the software without specific, written prior
  40. * permission.
  41. *
  42. * SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO
  43. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  44. * FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR
  45. * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  46. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  47. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  48. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  49. * -------------------------------------------------------------------- */
  50. #include <ctype.h>
  51. /* === Internal API ======================================================= */
  52. /* --- Internal Unicode Format -------------------------------------------- */
  53. /* Python 3.x requires unicode */
  54. #define Py_USING_UNICODE
  55. #ifndef SIZEOF_WCHAR_T
  56. #error Must define SIZEOF_WCHAR_T
  57. #endif
  58. #define Py_UNICODE_SIZE SIZEOF_WCHAR_T
  59. /* If wchar_t can be used for UCS-4 storage, set Py_UNICODE_WIDE.
  60. Otherwise, Unicode strings are stored as UCS-2 (with limited support
  61. for UTF-16) */
  62. #if Py_UNICODE_SIZE >= 4
  63. #define Py_UNICODE_WIDE
  64. #endif
  65. /* Set these flags if the platform has "wchar.h" and the
  66. wchar_t type is a 16-bit unsigned type */
  67. /* #define HAVE_WCHAR_H */
  68. /* #define HAVE_USABLE_WCHAR_T */
  69. /* If the compiler provides a wchar_t type we try to support it
  70. through the interface functions PyUnicode_FromWideChar(),
  71. PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(). */
  72. #ifdef HAVE_USABLE_WCHAR_T
  73. # ifndef HAVE_WCHAR_H
  74. # define HAVE_WCHAR_H
  75. # endif
  76. #endif
  77. #ifdef HAVE_WCHAR_H
  78. # include <wchar.h>
  79. #endif
  80. /* Py_UCS4 and Py_UCS2 are typedefs for the respective
  81. unicode representations. */
  82. typedef uint32_t Py_UCS4;
  83. typedef uint16_t Py_UCS2;
  84. typedef uint8_t Py_UCS1;
  85. #ifdef __cplusplus
  86. extern "C" {
  87. #endif
  88. PyAPI_DATA(PyTypeObject) PyUnicode_Type;
  89. PyAPI_DATA(PyTypeObject) PyUnicodeIter_Type;
  90. #define PyUnicode_Check(op) \
  91. PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_UNICODE_SUBCLASS)
  92. #define PyUnicode_CheckExact(op) Py_IS_TYPE(op, &PyUnicode_Type)
  93. /* --- Constants ---------------------------------------------------------- */
  94. /* This Unicode character will be used as replacement character during
  95. decoding if the errors argument is set to "replace". Note: the
  96. Unicode character U+FFFD is the official REPLACEMENT CHARACTER in
  97. Unicode 3.0. */
  98. #define Py_UNICODE_REPLACEMENT_CHARACTER ((Py_UCS4) 0xFFFD)
  99. /* === Public API ========================================================= */
  100. /* Similar to PyUnicode_FromUnicode(), but u points to UTF-8 encoded bytes */
  101. PyAPI_FUNC(PyObject*) PyUnicode_FromStringAndSize(
  102. const char *u, /* UTF-8 encoded string */
  103. Py_ssize_t size /* size of buffer */
  104. );
  105. /* Similar to PyUnicode_FromUnicode(), but u points to null-terminated
  106. UTF-8 encoded bytes. The size is determined with strlen(). */
  107. PyAPI_FUNC(PyObject*) PyUnicode_FromString(
  108. const char *u /* UTF-8 encoded string */
  109. );
  110. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
  111. PyAPI_FUNC(PyObject*) PyUnicode_Substring(
  112. PyObject *str,
  113. Py_ssize_t start,
  114. Py_ssize_t end);
  115. #endif
  116. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
  117. /* Copy the string into a UCS4 buffer including the null character if copy_null
  118. is set. Return NULL and raise an exception on error. Raise a SystemError if
  119. the buffer is smaller than the string. Return buffer on success.
  120. buflen is the length of the buffer in (Py_UCS4) characters. */
  121. PyAPI_FUNC(Py_UCS4*) PyUnicode_AsUCS4(
  122. PyObject *unicode,
  123. Py_UCS4* buffer,
  124. Py_ssize_t buflen,
  125. int copy_null);
  126. /* Copy the string into a UCS4 buffer. A new buffer is allocated using
  127. * PyMem_Malloc; if this fails, NULL is returned with a memory error
  128. exception set. */
  129. PyAPI_FUNC(Py_UCS4*) PyUnicode_AsUCS4Copy(PyObject *unicode);
  130. #endif
  131. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
  132. /* Get the length of the Unicode object. */
  133. PyAPI_FUNC(Py_ssize_t) PyUnicode_GetLength(
  134. PyObject *unicode
  135. );
  136. #endif
  137. /* Get the number of Py_UNICODE units in the
  138. string representation. */
  139. Py_DEPRECATED(3.3) PyAPI_FUNC(Py_ssize_t) PyUnicode_GetSize(
  140. PyObject *unicode /* Unicode object */
  141. );
  142. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
  143. /* Read a character from the string. */
  144. PyAPI_FUNC(Py_UCS4) PyUnicode_ReadChar(
  145. PyObject *unicode,
  146. Py_ssize_t index
  147. );
  148. /* Write a character to the string. The string must have been created through
  149. PyUnicode_New, must not be shared, and must not have been hashed yet.
  150. Return 0 on success, -1 on error. */
  151. PyAPI_FUNC(int) PyUnicode_WriteChar(
  152. PyObject *unicode,
  153. Py_ssize_t index,
  154. Py_UCS4 character
  155. );
  156. #endif
  157. /* Resize a Unicode object. The length is the number of characters, except
  158. if the kind of the string is PyUnicode_WCHAR_KIND: in this case, the length
  159. is the number of Py_UNICODE characters.
  160. *unicode is modified to point to the new (resized) object and 0
  161. returned on success.
  162. Try to resize the string in place (which is usually faster than allocating
  163. a new string and copy characters), or create a new string.
  164. Error handling is implemented as follows: an exception is set, -1
  165. is returned and *unicode left untouched.
  166. WARNING: The function doesn't check string content, the result may not be a
  167. string in canonical representation. */
  168. PyAPI_FUNC(int) PyUnicode_Resize(
  169. PyObject **unicode, /* Pointer to the Unicode object */
  170. Py_ssize_t length /* New length */
  171. );
  172. /* Decode obj to a Unicode object.
  173. bytes, bytearray and other bytes-like objects are decoded according to the
  174. given encoding and error handler. The encoding and error handler can be
  175. NULL to have the interface use UTF-8 and "strict".
  176. All other objects (including Unicode objects) raise an exception.
  177. The API returns NULL in case of an error. The caller is responsible
  178. for decref'ing the returned objects.
  179. */
  180. PyAPI_FUNC(PyObject*) PyUnicode_FromEncodedObject(
  181. PyObject *obj, /* Object */
  182. const char *encoding, /* encoding */
  183. const char *errors /* error handling */
  184. );
  185. /* Copy an instance of a Unicode subtype to a new true Unicode object if
  186. necessary. If obj is already a true Unicode object (not a subtype), return
  187. the reference with *incremented* refcount.
  188. The API returns NULL in case of an error. The caller is responsible
  189. for decref'ing the returned objects.
  190. */
  191. PyAPI_FUNC(PyObject*) PyUnicode_FromObject(
  192. PyObject *obj /* Object */
  193. );
  194. PyAPI_FUNC(PyObject *) PyUnicode_FromFormatV(
  195. const char *format, /* ASCII-encoded string */
  196. va_list vargs
  197. );
  198. PyAPI_FUNC(PyObject *) PyUnicode_FromFormat(
  199. const char *format, /* ASCII-encoded string */
  200. ...
  201. );
  202. PyAPI_FUNC(void) PyUnicode_InternInPlace(PyObject **);
  203. PyAPI_FUNC(void) PyUnicode_InternImmortal(PyObject **);
  204. PyAPI_FUNC(PyObject *) PyUnicode_InternFromString(
  205. const char *u /* UTF-8 encoded string */
  206. );
  207. /* Use only if you know it's a string */
  208. #define PyUnicode_CHECK_INTERNED(op) \
  209. (((PyASCIIObject *)(op))->state.interned)
  210. /* --- wchar_t support for platforms which support it --------------------- */
  211. #ifdef HAVE_WCHAR_H
  212. /* Create a Unicode Object from the wchar_t buffer w of the given
  213. size.
  214. The buffer is copied into the new object. */
  215. PyAPI_FUNC(PyObject*) PyUnicode_FromWideChar(
  216. const wchar_t *w, /* wchar_t buffer */
  217. Py_ssize_t size /* size of buffer */
  218. );
  219. /* Copies the Unicode Object contents into the wchar_t buffer w. At
  220. most size wchar_t characters are copied.
  221. Note that the resulting wchar_t string may or may not be
  222. 0-terminated. It is the responsibility of the caller to make sure
  223. that the wchar_t string is 0-terminated in case this is required by
  224. the application.
  225. Returns the number of wchar_t characters copied (excluding a
  226. possibly trailing 0-termination character) or -1 in case of an
  227. error. */
  228. PyAPI_FUNC(Py_ssize_t) PyUnicode_AsWideChar(
  229. PyObject *unicode, /* Unicode object */
  230. wchar_t *w, /* wchar_t buffer */
  231. Py_ssize_t size /* size of buffer */
  232. );
  233. /* Convert the Unicode object to a wide character string. The output string
  234. always ends with a nul character. If size is not NULL, write the number of
  235. wide characters (excluding the null character) into *size.
  236. Returns a buffer allocated by PyMem_Malloc() (use PyMem_Free() to free it)
  237. on success. On error, returns NULL, *size is undefined and raises a
  238. MemoryError. */
  239. PyAPI_FUNC(wchar_t*) PyUnicode_AsWideCharString(
  240. PyObject *unicode, /* Unicode object */
  241. Py_ssize_t *size /* number of characters of the result */
  242. );
  243. #endif
  244. /* --- Unicode ordinals --------------------------------------------------- */
  245. /* Create a Unicode Object from the given Unicode code point ordinal.
  246. The ordinal must be in range(0x110000). A ValueError is
  247. raised in case it is not.
  248. */
  249. PyAPI_FUNC(PyObject*) PyUnicode_FromOrdinal(int ordinal);
  250. /* === Builtin Codecs =====================================================
  251. Many of these APIs take two arguments encoding and errors. These
  252. parameters encoding and errors have the same semantics as the ones
  253. of the builtin str() API.
  254. Setting encoding to NULL causes the default encoding (UTF-8) to be used.
  255. Error handling is set by errors which may also be set to NULL
  256. meaning to use the default handling defined for the codec. Default
  257. error handling for all builtin codecs is "strict" (ValueErrors are
  258. raised).
  259. The codecs all use a similar interface. Only deviation from the
  260. generic ones are documented.
  261. */
  262. /* --- Manage the default encoding ---------------------------------------- */
  263. /* Returns "utf-8". */
  264. PyAPI_FUNC(const char*) PyUnicode_GetDefaultEncoding(void);
  265. /* --- Generic Codecs ----------------------------------------------------- */
  266. /* Create a Unicode object by decoding the encoded string s of the
  267. given size. */
  268. PyAPI_FUNC(PyObject*) PyUnicode_Decode(
  269. const char *s, /* encoded string */
  270. Py_ssize_t size, /* size of buffer */
  271. const char *encoding, /* encoding */
  272. const char *errors /* error handling */
  273. );
  274. /* Decode a Unicode object unicode and return the result as Python
  275. object.
  276. This API is DEPRECATED. The only supported standard encoding is rot13.
  277. Use PyCodec_Decode() to decode with rot13 and non-standard codecs
  278. that decode from str. */
  279. Py_DEPRECATED(3.6) PyAPI_FUNC(PyObject*) PyUnicode_AsDecodedObject(
  280. PyObject *unicode, /* Unicode object */
  281. const char *encoding, /* encoding */
  282. const char *errors /* error handling */
  283. );
  284. /* Decode a Unicode object unicode and return the result as Unicode
  285. object.
  286. This API is DEPRECATED. The only supported standard encoding is rot13.
  287. Use PyCodec_Decode() to decode with rot13 and non-standard codecs
  288. that decode from str to str. */
  289. Py_DEPRECATED(3.6) PyAPI_FUNC(PyObject*) PyUnicode_AsDecodedUnicode(
  290. PyObject *unicode, /* Unicode object */
  291. const char *encoding, /* encoding */
  292. const char *errors /* error handling */
  293. );
  294. /* Encodes a Unicode object and returns the result as Python
  295. object.
  296. This API is DEPRECATED. It is superseded by PyUnicode_AsEncodedString()
  297. since all standard encodings (except rot13) encode str to bytes.
  298. Use PyCodec_Encode() for encoding with rot13 and non-standard codecs
  299. that encode form str to non-bytes. */
  300. Py_DEPRECATED(3.6) PyAPI_FUNC(PyObject*) PyUnicode_AsEncodedObject(
  301. PyObject *unicode, /* Unicode object */
  302. const char *encoding, /* encoding */
  303. const char *errors /* error handling */
  304. );
  305. /* Encodes a Unicode object and returns the result as Python string
  306. object. */
  307. PyAPI_FUNC(PyObject*) PyUnicode_AsEncodedString(
  308. PyObject *unicode, /* Unicode object */
  309. const char *encoding, /* encoding */
  310. const char *errors /* error handling */
  311. );
  312. /* Encodes a Unicode object and returns the result as Unicode
  313. object.
  314. This API is DEPRECATED. The only supported standard encodings is rot13.
  315. Use PyCodec_Encode() to encode with rot13 and non-standard codecs
  316. that encode from str to str. */
  317. Py_DEPRECATED(3.6) PyAPI_FUNC(PyObject*) PyUnicode_AsEncodedUnicode(
  318. PyObject *unicode, /* Unicode object */
  319. const char *encoding, /* encoding */
  320. const char *errors /* error handling */
  321. );
  322. /* Build an encoding map. */
  323. PyAPI_FUNC(PyObject*) PyUnicode_BuildEncodingMap(
  324. PyObject* string /* 256 character map */
  325. );
  326. /* --- UTF-7 Codecs ------------------------------------------------------- */
  327. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF7(
  328. const char *string, /* UTF-7 encoded string */
  329. Py_ssize_t length, /* size of string */
  330. const char *errors /* error handling */
  331. );
  332. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF7Stateful(
  333. const char *string, /* UTF-7 encoded string */
  334. Py_ssize_t length, /* size of string */
  335. const char *errors, /* error handling */
  336. Py_ssize_t *consumed /* bytes consumed */
  337. );
  338. /* --- UTF-8 Codecs ------------------------------------------------------- */
  339. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF8(
  340. const char *string, /* UTF-8 encoded string */
  341. Py_ssize_t length, /* size of string */
  342. const char *errors /* error handling */
  343. );
  344. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF8Stateful(
  345. const char *string, /* UTF-8 encoded string */
  346. Py_ssize_t length, /* size of string */
  347. const char *errors, /* error handling */
  348. Py_ssize_t *consumed /* bytes consumed */
  349. );
  350. PyAPI_FUNC(PyObject*) PyUnicode_AsUTF8String(
  351. PyObject *unicode /* Unicode object */
  352. );
  353. /* --- UTF-32 Codecs ------------------------------------------------------ */
  354. /* Decodes length bytes from a UTF-32 encoded buffer string and returns
  355. the corresponding Unicode object.
  356. errors (if non-NULL) defines the error handling. It defaults
  357. to "strict".
  358. If byteorder is non-NULL, the decoder starts decoding using the
  359. given byte order:
  360. *byteorder == -1: little endian
  361. *byteorder == 0: native order
  362. *byteorder == 1: big endian
  363. In native mode, the first four bytes of the stream are checked for a
  364. BOM mark. If found, the BOM mark is analysed, the byte order
  365. adjusted and the BOM skipped. In the other modes, no BOM mark
  366. interpretation is done. After completion, *byteorder is set to the
  367. current byte order at the end of input data.
  368. If byteorder is NULL, the codec starts in native order mode.
  369. */
  370. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF32(
  371. const char *string, /* UTF-32 encoded string */
  372. Py_ssize_t length, /* size of string */
  373. const char *errors, /* error handling */
  374. int *byteorder /* pointer to byteorder to use
  375. 0=native;-1=LE,1=BE; updated on
  376. exit */
  377. );
  378. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF32Stateful(
  379. const char *string, /* UTF-32 encoded string */
  380. Py_ssize_t length, /* size of string */
  381. const char *errors, /* error handling */
  382. int *byteorder, /* pointer to byteorder to use
  383. 0=native;-1=LE,1=BE; updated on
  384. exit */
  385. Py_ssize_t *consumed /* bytes consumed */
  386. );
  387. /* Returns a Python string using the UTF-32 encoding in native byte
  388. order. The string always starts with a BOM mark. */
  389. PyAPI_FUNC(PyObject*) PyUnicode_AsUTF32String(
  390. PyObject *unicode /* Unicode object */
  391. );
  392. /* Returns a Python string object holding the UTF-32 encoded value of
  393. the Unicode data.
  394. If byteorder is not 0, output is written according to the following
  395. byte order:
  396. byteorder == -1: little endian
  397. byteorder == 0: native byte order (writes a BOM mark)
  398. byteorder == 1: big endian
  399. If byteorder is 0, the output string will always start with the
  400. Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark is
  401. prepended.
  402. */
  403. /* --- UTF-16 Codecs ------------------------------------------------------ */
  404. /* Decodes length bytes from a UTF-16 encoded buffer string and returns
  405. the corresponding Unicode object.
  406. errors (if non-NULL) defines the error handling. It defaults
  407. to "strict".
  408. If byteorder is non-NULL, the decoder starts decoding using the
  409. given byte order:
  410. *byteorder == -1: little endian
  411. *byteorder == 0: native order
  412. *byteorder == 1: big endian
  413. In native mode, the first two bytes of the stream are checked for a
  414. BOM mark. If found, the BOM mark is analysed, the byte order
  415. adjusted and the BOM skipped. In the other modes, no BOM mark
  416. interpretation is done. After completion, *byteorder is set to the
  417. current byte order at the end of input data.
  418. If byteorder is NULL, the codec starts in native order mode.
  419. */
  420. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF16(
  421. const char *string, /* UTF-16 encoded string */
  422. Py_ssize_t length, /* size of string */
  423. const char *errors, /* error handling */
  424. int *byteorder /* pointer to byteorder to use
  425. 0=native;-1=LE,1=BE; updated on
  426. exit */
  427. );
  428. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF16Stateful(
  429. const char *string, /* UTF-16 encoded string */
  430. Py_ssize_t length, /* size of string */
  431. const char *errors, /* error handling */
  432. int *byteorder, /* pointer to byteorder to use
  433. 0=native;-1=LE,1=BE; updated on
  434. exit */
  435. Py_ssize_t *consumed /* bytes consumed */
  436. );
  437. /* Returns a Python string using the UTF-16 encoding in native byte
  438. order. The string always starts with a BOM mark. */
  439. PyAPI_FUNC(PyObject*) PyUnicode_AsUTF16String(
  440. PyObject *unicode /* Unicode object */
  441. );
  442. /* --- Unicode-Escape Codecs ---------------------------------------------- */
  443. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUnicodeEscape(
  444. const char *string, /* Unicode-Escape encoded string */
  445. Py_ssize_t length, /* size of string */
  446. const char *errors /* error handling */
  447. );
  448. PyAPI_FUNC(PyObject*) PyUnicode_AsUnicodeEscapeString(
  449. PyObject *unicode /* Unicode object */
  450. );
  451. /* --- Raw-Unicode-Escape Codecs ------------------------------------------ */
  452. PyAPI_FUNC(PyObject*) PyUnicode_DecodeRawUnicodeEscape(
  453. const char *string, /* Raw-Unicode-Escape encoded string */
  454. Py_ssize_t length, /* size of string */
  455. const char *errors /* error handling */
  456. );
  457. PyAPI_FUNC(PyObject*) PyUnicode_AsRawUnicodeEscapeString(
  458. PyObject *unicode /* Unicode object */
  459. );
  460. /* --- Latin-1 Codecs -----------------------------------------------------
  461. Note: Latin-1 corresponds to the first 256 Unicode ordinals. */
  462. PyAPI_FUNC(PyObject*) PyUnicode_DecodeLatin1(
  463. const char *string, /* Latin-1 encoded string */
  464. Py_ssize_t length, /* size of string */
  465. const char *errors /* error handling */
  466. );
  467. PyAPI_FUNC(PyObject*) PyUnicode_AsLatin1String(
  468. PyObject *unicode /* Unicode object */
  469. );
  470. /* --- ASCII Codecs -------------------------------------------------------
  471. Only 7-bit ASCII data is excepted. All other codes generate errors.
  472. */
  473. PyAPI_FUNC(PyObject*) PyUnicode_DecodeASCII(
  474. const char *string, /* ASCII encoded string */
  475. Py_ssize_t length, /* size of string */
  476. const char *errors /* error handling */
  477. );
  478. PyAPI_FUNC(PyObject*) PyUnicode_AsASCIIString(
  479. PyObject *unicode /* Unicode object */
  480. );
  481. /* --- Character Map Codecs -----------------------------------------------
  482. This codec uses mappings to encode and decode characters.
  483. Decoding mappings must map byte ordinals (integers in the range from 0 to
  484. 255) to Unicode strings, integers (which are then interpreted as Unicode
  485. ordinals) or None. Unmapped data bytes (ones which cause a LookupError)
  486. as well as mapped to None, 0xFFFE or '\ufffe' are treated as "undefined
  487. mapping" and cause an error.
  488. Encoding mappings must map Unicode ordinal integers to bytes objects,
  489. integers in the range from 0 to 255 or None. Unmapped character
  490. ordinals (ones which cause a LookupError) as well as mapped to
  491. None are treated as "undefined mapping" and cause an error.
  492. */
  493. PyAPI_FUNC(PyObject*) PyUnicode_DecodeCharmap(
  494. const char *string, /* Encoded string */
  495. Py_ssize_t length, /* size of string */
  496. PyObject *mapping, /* decoding mapping */
  497. const char *errors /* error handling */
  498. );
  499. PyAPI_FUNC(PyObject*) PyUnicode_AsCharmapString(
  500. PyObject *unicode, /* Unicode object */
  501. PyObject *mapping /* encoding mapping */
  502. );
  503. /* --- MBCS codecs for Windows -------------------------------------------- */
  504. #ifdef MS_WINDOWS
  505. PyAPI_FUNC(PyObject*) PyUnicode_DecodeMBCS(
  506. const char *string, /* MBCS encoded string */
  507. Py_ssize_t length, /* size of string */
  508. const char *errors /* error handling */
  509. );
  510. PyAPI_FUNC(PyObject*) PyUnicode_DecodeMBCSStateful(
  511. const char *string, /* MBCS encoded string */
  512. Py_ssize_t length, /* size of string */
  513. const char *errors, /* error handling */
  514. Py_ssize_t *consumed /* bytes consumed */
  515. );
  516. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
  517. PyAPI_FUNC(PyObject*) PyUnicode_DecodeCodePageStateful(
  518. int code_page, /* code page number */
  519. const char *string, /* encoded string */
  520. Py_ssize_t length, /* size of string */
  521. const char *errors, /* error handling */
  522. Py_ssize_t *consumed /* bytes consumed */
  523. );
  524. #endif
  525. PyAPI_FUNC(PyObject*) PyUnicode_AsMBCSString(
  526. PyObject *unicode /* Unicode object */
  527. );
  528. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
  529. PyAPI_FUNC(PyObject*) PyUnicode_EncodeCodePage(
  530. int code_page, /* code page number */
  531. PyObject *unicode, /* Unicode object */
  532. const char *errors /* error handling */
  533. );
  534. #endif
  535. #endif /* MS_WINDOWS */
  536. /* --- Locale encoding --------------------------------------------------- */
  537. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
  538. /* Decode a string from the current locale encoding. The decoder is strict if
  539. *surrogateescape* is equal to zero, otherwise it uses the 'surrogateescape'
  540. error handler (PEP 383) to escape undecodable bytes. If a byte sequence can
  541. be decoded as a surrogate character and *surrogateescape* is not equal to
  542. zero, the byte sequence is escaped using the 'surrogateescape' error handler
  543. instead of being decoded. *str* must end with a null character but cannot
  544. contain embedded null characters. */
  545. PyAPI_FUNC(PyObject*) PyUnicode_DecodeLocaleAndSize(
  546. const char *str,
  547. Py_ssize_t len,
  548. const char *errors);
  549. /* Similar to PyUnicode_DecodeLocaleAndSize(), but compute the string
  550. length using strlen(). */
  551. PyAPI_FUNC(PyObject*) PyUnicode_DecodeLocale(
  552. const char *str,
  553. const char *errors);
  554. /* Encode a Unicode object to the current locale encoding. The encoder is
  555. strict is *surrogateescape* is equal to zero, otherwise the
  556. "surrogateescape" error handler is used. Return a bytes object. The string
  557. cannot contain embedded null characters. */
  558. PyAPI_FUNC(PyObject*) PyUnicode_EncodeLocale(
  559. PyObject *unicode,
  560. const char *errors
  561. );
  562. #endif
  563. /* --- File system encoding ---------------------------------------------- */
  564. /* ParseTuple converter: encode str objects to bytes using
  565. PyUnicode_EncodeFSDefault(); bytes objects are output as-is. */
  566. PyAPI_FUNC(int) PyUnicode_FSConverter(PyObject*, void*);
  567. /* ParseTuple converter: decode bytes objects to unicode using
  568. PyUnicode_DecodeFSDefaultAndSize(); str objects are output as-is. */
  569. PyAPI_FUNC(int) PyUnicode_FSDecoder(PyObject*, void*);
  570. /* Decode a null-terminated string using Py_FileSystemDefaultEncoding
  571. and the "surrogateescape" error handler.
  572. If Py_FileSystemDefaultEncoding is not set, fall back to the locale
  573. encoding.
  574. Use PyUnicode_DecodeFSDefaultAndSize() if the string length is known.
  575. */
  576. PyAPI_FUNC(PyObject*) PyUnicode_DecodeFSDefault(
  577. const char *s /* encoded string */
  578. );
  579. /* Decode a string using Py_FileSystemDefaultEncoding
  580. and the "surrogateescape" error handler.
  581. If Py_FileSystemDefaultEncoding is not set, fall back to the locale
  582. encoding.
  583. */
  584. PyAPI_FUNC(PyObject*) PyUnicode_DecodeFSDefaultAndSize(
  585. const char *s, /* encoded string */
  586. Py_ssize_t size /* size */
  587. );
  588. /* Encode a Unicode object to Py_FileSystemDefaultEncoding with the
  589. "surrogateescape" error handler, and return bytes.
  590. If Py_FileSystemDefaultEncoding is not set, fall back to the locale
  591. encoding.
  592. */
  593. PyAPI_FUNC(PyObject*) PyUnicode_EncodeFSDefault(
  594. PyObject *unicode
  595. );
  596. /* --- Methods & Slots ----------------------------------------------------
  597. These are capable of handling Unicode objects and strings on input
  598. (we refer to them as strings in the descriptions) and return
  599. Unicode objects or integers as appropriate. */
  600. /* Concat two strings giving a new Unicode string. */
  601. PyAPI_FUNC(PyObject*) PyUnicode_Concat(
  602. PyObject *left, /* Left string */
  603. PyObject *right /* Right string */
  604. );
  605. /* Concat two strings and put the result in *pleft
  606. (sets *pleft to NULL on error) */
  607. PyAPI_FUNC(void) PyUnicode_Append(
  608. PyObject **pleft, /* Pointer to left string */
  609. PyObject *right /* Right string */
  610. );
  611. /* Concat two strings, put the result in *pleft and drop the right object
  612. (sets *pleft to NULL on error) */
  613. PyAPI_FUNC(void) PyUnicode_AppendAndDel(
  614. PyObject **pleft, /* Pointer to left string */
  615. PyObject *right /* Right string */
  616. );
  617. /* Split a string giving a list of Unicode strings.
  618. If sep is NULL, splitting will be done at all whitespace
  619. substrings. Otherwise, splits occur at the given separator.
  620. At most maxsplit splits will be done. If negative, no limit is set.
  621. Separators are not included in the resulting list.
  622. */
  623. PyAPI_FUNC(PyObject*) PyUnicode_Split(
  624. PyObject *s, /* String to split */
  625. PyObject *sep, /* String separator */
  626. Py_ssize_t maxsplit /* Maxsplit count */
  627. );
  628. /* Dito, but split at line breaks.
  629. CRLF is considered to be one line break. Line breaks are not
  630. included in the resulting list. */
  631. PyAPI_FUNC(PyObject*) PyUnicode_Splitlines(
  632. PyObject *s, /* String to split */
  633. int keepends /* If true, line end markers are included */
  634. );
  635. /* Partition a string using a given separator. */
  636. PyAPI_FUNC(PyObject*) PyUnicode_Partition(
  637. PyObject *s, /* String to partition */
  638. PyObject *sep /* String separator */
  639. );
  640. /* Partition a string using a given separator, searching from the end of the
  641. string. */
  642. PyAPI_FUNC(PyObject*) PyUnicode_RPartition(
  643. PyObject *s, /* String to partition */
  644. PyObject *sep /* String separator */
  645. );
  646. /* Split a string giving a list of Unicode strings.
  647. If sep is NULL, splitting will be done at all whitespace
  648. substrings. Otherwise, splits occur at the given separator.
  649. At most maxsplit splits will be done. But unlike PyUnicode_Split
  650. PyUnicode_RSplit splits from the end of the string. If negative,
  651. no limit is set.
  652. Separators are not included in the resulting list.
  653. */
  654. PyAPI_FUNC(PyObject*) PyUnicode_RSplit(
  655. PyObject *s, /* String to split */
  656. PyObject *sep, /* String separator */
  657. Py_ssize_t maxsplit /* Maxsplit count */
  658. );
  659. /* Translate a string by applying a character mapping table to it and
  660. return the resulting Unicode object.
  661. The mapping table must map Unicode ordinal integers to Unicode strings,
  662. Unicode ordinal integers or None (causing deletion of the character).
  663. Mapping tables may be dictionaries or sequences. Unmapped character
  664. ordinals (ones which cause a LookupError) are left untouched and
  665. are copied as-is.
  666. */
  667. PyAPI_FUNC(PyObject *) PyUnicode_Translate(
  668. PyObject *str, /* String */
  669. PyObject *table, /* Translate table */
  670. const char *errors /* error handling */
  671. );
  672. /* Join a sequence of strings using the given separator and return
  673. the resulting Unicode string. */
  674. PyAPI_FUNC(PyObject*) PyUnicode_Join(
  675. PyObject *separator, /* Separator string */
  676. PyObject *seq /* Sequence object */
  677. );
  678. /* Return 1 if substr matches str[start:end] at the given tail end, 0
  679. otherwise. */
  680. PyAPI_FUNC(Py_ssize_t) PyUnicode_Tailmatch(
  681. PyObject *str, /* String */
  682. PyObject *substr, /* Prefix or Suffix string */
  683. Py_ssize_t start, /* Start index */
  684. Py_ssize_t end, /* Stop index */
  685. int direction /* Tail end: -1 prefix, +1 suffix */
  686. );
  687. /* Return the first position of substr in str[start:end] using the
  688. given search direction or -1 if not found. -2 is returned in case
  689. an error occurred and an exception is set. */
  690. PyAPI_FUNC(Py_ssize_t) PyUnicode_Find(
  691. PyObject *str, /* String */
  692. PyObject *substr, /* Substring to find */
  693. Py_ssize_t start, /* Start index */
  694. Py_ssize_t end, /* Stop index */
  695. int direction /* Find direction: +1 forward, -1 backward */
  696. );
  697. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
  698. /* Like PyUnicode_Find, but search for single character only. */
  699. PyAPI_FUNC(Py_ssize_t) PyUnicode_FindChar(
  700. PyObject *str,
  701. Py_UCS4 ch,
  702. Py_ssize_t start,
  703. Py_ssize_t end,
  704. int direction
  705. );
  706. #endif
  707. /* Count the number of occurrences of substr in str[start:end]. */
  708. PyAPI_FUNC(Py_ssize_t) PyUnicode_Count(
  709. PyObject *str, /* String */
  710. PyObject *substr, /* Substring to count */
  711. Py_ssize_t start, /* Start index */
  712. Py_ssize_t end /* Stop index */
  713. );
  714. /* Replace at most maxcount occurrences of substr in str with replstr
  715. and return the resulting Unicode object. */
  716. PyAPI_FUNC(PyObject *) PyUnicode_Replace(
  717. PyObject *str, /* String */
  718. PyObject *substr, /* Substring to find */
  719. PyObject *replstr, /* Substring to replace */
  720. Py_ssize_t maxcount /* Max. number of replacements to apply;
  721. -1 = all */
  722. );
  723. /* Compare two strings and return -1, 0, 1 for less than, equal,
  724. greater than resp.
  725. Raise an exception and return -1 on error. */
  726. PyAPI_FUNC(int) PyUnicode_Compare(
  727. PyObject *left, /* Left string */
  728. PyObject *right /* Right string */
  729. );
  730. /* Compare a Unicode object with C string and return -1, 0, 1 for less than,
  731. equal, and greater than, respectively. It is best to pass only
  732. ASCII-encoded strings, but the function interprets the input string as
  733. ISO-8859-1 if it contains non-ASCII characters.
  734. This function does not raise exceptions. */
  735. PyAPI_FUNC(int) PyUnicode_CompareWithASCIIString(
  736. PyObject *left,
  737. const char *right /* ASCII-encoded string */
  738. );
  739. /* Rich compare two strings and return one of the following:
  740. - NULL in case an exception was raised
  741. - Py_True or Py_False for successful comparisons
  742. - Py_NotImplemented in case the type combination is unknown
  743. Possible values for op:
  744. Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE
  745. */
  746. PyAPI_FUNC(PyObject *) PyUnicode_RichCompare(
  747. PyObject *left, /* Left string */
  748. PyObject *right, /* Right string */
  749. int op /* Operation: Py_EQ, Py_NE, Py_GT, etc. */
  750. );
  751. /* Apply an argument tuple or dictionary to a format string and return
  752. the resulting Unicode string. */
  753. PyAPI_FUNC(PyObject *) PyUnicode_Format(
  754. PyObject *format, /* Format string */
  755. PyObject *args /* Argument tuple or dictionary */
  756. );
  757. /* Checks whether element is contained in container and return 1/0
  758. accordingly.
  759. element has to coerce to a one element Unicode string. -1 is
  760. returned in case of an error. */
  761. PyAPI_FUNC(int) PyUnicode_Contains(
  762. PyObject *container, /* Container string */
  763. PyObject *element /* Element string */
  764. );
  765. /* Checks whether argument is a valid identifier. */
  766. PyAPI_FUNC(int) PyUnicode_IsIdentifier(PyObject *s);
  767. /* === Characters Type APIs =============================================== */
  768. #ifndef Py_LIMITED_API
  769. # define Py_CPYTHON_UNICODEOBJECT_H
  770. # include "cpython/unicodeobject.h"
  771. # undef Py_CPYTHON_UNICODEOBJECT_H
  772. #endif
  773. #ifdef __cplusplus
  774. }
  775. #endif
  776. #endif /* !Py_UNICODEOBJECT_H */