unicodeobject.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  1. #ifndef Py_CPYTHON_UNICODEOBJECT_H
  2. # error "this header file must not be included directly"
  3. #endif
  4. /* Py_UNICODE was the native Unicode storage format (code unit) used by
  5. Python and represents a single Unicode element in the Unicode type.
  6. With PEP 393, Py_UNICODE is deprecated and replaced with a
  7. typedef to wchar_t. */
  8. #define PY_UNICODE_TYPE wchar_t
  9. /* Py_DEPRECATED(3.3) */ typedef wchar_t Py_UNICODE;
  10. /* --- Internal Unicode Operations ---------------------------------------- */
  11. // Static inline functions to work with surrogates
  12. static inline int Py_UNICODE_IS_SURROGATE(Py_UCS4 ch) {
  13. return (0xD800 <= ch && ch <= 0xDFFF);
  14. }
  15. static inline int Py_UNICODE_IS_HIGH_SURROGATE(Py_UCS4 ch) {
  16. return (0xD800 <= ch && ch <= 0xDBFF);
  17. }
  18. static inline int Py_UNICODE_IS_LOW_SURROGATE(Py_UCS4 ch) {
  19. return (0xDC00 <= ch && ch <= 0xDFFF);
  20. }
  21. // Join two surrogate characters and return a single Py_UCS4 value.
  22. static inline Py_UCS4 Py_UNICODE_JOIN_SURROGATES(Py_UCS4 high, Py_UCS4 low) {
  23. assert(Py_UNICODE_IS_HIGH_SURROGATE(high));
  24. assert(Py_UNICODE_IS_LOW_SURROGATE(low));
  25. return 0x10000 + (((high & 0x03FF) << 10) | (low & 0x03FF));
  26. }
  27. // High surrogate = top 10 bits added to 0xD800.
  28. // The character must be in the range [U+10000; U+10ffff].
  29. static inline Py_UCS4 Py_UNICODE_HIGH_SURROGATE(Py_UCS4 ch) {
  30. assert(0x10000 <= ch && ch <= 0x10ffff);
  31. return (0xD800 - (0x10000 >> 10) + (ch >> 10));
  32. }
  33. // Low surrogate = bottom 10 bits added to 0xDC00.
  34. // The character must be in the range [U+10000; U+10ffff].
  35. static inline Py_UCS4 Py_UNICODE_LOW_SURROGATE(Py_UCS4 ch) {
  36. assert(0x10000 <= ch && ch <= 0x10ffff);
  37. return (0xDC00 + (ch & 0x3FF));
  38. }
  39. /* --- Unicode Type ------------------------------------------------------- */
  40. /* ASCII-only strings created through PyUnicode_New use the PyASCIIObject
  41. structure. state.ascii and state.compact are set, and the data
  42. immediately follow the structure. utf8_length can be found
  43. in the length field; the utf8 pointer is equal to the data pointer. */
  44. typedef struct {
  45. /* There are 4 forms of Unicode strings:
  46. - compact ascii:
  47. * structure = PyASCIIObject
  48. * test: PyUnicode_IS_COMPACT_ASCII(op)
  49. * kind = PyUnicode_1BYTE_KIND
  50. * compact = 1
  51. * ascii = 1
  52. * (length is the length of the utf8)
  53. * (data starts just after the structure)
  54. * (since ASCII is decoded from UTF-8, the utf8 string are the data)
  55. - compact:
  56. * structure = PyCompactUnicodeObject
  57. * test: PyUnicode_IS_COMPACT(op) && !PyUnicode_IS_ASCII(op)
  58. * kind = PyUnicode_1BYTE_KIND, PyUnicode_2BYTE_KIND or
  59. PyUnicode_4BYTE_KIND
  60. * compact = 1
  61. * ascii = 0
  62. * utf8 is not shared with data
  63. * utf8_length = 0 if utf8 is NULL
  64. * (data starts just after the structure)
  65. - legacy string:
  66. * structure = PyUnicodeObject structure
  67. * test: !PyUnicode_IS_COMPACT(op)
  68. * kind = PyUnicode_1BYTE_KIND, PyUnicode_2BYTE_KIND or
  69. PyUnicode_4BYTE_KIND
  70. * compact = 0
  71. * data.any is not NULL
  72. * utf8 is shared and utf8_length = length with data.any if ascii = 1
  73. * utf8_length = 0 if utf8 is NULL
  74. Compact strings use only one memory block (structure + characters),
  75. whereas legacy strings use one block for the structure and one block
  76. for characters.
  77. Legacy strings are created by subclasses of Unicode.
  78. See also _PyUnicode_CheckConsistency().
  79. */
  80. PyObject_HEAD
  81. Py_ssize_t length; /* Number of code points in the string */
  82. Py_hash_t hash; /* Hash value; -1 if not set */
  83. struct {
  84. /* If interned is non-zero, the two references from the
  85. dictionary to this object are *not* counted in ob_refcnt.
  86. The possible values here are:
  87. 0: Not Interned
  88. 1: Interned
  89. 2: Interned and Immortal
  90. 3: Interned, Immortal, and Static
  91. This categorization allows the runtime to determine the right
  92. cleanup mechanism at runtime shutdown. */
  93. unsigned int interned:2;
  94. /* Character size:
  95. - PyUnicode_1BYTE_KIND (1):
  96. * character type = Py_UCS1 (8 bits, unsigned)
  97. * all characters are in the range U+0000-U+00FF (latin1)
  98. * if ascii is set, all characters are in the range U+0000-U+007F
  99. (ASCII), otherwise at least one character is in the range
  100. U+0080-U+00FF
  101. - PyUnicode_2BYTE_KIND (2):
  102. * character type = Py_UCS2 (16 bits, unsigned)
  103. * all characters are in the range U+0000-U+FFFF (BMP)
  104. * at least one character is in the range U+0100-U+FFFF
  105. - PyUnicode_4BYTE_KIND (4):
  106. * character type = Py_UCS4 (32 bits, unsigned)
  107. * all characters are in the range U+0000-U+10FFFF
  108. * at least one character is in the range U+10000-U+10FFFF
  109. */
  110. unsigned int kind:3;
  111. /* Compact is with respect to the allocation scheme. Compact unicode
  112. objects only require one memory block while non-compact objects use
  113. one block for the PyUnicodeObject struct and another for its data
  114. buffer. */
  115. unsigned int compact:1;
  116. /* The string only contains characters in the range U+0000-U+007F (ASCII)
  117. and the kind is PyUnicode_1BYTE_KIND. If ascii is set and compact is
  118. set, use the PyASCIIObject structure. */
  119. unsigned int ascii:1;
  120. /* Padding to ensure that PyUnicode_DATA() is always aligned to
  121. 4 bytes (see issue #19537 on m68k). */
  122. unsigned int :25;
  123. } state;
  124. } PyASCIIObject;
  125. /* Non-ASCII strings allocated through PyUnicode_New use the
  126. PyCompactUnicodeObject structure. state.compact is set, and the data
  127. immediately follow the structure. */
  128. typedef struct {
  129. PyASCIIObject _base;
  130. Py_ssize_t utf8_length; /* Number of bytes in utf8, excluding the
  131. * terminating \0. */
  132. char *utf8; /* UTF-8 representation (null-terminated) */
  133. } PyCompactUnicodeObject;
  134. /* Object format for Unicode subclasses. */
  135. typedef struct {
  136. PyCompactUnicodeObject _base;
  137. union {
  138. void *any;
  139. Py_UCS1 *latin1;
  140. Py_UCS2 *ucs2;
  141. Py_UCS4 *ucs4;
  142. } data; /* Canonical, smallest-form Unicode buffer */
  143. } PyUnicodeObject;
  144. PyAPI_FUNC(int) _PyUnicode_CheckConsistency(
  145. PyObject *op,
  146. int check_content);
  147. #define _PyASCIIObject_CAST(op) \
  148. (assert(PyUnicode_Check(op)), \
  149. _Py_CAST(PyASCIIObject*, (op)))
  150. #define _PyCompactUnicodeObject_CAST(op) \
  151. (assert(PyUnicode_Check(op)), \
  152. _Py_CAST(PyCompactUnicodeObject*, (op)))
  153. #define _PyUnicodeObject_CAST(op) \
  154. (assert(PyUnicode_Check(op)), \
  155. _Py_CAST(PyUnicodeObject*, (op)))
  156. /* --- Flexible String Representation Helper Macros (PEP 393) -------------- */
  157. /* Values for PyASCIIObject.state: */
  158. /* Interning state. */
  159. #define SSTATE_NOT_INTERNED 0
  160. #define SSTATE_INTERNED_MORTAL 1
  161. #define SSTATE_INTERNED_IMMORTAL 2
  162. #define SSTATE_INTERNED_IMMORTAL_STATIC 3
  163. /* Use only if you know it's a string */
  164. static inline unsigned int PyUnicode_CHECK_INTERNED(PyObject *op) {
  165. return _PyASCIIObject_CAST(op)->state.interned;
  166. }
  167. #define PyUnicode_CHECK_INTERNED(op) PyUnicode_CHECK_INTERNED(_PyObject_CAST(op))
  168. /* For backward compatibility */
  169. static inline unsigned int PyUnicode_IS_READY(PyObject* Py_UNUSED(op)) {
  170. return 1;
  171. }
  172. #define PyUnicode_IS_READY(op) PyUnicode_IS_READY(_PyObject_CAST(op))
  173. /* Return true if the string contains only ASCII characters, or 0 if not. The
  174. string may be compact (PyUnicode_IS_COMPACT_ASCII) or not, but must be
  175. ready. */
  176. static inline unsigned int PyUnicode_IS_ASCII(PyObject *op) {
  177. return _PyASCIIObject_CAST(op)->state.ascii;
  178. }
  179. #define PyUnicode_IS_ASCII(op) PyUnicode_IS_ASCII(_PyObject_CAST(op))
  180. /* Return true if the string is compact or 0 if not.
  181. No type checks or Ready calls are performed. */
  182. static inline unsigned int PyUnicode_IS_COMPACT(PyObject *op) {
  183. return _PyASCIIObject_CAST(op)->state.compact;
  184. }
  185. #define PyUnicode_IS_COMPACT(op) PyUnicode_IS_COMPACT(_PyObject_CAST(op))
  186. /* Return true if the string is a compact ASCII string (use PyASCIIObject
  187. structure), or 0 if not. No type checks or Ready calls are performed. */
  188. static inline int PyUnicode_IS_COMPACT_ASCII(PyObject *op) {
  189. return (_PyASCIIObject_CAST(op)->state.ascii && PyUnicode_IS_COMPACT(op));
  190. }
  191. #define PyUnicode_IS_COMPACT_ASCII(op) PyUnicode_IS_COMPACT_ASCII(_PyObject_CAST(op))
  192. enum PyUnicode_Kind {
  193. /* Return values of the PyUnicode_KIND() function: */
  194. PyUnicode_1BYTE_KIND = 1,
  195. PyUnicode_2BYTE_KIND = 2,
  196. PyUnicode_4BYTE_KIND = 4
  197. };
  198. // PyUnicode_KIND(): Return one of the PyUnicode_*_KIND values defined above.
  199. //
  200. // gh-89653: Converting this macro to a static inline function would introduce
  201. // new compiler warnings on "kind < PyUnicode_KIND(str)" (compare signed and
  202. // unsigned numbers) where kind type is an int or on
  203. // "unsigned int kind = PyUnicode_KIND(str)" (cast signed to unsigned).
  204. #define PyUnicode_KIND(op) _Py_RVALUE(_PyASCIIObject_CAST(op)->state.kind)
  205. /* Return a void pointer to the raw unicode buffer. */
  206. static inline void* _PyUnicode_COMPACT_DATA(PyObject *op) {
  207. if (PyUnicode_IS_ASCII(op)) {
  208. return _Py_STATIC_CAST(void*, (_PyASCIIObject_CAST(op) + 1));
  209. }
  210. return _Py_STATIC_CAST(void*, (_PyCompactUnicodeObject_CAST(op) + 1));
  211. }
  212. static inline void* _PyUnicode_NONCOMPACT_DATA(PyObject *op) {
  213. void *data;
  214. assert(!PyUnicode_IS_COMPACT(op));
  215. data = _PyUnicodeObject_CAST(op)->data.any;
  216. assert(data != NULL);
  217. return data;
  218. }
  219. static inline void* PyUnicode_DATA(PyObject *op) {
  220. if (PyUnicode_IS_COMPACT(op)) {
  221. return _PyUnicode_COMPACT_DATA(op);
  222. }
  223. return _PyUnicode_NONCOMPACT_DATA(op);
  224. }
  225. #define PyUnicode_DATA(op) PyUnicode_DATA(_PyObject_CAST(op))
  226. /* Return pointers to the canonical representation cast to unsigned char,
  227. Py_UCS2, or Py_UCS4 for direct character access.
  228. No checks are performed, use PyUnicode_KIND() before to ensure
  229. these will work correctly. */
  230. #define PyUnicode_1BYTE_DATA(op) _Py_STATIC_CAST(Py_UCS1*, PyUnicode_DATA(op))
  231. #define PyUnicode_2BYTE_DATA(op) _Py_STATIC_CAST(Py_UCS2*, PyUnicode_DATA(op))
  232. #define PyUnicode_4BYTE_DATA(op) _Py_STATIC_CAST(Py_UCS4*, PyUnicode_DATA(op))
  233. /* Returns the length of the unicode string. */
  234. static inline Py_ssize_t PyUnicode_GET_LENGTH(PyObject *op) {
  235. return _PyASCIIObject_CAST(op)->length;
  236. }
  237. #define PyUnicode_GET_LENGTH(op) PyUnicode_GET_LENGTH(_PyObject_CAST(op))
  238. /* Write into the canonical representation, this function does not do any sanity
  239. checks and is intended for usage in loops. The caller should cache the
  240. kind and data pointers obtained from other function calls.
  241. index is the index in the string (starts at 0) and value is the new
  242. code point value which should be written to that location. */
  243. static inline void PyUnicode_WRITE(int kind, void *data,
  244. Py_ssize_t index, Py_UCS4 value)
  245. {
  246. assert(index >= 0);
  247. if (kind == PyUnicode_1BYTE_KIND) {
  248. assert(value <= 0xffU);
  249. _Py_STATIC_CAST(Py_UCS1*, data)[index] = _Py_STATIC_CAST(Py_UCS1, value);
  250. }
  251. else if (kind == PyUnicode_2BYTE_KIND) {
  252. assert(value <= 0xffffU);
  253. _Py_STATIC_CAST(Py_UCS2*, data)[index] = _Py_STATIC_CAST(Py_UCS2, value);
  254. }
  255. else {
  256. assert(kind == PyUnicode_4BYTE_KIND);
  257. assert(value <= 0x10ffffU);
  258. _Py_STATIC_CAST(Py_UCS4*, data)[index] = value;
  259. }
  260. }
  261. #define PyUnicode_WRITE(kind, data, index, value) \
  262. PyUnicode_WRITE(_Py_STATIC_CAST(int, kind), _Py_CAST(void*, data), \
  263. (index), _Py_STATIC_CAST(Py_UCS4, value))
  264. /* Read a code point from the string's canonical representation. No checks
  265. or ready calls are performed. */
  266. static inline Py_UCS4 PyUnicode_READ(int kind,
  267. const void *data, Py_ssize_t index)
  268. {
  269. assert(index >= 0);
  270. if (kind == PyUnicode_1BYTE_KIND) {
  271. return _Py_STATIC_CAST(const Py_UCS1*, data)[index];
  272. }
  273. if (kind == PyUnicode_2BYTE_KIND) {
  274. return _Py_STATIC_CAST(const Py_UCS2*, data)[index];
  275. }
  276. assert(kind == PyUnicode_4BYTE_KIND);
  277. return _Py_STATIC_CAST(const Py_UCS4*, data)[index];
  278. }
  279. #define PyUnicode_READ(kind, data, index) \
  280. PyUnicode_READ(_Py_STATIC_CAST(int, kind), \
  281. _Py_STATIC_CAST(const void*, data), \
  282. (index))
  283. /* PyUnicode_READ_CHAR() is less efficient than PyUnicode_READ() because it
  284. calls PyUnicode_KIND() and might call it twice. For single reads, use
  285. PyUnicode_READ_CHAR, for multiple consecutive reads callers should
  286. cache kind and use PyUnicode_READ instead. */
  287. static inline Py_UCS4 PyUnicode_READ_CHAR(PyObject *unicode, Py_ssize_t index)
  288. {
  289. int kind;
  290. assert(index >= 0);
  291. // Tolerate reading the NUL character at str[len(str)]
  292. assert(index <= PyUnicode_GET_LENGTH(unicode));
  293. kind = PyUnicode_KIND(unicode);
  294. if (kind == PyUnicode_1BYTE_KIND) {
  295. return PyUnicode_1BYTE_DATA(unicode)[index];
  296. }
  297. if (kind == PyUnicode_2BYTE_KIND) {
  298. return PyUnicode_2BYTE_DATA(unicode)[index];
  299. }
  300. assert(kind == PyUnicode_4BYTE_KIND);
  301. return PyUnicode_4BYTE_DATA(unicode)[index];
  302. }
  303. #define PyUnicode_READ_CHAR(unicode, index) \
  304. PyUnicode_READ_CHAR(_PyObject_CAST(unicode), (index))
  305. /* Return a maximum character value which is suitable for creating another
  306. string based on op. This is always an approximation but more efficient
  307. than iterating over the string. */
  308. static inline Py_UCS4 PyUnicode_MAX_CHAR_VALUE(PyObject *op)
  309. {
  310. int kind;
  311. if (PyUnicode_IS_ASCII(op)) {
  312. return 0x7fU;
  313. }
  314. kind = PyUnicode_KIND(op);
  315. if (kind == PyUnicode_1BYTE_KIND) {
  316. return 0xffU;
  317. }
  318. if (kind == PyUnicode_2BYTE_KIND) {
  319. return 0xffffU;
  320. }
  321. assert(kind == PyUnicode_4BYTE_KIND);
  322. return 0x10ffffU;
  323. }
  324. #define PyUnicode_MAX_CHAR_VALUE(op) \
  325. PyUnicode_MAX_CHAR_VALUE(_PyObject_CAST(op))
  326. /* === Public API ========================================================= */
  327. /* --- Plain Py_UNICODE --------------------------------------------------- */
  328. /* With PEP 393, this is the recommended way to allocate a new unicode object.
  329. This function will allocate the object and its buffer in a single memory
  330. block. Objects created using this function are not resizable. */
  331. PyAPI_FUNC(PyObject*) PyUnicode_New(
  332. Py_ssize_t size, /* Number of code points in the new string */
  333. Py_UCS4 maxchar /* maximum code point value in the string */
  334. );
  335. /* For backward compatibility */
  336. static inline int PyUnicode_READY(PyObject* Py_UNUSED(op))
  337. {
  338. return 0;
  339. }
  340. #define PyUnicode_READY(op) PyUnicode_READY(_PyObject_CAST(op))
  341. /* Get a copy of a Unicode string. */
  342. PyAPI_FUNC(PyObject*) _PyUnicode_Copy(
  343. PyObject *unicode
  344. );
  345. /* Copy character from one unicode object into another, this function performs
  346. character conversion when necessary and falls back to memcpy() if possible.
  347. Fail if to is too small (smaller than *how_many* or smaller than
  348. len(from)-from_start), or if kind(from[from_start:from_start+how_many]) >
  349. kind(to), or if *to* has more than 1 reference.
  350. Return the number of written character, or return -1 and raise an exception
  351. on error.
  352. Pseudo-code:
  353. how_many = min(how_many, len(from) - from_start)
  354. to[to_start:to_start+how_many] = from[from_start:from_start+how_many]
  355. return how_many
  356. Note: The function doesn't write a terminating null character.
  357. */
  358. PyAPI_FUNC(Py_ssize_t) PyUnicode_CopyCharacters(
  359. PyObject *to,
  360. Py_ssize_t to_start,
  361. PyObject *from,
  362. Py_ssize_t from_start,
  363. Py_ssize_t how_many
  364. );
  365. /* Unsafe version of PyUnicode_CopyCharacters(): don't check arguments and so
  366. may crash if parameters are invalid (e.g. if the output string
  367. is too short). */
  368. PyAPI_FUNC(void) _PyUnicode_FastCopyCharacters(
  369. PyObject *to,
  370. Py_ssize_t to_start,
  371. PyObject *from,
  372. Py_ssize_t from_start,
  373. Py_ssize_t how_many
  374. );
  375. /* Fill a string with a character: write fill_char into
  376. unicode[start:start+length].
  377. Fail if fill_char is bigger than the string maximum character, or if the
  378. string has more than 1 reference.
  379. Return the number of written character, or return -1 and raise an exception
  380. on error. */
  381. PyAPI_FUNC(Py_ssize_t) PyUnicode_Fill(
  382. PyObject *unicode,
  383. Py_ssize_t start,
  384. Py_ssize_t length,
  385. Py_UCS4 fill_char
  386. );
  387. /* Unsafe version of PyUnicode_Fill(): don't check arguments and so may crash
  388. if parameters are invalid (e.g. if length is longer than the string). */
  389. PyAPI_FUNC(void) _PyUnicode_FastFill(
  390. PyObject *unicode,
  391. Py_ssize_t start,
  392. Py_ssize_t length,
  393. Py_UCS4 fill_char
  394. );
  395. /* Create a new string from a buffer of Py_UCS1, Py_UCS2 or Py_UCS4 characters.
  396. Scan the string to find the maximum character. */
  397. PyAPI_FUNC(PyObject*) PyUnicode_FromKindAndData(
  398. int kind,
  399. const void *buffer,
  400. Py_ssize_t size);
  401. /* Create a new string from a buffer of ASCII characters.
  402. WARNING: Don't check if the string contains any non-ASCII character. */
  403. PyAPI_FUNC(PyObject*) _PyUnicode_FromASCII(
  404. const char *buffer,
  405. Py_ssize_t size);
  406. /* Compute the maximum character of the substring unicode[start:end].
  407. Return 127 for an empty string. */
  408. PyAPI_FUNC(Py_UCS4) _PyUnicode_FindMaxChar (
  409. PyObject *unicode,
  410. Py_ssize_t start,
  411. Py_ssize_t end);
  412. /* --- _PyUnicodeWriter API ----------------------------------------------- */
  413. typedef struct {
  414. PyObject *buffer;
  415. void *data;
  416. int kind;
  417. Py_UCS4 maxchar;
  418. Py_ssize_t size;
  419. Py_ssize_t pos;
  420. /* minimum number of allocated characters (default: 0) */
  421. Py_ssize_t min_length;
  422. /* minimum character (default: 127, ASCII) */
  423. Py_UCS4 min_char;
  424. /* If non-zero, overallocate the buffer (default: 0). */
  425. unsigned char overallocate;
  426. /* If readonly is 1, buffer is a shared string (cannot be modified)
  427. and size is set to 0. */
  428. unsigned char readonly;
  429. } _PyUnicodeWriter ;
  430. /* Initialize a Unicode writer.
  431. *
  432. * By default, the minimum buffer size is 0 character and overallocation is
  433. * disabled. Set min_length, min_char and overallocate attributes to control
  434. * the allocation of the buffer. */
  435. PyAPI_FUNC(void)
  436. _PyUnicodeWriter_Init(_PyUnicodeWriter *writer);
  437. /* Prepare the buffer to write 'length' characters
  438. with the specified maximum character.
  439. Return 0 on success, raise an exception and return -1 on error. */
  440. #define _PyUnicodeWriter_Prepare(WRITER, LENGTH, MAXCHAR) \
  441. (((MAXCHAR) <= (WRITER)->maxchar \
  442. && (LENGTH) <= (WRITER)->size - (WRITER)->pos) \
  443. ? 0 \
  444. : (((LENGTH) == 0) \
  445. ? 0 \
  446. : _PyUnicodeWriter_PrepareInternal((WRITER), (LENGTH), (MAXCHAR))))
  447. /* Don't call this function directly, use the _PyUnicodeWriter_Prepare() macro
  448. instead. */
  449. PyAPI_FUNC(int)
  450. _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer,
  451. Py_ssize_t length, Py_UCS4 maxchar);
  452. /* Prepare the buffer to have at least the kind KIND.
  453. For example, kind=PyUnicode_2BYTE_KIND ensures that the writer will
  454. support characters in range U+000-U+FFFF.
  455. Return 0 on success, raise an exception and return -1 on error. */
  456. #define _PyUnicodeWriter_PrepareKind(WRITER, KIND) \
  457. ((KIND) <= (WRITER)->kind \
  458. ? 0 \
  459. : _PyUnicodeWriter_PrepareKindInternal((WRITER), (KIND)))
  460. /* Don't call this function directly, use the _PyUnicodeWriter_PrepareKind()
  461. macro instead. */
  462. PyAPI_FUNC(int)
  463. _PyUnicodeWriter_PrepareKindInternal(_PyUnicodeWriter *writer,
  464. int kind);
  465. /* Append a Unicode character.
  466. Return 0 on success, raise an exception and return -1 on error. */
  467. PyAPI_FUNC(int)
  468. _PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer,
  469. Py_UCS4 ch
  470. );
  471. /* Append a Unicode string.
  472. Return 0 on success, raise an exception and return -1 on error. */
  473. PyAPI_FUNC(int)
  474. _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer,
  475. PyObject *str /* Unicode string */
  476. );
  477. /* Append a substring of a Unicode string.
  478. Return 0 on success, raise an exception and return -1 on error. */
  479. PyAPI_FUNC(int)
  480. _PyUnicodeWriter_WriteSubstring(_PyUnicodeWriter *writer,
  481. PyObject *str, /* Unicode string */
  482. Py_ssize_t start,
  483. Py_ssize_t end
  484. );
  485. /* Append an ASCII-encoded byte string.
  486. Return 0 on success, raise an exception and return -1 on error. */
  487. PyAPI_FUNC(int)
  488. _PyUnicodeWriter_WriteASCIIString(_PyUnicodeWriter *writer,
  489. const char *str, /* ASCII-encoded byte string */
  490. Py_ssize_t len /* number of bytes, or -1 if unknown */
  491. );
  492. /* Append a latin1-encoded byte string.
  493. Return 0 on success, raise an exception and return -1 on error. */
  494. PyAPI_FUNC(int)
  495. _PyUnicodeWriter_WriteLatin1String(_PyUnicodeWriter *writer,
  496. const char *str, /* latin1-encoded byte string */
  497. Py_ssize_t len /* length in bytes */
  498. );
  499. /* Get the value of the writer as a Unicode string. Clear the
  500. buffer of the writer. Raise an exception and return NULL
  501. on error. */
  502. PyAPI_FUNC(PyObject *)
  503. _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer);
  504. /* Deallocate memory of a writer (clear its internal buffer). */
  505. PyAPI_FUNC(void)
  506. _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer);
  507. /* Format the object based on the format_spec, as defined in PEP 3101
  508. (Advanced String Formatting). */
  509. PyAPI_FUNC(int) _PyUnicode_FormatAdvancedWriter(
  510. _PyUnicodeWriter *writer,
  511. PyObject *obj,
  512. PyObject *format_spec,
  513. Py_ssize_t start,
  514. Py_ssize_t end);
  515. /* --- Manage the default encoding ---------------------------------------- */
  516. /* Returns a pointer to the default encoding (UTF-8) of the
  517. Unicode object unicode.
  518. Like PyUnicode_AsUTF8AndSize(), this also caches the UTF-8 representation
  519. in the unicodeobject.
  520. _PyUnicode_AsString is a #define for PyUnicode_AsUTF8 to
  521. support the previous internal function with the same behaviour.
  522. Use of this API is DEPRECATED since no size information can be
  523. extracted from the returned data.
  524. */
  525. PyAPI_FUNC(const char *) PyUnicode_AsUTF8(PyObject *unicode);
  526. #define _PyUnicode_AsString PyUnicode_AsUTF8
  527. /* --- UTF-7 Codecs ------------------------------------------------------- */
  528. PyAPI_FUNC(PyObject*) _PyUnicode_EncodeUTF7(
  529. PyObject *unicode, /* Unicode object */
  530. int base64SetO, /* Encode RFC2152 Set O characters in base64 */
  531. int base64WhiteSpace, /* Encode whitespace (sp, ht, nl, cr) in base64 */
  532. const char *errors /* error handling */
  533. );
  534. /* --- UTF-8 Codecs ------------------------------------------------------- */
  535. PyAPI_FUNC(PyObject*) _PyUnicode_AsUTF8String(
  536. PyObject *unicode,
  537. const char *errors);
  538. /* --- UTF-32 Codecs ------------------------------------------------------ */
  539. PyAPI_FUNC(PyObject*) _PyUnicode_EncodeUTF32(
  540. PyObject *object, /* Unicode object */
  541. const char *errors, /* error handling */
  542. int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
  543. );
  544. /* --- UTF-16 Codecs ------------------------------------------------------ */
  545. /* Returns a Python string object holding the UTF-16 encoded value of
  546. the Unicode data.
  547. If byteorder is not 0, output is written according to the following
  548. byte order:
  549. byteorder == -1: little endian
  550. byteorder == 0: native byte order (writes a BOM mark)
  551. byteorder == 1: big endian
  552. If byteorder is 0, the output string will always start with the
  553. Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark is
  554. prepended.
  555. */
  556. PyAPI_FUNC(PyObject*) _PyUnicode_EncodeUTF16(
  557. PyObject* unicode, /* Unicode object */
  558. const char *errors, /* error handling */
  559. int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
  560. );
  561. /* --- Unicode-Escape Codecs ---------------------------------------------- */
  562. /* Variant of PyUnicode_DecodeUnicodeEscape that supports partial decoding. */
  563. PyAPI_FUNC(PyObject*) _PyUnicode_DecodeUnicodeEscapeStateful(
  564. const char *string, /* Unicode-Escape encoded string */
  565. Py_ssize_t length, /* size of string */
  566. const char *errors, /* error handling */
  567. Py_ssize_t *consumed /* bytes consumed */
  568. );
  569. /* Helper for PyUnicode_DecodeUnicodeEscape that detects invalid escape
  570. chars. */
  571. PyAPI_FUNC(PyObject*) _PyUnicode_DecodeUnicodeEscapeInternal(
  572. const char *string, /* Unicode-Escape encoded string */
  573. Py_ssize_t length, /* size of string */
  574. const char *errors, /* error handling */
  575. Py_ssize_t *consumed, /* bytes consumed */
  576. const char **first_invalid_escape /* on return, points to first
  577. invalid escaped char in
  578. string. */
  579. );
  580. /* --- Raw-Unicode-Escape Codecs ---------------------------------------------- */
  581. /* Variant of PyUnicode_DecodeRawUnicodeEscape that supports partial decoding. */
  582. PyAPI_FUNC(PyObject*) _PyUnicode_DecodeRawUnicodeEscapeStateful(
  583. const char *string, /* Unicode-Escape encoded string */
  584. Py_ssize_t length, /* size of string */
  585. const char *errors, /* error handling */
  586. Py_ssize_t *consumed /* bytes consumed */
  587. );
  588. /* --- Latin-1 Codecs ----------------------------------------------------- */
  589. PyAPI_FUNC(PyObject*) _PyUnicode_AsLatin1String(
  590. PyObject* unicode,
  591. const char* errors);
  592. /* --- ASCII Codecs ------------------------------------------------------- */
  593. PyAPI_FUNC(PyObject*) _PyUnicode_AsASCIIString(
  594. PyObject* unicode,
  595. const char* errors);
  596. /* --- Character Map Codecs ----------------------------------------------- */
  597. /* Translate an Unicode object by applying a character mapping table to
  598. it and return the resulting Unicode object.
  599. The mapping table must map Unicode ordinal integers to Unicode strings,
  600. Unicode ordinal integers or None (causing deletion of the character).
  601. Mapping tables may be dictionaries or sequences. Unmapped character
  602. ordinals (ones which cause a LookupError) are left untouched and
  603. are copied as-is.
  604. */
  605. PyAPI_FUNC(PyObject*) _PyUnicode_EncodeCharmap(
  606. PyObject *unicode, /* Unicode object */
  607. PyObject *mapping, /* encoding mapping */
  608. const char *errors /* error handling */
  609. );
  610. /* --- Decimal Encoder ---------------------------------------------------- */
  611. /* Coverts a Unicode object holding a decimal value to an ASCII string
  612. for using in int, float and complex parsers.
  613. Transforms code points that have decimal digit property to the
  614. corresponding ASCII digit code points. Transforms spaces to ASCII.
  615. Transforms code points starting from the first non-ASCII code point that
  616. is neither a decimal digit nor a space to the end into '?'. */
  617. PyAPI_FUNC(PyObject*) _PyUnicode_TransformDecimalAndSpaceToASCII(
  618. PyObject *unicode /* Unicode object */
  619. );
  620. /* --- Methods & Slots ---------------------------------------------------- */
  621. PyAPI_FUNC(PyObject *) _PyUnicode_JoinArray(
  622. PyObject *separator,
  623. PyObject *const *items,
  624. Py_ssize_t seqlen
  625. );
  626. /* Test whether a unicode is equal to ASCII identifier. Return 1 if true,
  627. 0 otherwise. The right argument must be ASCII identifier.
  628. Any error occurs inside will be cleared before return. */
  629. PyAPI_FUNC(int) _PyUnicode_EqualToASCIIId(
  630. PyObject *left, /* Left string */
  631. _Py_Identifier *right /* Right identifier */
  632. );
  633. /* Test whether a unicode is equal to ASCII string. Return 1 if true,
  634. 0 otherwise. The right argument must be ASCII-encoded string.
  635. Any error occurs inside will be cleared before return. */
  636. PyAPI_FUNC(int) _PyUnicode_EqualToASCIIString(
  637. PyObject *left,
  638. const char *right /* ASCII-encoded string */
  639. );
  640. /* Externally visible for str.strip(unicode) */
  641. PyAPI_FUNC(PyObject *) _PyUnicode_XStrip(
  642. PyObject *self,
  643. int striptype,
  644. PyObject *sepobj
  645. );
  646. /* Using explicit passed-in values, insert the thousands grouping
  647. into the string pointed to by buffer. For the argument descriptions,
  648. see Objects/stringlib/localeutil.h */
  649. PyAPI_FUNC(Py_ssize_t) _PyUnicode_InsertThousandsGrouping(
  650. _PyUnicodeWriter *writer,
  651. Py_ssize_t n_buffer,
  652. PyObject *digits,
  653. Py_ssize_t d_pos,
  654. Py_ssize_t n_digits,
  655. Py_ssize_t min_width,
  656. const char *grouping,
  657. PyObject *thousands_sep,
  658. Py_UCS4 *maxchar);
  659. /* === Characters Type APIs =============================================== */
  660. /* These should not be used directly. Use the Py_UNICODE_IS* and
  661. Py_UNICODE_TO* macros instead.
  662. These APIs are implemented in Objects/unicodectype.c.
  663. */
  664. PyAPI_FUNC(int) _PyUnicode_IsLowercase(
  665. Py_UCS4 ch /* Unicode character */
  666. );
  667. PyAPI_FUNC(int) _PyUnicode_IsUppercase(
  668. Py_UCS4 ch /* Unicode character */
  669. );
  670. PyAPI_FUNC(int) _PyUnicode_IsTitlecase(
  671. Py_UCS4 ch /* Unicode character */
  672. );
  673. PyAPI_FUNC(int) _PyUnicode_IsXidStart(
  674. Py_UCS4 ch /* Unicode character */
  675. );
  676. PyAPI_FUNC(int) _PyUnicode_IsXidContinue(
  677. Py_UCS4 ch /* Unicode character */
  678. );
  679. PyAPI_FUNC(int) _PyUnicode_IsWhitespace(
  680. const Py_UCS4 ch /* Unicode character */
  681. );
  682. PyAPI_FUNC(int) _PyUnicode_IsLinebreak(
  683. const Py_UCS4 ch /* Unicode character */
  684. );
  685. /* Py_DEPRECATED(3.3) */ PyAPI_FUNC(Py_UCS4) _PyUnicode_ToLowercase(
  686. Py_UCS4 ch /* Unicode character */
  687. );
  688. /* Py_DEPRECATED(3.3) */ PyAPI_FUNC(Py_UCS4) _PyUnicode_ToUppercase(
  689. Py_UCS4 ch /* Unicode character */
  690. );
  691. Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UCS4) _PyUnicode_ToTitlecase(
  692. Py_UCS4 ch /* Unicode character */
  693. );
  694. PyAPI_FUNC(int) _PyUnicode_ToLowerFull(
  695. Py_UCS4 ch, /* Unicode character */
  696. Py_UCS4 *res
  697. );
  698. PyAPI_FUNC(int) _PyUnicode_ToTitleFull(
  699. Py_UCS4 ch, /* Unicode character */
  700. Py_UCS4 *res
  701. );
  702. PyAPI_FUNC(int) _PyUnicode_ToUpperFull(
  703. Py_UCS4 ch, /* Unicode character */
  704. Py_UCS4 *res
  705. );
  706. PyAPI_FUNC(int) _PyUnicode_ToFoldedFull(
  707. Py_UCS4 ch, /* Unicode character */
  708. Py_UCS4 *res
  709. );
  710. PyAPI_FUNC(int) _PyUnicode_IsCaseIgnorable(
  711. Py_UCS4 ch /* Unicode character */
  712. );
  713. PyAPI_FUNC(int) _PyUnicode_IsCased(
  714. Py_UCS4 ch /* Unicode character */
  715. );
  716. PyAPI_FUNC(int) _PyUnicode_ToDecimalDigit(
  717. Py_UCS4 ch /* Unicode character */
  718. );
  719. PyAPI_FUNC(int) _PyUnicode_ToDigit(
  720. Py_UCS4 ch /* Unicode character */
  721. );
  722. PyAPI_FUNC(double) _PyUnicode_ToNumeric(
  723. Py_UCS4 ch /* Unicode character */
  724. );
  725. PyAPI_FUNC(int) _PyUnicode_IsDecimalDigit(
  726. Py_UCS4 ch /* Unicode character */
  727. );
  728. PyAPI_FUNC(int) _PyUnicode_IsDigit(
  729. Py_UCS4 ch /* Unicode character */
  730. );
  731. PyAPI_FUNC(int) _PyUnicode_IsNumeric(
  732. Py_UCS4 ch /* Unicode character */
  733. );
  734. PyAPI_FUNC(int) _PyUnicode_IsPrintable(
  735. Py_UCS4 ch /* Unicode character */
  736. );
  737. PyAPI_FUNC(int) _PyUnicode_IsAlpha(
  738. Py_UCS4 ch /* Unicode character */
  739. );
  740. // Helper array used by Py_UNICODE_ISSPACE().
  741. PyAPI_DATA(const unsigned char) _Py_ascii_whitespace[];
  742. // Since splitting on whitespace is an important use case, and
  743. // whitespace in most situations is solely ASCII whitespace, we
  744. // optimize for the common case by using a quick look-up table
  745. // _Py_ascii_whitespace (see below) with an inlined check.
  746. static inline int Py_UNICODE_ISSPACE(Py_UCS4 ch) {
  747. if (ch < 128) {
  748. return _Py_ascii_whitespace[ch];
  749. }
  750. return _PyUnicode_IsWhitespace(ch);
  751. }
  752. #define Py_UNICODE_ISLOWER(ch) _PyUnicode_IsLowercase(ch)
  753. #define Py_UNICODE_ISUPPER(ch) _PyUnicode_IsUppercase(ch)
  754. #define Py_UNICODE_ISTITLE(ch) _PyUnicode_IsTitlecase(ch)
  755. #define Py_UNICODE_ISLINEBREAK(ch) _PyUnicode_IsLinebreak(ch)
  756. #define Py_UNICODE_TOLOWER(ch) _PyUnicode_ToLowercase(ch)
  757. #define Py_UNICODE_TOUPPER(ch) _PyUnicode_ToUppercase(ch)
  758. #define Py_UNICODE_TOTITLE(ch) _PyUnicode_ToTitlecase(ch)
  759. #define Py_UNICODE_ISDECIMAL(ch) _PyUnicode_IsDecimalDigit(ch)
  760. #define Py_UNICODE_ISDIGIT(ch) _PyUnicode_IsDigit(ch)
  761. #define Py_UNICODE_ISNUMERIC(ch) _PyUnicode_IsNumeric(ch)
  762. #define Py_UNICODE_ISPRINTABLE(ch) _PyUnicode_IsPrintable(ch)
  763. #define Py_UNICODE_TODECIMAL(ch) _PyUnicode_ToDecimalDigit(ch)
  764. #define Py_UNICODE_TODIGIT(ch) _PyUnicode_ToDigit(ch)
  765. #define Py_UNICODE_TONUMERIC(ch) _PyUnicode_ToNumeric(ch)
  766. #define Py_UNICODE_ISALPHA(ch) _PyUnicode_IsAlpha(ch)
  767. static inline int Py_UNICODE_ISALNUM(Py_UCS4 ch) {
  768. return (Py_UNICODE_ISALPHA(ch)
  769. || Py_UNICODE_ISDECIMAL(ch)
  770. || Py_UNICODE_ISDIGIT(ch)
  771. || Py_UNICODE_ISNUMERIC(ch));
  772. }
  773. /* === Misc functions ===================================================== */
  774. PyAPI_FUNC(PyObject*) _PyUnicode_FormatLong(PyObject *, int, int, int);
  775. /* Return an interned Unicode object for an Identifier; may fail if there is no memory.*/
  776. PyAPI_FUNC(PyObject*) _PyUnicode_FromId(_Py_Identifier*);
  777. /* Fast equality check when the inputs are known to be exact unicode types
  778. and where the hash values are equal (i.e. a very probable match) */
  779. PyAPI_FUNC(int) _PyUnicode_EQ(PyObject *, PyObject *);
  780. /* Equality check. */
  781. PyAPI_FUNC(int) _PyUnicode_Equal(PyObject *, PyObject *);
  782. PyAPI_FUNC(int) _PyUnicode_WideCharString_Converter(PyObject *, void *);
  783. PyAPI_FUNC(int) _PyUnicode_WideCharString_Opt_Converter(PyObject *, void *);
  784. PyAPI_FUNC(Py_ssize_t) _PyUnicode_ScanIdentifier(PyObject *);