unicodeobject.h 34 KB

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