unicodeobject.h 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  1. #ifndef Py_CPYTHON_UNICODEOBJECT_H
  2. # error "this header file must not be included directly"
  3. #endif
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. /* Py_UNICODE was the native Unicode storage format (code unit) used by
  8. Python and represents a single Unicode element in the Unicode type.
  9. With PEP 393, Py_UNICODE is deprecated and replaced with a
  10. typedef to wchar_t. */
  11. #define PY_UNICODE_TYPE wchar_t
  12. /* Py_DEPRECATED(3.3) */ typedef wchar_t Py_UNICODE;
  13. /* --- Internal Unicode Operations ---------------------------------------- */
  14. /* Since splitting on whitespace is an important use case, and
  15. whitespace in most situations is solely ASCII whitespace, we
  16. optimize for the common case by using a quick look-up table
  17. _Py_ascii_whitespace (see below) with an inlined check.
  18. */
  19. #define Py_UNICODE_ISSPACE(ch) \
  20. ((Py_UCS4)(ch) < 128U ? _Py_ascii_whitespace[(ch)] : _PyUnicode_IsWhitespace(ch))
  21. #define Py_UNICODE_ISLOWER(ch) _PyUnicode_IsLowercase(ch)
  22. #define Py_UNICODE_ISUPPER(ch) _PyUnicode_IsUppercase(ch)
  23. #define Py_UNICODE_ISTITLE(ch) _PyUnicode_IsTitlecase(ch)
  24. #define Py_UNICODE_ISLINEBREAK(ch) _PyUnicode_IsLinebreak(ch)
  25. #define Py_UNICODE_TOLOWER(ch) _PyUnicode_ToLowercase(ch)
  26. #define Py_UNICODE_TOUPPER(ch) _PyUnicode_ToUppercase(ch)
  27. #define Py_UNICODE_TOTITLE(ch) _PyUnicode_ToTitlecase(ch)
  28. #define Py_UNICODE_ISDECIMAL(ch) _PyUnicode_IsDecimalDigit(ch)
  29. #define Py_UNICODE_ISDIGIT(ch) _PyUnicode_IsDigit(ch)
  30. #define Py_UNICODE_ISNUMERIC(ch) _PyUnicode_IsNumeric(ch)
  31. #define Py_UNICODE_ISPRINTABLE(ch) _PyUnicode_IsPrintable(ch)
  32. #define Py_UNICODE_TODECIMAL(ch) _PyUnicode_ToDecimalDigit(ch)
  33. #define Py_UNICODE_TODIGIT(ch) _PyUnicode_ToDigit(ch)
  34. #define Py_UNICODE_TONUMERIC(ch) _PyUnicode_ToNumeric(ch)
  35. #define Py_UNICODE_ISALPHA(ch) _PyUnicode_IsAlpha(ch)
  36. #define Py_UNICODE_ISALNUM(ch) \
  37. (Py_UNICODE_ISALPHA(ch) || \
  38. Py_UNICODE_ISDECIMAL(ch) || \
  39. Py_UNICODE_ISDIGIT(ch) || \
  40. Py_UNICODE_ISNUMERIC(ch))
  41. Py_DEPRECATED(3.3) static inline void
  42. Py_UNICODE_COPY(Py_UNICODE *target, const Py_UNICODE *source, Py_ssize_t length) {
  43. memcpy(target, source, (size_t)(length) * sizeof(Py_UNICODE));
  44. }
  45. Py_DEPRECATED(3.3) static inline void
  46. Py_UNICODE_FILL(Py_UNICODE *target, Py_UNICODE value, Py_ssize_t length) {
  47. Py_ssize_t i;
  48. for (i = 0; i < length; i++) {
  49. target[i] = value;
  50. }
  51. }
  52. /* macros to work with surrogates */
  53. #define Py_UNICODE_IS_SURROGATE(ch) (0xD800 <= (ch) && (ch) <= 0xDFFF)
  54. #define Py_UNICODE_IS_HIGH_SURROGATE(ch) (0xD800 <= (ch) && (ch) <= 0xDBFF)
  55. #define Py_UNICODE_IS_LOW_SURROGATE(ch) (0xDC00 <= (ch) && (ch) <= 0xDFFF)
  56. /* Join two surrogate characters and return a single Py_UCS4 value. */
  57. #define Py_UNICODE_JOIN_SURROGATES(high, low) \
  58. (((((Py_UCS4)(high) & 0x03FF) << 10) | \
  59. ((Py_UCS4)(low) & 0x03FF)) + 0x10000)
  60. /* high surrogate = top 10 bits added to D800 */
  61. #define Py_UNICODE_HIGH_SURROGATE(ch) (0xD800 - (0x10000 >> 10) + ((ch) >> 10))
  62. /* low surrogate = bottom 10 bits added to DC00 */
  63. #define Py_UNICODE_LOW_SURROGATE(ch) (0xDC00 + ((ch) & 0x3FF))
  64. /* --- Unicode Type ------------------------------------------------------- */
  65. /* ASCII-only strings created through PyUnicode_New use the PyASCIIObject
  66. structure. state.ascii and state.compact are set, and the data
  67. immediately follow the structure. utf8_length and wstr_length can be found
  68. in the length field; the utf8 pointer is equal to the data pointer. */
  69. typedef struct {
  70. /* There are 4 forms of Unicode strings:
  71. - compact ascii:
  72. * structure = PyASCIIObject
  73. * test: PyUnicode_IS_COMPACT_ASCII(op)
  74. * kind = PyUnicode_1BYTE_KIND
  75. * compact = 1
  76. * ascii = 1
  77. * ready = 1
  78. * (length is the length of the utf8 and wstr strings)
  79. * (data starts just after the structure)
  80. * (since ASCII is decoded from UTF-8, the utf8 string are the data)
  81. - compact:
  82. * structure = PyCompactUnicodeObject
  83. * test: PyUnicode_IS_COMPACT(op) && !PyUnicode_IS_ASCII(op)
  84. * kind = PyUnicode_1BYTE_KIND, PyUnicode_2BYTE_KIND or
  85. PyUnicode_4BYTE_KIND
  86. * compact = 1
  87. * ready = 1
  88. * ascii = 0
  89. * utf8 is not shared with data
  90. * utf8_length = 0 if utf8 is NULL
  91. * wstr is shared with data and wstr_length=length
  92. if kind=PyUnicode_2BYTE_KIND and sizeof(wchar_t)=2
  93. or if kind=PyUnicode_4BYTE_KIND and sizeof(wchar_t)=4
  94. * wstr_length = 0 if wstr is NULL
  95. * (data starts just after the structure)
  96. - legacy string, not ready:
  97. * structure = PyUnicodeObject
  98. * test: kind == PyUnicode_WCHAR_KIND
  99. * length = 0 (use wstr_length)
  100. * hash = -1
  101. * kind = PyUnicode_WCHAR_KIND
  102. * compact = 0
  103. * ascii = 0
  104. * ready = 0
  105. * interned = SSTATE_NOT_INTERNED
  106. * wstr is not NULL
  107. * data.any is NULL
  108. * utf8 is NULL
  109. * utf8_length = 0
  110. - legacy string, ready:
  111. * structure = PyUnicodeObject structure
  112. * test: !PyUnicode_IS_COMPACT(op) && kind != PyUnicode_WCHAR_KIND
  113. * kind = PyUnicode_1BYTE_KIND, PyUnicode_2BYTE_KIND or
  114. PyUnicode_4BYTE_KIND
  115. * compact = 0
  116. * ready = 1
  117. * data.any is not NULL
  118. * utf8 is shared and utf8_length = length with data.any if ascii = 1
  119. * utf8_length = 0 if utf8 is NULL
  120. * wstr is shared with data.any and wstr_length = length
  121. if kind=PyUnicode_2BYTE_KIND and sizeof(wchar_t)=2
  122. or if kind=PyUnicode_4BYTE_KIND and sizeof(wchar_4)=4
  123. * wstr_length = 0 if wstr is NULL
  124. Compact strings use only one memory block (structure + characters),
  125. whereas legacy strings use one block for the structure and one block
  126. for characters.
  127. Legacy strings are created by PyUnicode_FromUnicode() and
  128. PyUnicode_FromStringAndSize(NULL, size) functions. They become ready
  129. when PyUnicode_READY() is called.
  130. See also _PyUnicode_CheckConsistency().
  131. */
  132. PyObject_HEAD
  133. Py_ssize_t length; /* Number of code points in the string */
  134. Py_hash_t hash; /* Hash value; -1 if not set */
  135. struct {
  136. /*
  137. SSTATE_NOT_INTERNED (0)
  138. SSTATE_INTERNED_MORTAL (1)
  139. SSTATE_INTERNED_IMMORTAL (2)
  140. If interned != SSTATE_NOT_INTERNED, the two references from the
  141. dictionary to this object are *not* counted in ob_refcnt.
  142. */
  143. unsigned int interned:2;
  144. /* Character size:
  145. - PyUnicode_WCHAR_KIND (0):
  146. * character type = wchar_t (16 or 32 bits, depending on the
  147. platform)
  148. - PyUnicode_1BYTE_KIND (1):
  149. * character type = Py_UCS1 (8 bits, unsigned)
  150. * all characters are in the range U+0000-U+00FF (latin1)
  151. * if ascii is set, all characters are in the range U+0000-U+007F
  152. (ASCII), otherwise at least one character is in the range
  153. U+0080-U+00FF
  154. - PyUnicode_2BYTE_KIND (2):
  155. * character type = Py_UCS2 (16 bits, unsigned)
  156. * all characters are in the range U+0000-U+FFFF (BMP)
  157. * at least one character is in the range U+0100-U+FFFF
  158. - PyUnicode_4BYTE_KIND (4):
  159. * character type = Py_UCS4 (32 bits, unsigned)
  160. * all characters are in the range U+0000-U+10FFFF
  161. * at least one character is in the range U+10000-U+10FFFF
  162. */
  163. unsigned int kind:3;
  164. /* Compact is with respect to the allocation scheme. Compact unicode
  165. objects only require one memory block while non-compact objects use
  166. one block for the PyUnicodeObject struct and another for its data
  167. buffer. */
  168. unsigned int compact:1;
  169. /* The string only contains characters in the range U+0000-U+007F (ASCII)
  170. and the kind is PyUnicode_1BYTE_KIND. If ascii is set and compact is
  171. set, use the PyASCIIObject structure. */
  172. unsigned int ascii:1;
  173. /* The ready flag indicates whether the object layout is initialized
  174. completely. This means that this is either a compact object, or
  175. the data pointer is filled out. The bit is redundant, and helps
  176. to minimize the test in PyUnicode_IS_READY(). */
  177. unsigned int ready:1;
  178. /* Padding to ensure that PyUnicode_DATA() is always aligned to
  179. 4 bytes (see issue #19537 on m68k). */
  180. unsigned int :24;
  181. } state;
  182. wchar_t *wstr; /* wchar_t representation (null-terminated) */
  183. } PyASCIIObject;
  184. /* Non-ASCII strings allocated through PyUnicode_New use the
  185. PyCompactUnicodeObject structure. state.compact is set, and the data
  186. immediately follow the structure. */
  187. typedef struct {
  188. PyASCIIObject _base;
  189. Py_ssize_t utf8_length; /* Number of bytes in utf8, excluding the
  190. * terminating \0. */
  191. char *utf8; /* UTF-8 representation (null-terminated) */
  192. Py_ssize_t wstr_length; /* Number of code points in wstr, possible
  193. * surrogates count as two code points. */
  194. } PyCompactUnicodeObject;
  195. /* Strings allocated through PyUnicode_FromUnicode(NULL, len) use the
  196. PyUnicodeObject structure. The actual string data is initially in the wstr
  197. block, and copied into the data block using _PyUnicode_Ready. */
  198. typedef struct {
  199. PyCompactUnicodeObject _base;
  200. union {
  201. void *any;
  202. Py_UCS1 *latin1;
  203. Py_UCS2 *ucs2;
  204. Py_UCS4 *ucs4;
  205. } data; /* Canonical, smallest-form Unicode buffer */
  206. } PyUnicodeObject;
  207. PyAPI_FUNC(int) _PyUnicode_CheckConsistency(
  208. PyObject *op,
  209. int check_content);
  210. /* Fast access macros */
  211. /* Returns the deprecated Py_UNICODE representation's size in code units
  212. (this includes surrogate pairs as 2 units).
  213. If the Py_UNICODE representation is not available, it will be computed
  214. on request. Use PyUnicode_GET_LENGTH() for the length in code points. */
  215. /* Py_DEPRECATED(3.3) */
  216. #define PyUnicode_GET_SIZE(op) \
  217. (assert(PyUnicode_Check(op)), \
  218. (((PyASCIIObject *)(op))->wstr) ? \
  219. PyUnicode_WSTR_LENGTH(op) : \
  220. ((void)PyUnicode_AsUnicode(_PyObject_CAST(op)),\
  221. assert(((PyASCIIObject *)(op))->wstr), \
  222. PyUnicode_WSTR_LENGTH(op)))
  223. /* Py_DEPRECATED(3.3) */
  224. #define PyUnicode_GET_DATA_SIZE(op) \
  225. (PyUnicode_GET_SIZE(op) * Py_UNICODE_SIZE)
  226. /* Alias for PyUnicode_AsUnicode(). This will create a wchar_t/Py_UNICODE
  227. representation on demand. Using this macro is very inefficient now,
  228. try to port your code to use the new PyUnicode_*BYTE_DATA() macros or
  229. use PyUnicode_WRITE() and PyUnicode_READ(). */
  230. /* Py_DEPRECATED(3.3) */
  231. #define PyUnicode_AS_UNICODE(op) \
  232. (assert(PyUnicode_Check(op)), \
  233. (((PyASCIIObject *)(op))->wstr) ? (((PyASCIIObject *)(op))->wstr) : \
  234. PyUnicode_AsUnicode(_PyObject_CAST(op)))
  235. /* Py_DEPRECATED(3.3) */
  236. #define PyUnicode_AS_DATA(op) \
  237. ((const char *)(PyUnicode_AS_UNICODE(op)))
  238. /* --- Flexible String Representation Helper Macros (PEP 393) -------------- */
  239. /* Values for PyASCIIObject.state: */
  240. /* Interning state. */
  241. #define SSTATE_NOT_INTERNED 0
  242. #define SSTATE_INTERNED_MORTAL 1
  243. #define SSTATE_INTERNED_IMMORTAL 2
  244. /* Return true if the string contains only ASCII characters, or 0 if not. The
  245. string may be compact (PyUnicode_IS_COMPACT_ASCII) or not, but must be
  246. ready. */
  247. #define PyUnicode_IS_ASCII(op) \
  248. (assert(PyUnicode_Check(op)), \
  249. assert(PyUnicode_IS_READY(op)), \
  250. ((PyASCIIObject*)op)->state.ascii)
  251. /* Return true if the string is compact or 0 if not.
  252. No type checks or Ready calls are performed. */
  253. #define PyUnicode_IS_COMPACT(op) \
  254. (((PyASCIIObject*)(op))->state.compact)
  255. /* Return true if the string is a compact ASCII string (use PyASCIIObject
  256. structure), or 0 if not. No type checks or Ready calls are performed. */
  257. #define PyUnicode_IS_COMPACT_ASCII(op) \
  258. (((PyASCIIObject*)op)->state.ascii && PyUnicode_IS_COMPACT(op))
  259. enum PyUnicode_Kind {
  260. /* String contains only wstr byte characters. This is only possible
  261. when the string was created with a legacy API and _PyUnicode_Ready()
  262. has not been called yet. */
  263. PyUnicode_WCHAR_KIND = 0,
  264. /* Return values of the PyUnicode_KIND() macro: */
  265. PyUnicode_1BYTE_KIND = 1,
  266. PyUnicode_2BYTE_KIND = 2,
  267. PyUnicode_4BYTE_KIND = 4
  268. };
  269. /* Return pointers to the canonical representation cast to unsigned char,
  270. Py_UCS2, or Py_UCS4 for direct character access.
  271. No checks are performed, use PyUnicode_KIND() before to ensure
  272. these will work correctly. */
  273. #define PyUnicode_1BYTE_DATA(op) ((Py_UCS1*)PyUnicode_DATA(op))
  274. #define PyUnicode_2BYTE_DATA(op) ((Py_UCS2*)PyUnicode_DATA(op))
  275. #define PyUnicode_4BYTE_DATA(op) ((Py_UCS4*)PyUnicode_DATA(op))
  276. /* Return one of the PyUnicode_*_KIND values defined above. */
  277. #define PyUnicode_KIND(op) \
  278. (assert(PyUnicode_Check(op)), \
  279. assert(PyUnicode_IS_READY(op)), \
  280. ((PyASCIIObject *)(op))->state.kind)
  281. /* Return a void pointer to the raw unicode buffer. */
  282. #define _PyUnicode_COMPACT_DATA(op) \
  283. (PyUnicode_IS_ASCII(op) ? \
  284. ((void*)((PyASCIIObject*)(op) + 1)) : \
  285. ((void*)((PyCompactUnicodeObject*)(op) + 1)))
  286. #define _PyUnicode_NONCOMPACT_DATA(op) \
  287. (assert(((PyUnicodeObject*)(op))->data.any), \
  288. ((((PyUnicodeObject *)(op))->data.any)))
  289. #define PyUnicode_DATA(op) \
  290. (assert(PyUnicode_Check(op)), \
  291. PyUnicode_IS_COMPACT(op) ? _PyUnicode_COMPACT_DATA(op) : \
  292. _PyUnicode_NONCOMPACT_DATA(op))
  293. /* In the access macros below, "kind" may be evaluated more than once.
  294. All other macro parameters are evaluated exactly once, so it is safe
  295. to put side effects into them (such as increasing the index). */
  296. /* Write into the canonical representation, this macro does not do any sanity
  297. checks and is intended for usage in loops. The caller should cache the
  298. kind and data pointers obtained from other macro calls.
  299. index is the index in the string (starts at 0) and value is the new
  300. code point value which should be written to that location. */
  301. #define PyUnicode_WRITE(kind, data, index, value) \
  302. do { \
  303. switch ((kind)) { \
  304. case PyUnicode_1BYTE_KIND: { \
  305. ((Py_UCS1 *)(data))[(index)] = (Py_UCS1)(value); \
  306. break; \
  307. } \
  308. case PyUnicode_2BYTE_KIND: { \
  309. ((Py_UCS2 *)(data))[(index)] = (Py_UCS2)(value); \
  310. break; \
  311. } \
  312. default: { \
  313. assert((kind) == PyUnicode_4BYTE_KIND); \
  314. ((Py_UCS4 *)(data))[(index)] = (Py_UCS4)(value); \
  315. } \
  316. } \
  317. } while (0)
  318. /* Read a code point from the string's canonical representation. No checks
  319. or ready calls are performed. */
  320. #define PyUnicode_READ(kind, data, index) \
  321. ((Py_UCS4) \
  322. ((kind) == PyUnicode_1BYTE_KIND ? \
  323. ((const Py_UCS1 *)(data))[(index)] : \
  324. ((kind) == PyUnicode_2BYTE_KIND ? \
  325. ((const Py_UCS2 *)(data))[(index)] : \
  326. ((const Py_UCS4 *)(data))[(index)] \
  327. ) \
  328. ))
  329. /* PyUnicode_READ_CHAR() is less efficient than PyUnicode_READ() because it
  330. calls PyUnicode_KIND() and might call it twice. For single reads, use
  331. PyUnicode_READ_CHAR, for multiple consecutive reads callers should
  332. cache kind and use PyUnicode_READ instead. */
  333. #define PyUnicode_READ_CHAR(unicode, index) \
  334. (assert(PyUnicode_Check(unicode)), \
  335. assert(PyUnicode_IS_READY(unicode)), \
  336. (Py_UCS4) \
  337. (PyUnicode_KIND((unicode)) == PyUnicode_1BYTE_KIND ? \
  338. ((const Py_UCS1 *)(PyUnicode_DATA((unicode))))[(index)] : \
  339. (PyUnicode_KIND((unicode)) == PyUnicode_2BYTE_KIND ? \
  340. ((const Py_UCS2 *)(PyUnicode_DATA((unicode))))[(index)] : \
  341. ((const Py_UCS4 *)(PyUnicode_DATA((unicode))))[(index)] \
  342. ) \
  343. ))
  344. /* Returns the length of the unicode string. The caller has to make sure that
  345. the string has it's canonical representation set before calling
  346. this macro. Call PyUnicode_(FAST_)Ready to ensure that. */
  347. #define PyUnicode_GET_LENGTH(op) \
  348. (assert(PyUnicode_Check(op)), \
  349. assert(PyUnicode_IS_READY(op)), \
  350. ((PyASCIIObject *)(op))->length)
  351. /* Fast check to determine whether an object is ready. Equivalent to
  352. PyUnicode_IS_COMPACT(op) || ((PyUnicodeObject*)(op))->data.any */
  353. #define PyUnicode_IS_READY(op) (((PyASCIIObject*)op)->state.ready)
  354. /* PyUnicode_READY() does less work than _PyUnicode_Ready() in the best
  355. case. If the canonical representation is not yet set, it will still call
  356. _PyUnicode_Ready().
  357. Returns 0 on success and -1 on errors. */
  358. #define PyUnicode_READY(op) \
  359. (assert(PyUnicode_Check(op)), \
  360. (PyUnicode_IS_READY(op) ? \
  361. 0 : _PyUnicode_Ready(_PyObject_CAST(op))))
  362. /* Return a maximum character value which is suitable for creating another
  363. string based on op. This is always an approximation but more efficient
  364. than iterating over the string. */
  365. #define PyUnicode_MAX_CHAR_VALUE(op) \
  366. (assert(PyUnicode_IS_READY(op)), \
  367. (PyUnicode_IS_ASCII(op) ? \
  368. (0x7f) : \
  369. (PyUnicode_KIND(op) == PyUnicode_1BYTE_KIND ? \
  370. (0xffU) : \
  371. (PyUnicode_KIND(op) == PyUnicode_2BYTE_KIND ? \
  372. (0xffffU) : \
  373. (0x10ffffU)))))
  374. Py_DEPRECATED(3.3)
  375. static inline Py_ssize_t _PyUnicode_get_wstr_length(PyObject *op) {
  376. return PyUnicode_IS_COMPACT_ASCII(op) ?
  377. ((PyASCIIObject*)op)->length :
  378. ((PyCompactUnicodeObject*)op)->wstr_length;
  379. }
  380. #define PyUnicode_WSTR_LENGTH(op) _PyUnicode_get_wstr_length((PyObject*)op)
  381. /* === Public API ========================================================= */
  382. /* --- Plain Py_UNICODE --------------------------------------------------- */
  383. /* With PEP 393, this is the recommended way to allocate a new unicode object.
  384. This function will allocate the object and its buffer in a single memory
  385. block. Objects created using this function are not resizable. */
  386. PyAPI_FUNC(PyObject*) PyUnicode_New(
  387. Py_ssize_t size, /* Number of code points in the new string */
  388. Py_UCS4 maxchar /* maximum code point value in the string */
  389. );
  390. /* Initializes the canonical string representation from the deprecated
  391. wstr/Py_UNICODE representation. This function is used to convert Unicode
  392. objects which were created using the old API to the new flexible format
  393. introduced with PEP 393.
  394. Don't call this function directly, use the public PyUnicode_READY() macro
  395. instead. */
  396. PyAPI_FUNC(int) _PyUnicode_Ready(
  397. PyObject *unicode /* Unicode object */
  398. );
  399. /* Get a copy of a Unicode string. */
  400. PyAPI_FUNC(PyObject*) _PyUnicode_Copy(
  401. PyObject *unicode
  402. );
  403. /* Copy character from one unicode object into another, this function performs
  404. character conversion when necessary and falls back to memcpy() if possible.
  405. Fail if to is too small (smaller than *how_many* or smaller than
  406. len(from)-from_start), or if kind(from[from_start:from_start+how_many]) >
  407. kind(to), or if *to* has more than 1 reference.
  408. Return the number of written character, or return -1 and raise an exception
  409. on error.
  410. Pseudo-code:
  411. how_many = min(how_many, len(from) - from_start)
  412. to[to_start:to_start+how_many] = from[from_start:from_start+how_many]
  413. return how_many
  414. Note: The function doesn't write a terminating null character.
  415. */
  416. PyAPI_FUNC(Py_ssize_t) PyUnicode_CopyCharacters(
  417. PyObject *to,
  418. Py_ssize_t to_start,
  419. PyObject *from,
  420. Py_ssize_t from_start,
  421. Py_ssize_t how_many
  422. );
  423. /* Unsafe version of PyUnicode_CopyCharacters(): don't check arguments and so
  424. may crash if parameters are invalid (e.g. if the output string
  425. is too short). */
  426. PyAPI_FUNC(void) _PyUnicode_FastCopyCharacters(
  427. PyObject *to,
  428. Py_ssize_t to_start,
  429. PyObject *from,
  430. Py_ssize_t from_start,
  431. Py_ssize_t how_many
  432. );
  433. /* Fill a string with a character: write fill_char into
  434. unicode[start:start+length].
  435. Fail if fill_char is bigger than the string maximum character, or if the
  436. string has more than 1 reference.
  437. Return the number of written character, or return -1 and raise an exception
  438. on error. */
  439. PyAPI_FUNC(Py_ssize_t) PyUnicode_Fill(
  440. PyObject *unicode,
  441. Py_ssize_t start,
  442. Py_ssize_t length,
  443. Py_UCS4 fill_char
  444. );
  445. /* Unsafe version of PyUnicode_Fill(): don't check arguments and so may crash
  446. if parameters are invalid (e.g. if length is longer than the string). */
  447. PyAPI_FUNC(void) _PyUnicode_FastFill(
  448. PyObject *unicode,
  449. Py_ssize_t start,
  450. Py_ssize_t length,
  451. Py_UCS4 fill_char
  452. );
  453. /* Create a Unicode Object from the Py_UNICODE buffer u of the given
  454. size.
  455. u may be NULL which causes the contents to be undefined. It is the
  456. user's responsibility to fill in the needed data afterwards. Note
  457. that modifying the Unicode object contents after construction is
  458. only allowed if u was set to NULL.
  459. The buffer is copied into the new object. */
  460. Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_FromUnicode(
  461. const Py_UNICODE *u, /* Unicode buffer */
  462. Py_ssize_t size /* size of buffer */
  463. );
  464. /* Create a new string from a buffer of Py_UCS1, Py_UCS2 or Py_UCS4 characters.
  465. Scan the string to find the maximum character. */
  466. PyAPI_FUNC(PyObject*) PyUnicode_FromKindAndData(
  467. int kind,
  468. const void *buffer,
  469. Py_ssize_t size);
  470. /* Create a new string from a buffer of ASCII characters.
  471. WARNING: Don't check if the string contains any non-ASCII character. */
  472. PyAPI_FUNC(PyObject*) _PyUnicode_FromASCII(
  473. const char *buffer,
  474. Py_ssize_t size);
  475. /* Compute the maximum character of the substring unicode[start:end].
  476. Return 127 for an empty string. */
  477. PyAPI_FUNC(Py_UCS4) _PyUnicode_FindMaxChar (
  478. PyObject *unicode,
  479. Py_ssize_t start,
  480. Py_ssize_t end);
  481. /* Return a read-only pointer to the Unicode object's internal
  482. Py_UNICODE buffer.
  483. If the wchar_t/Py_UNICODE representation is not yet available, this
  484. function will calculate it. */
  485. Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicode(
  486. PyObject *unicode /* Unicode object */
  487. );
  488. /* Similar to PyUnicode_AsUnicode(), but raises a ValueError if the string
  489. contains null characters. */
  490. Py_DEPRECATED(3.3) PyAPI_FUNC(const Py_UNICODE *) _PyUnicode_AsUnicode(
  491. PyObject *unicode /* Unicode object */
  492. );
  493. /* Return a read-only pointer to the Unicode object's internal
  494. Py_UNICODE buffer and save the length at size.
  495. If the wchar_t/Py_UNICODE representation is not yet available, this
  496. function will calculate it. */
  497. Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicodeAndSize(
  498. PyObject *unicode, /* Unicode object */
  499. Py_ssize_t *size /* location where to save the length */
  500. );
  501. /* Get the maximum ordinal for a Unicode character. */
  502. Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE) PyUnicode_GetMax(void);
  503. /* --- _PyUnicodeWriter API ----------------------------------------------- */
  504. typedef struct {
  505. PyObject *buffer;
  506. void *data;
  507. enum PyUnicode_Kind kind;
  508. Py_UCS4 maxchar;
  509. Py_ssize_t size;
  510. Py_ssize_t pos;
  511. /* minimum number of allocated characters (default: 0) */
  512. Py_ssize_t min_length;
  513. /* minimum character (default: 127, ASCII) */
  514. Py_UCS4 min_char;
  515. /* If non-zero, overallocate the buffer (default: 0). */
  516. unsigned char overallocate;
  517. /* If readonly is 1, buffer is a shared string (cannot be modified)
  518. and size is set to 0. */
  519. unsigned char readonly;
  520. } _PyUnicodeWriter ;
  521. /* Initialize a Unicode writer.
  522. *
  523. * By default, the minimum buffer size is 0 character and overallocation is
  524. * disabled. Set min_length, min_char and overallocate attributes to control
  525. * the allocation of the buffer. */
  526. PyAPI_FUNC(void)
  527. _PyUnicodeWriter_Init(_PyUnicodeWriter *writer);
  528. /* Prepare the buffer to write 'length' characters
  529. with the specified maximum character.
  530. Return 0 on success, raise an exception and return -1 on error. */
  531. #define _PyUnicodeWriter_Prepare(WRITER, LENGTH, MAXCHAR) \
  532. (((MAXCHAR) <= (WRITER)->maxchar \
  533. && (LENGTH) <= (WRITER)->size - (WRITER)->pos) \
  534. ? 0 \
  535. : (((LENGTH) == 0) \
  536. ? 0 \
  537. : _PyUnicodeWriter_PrepareInternal((WRITER), (LENGTH), (MAXCHAR))))
  538. /* Don't call this function directly, use the _PyUnicodeWriter_Prepare() macro
  539. instead. */
  540. PyAPI_FUNC(int)
  541. _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer,
  542. Py_ssize_t length, Py_UCS4 maxchar);
  543. /* Prepare the buffer to have at least the kind KIND.
  544. For example, kind=PyUnicode_2BYTE_KIND ensures that the writer will
  545. support characters in range U+000-U+FFFF.
  546. Return 0 on success, raise an exception and return -1 on error. */
  547. #define _PyUnicodeWriter_PrepareKind(WRITER, KIND) \
  548. (assert((KIND) != PyUnicode_WCHAR_KIND), \
  549. (KIND) <= (WRITER)->kind \
  550. ? 0 \
  551. : _PyUnicodeWriter_PrepareKindInternal((WRITER), (KIND)))
  552. /* Don't call this function directly, use the _PyUnicodeWriter_PrepareKind()
  553. macro instead. */
  554. PyAPI_FUNC(int)
  555. _PyUnicodeWriter_PrepareKindInternal(_PyUnicodeWriter *writer,
  556. enum PyUnicode_Kind kind);
  557. /* Append a Unicode character.
  558. Return 0 on success, raise an exception and return -1 on error. */
  559. PyAPI_FUNC(int)
  560. _PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer,
  561. Py_UCS4 ch
  562. );
  563. /* Append a Unicode string.
  564. Return 0 on success, raise an exception and return -1 on error. */
  565. PyAPI_FUNC(int)
  566. _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer,
  567. PyObject *str /* Unicode string */
  568. );
  569. /* Append a substring of a Unicode string.
  570. Return 0 on success, raise an exception and return -1 on error. */
  571. PyAPI_FUNC(int)
  572. _PyUnicodeWriter_WriteSubstring(_PyUnicodeWriter *writer,
  573. PyObject *str, /* Unicode string */
  574. Py_ssize_t start,
  575. Py_ssize_t end
  576. );
  577. /* Append an ASCII-encoded byte string.
  578. Return 0 on success, raise an exception and return -1 on error. */
  579. PyAPI_FUNC(int)
  580. _PyUnicodeWriter_WriteASCIIString(_PyUnicodeWriter *writer,
  581. const char *str, /* ASCII-encoded byte string */
  582. Py_ssize_t len /* number of bytes, or -1 if unknown */
  583. );
  584. /* Append a latin1-encoded byte string.
  585. Return 0 on success, raise an exception and return -1 on error. */
  586. PyAPI_FUNC(int)
  587. _PyUnicodeWriter_WriteLatin1String(_PyUnicodeWriter *writer,
  588. const char *str, /* latin1-encoded byte string */
  589. Py_ssize_t len /* length in bytes */
  590. );
  591. /* Get the value of the writer as a Unicode string. Clear the
  592. buffer of the writer. Raise an exception and return NULL
  593. on error. */
  594. PyAPI_FUNC(PyObject *)
  595. _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer);
  596. /* Deallocate memory of a writer (clear its internal buffer). */
  597. PyAPI_FUNC(void)
  598. _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer);
  599. /* Format the object based on the format_spec, as defined in PEP 3101
  600. (Advanced String Formatting). */
  601. PyAPI_FUNC(int) _PyUnicode_FormatAdvancedWriter(
  602. _PyUnicodeWriter *writer,
  603. PyObject *obj,
  604. PyObject *format_spec,
  605. Py_ssize_t start,
  606. Py_ssize_t end);
  607. /* --- Manage the default encoding ---------------------------------------- */
  608. /* Returns a pointer to the default encoding (UTF-8) of the
  609. Unicode object unicode and the size of the encoded representation
  610. in bytes stored in *size.
  611. In case of an error, no *size is set.
  612. This function caches the UTF-8 encoded string in the unicodeobject
  613. and subsequent calls will return the same string. The memory is released
  614. when the unicodeobject is deallocated.
  615. _PyUnicode_AsStringAndSize is a #define for PyUnicode_AsUTF8AndSize to
  616. support the previous internal function with the same behaviour.
  617. */
  618. PyAPI_FUNC(const char *) PyUnicode_AsUTF8AndSize(
  619. PyObject *unicode,
  620. Py_ssize_t *size);
  621. #define _PyUnicode_AsStringAndSize PyUnicode_AsUTF8AndSize
  622. /* Returns a pointer to the default encoding (UTF-8) of the
  623. Unicode object unicode.
  624. Like PyUnicode_AsUTF8AndSize(), this also caches the UTF-8 representation
  625. in the unicodeobject.
  626. _PyUnicode_AsString is a #define for PyUnicode_AsUTF8 to
  627. support the previous internal function with the same behaviour.
  628. Use of this API is DEPRECATED since no size information can be
  629. extracted from the returned data.
  630. */
  631. PyAPI_FUNC(const char *) PyUnicode_AsUTF8(PyObject *unicode);
  632. #define _PyUnicode_AsString PyUnicode_AsUTF8
  633. /* --- Generic Codecs ----------------------------------------------------- */
  634. /* Encodes a Py_UNICODE buffer of the given size and returns a
  635. Python string object. */
  636. Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_Encode(
  637. const Py_UNICODE *s, /* Unicode char buffer */
  638. Py_ssize_t size, /* number of Py_UNICODE chars to encode */
  639. const char *encoding, /* encoding */
  640. const char *errors /* error handling */
  641. );
  642. /* --- UTF-7 Codecs ------------------------------------------------------- */
  643. Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF7(
  644. const Py_UNICODE *data, /* Unicode char buffer */
  645. Py_ssize_t length, /* number of Py_UNICODE chars to encode */
  646. int base64SetO, /* Encode RFC2152 Set O characters in base64 */
  647. int base64WhiteSpace, /* Encode whitespace (sp, ht, nl, cr) in base64 */
  648. const char *errors /* error handling */
  649. );
  650. PyAPI_FUNC(PyObject*) _PyUnicode_EncodeUTF7(
  651. PyObject *unicode, /* Unicode object */
  652. int base64SetO, /* Encode RFC2152 Set O characters in base64 */
  653. int base64WhiteSpace, /* Encode whitespace (sp, ht, nl, cr) in base64 */
  654. const char *errors /* error handling */
  655. );
  656. /* --- UTF-8 Codecs ------------------------------------------------------- */
  657. PyAPI_FUNC(PyObject*) _PyUnicode_AsUTF8String(
  658. PyObject *unicode,
  659. const char *errors);
  660. Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF8(
  661. const Py_UNICODE *data, /* Unicode char buffer */
  662. Py_ssize_t length, /* number of Py_UNICODE chars to encode */
  663. const char *errors /* error handling */
  664. );
  665. /* --- UTF-32 Codecs ------------------------------------------------------ */
  666. Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF32(
  667. const Py_UNICODE *data, /* Unicode char buffer */
  668. Py_ssize_t length, /* number of Py_UNICODE chars to encode */
  669. const char *errors, /* error handling */
  670. int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
  671. );
  672. PyAPI_FUNC(PyObject*) _PyUnicode_EncodeUTF32(
  673. PyObject *object, /* Unicode object */
  674. const char *errors, /* error handling */
  675. int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
  676. );
  677. /* --- UTF-16 Codecs ------------------------------------------------------ */
  678. /* Returns a Python string object holding the UTF-16 encoded value of
  679. the Unicode data.
  680. If byteorder is not 0, output is written according to the following
  681. byte order:
  682. byteorder == -1: little endian
  683. byteorder == 0: native byte order (writes a BOM mark)
  684. byteorder == 1: big endian
  685. If byteorder is 0, the output string will always start with the
  686. Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark is
  687. prepended.
  688. Note that Py_UNICODE data is being interpreted as UTF-16 reduced to
  689. UCS-2. This trick makes it possible to add full UTF-16 capabilities
  690. at a later point without compromising the APIs.
  691. */
  692. Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF16(
  693. const Py_UNICODE *data, /* Unicode char buffer */
  694. Py_ssize_t length, /* number of Py_UNICODE chars to encode */
  695. const char *errors, /* error handling */
  696. int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
  697. );
  698. PyAPI_FUNC(PyObject*) _PyUnicode_EncodeUTF16(
  699. PyObject* unicode, /* Unicode object */
  700. const char *errors, /* error handling */
  701. int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
  702. );
  703. /* --- Unicode-Escape Codecs ---------------------------------------------- */
  704. /* Variant of PyUnicode_DecodeUnicodeEscape that supports partial decoding. */
  705. PyAPI_FUNC(PyObject*) _PyUnicode_DecodeUnicodeEscapeStateful(
  706. const char *string, /* Unicode-Escape encoded string */
  707. Py_ssize_t length, /* size of string */
  708. const char *errors, /* error handling */
  709. Py_ssize_t *consumed /* bytes consumed */
  710. );
  711. /* Helper for PyUnicode_DecodeUnicodeEscape that detects invalid escape
  712. chars. */
  713. PyAPI_FUNC(PyObject*) _PyUnicode_DecodeUnicodeEscapeInternal(
  714. const char *string, /* Unicode-Escape encoded string */
  715. Py_ssize_t length, /* size of string */
  716. const char *errors, /* error handling */
  717. Py_ssize_t *consumed, /* bytes consumed */
  718. const char **first_invalid_escape /* on return, points to first
  719. invalid escaped char in
  720. string. */
  721. );
  722. /*
  723. * START Anaconda (tkoch):
  724. *
  725. * For compatibility with packages like typed_ast and mypy compiled against
  726. * Python 3.9.7.
  727. */
  728. PyAPI_FUNC(PyObject*) _PyUnicode_DecodeUnicodeEscape(
  729. const char *string, /* Unicode-Escape encoded string */
  730. Py_ssize_t length, /* size of string */
  731. const char *errors, /* error handling */
  732. const char **first_invalid_escape /* on return, points to first
  733. invalid escaped char in
  734. string. */
  735. );
  736. /* Anaconda END */
  737. Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_EncodeUnicodeEscape(
  738. const Py_UNICODE *data, /* Unicode char buffer */
  739. Py_ssize_t length /* Number of Py_UNICODE chars to encode */
  740. );
  741. /* --- Raw-Unicode-Escape Codecs ------------------------------------------ */
  742. Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_EncodeRawUnicodeEscape(
  743. const Py_UNICODE *data, /* Unicode char buffer */
  744. Py_ssize_t length /* Number of Py_UNICODE chars to encode */
  745. );
  746. /* Variant of PyUnicode_DecodeRawUnicodeEscape that supports partial decoding. */
  747. PyAPI_FUNC(PyObject*) _PyUnicode_DecodeRawUnicodeEscapeStateful(
  748. const char *string, /* Unicode-Escape encoded string */
  749. Py_ssize_t length, /* size of string */
  750. const char *errors, /* error handling */
  751. Py_ssize_t *consumed /* bytes consumed */
  752. );
  753. /* --- Latin-1 Codecs ----------------------------------------------------- */
  754. PyAPI_FUNC(PyObject*) _PyUnicode_AsLatin1String(
  755. PyObject* unicode,
  756. const char* errors);
  757. Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_EncodeLatin1(
  758. const Py_UNICODE *data, /* Unicode char buffer */
  759. Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
  760. const char *errors /* error handling */
  761. );
  762. /* --- ASCII Codecs ------------------------------------------------------- */
  763. PyAPI_FUNC(PyObject*) _PyUnicode_AsASCIIString(
  764. PyObject* unicode,
  765. const char* errors);
  766. Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_EncodeASCII(
  767. const Py_UNICODE *data, /* Unicode char buffer */
  768. Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
  769. const char *errors /* error handling */
  770. );
  771. /* --- Character Map Codecs ----------------------------------------------- */
  772. Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_EncodeCharmap(
  773. const Py_UNICODE *data, /* Unicode char buffer */
  774. Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
  775. PyObject *mapping, /* encoding mapping */
  776. const char *errors /* error handling */
  777. );
  778. PyAPI_FUNC(PyObject*) _PyUnicode_EncodeCharmap(
  779. PyObject *unicode, /* Unicode object */
  780. PyObject *mapping, /* encoding mapping */
  781. const char *errors /* error handling */
  782. );
  783. /* Translate a Py_UNICODE buffer of the given length by applying a
  784. character mapping table to it and return the resulting Unicode
  785. object.
  786. The mapping table must map Unicode ordinal integers to Unicode strings,
  787. Unicode ordinal integers or None (causing deletion of the character).
  788. Mapping tables may be dictionaries or sequences. Unmapped character
  789. ordinals (ones which cause a LookupError) are left untouched and
  790. are copied as-is.
  791. */
  792. Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject *) PyUnicode_TranslateCharmap(
  793. const Py_UNICODE *data, /* Unicode char buffer */
  794. Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
  795. PyObject *table, /* Translate table */
  796. const char *errors /* error handling */
  797. );
  798. /* --- MBCS codecs for Windows -------------------------------------------- */
  799. #ifdef MS_WINDOWS
  800. Py_DEPRECATED(3.3) PyAPI_FUNC(PyObject*) PyUnicode_EncodeMBCS(
  801. const Py_UNICODE *data, /* Unicode char buffer */
  802. Py_ssize_t length, /* number of Py_UNICODE chars to encode */
  803. const char *errors /* error handling */
  804. );
  805. #endif
  806. /* --- Decimal Encoder ---------------------------------------------------- */
  807. /* Takes a Unicode string holding a decimal value and writes it into
  808. an output buffer using standard ASCII digit codes.
  809. The output buffer has to provide at least length+1 bytes of storage
  810. area. The output string is 0-terminated.
  811. The encoder converts whitespace to ' ', decimal characters to their
  812. corresponding ASCII digit and all other Latin-1 characters except
  813. \0 as-is. Characters outside this range (Unicode ordinals 1-256)
  814. are treated as errors. This includes embedded NULL bytes.
  815. Error handling is defined by the errors argument:
  816. NULL or "strict": raise a ValueError
  817. "ignore": ignore the wrong characters (these are not copied to the
  818. output buffer)
  819. "replace": replaces illegal characters with '?'
  820. Returns 0 on success, -1 on failure.
  821. */
  822. Py_DEPRECATED(3.3) PyAPI_FUNC(int) PyUnicode_EncodeDecimal(
  823. Py_UNICODE *s, /* Unicode buffer */
  824. Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
  825. char *output, /* Output buffer; must have size >= length */
  826. const char *errors /* error handling */
  827. );
  828. /* Transforms code points that have decimal digit property to the
  829. corresponding ASCII digit code points.
  830. Returns a new Unicode string on success, NULL on failure.
  831. */
  832. Py_DEPRECATED(3.3)
  833. PyAPI_FUNC(PyObject*) PyUnicode_TransformDecimalToASCII(
  834. Py_UNICODE *s, /* Unicode buffer */
  835. Py_ssize_t length /* Number of Py_UNICODE chars to transform */
  836. );
  837. /* Coverts a Unicode object holding a decimal value to an ASCII string
  838. for using in int, float and complex parsers.
  839. Transforms code points that have decimal digit property to the
  840. corresponding ASCII digit code points. Transforms spaces to ASCII.
  841. Transforms code points starting from the first non-ASCII code point that
  842. is neither a decimal digit nor a space to the end into '?'. */
  843. PyAPI_FUNC(PyObject*) _PyUnicode_TransformDecimalAndSpaceToASCII(
  844. PyObject *unicode /* Unicode object */
  845. );
  846. /* --- Methods & Slots ---------------------------------------------------- */
  847. PyAPI_FUNC(PyObject *) _PyUnicode_JoinArray(
  848. PyObject *separator,
  849. PyObject *const *items,
  850. Py_ssize_t seqlen
  851. );
  852. /* Test whether a unicode is equal to ASCII identifier. Return 1 if true,
  853. 0 otherwise. The right argument must be ASCII identifier.
  854. Any error occurs inside will be cleared before return. */
  855. PyAPI_FUNC(int) _PyUnicode_EqualToASCIIId(
  856. PyObject *left, /* Left string */
  857. _Py_Identifier *right /* Right identifier */
  858. );
  859. /* Test whether a unicode is equal to ASCII string. Return 1 if true,
  860. 0 otherwise. The right argument must be ASCII-encoded string.
  861. Any error occurs inside will be cleared before return. */
  862. PyAPI_FUNC(int) _PyUnicode_EqualToASCIIString(
  863. PyObject *left,
  864. const char *right /* ASCII-encoded string */
  865. );
  866. /* Externally visible for str.strip(unicode) */
  867. PyAPI_FUNC(PyObject *) _PyUnicode_XStrip(
  868. PyObject *self,
  869. int striptype,
  870. PyObject *sepobj
  871. );
  872. /* Using explicit passed-in values, insert the thousands grouping
  873. into the string pointed to by buffer. For the argument descriptions,
  874. see Objects/stringlib/localeutil.h */
  875. PyAPI_FUNC(Py_ssize_t) _PyUnicode_InsertThousandsGrouping(
  876. _PyUnicodeWriter *writer,
  877. Py_ssize_t n_buffer,
  878. PyObject *digits,
  879. Py_ssize_t d_pos,
  880. Py_ssize_t n_digits,
  881. Py_ssize_t min_width,
  882. const char *grouping,
  883. PyObject *thousands_sep,
  884. Py_UCS4 *maxchar);
  885. /* === Characters Type APIs =============================================== */
  886. /* Helper array used by Py_UNICODE_ISSPACE(). */
  887. PyAPI_DATA(const unsigned char) _Py_ascii_whitespace[];
  888. /* These should not be used directly. Use the Py_UNICODE_IS* and
  889. Py_UNICODE_TO* macros instead.
  890. These APIs are implemented in Objects/unicodectype.c.
  891. */
  892. PyAPI_FUNC(int) _PyUnicode_IsLowercase(
  893. Py_UCS4 ch /* Unicode character */
  894. );
  895. PyAPI_FUNC(int) _PyUnicode_IsUppercase(
  896. Py_UCS4 ch /* Unicode character */
  897. );
  898. PyAPI_FUNC(int) _PyUnicode_IsTitlecase(
  899. Py_UCS4 ch /* Unicode character */
  900. );
  901. PyAPI_FUNC(int) _PyUnicode_IsXidStart(
  902. Py_UCS4 ch /* Unicode character */
  903. );
  904. PyAPI_FUNC(int) _PyUnicode_IsXidContinue(
  905. Py_UCS4 ch /* Unicode character */
  906. );
  907. PyAPI_FUNC(int) _PyUnicode_IsWhitespace(
  908. const Py_UCS4 ch /* Unicode character */
  909. );
  910. PyAPI_FUNC(int) _PyUnicode_IsLinebreak(
  911. const Py_UCS4 ch /* Unicode character */
  912. );
  913. /* Py_DEPRECATED(3.3) */ PyAPI_FUNC(Py_UCS4) _PyUnicode_ToLowercase(
  914. Py_UCS4 ch /* Unicode character */
  915. );
  916. /* Py_DEPRECATED(3.3) */ PyAPI_FUNC(Py_UCS4) _PyUnicode_ToUppercase(
  917. Py_UCS4 ch /* Unicode character */
  918. );
  919. Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UCS4) _PyUnicode_ToTitlecase(
  920. Py_UCS4 ch /* Unicode character */
  921. );
  922. PyAPI_FUNC(int) _PyUnicode_ToLowerFull(
  923. Py_UCS4 ch, /* Unicode character */
  924. Py_UCS4 *res
  925. );
  926. PyAPI_FUNC(int) _PyUnicode_ToTitleFull(
  927. Py_UCS4 ch, /* Unicode character */
  928. Py_UCS4 *res
  929. );
  930. PyAPI_FUNC(int) _PyUnicode_ToUpperFull(
  931. Py_UCS4 ch, /* Unicode character */
  932. Py_UCS4 *res
  933. );
  934. PyAPI_FUNC(int) _PyUnicode_ToFoldedFull(
  935. Py_UCS4 ch, /* Unicode character */
  936. Py_UCS4 *res
  937. );
  938. PyAPI_FUNC(int) _PyUnicode_IsCaseIgnorable(
  939. Py_UCS4 ch /* Unicode character */
  940. );
  941. PyAPI_FUNC(int) _PyUnicode_IsCased(
  942. Py_UCS4 ch /* Unicode character */
  943. );
  944. PyAPI_FUNC(int) _PyUnicode_ToDecimalDigit(
  945. Py_UCS4 ch /* Unicode character */
  946. );
  947. PyAPI_FUNC(int) _PyUnicode_ToDigit(
  948. Py_UCS4 ch /* Unicode character */
  949. );
  950. PyAPI_FUNC(double) _PyUnicode_ToNumeric(
  951. Py_UCS4 ch /* Unicode character */
  952. );
  953. PyAPI_FUNC(int) _PyUnicode_IsDecimalDigit(
  954. Py_UCS4 ch /* Unicode character */
  955. );
  956. PyAPI_FUNC(int) _PyUnicode_IsDigit(
  957. Py_UCS4 ch /* Unicode character */
  958. );
  959. PyAPI_FUNC(int) _PyUnicode_IsNumeric(
  960. Py_UCS4 ch /* Unicode character */
  961. );
  962. PyAPI_FUNC(int) _PyUnicode_IsPrintable(
  963. Py_UCS4 ch /* Unicode character */
  964. );
  965. PyAPI_FUNC(int) _PyUnicode_IsAlpha(
  966. Py_UCS4 ch /* Unicode character */
  967. );
  968. Py_DEPRECATED(3.3) PyAPI_FUNC(size_t) Py_UNICODE_strlen(
  969. const Py_UNICODE *u
  970. );
  971. Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE*) Py_UNICODE_strcpy(
  972. Py_UNICODE *s1,
  973. const Py_UNICODE *s2);
  974. Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE*) Py_UNICODE_strcat(
  975. Py_UNICODE *s1, const Py_UNICODE *s2);
  976. Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE*) Py_UNICODE_strncpy(
  977. Py_UNICODE *s1,
  978. const Py_UNICODE *s2,
  979. size_t n);
  980. Py_DEPRECATED(3.3) PyAPI_FUNC(int) Py_UNICODE_strcmp(
  981. const Py_UNICODE *s1,
  982. const Py_UNICODE *s2
  983. );
  984. Py_DEPRECATED(3.3) PyAPI_FUNC(int) Py_UNICODE_strncmp(
  985. const Py_UNICODE *s1,
  986. const Py_UNICODE *s2,
  987. size_t n
  988. );
  989. Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE*) Py_UNICODE_strchr(
  990. const Py_UNICODE *s,
  991. Py_UNICODE c
  992. );
  993. Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE*) Py_UNICODE_strrchr(
  994. const Py_UNICODE *s,
  995. Py_UNICODE c
  996. );
  997. PyAPI_FUNC(PyObject*) _PyUnicode_FormatLong(PyObject *, int, int, int);
  998. /* Create a copy of a unicode string ending with a nul character. Return NULL
  999. and raise a MemoryError exception on memory allocation failure, otherwise
  1000. return a new allocated buffer (use PyMem_Free() to free the buffer). */
  1001. Py_DEPRECATED(3.3) PyAPI_FUNC(Py_UNICODE*) PyUnicode_AsUnicodeCopy(
  1002. PyObject *unicode
  1003. );
  1004. /* Return an interned Unicode object for an Identifier; may fail if there is no memory.*/
  1005. PyAPI_FUNC(PyObject*) _PyUnicode_FromId(_Py_Identifier*);
  1006. /* Fast equality check when the inputs are known to be exact unicode types
  1007. and where the hash values are equal (i.e. a very probable match) */
  1008. PyAPI_FUNC(int) _PyUnicode_EQ(PyObject *, PyObject *);
  1009. PyAPI_FUNC(Py_ssize_t) _PyUnicode_ScanIdentifier(PyObject *);
  1010. #ifdef __cplusplus
  1011. }
  1012. #endif