unicodeobject.h 45 KB

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