unicodeobject.h 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334
  1. #ifndef Py_UNICODEOBJECT_H
  2. #define Py_UNICODEOBJECT_H
  3. #include <stdarg.h>
  4. /*
  5. Unicode implementation based on original code by Fredrik Lundh,
  6. modified by Marc-Andre Lemburg (mal@lemburg.com) according to the
  7. Unicode Integration Proposal. (See
  8. http://www.egenix.com/files/python/unicode-proposal.txt).
  9. Copyright (c) Corporation for National Research Initiatives.
  10. Original header:
  11. --------------------------------------------------------------------
  12. * Yet another Unicode string type for Python. This type supports the
  13. * 16-bit Basic Multilingual Plane (BMP) only.
  14. *
  15. * Written by Fredrik Lundh, January 1999.
  16. *
  17. * Copyright (c) 1999 by Secret Labs AB.
  18. * Copyright (c) 1999 by Fredrik Lundh.
  19. *
  20. * fredrik@pythonware.com
  21. * http://www.pythonware.com
  22. *
  23. * --------------------------------------------------------------------
  24. * This Unicode String Type is
  25. *
  26. * Copyright (c) 1999 by Secret Labs AB
  27. * Copyright (c) 1999 by Fredrik Lundh
  28. *
  29. * By obtaining, using, and/or copying this software and/or its
  30. * associated documentation, you agree that you have read, understood,
  31. * and will comply with the following terms and conditions:
  32. *
  33. * Permission to use, copy, modify, and distribute this software and its
  34. * associated documentation for any purpose and without fee is hereby
  35. * granted, provided that the above copyright notice appears in all
  36. * copies, and that both that copyright notice and this permission notice
  37. * appear in supporting documentation, and that the name of Secret Labs
  38. * AB or the author not be used in advertising or publicity pertaining to
  39. * distribution of the software without specific, written prior
  40. * permission.
  41. *
  42. * SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO
  43. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  44. * FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR
  45. * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  46. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  47. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  48. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  49. * -------------------------------------------------------------------- */
  50. #include <ctype.h>
  51. /* === Internal API ======================================================= */
  52. /* --- Internal Unicode Format -------------------------------------------- */
  53. /* Python 3.x requires unicode */
  54. #define Py_USING_UNICODE
  55. #ifndef SIZEOF_WCHAR_T
  56. #error Must define SIZEOF_WCHAR_T
  57. #endif
  58. #define Py_UNICODE_SIZE SIZEOF_WCHAR_T
  59. /* If wchar_t can be used for UCS-4 storage, set Py_UNICODE_WIDE.
  60. Otherwise, Unicode strings are stored as UCS-2 (with limited support
  61. for UTF-16) */
  62. #if Py_UNICODE_SIZE >= 4
  63. #define Py_UNICODE_WIDE
  64. #endif
  65. /* Set these flags if the platform has "wchar.h" and the
  66. wchar_t type is a 16-bit unsigned type */
  67. /* #define HAVE_WCHAR_H */
  68. /* #define HAVE_USABLE_WCHAR_T */
  69. /* Py_UNICODE was the native Unicode storage format (code unit) used by
  70. Python and represents a single Unicode element in the Unicode type.
  71. With PEP 393, Py_UNICODE is deprecated and replaced with a
  72. typedef to wchar_t. */
  73. #ifndef Py_LIMITED_API
  74. #define PY_UNICODE_TYPE wchar_t
  75. typedef wchar_t Py_UNICODE /* Py_DEPRECATED(3.3) */;
  76. #endif
  77. /* If the compiler provides a wchar_t type we try to support it
  78. through the interface functions PyUnicode_FromWideChar(),
  79. PyUnicode_AsWideChar() and PyUnicode_AsWideCharString(). */
  80. #ifdef HAVE_USABLE_WCHAR_T
  81. # ifndef HAVE_WCHAR_H
  82. # define HAVE_WCHAR_H
  83. # endif
  84. #endif
  85. #ifdef HAVE_WCHAR_H
  86. # include <wchar.h>
  87. #endif
  88. /* Py_UCS4 and Py_UCS2 are typedefs for the respective
  89. unicode representations. */
  90. typedef uint32_t Py_UCS4;
  91. typedef uint16_t Py_UCS2;
  92. typedef uint8_t Py_UCS1;
  93. /* --- Internal Unicode Operations ---------------------------------------- */
  94. /* Since splitting on whitespace is an important use case, and
  95. whitespace in most situations is solely ASCII whitespace, we
  96. optimize for the common case by using a quick look-up table
  97. _Py_ascii_whitespace (see below) with an inlined check.
  98. */
  99. #ifndef Py_LIMITED_API
  100. #define Py_UNICODE_ISSPACE(ch) \
  101. ((ch) < 128U ? _Py_ascii_whitespace[(ch)] : _PyUnicode_IsWhitespace(ch))
  102. #define Py_UNICODE_ISLOWER(ch) _PyUnicode_IsLowercase(ch)
  103. #define Py_UNICODE_ISUPPER(ch) _PyUnicode_IsUppercase(ch)
  104. #define Py_UNICODE_ISTITLE(ch) _PyUnicode_IsTitlecase(ch)
  105. #define Py_UNICODE_ISLINEBREAK(ch) _PyUnicode_IsLinebreak(ch)
  106. #define Py_UNICODE_TOLOWER(ch) _PyUnicode_ToLowercase(ch)
  107. #define Py_UNICODE_TOUPPER(ch) _PyUnicode_ToUppercase(ch)
  108. #define Py_UNICODE_TOTITLE(ch) _PyUnicode_ToTitlecase(ch)
  109. #define Py_UNICODE_ISDECIMAL(ch) _PyUnicode_IsDecimalDigit(ch)
  110. #define Py_UNICODE_ISDIGIT(ch) _PyUnicode_IsDigit(ch)
  111. #define Py_UNICODE_ISNUMERIC(ch) _PyUnicode_IsNumeric(ch)
  112. #define Py_UNICODE_ISPRINTABLE(ch) _PyUnicode_IsPrintable(ch)
  113. #define Py_UNICODE_TODECIMAL(ch) _PyUnicode_ToDecimalDigit(ch)
  114. #define Py_UNICODE_TODIGIT(ch) _PyUnicode_ToDigit(ch)
  115. #define Py_UNICODE_TONUMERIC(ch) _PyUnicode_ToNumeric(ch)
  116. #define Py_UNICODE_ISALPHA(ch) _PyUnicode_IsAlpha(ch)
  117. #define Py_UNICODE_ISALNUM(ch) \
  118. (Py_UNICODE_ISALPHA(ch) || \
  119. Py_UNICODE_ISDECIMAL(ch) || \
  120. Py_UNICODE_ISDIGIT(ch) || \
  121. Py_UNICODE_ISNUMERIC(ch))
  122. #define Py_UNICODE_COPY(target, source, length) \
  123. memcpy((target), (source), (length)*sizeof(Py_UNICODE))
  124. #define Py_UNICODE_FILL(target, value, length) \
  125. do {Py_ssize_t i_; Py_UNICODE *t_ = (target); Py_UNICODE v_ = (value);\
  126. for (i_ = 0; i_ < (length); i_++) t_[i_] = v_;\
  127. } while (0)
  128. /* macros to work with surrogates */
  129. #define Py_UNICODE_IS_SURROGATE(ch) (0xD800 <= (ch) && (ch) <= 0xDFFF)
  130. #define Py_UNICODE_IS_HIGH_SURROGATE(ch) (0xD800 <= (ch) && (ch) <= 0xDBFF)
  131. #define Py_UNICODE_IS_LOW_SURROGATE(ch) (0xDC00 <= (ch) && (ch) <= 0xDFFF)
  132. /* Join two surrogate characters and return a single Py_UCS4 value. */
  133. #define Py_UNICODE_JOIN_SURROGATES(high, low) \
  134. (((((Py_UCS4)(high) & 0x03FF) << 10) | \
  135. ((Py_UCS4)(low) & 0x03FF)) + 0x10000)
  136. /* high surrogate = top 10 bits added to D800 */
  137. #define Py_UNICODE_HIGH_SURROGATE(ch) (0xD800 - (0x10000 >> 10) + ((ch) >> 10))
  138. /* low surrogate = bottom 10 bits added to DC00 */
  139. #define Py_UNICODE_LOW_SURROGATE(ch) (0xDC00 + ((ch) & 0x3FF))
  140. /* Check if substring matches at given offset. The offset must be
  141. valid, and the substring must not be empty. */
  142. #define Py_UNICODE_MATCH(string, offset, substring) \
  143. ((*((string)->wstr + (offset)) == *((substring)->wstr)) && \
  144. ((*((string)->wstr + (offset) + (substring)->wstr_length-1) == *((substring)->wstr + (substring)->wstr_length-1))) && \
  145. !memcmp((string)->wstr + (offset), (substring)->wstr, (substring)->wstr_length*sizeof(Py_UNICODE)))
  146. #endif /* Py_LIMITED_API */
  147. #ifdef __cplusplus
  148. extern "C" {
  149. #endif
  150. /* --- Unicode Type ------------------------------------------------------- */
  151. #ifndef Py_LIMITED_API
  152. /* ASCII-only strings created through PyUnicode_New use the PyASCIIObject
  153. structure. state.ascii and state.compact are set, and the data
  154. immediately follow the structure. utf8_length and wstr_length can be found
  155. in the length field; the utf8 pointer is equal to the data pointer. */
  156. typedef struct {
  157. /* There are 4 forms of Unicode strings:
  158. - compact ascii:
  159. * structure = PyASCIIObject
  160. * test: PyUnicode_IS_COMPACT_ASCII(op)
  161. * kind = PyUnicode_1BYTE_KIND
  162. * compact = 1
  163. * ascii = 1
  164. * ready = 1
  165. * (length is the length of the utf8 and wstr strings)
  166. * (data starts just after the structure)
  167. * (since ASCII is decoded from UTF-8, the utf8 string are the data)
  168. - compact:
  169. * structure = PyCompactUnicodeObject
  170. * test: PyUnicode_IS_COMPACT(op) && !PyUnicode_IS_ASCII(op)
  171. * kind = PyUnicode_1BYTE_KIND, PyUnicode_2BYTE_KIND or
  172. PyUnicode_4BYTE_KIND
  173. * compact = 1
  174. * ready = 1
  175. * ascii = 0
  176. * utf8 is not shared with data
  177. * utf8_length = 0 if utf8 is NULL
  178. * wstr is shared with data and wstr_length=length
  179. if kind=PyUnicode_2BYTE_KIND and sizeof(wchar_t)=2
  180. or if kind=PyUnicode_4BYTE_KIND and sizeof(wchar_t)=4
  181. * wstr_length = 0 if wstr is NULL
  182. * (data starts just after the structure)
  183. - legacy string, not ready:
  184. * structure = PyUnicodeObject
  185. * test: kind == PyUnicode_WCHAR_KIND
  186. * length = 0 (use wstr_length)
  187. * hash = -1
  188. * kind = PyUnicode_WCHAR_KIND
  189. * compact = 0
  190. * ascii = 0
  191. * ready = 0
  192. * interned = SSTATE_NOT_INTERNED
  193. * wstr is not NULL
  194. * data.any is NULL
  195. * utf8 is NULL
  196. * utf8_length = 0
  197. - legacy string, ready:
  198. * structure = PyUnicodeObject structure
  199. * test: !PyUnicode_IS_COMPACT(op) && kind != PyUnicode_WCHAR_KIND
  200. * kind = PyUnicode_1BYTE_KIND, PyUnicode_2BYTE_KIND or
  201. PyUnicode_4BYTE_KIND
  202. * compact = 0
  203. * ready = 1
  204. * data.any is not NULL
  205. * utf8 is shared and utf8_length = length with data.any if ascii = 1
  206. * utf8_length = 0 if utf8 is NULL
  207. * wstr is shared with data.any and wstr_length = length
  208. if kind=PyUnicode_2BYTE_KIND and sizeof(wchar_t)=2
  209. or if kind=PyUnicode_4BYTE_KIND and sizeof(wchar_4)=4
  210. * wstr_length = 0 if wstr is NULL
  211. Compact strings use only one memory block (structure + characters),
  212. whereas legacy strings use one block for the structure and one block
  213. for characters.
  214. Legacy strings are created by PyUnicode_FromUnicode() and
  215. PyUnicode_FromStringAndSize(NULL, size) functions. They become ready
  216. when PyUnicode_READY() is called.
  217. See also _PyUnicode_CheckConsistency().
  218. */
  219. PyObject_HEAD
  220. Py_ssize_t length; /* Number of code points in the string */
  221. Py_hash_t hash; /* Hash value; -1 if not set */
  222. struct {
  223. /*
  224. SSTATE_NOT_INTERNED (0)
  225. SSTATE_INTERNED_MORTAL (1)
  226. SSTATE_INTERNED_IMMORTAL (2)
  227. If interned != SSTATE_NOT_INTERNED, the two references from the
  228. dictionary to this object are *not* counted in ob_refcnt.
  229. */
  230. unsigned int interned:2;
  231. /* Character size:
  232. - PyUnicode_WCHAR_KIND (0):
  233. * character type = wchar_t (16 or 32 bits, depending on the
  234. platform)
  235. - PyUnicode_1BYTE_KIND (1):
  236. * character type = Py_UCS1 (8 bits, unsigned)
  237. * all characters are in the range U+0000-U+00FF (latin1)
  238. * if ascii is set, all characters are in the range U+0000-U+007F
  239. (ASCII), otherwise at least one character is in the range
  240. U+0080-U+00FF
  241. - PyUnicode_2BYTE_KIND (2):
  242. * character type = Py_UCS2 (16 bits, unsigned)
  243. * all characters are in the range U+0000-U+FFFF (BMP)
  244. * at least one character is in the range U+0100-U+FFFF
  245. - PyUnicode_4BYTE_KIND (4):
  246. * character type = Py_UCS4 (32 bits, unsigned)
  247. * all characters are in the range U+0000-U+10FFFF
  248. * at least one character is in the range U+10000-U+10FFFF
  249. */
  250. unsigned int kind:3;
  251. /* Compact is with respect to the allocation scheme. Compact unicode
  252. objects only require one memory block while non-compact objects use
  253. one block for the PyUnicodeObject struct and another for its data
  254. buffer. */
  255. unsigned int compact:1;
  256. /* The string only contains characters in the range U+0000-U+007F (ASCII)
  257. and the kind is PyUnicode_1BYTE_KIND. If ascii is set and compact is
  258. set, use the PyASCIIObject structure. */
  259. unsigned int ascii:1;
  260. /* The ready flag indicates whether the object layout is initialized
  261. completely. This means that this is either a compact object, or
  262. the data pointer is filled out. The bit is redundant, and helps
  263. to minimize the test in PyUnicode_IS_READY(). */
  264. unsigned int ready:1;
  265. /* Padding to ensure that PyUnicode_DATA() is always aligned to
  266. 4 bytes (see issue #19537 on m68k). */
  267. unsigned int :24;
  268. } state;
  269. wchar_t *wstr; /* wchar_t representation (null-terminated) */
  270. } PyASCIIObject;
  271. /* Non-ASCII strings allocated through PyUnicode_New use the
  272. PyCompactUnicodeObject structure. state.compact is set, and the data
  273. immediately follow the structure. */
  274. typedef struct {
  275. PyASCIIObject _base;
  276. Py_ssize_t utf8_length; /* Number of bytes in utf8, excluding the
  277. * terminating \0. */
  278. char *utf8; /* UTF-8 representation (null-terminated) */
  279. Py_ssize_t wstr_length; /* Number of code points in wstr, possible
  280. * surrogates count as two code points. */
  281. } PyCompactUnicodeObject;
  282. /* Strings allocated through PyUnicode_FromUnicode(NULL, len) use the
  283. PyUnicodeObject structure. The actual string data is initially in the wstr
  284. block, and copied into the data block using _PyUnicode_Ready. */
  285. typedef struct {
  286. PyCompactUnicodeObject _base;
  287. union {
  288. void *any;
  289. Py_UCS1 *latin1;
  290. Py_UCS2 *ucs2;
  291. Py_UCS4 *ucs4;
  292. } data; /* Canonical, smallest-form Unicode buffer */
  293. } PyUnicodeObject;
  294. #endif
  295. PyAPI_DATA(PyTypeObject) PyUnicode_Type;
  296. PyAPI_DATA(PyTypeObject) PyUnicodeIter_Type;
  297. #define PyUnicode_Check(op) \
  298. PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_UNICODE_SUBCLASS)
  299. #define PyUnicode_CheckExact(op) (Py_TYPE(op) == &PyUnicode_Type)
  300. /* Fast access macros */
  301. #ifndef Py_LIMITED_API
  302. #define PyUnicode_WSTR_LENGTH(op) \
  303. (PyUnicode_IS_COMPACT_ASCII(op) ? \
  304. ((PyASCIIObject*)op)->length : \
  305. ((PyCompactUnicodeObject*)op)->wstr_length)
  306. /* Returns the deprecated Py_UNICODE representation's size in code units
  307. (this includes surrogate pairs as 2 units).
  308. If the Py_UNICODE representation is not available, it will be computed
  309. on request. Use PyUnicode_GET_LENGTH() for the length in code points. */
  310. #define PyUnicode_GET_SIZE(op) \
  311. (assert(PyUnicode_Check(op)), \
  312. (((PyASCIIObject *)(op))->wstr) ? \
  313. PyUnicode_WSTR_LENGTH(op) : \
  314. ((void)PyUnicode_AsUnicode((PyObject *)(op)), \
  315. assert(((PyASCIIObject *)(op))->wstr), \
  316. PyUnicode_WSTR_LENGTH(op)))
  317. /* Py_DEPRECATED(3.3) */
  318. #define PyUnicode_GET_DATA_SIZE(op) \
  319. (PyUnicode_GET_SIZE(op) * Py_UNICODE_SIZE)
  320. /* Py_DEPRECATED(3.3) */
  321. /* Alias for PyUnicode_AsUnicode(). This will create a wchar_t/Py_UNICODE
  322. representation on demand. Using this macro is very inefficient now,
  323. try to port your code to use the new PyUnicode_*BYTE_DATA() macros or
  324. use PyUnicode_WRITE() and PyUnicode_READ(). */
  325. #define PyUnicode_AS_UNICODE(op) \
  326. (assert(PyUnicode_Check(op)), \
  327. (((PyASCIIObject *)(op))->wstr) ? (((PyASCIIObject *)(op))->wstr) : \
  328. PyUnicode_AsUnicode((PyObject *)(op)))
  329. /* Py_DEPRECATED(3.3) */
  330. #define PyUnicode_AS_DATA(op) \
  331. ((const char *)(PyUnicode_AS_UNICODE(op)))
  332. /* Py_DEPRECATED(3.3) */
  333. /* --- Flexible String Representation Helper Macros (PEP 393) -------------- */
  334. /* Values for PyASCIIObject.state: */
  335. /* Interning state. */
  336. #define SSTATE_NOT_INTERNED 0
  337. #define SSTATE_INTERNED_MORTAL 1
  338. #define SSTATE_INTERNED_IMMORTAL 2
  339. /* Return true if the string contains only ASCII characters, or 0 if not. The
  340. string may be compact (PyUnicode_IS_COMPACT_ASCII) or not, but must be
  341. ready. */
  342. #define PyUnicode_IS_ASCII(op) \
  343. (assert(PyUnicode_Check(op)), \
  344. assert(PyUnicode_IS_READY(op)), \
  345. ((PyASCIIObject*)op)->state.ascii)
  346. /* Return true if the string is compact or 0 if not.
  347. No type checks or Ready calls are performed. */
  348. #define PyUnicode_IS_COMPACT(op) \
  349. (((PyASCIIObject*)(op))->state.compact)
  350. /* Return true if the string is a compact ASCII string (use PyASCIIObject
  351. structure), or 0 if not. No type checks or Ready calls are performed. */
  352. #define PyUnicode_IS_COMPACT_ASCII(op) \
  353. (((PyASCIIObject*)op)->state.ascii && PyUnicode_IS_COMPACT(op))
  354. enum PyUnicode_Kind {
  355. /* String contains only wstr byte characters. This is only possible
  356. when the string was created with a legacy API and _PyUnicode_Ready()
  357. has not been called yet. */
  358. PyUnicode_WCHAR_KIND = 0,
  359. /* Return values of the PyUnicode_KIND() macro: */
  360. PyUnicode_1BYTE_KIND = 1,
  361. PyUnicode_2BYTE_KIND = 2,
  362. PyUnicode_4BYTE_KIND = 4
  363. };
  364. /* Return pointers to the canonical representation cast to unsigned char,
  365. Py_UCS2, or Py_UCS4 for direct character access.
  366. No checks are performed, use PyUnicode_KIND() before to ensure
  367. these will work correctly. */
  368. #define PyUnicode_1BYTE_DATA(op) ((Py_UCS1*)PyUnicode_DATA(op))
  369. #define PyUnicode_2BYTE_DATA(op) ((Py_UCS2*)PyUnicode_DATA(op))
  370. #define PyUnicode_4BYTE_DATA(op) ((Py_UCS4*)PyUnicode_DATA(op))
  371. /* Return one of the PyUnicode_*_KIND values defined above. */
  372. #define PyUnicode_KIND(op) \
  373. (assert(PyUnicode_Check(op)), \
  374. assert(PyUnicode_IS_READY(op)), \
  375. ((PyASCIIObject *)(op))->state.kind)
  376. /* Return a void pointer to the raw unicode buffer. */
  377. #define _PyUnicode_COMPACT_DATA(op) \
  378. (PyUnicode_IS_ASCII(op) ? \
  379. ((void*)((PyASCIIObject*)(op) + 1)) : \
  380. ((void*)((PyCompactUnicodeObject*)(op) + 1)))
  381. #define _PyUnicode_NONCOMPACT_DATA(op) \
  382. (assert(((PyUnicodeObject*)(op))->data.any), \
  383. ((((PyUnicodeObject *)(op))->data.any)))
  384. #define PyUnicode_DATA(op) \
  385. (assert(PyUnicode_Check(op)), \
  386. PyUnicode_IS_COMPACT(op) ? _PyUnicode_COMPACT_DATA(op) : \
  387. _PyUnicode_NONCOMPACT_DATA(op))
  388. /* In the access macros below, "kind" may be evaluated more than once.
  389. All other macro parameters are evaluated exactly once, so it is safe
  390. to put side effects into them (such as increasing the index). */
  391. /* Write into the canonical representation, this macro does not do any sanity
  392. checks and is intended for usage in loops. The caller should cache the
  393. kind and data pointers obtained from other macro calls.
  394. index is the index in the string (starts at 0) and value is the new
  395. code point value which should be written to that location. */
  396. #define PyUnicode_WRITE(kind, data, index, value) \
  397. do { \
  398. switch ((kind)) { \
  399. case PyUnicode_1BYTE_KIND: { \
  400. ((Py_UCS1 *)(data))[(index)] = (Py_UCS1)(value); \
  401. break; \
  402. } \
  403. case PyUnicode_2BYTE_KIND: { \
  404. ((Py_UCS2 *)(data))[(index)] = (Py_UCS2)(value); \
  405. break; \
  406. } \
  407. default: { \
  408. assert((kind) == PyUnicode_4BYTE_KIND); \
  409. ((Py_UCS4 *)(data))[(index)] = (Py_UCS4)(value); \
  410. } \
  411. } \
  412. } while (0)
  413. /* Read a code point from the string's canonical representation. No checks
  414. or ready calls are performed. */
  415. #define PyUnicode_READ(kind, data, index) \
  416. ((Py_UCS4) \
  417. ((kind) == PyUnicode_1BYTE_KIND ? \
  418. ((const Py_UCS1 *)(data))[(index)] : \
  419. ((kind) == PyUnicode_2BYTE_KIND ? \
  420. ((const Py_UCS2 *)(data))[(index)] : \
  421. ((const Py_UCS4 *)(data))[(index)] \
  422. ) \
  423. ))
  424. /* PyUnicode_READ_CHAR() is less efficient than PyUnicode_READ() because it
  425. calls PyUnicode_KIND() and might call it twice. For single reads, use
  426. PyUnicode_READ_CHAR, for multiple consecutive reads callers should
  427. cache kind and use PyUnicode_READ instead. */
  428. #define PyUnicode_READ_CHAR(unicode, index) \
  429. (assert(PyUnicode_Check(unicode)), \
  430. assert(PyUnicode_IS_READY(unicode)), \
  431. (Py_UCS4) \
  432. (PyUnicode_KIND((unicode)) == PyUnicode_1BYTE_KIND ? \
  433. ((const Py_UCS1 *)(PyUnicode_DATA((unicode))))[(index)] : \
  434. (PyUnicode_KIND((unicode)) == PyUnicode_2BYTE_KIND ? \
  435. ((const Py_UCS2 *)(PyUnicode_DATA((unicode))))[(index)] : \
  436. ((const Py_UCS4 *)(PyUnicode_DATA((unicode))))[(index)] \
  437. ) \
  438. ))
  439. /* Returns the length of the unicode string. The caller has to make sure that
  440. the string has it's canonical representation set before calling
  441. this macro. Call PyUnicode_(FAST_)Ready to ensure that. */
  442. #define PyUnicode_GET_LENGTH(op) \
  443. (assert(PyUnicode_Check(op)), \
  444. assert(PyUnicode_IS_READY(op)), \
  445. ((PyASCIIObject *)(op))->length)
  446. /* Fast check to determine whether an object is ready. Equivalent to
  447. PyUnicode_IS_COMPACT(op) || ((PyUnicodeObject*)(op))->data.any) */
  448. #define PyUnicode_IS_READY(op) (((PyASCIIObject*)op)->state.ready)
  449. /* PyUnicode_READY() does less work than _PyUnicode_Ready() in the best
  450. case. If the canonical representation is not yet set, it will still call
  451. _PyUnicode_Ready().
  452. Returns 0 on success and -1 on errors. */
  453. #define PyUnicode_READY(op) \
  454. (assert(PyUnicode_Check(op)), \
  455. (PyUnicode_IS_READY(op) ? \
  456. 0 : _PyUnicode_Ready((PyObject *)(op))))
  457. /* Return a maximum character value which is suitable for creating another
  458. string based on op. This is always an approximation but more efficient
  459. than iterating over the string. */
  460. #define PyUnicode_MAX_CHAR_VALUE(op) \
  461. (assert(PyUnicode_IS_READY(op)), \
  462. (PyUnicode_IS_ASCII(op) ? \
  463. (0x7f) : \
  464. (PyUnicode_KIND(op) == PyUnicode_1BYTE_KIND ? \
  465. (0xffU) : \
  466. (PyUnicode_KIND(op) == PyUnicode_2BYTE_KIND ? \
  467. (0xffffU) : \
  468. (0x10ffffU)))))
  469. #endif
  470. /* --- Constants ---------------------------------------------------------- */
  471. /* This Unicode character will be used as replacement character during
  472. decoding if the errors argument is set to "replace". Note: the
  473. Unicode character U+FFFD is the official REPLACEMENT CHARACTER in
  474. Unicode 3.0. */
  475. #define Py_UNICODE_REPLACEMENT_CHARACTER ((Py_UCS4) 0xFFFD)
  476. /* === Public API ========================================================= */
  477. /* --- Plain Py_UNICODE --------------------------------------------------- */
  478. /* With PEP 393, this is the recommended way to allocate a new unicode object.
  479. This function will allocate the object and its buffer in a single memory
  480. block. Objects created using this function are not resizable. */
  481. #ifndef Py_LIMITED_API
  482. PyAPI_FUNC(PyObject*) PyUnicode_New(
  483. Py_ssize_t size, /* Number of code points in the new string */
  484. Py_UCS4 maxchar /* maximum code point value in the string */
  485. );
  486. #endif
  487. /* Initializes the canonical string representation from the deprecated
  488. wstr/Py_UNICODE representation. This function is used to convert Unicode
  489. objects which were created using the old API to the new flexible format
  490. introduced with PEP 393.
  491. Don't call this function directly, use the public PyUnicode_READY() macro
  492. instead. */
  493. #ifndef Py_LIMITED_API
  494. PyAPI_FUNC(int) _PyUnicode_Ready(
  495. PyObject *unicode /* Unicode object */
  496. );
  497. #endif
  498. /* Get a copy of a Unicode string. */
  499. #ifndef Py_LIMITED_API
  500. PyAPI_FUNC(PyObject*) _PyUnicode_Copy(
  501. PyObject *unicode
  502. );
  503. #endif
  504. /* Copy character from one unicode object into another, this function performs
  505. character conversion when necessary and falls back to memcpy() if possible.
  506. Fail if to is too small (smaller than *how_many* or smaller than
  507. len(from)-from_start), or if kind(from[from_start:from_start+how_many]) >
  508. kind(to), or if *to* has more than 1 reference.
  509. Return the number of written character, or return -1 and raise an exception
  510. on error.
  511. Pseudo-code:
  512. how_many = min(how_many, len(from) - from_start)
  513. to[to_start:to_start+how_many] = from[from_start:from_start+how_many]
  514. return how_many
  515. Note: The function doesn't write a terminating null character.
  516. */
  517. #ifndef Py_LIMITED_API
  518. PyAPI_FUNC(Py_ssize_t) PyUnicode_CopyCharacters(
  519. PyObject *to,
  520. Py_ssize_t to_start,
  521. PyObject *from,
  522. Py_ssize_t from_start,
  523. Py_ssize_t how_many
  524. );
  525. /* Unsafe version of PyUnicode_CopyCharacters(): don't check arguments and so
  526. may crash if parameters are invalid (e.g. if the output string
  527. is too short). */
  528. PyAPI_FUNC(void) _PyUnicode_FastCopyCharacters(
  529. PyObject *to,
  530. Py_ssize_t to_start,
  531. PyObject *from,
  532. Py_ssize_t from_start,
  533. Py_ssize_t how_many
  534. );
  535. #endif
  536. #ifndef Py_LIMITED_API
  537. /* Fill a string with a character: write fill_char into
  538. unicode[start:start+length].
  539. Fail if fill_char is bigger than the string maximum character, or if the
  540. string has more than 1 reference.
  541. Return the number of written character, or return -1 and raise an exception
  542. on error. */
  543. PyAPI_FUNC(Py_ssize_t) PyUnicode_Fill(
  544. PyObject *unicode,
  545. Py_ssize_t start,
  546. Py_ssize_t length,
  547. Py_UCS4 fill_char
  548. );
  549. /* Unsafe version of PyUnicode_Fill(): don't check arguments and so may crash
  550. if parameters are invalid (e.g. if length is longer than the string). */
  551. PyAPI_FUNC(void) _PyUnicode_FastFill(
  552. PyObject *unicode,
  553. Py_ssize_t start,
  554. Py_ssize_t length,
  555. Py_UCS4 fill_char
  556. );
  557. #endif
  558. /* Create a Unicode Object from the Py_UNICODE buffer u of the given
  559. size.
  560. u may be NULL which causes the contents to be undefined. It is the
  561. user's responsibility to fill in the needed data afterwards. Note
  562. that modifying the Unicode object contents after construction is
  563. only allowed if u was set to NULL.
  564. The buffer is copied into the new object. */
  565. #ifndef Py_LIMITED_API
  566. PyAPI_FUNC(PyObject*) PyUnicode_FromUnicode(
  567. const Py_UNICODE *u, /* Unicode buffer */
  568. Py_ssize_t size /* size of buffer */
  569. ) /* Py_DEPRECATED(3.3) */;
  570. #endif
  571. /* Similar to PyUnicode_FromUnicode(), but u points to UTF-8 encoded bytes */
  572. PyAPI_FUNC(PyObject*) PyUnicode_FromStringAndSize(
  573. const char *u, /* UTF-8 encoded string */
  574. Py_ssize_t size /* size of buffer */
  575. );
  576. /* Similar to PyUnicode_FromUnicode(), but u points to null-terminated
  577. UTF-8 encoded bytes. The size is determined with strlen(). */
  578. PyAPI_FUNC(PyObject*) PyUnicode_FromString(
  579. const char *u /* UTF-8 encoded string */
  580. );
  581. #ifndef Py_LIMITED_API
  582. /* Create a new string from a buffer of Py_UCS1, Py_UCS2 or Py_UCS4 characters.
  583. Scan the string to find the maximum character. */
  584. PyAPI_FUNC(PyObject*) PyUnicode_FromKindAndData(
  585. int kind,
  586. const void *buffer,
  587. Py_ssize_t size);
  588. /* Create a new string from a buffer of ASCII characters.
  589. WARNING: Don't check if the string contains any non-ASCII character. */
  590. PyAPI_FUNC(PyObject*) _PyUnicode_FromASCII(
  591. const char *buffer,
  592. Py_ssize_t size);
  593. #endif
  594. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
  595. PyAPI_FUNC(PyObject*) PyUnicode_Substring(
  596. PyObject *str,
  597. Py_ssize_t start,
  598. Py_ssize_t end);
  599. #endif
  600. #ifndef Py_LIMITED_API
  601. /* Compute the maximum character of the substring unicode[start:end].
  602. Return 127 for an empty string. */
  603. PyAPI_FUNC(Py_UCS4) _PyUnicode_FindMaxChar (
  604. PyObject *unicode,
  605. Py_ssize_t start,
  606. Py_ssize_t end);
  607. #endif
  608. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
  609. /* Copy the string into a UCS4 buffer including the null character if copy_null
  610. is set. Return NULL and raise an exception on error. Raise a SystemError if
  611. the buffer is smaller than the string. Return buffer on success.
  612. buflen is the length of the buffer in (Py_UCS4) characters. */
  613. PyAPI_FUNC(Py_UCS4*) PyUnicode_AsUCS4(
  614. PyObject *unicode,
  615. Py_UCS4* buffer,
  616. Py_ssize_t buflen,
  617. int copy_null);
  618. /* Copy the string into a UCS4 buffer. A new buffer is allocated using
  619. * PyMem_Malloc; if this fails, NULL is returned with a memory error
  620. exception set. */
  621. PyAPI_FUNC(Py_UCS4*) PyUnicode_AsUCS4Copy(PyObject *unicode);
  622. #endif
  623. #ifndef Py_LIMITED_API
  624. /* Return a read-only pointer to the Unicode object's internal
  625. Py_UNICODE buffer.
  626. If the wchar_t/Py_UNICODE representation is not yet available, this
  627. function will calculate it. */
  628. PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicode(
  629. PyObject *unicode /* Unicode object */
  630. ) /* Py_DEPRECATED(3.3) */;
  631. /* Similar to PyUnicode_AsUnicode(), but raises a ValueError if the string
  632. contains null characters. */
  633. PyAPI_FUNC(const Py_UNICODE *) _PyUnicode_AsUnicode(
  634. PyObject *unicode /* Unicode object */
  635. );
  636. /* Return a read-only pointer to the Unicode object's internal
  637. Py_UNICODE buffer and save the length at size.
  638. If the wchar_t/Py_UNICODE representation is not yet available, this
  639. function will calculate it. */
  640. PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicodeAndSize(
  641. PyObject *unicode, /* Unicode object */
  642. Py_ssize_t *size /* location where to save the length */
  643. ) /* Py_DEPRECATED(3.3) */;
  644. #endif
  645. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
  646. /* Get the length of the Unicode object. */
  647. PyAPI_FUNC(Py_ssize_t) PyUnicode_GetLength(
  648. PyObject *unicode
  649. );
  650. #endif
  651. /* Get the number of Py_UNICODE units in the
  652. string representation. */
  653. PyAPI_FUNC(Py_ssize_t) PyUnicode_GetSize(
  654. PyObject *unicode /* Unicode object */
  655. ) Py_DEPRECATED(3.3);
  656. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
  657. /* Read a character from the string. */
  658. PyAPI_FUNC(Py_UCS4) PyUnicode_ReadChar(
  659. PyObject *unicode,
  660. Py_ssize_t index
  661. );
  662. /* Write a character to the string. The string must have been created through
  663. PyUnicode_New, must not be shared, and must not have been hashed yet.
  664. Return 0 on success, -1 on error. */
  665. PyAPI_FUNC(int) PyUnicode_WriteChar(
  666. PyObject *unicode,
  667. Py_ssize_t index,
  668. Py_UCS4 character
  669. );
  670. #endif
  671. #ifndef Py_LIMITED_API
  672. /* Get the maximum ordinal for a Unicode character. */
  673. PyAPI_FUNC(Py_UNICODE) PyUnicode_GetMax(void) Py_DEPRECATED(3.3);
  674. #endif
  675. /* Resize a Unicode object. The length is the number of characters, except
  676. if the kind of the string is PyUnicode_WCHAR_KIND: in this case, the length
  677. is the number of Py_UNICODE characters.
  678. *unicode is modified to point to the new (resized) object and 0
  679. returned on success.
  680. Try to resize the string in place (which is usually faster than allocating
  681. a new string and copy characters), or create a new string.
  682. Error handling is implemented as follows: an exception is set, -1
  683. is returned and *unicode left untouched.
  684. WARNING: The function doesn't check string content, the result may not be a
  685. string in canonical representation. */
  686. PyAPI_FUNC(int) PyUnicode_Resize(
  687. PyObject **unicode, /* Pointer to the Unicode object */
  688. Py_ssize_t length /* New length */
  689. );
  690. /* Decode obj to a Unicode object.
  691. bytes, bytearray and other bytes-like objects are decoded according to the
  692. given encoding and error handler. The encoding and error handler can be
  693. NULL to have the interface use UTF-8 and "strict".
  694. All other objects (including Unicode objects) raise an exception.
  695. The API returns NULL in case of an error. The caller is responsible
  696. for decref'ing the returned objects.
  697. */
  698. PyAPI_FUNC(PyObject*) PyUnicode_FromEncodedObject(
  699. PyObject *obj, /* Object */
  700. const char *encoding, /* encoding */
  701. const char *errors /* error handling */
  702. );
  703. /* Copy an instance of a Unicode subtype to a new true Unicode object if
  704. necessary. If obj is already a true Unicode object (not a subtype), return
  705. the reference with *incremented* refcount.
  706. The API returns NULL in case of an error. The caller is responsible
  707. for decref'ing the returned objects.
  708. */
  709. PyAPI_FUNC(PyObject*) PyUnicode_FromObject(
  710. PyObject *obj /* Object */
  711. );
  712. PyAPI_FUNC(PyObject *) PyUnicode_FromFormatV(
  713. const char *format, /* ASCII-encoded string */
  714. va_list vargs
  715. );
  716. PyAPI_FUNC(PyObject *) PyUnicode_FromFormat(
  717. const char *format, /* ASCII-encoded string */
  718. ...
  719. );
  720. #ifndef Py_LIMITED_API
  721. typedef struct {
  722. PyObject *buffer;
  723. void *data;
  724. enum PyUnicode_Kind kind;
  725. Py_UCS4 maxchar;
  726. Py_ssize_t size;
  727. Py_ssize_t pos;
  728. /* minimum number of allocated characters (default: 0) */
  729. Py_ssize_t min_length;
  730. /* minimum character (default: 127, ASCII) */
  731. Py_UCS4 min_char;
  732. /* If non-zero, overallocate the buffer (default: 0). */
  733. unsigned char overallocate;
  734. /* If readonly is 1, buffer is a shared string (cannot be modified)
  735. and size is set to 0. */
  736. unsigned char readonly;
  737. } _PyUnicodeWriter ;
  738. /* Initialize a Unicode writer.
  739. *
  740. * By default, the minimum buffer size is 0 character and overallocation is
  741. * disabled. Set min_length, min_char and overallocate attributes to control
  742. * the allocation of the buffer. */
  743. PyAPI_FUNC(void)
  744. _PyUnicodeWriter_Init(_PyUnicodeWriter *writer);
  745. /* Prepare the buffer to write 'length' characters
  746. with the specified maximum character.
  747. Return 0 on success, raise an exception and return -1 on error. */
  748. #define _PyUnicodeWriter_Prepare(WRITER, LENGTH, MAXCHAR) \
  749. (((MAXCHAR) <= (WRITER)->maxchar \
  750. && (LENGTH) <= (WRITER)->size - (WRITER)->pos) \
  751. ? 0 \
  752. : (((LENGTH) == 0) \
  753. ? 0 \
  754. : _PyUnicodeWriter_PrepareInternal((WRITER), (LENGTH), (MAXCHAR))))
  755. /* Don't call this function directly, use the _PyUnicodeWriter_Prepare() macro
  756. instead. */
  757. PyAPI_FUNC(int)
  758. _PyUnicodeWriter_PrepareInternal(_PyUnicodeWriter *writer,
  759. Py_ssize_t length, Py_UCS4 maxchar);
  760. /* Prepare the buffer to have at least the kind KIND.
  761. For example, kind=PyUnicode_2BYTE_KIND ensures that the writer will
  762. support characters in range U+000-U+FFFF.
  763. Return 0 on success, raise an exception and return -1 on error. */
  764. #define _PyUnicodeWriter_PrepareKind(WRITER, KIND) \
  765. (assert((KIND) != PyUnicode_WCHAR_KIND), \
  766. (KIND) <= (WRITER)->kind \
  767. ? 0 \
  768. : _PyUnicodeWriter_PrepareKindInternal((WRITER), (KIND)))
  769. /* Don't call this function directly, use the _PyUnicodeWriter_PrepareKind()
  770. macro instead. */
  771. PyAPI_FUNC(int)
  772. _PyUnicodeWriter_PrepareKindInternal(_PyUnicodeWriter *writer,
  773. enum PyUnicode_Kind kind);
  774. /* Append a Unicode character.
  775. Return 0 on success, raise an exception and return -1 on error. */
  776. PyAPI_FUNC(int)
  777. _PyUnicodeWriter_WriteChar(_PyUnicodeWriter *writer,
  778. Py_UCS4 ch
  779. );
  780. /* Append a Unicode string.
  781. Return 0 on success, raise an exception and return -1 on error. */
  782. PyAPI_FUNC(int)
  783. _PyUnicodeWriter_WriteStr(_PyUnicodeWriter *writer,
  784. PyObject *str /* Unicode string */
  785. );
  786. /* Append a substring of a Unicode string.
  787. Return 0 on success, raise an exception and return -1 on error. */
  788. PyAPI_FUNC(int)
  789. _PyUnicodeWriter_WriteSubstring(_PyUnicodeWriter *writer,
  790. PyObject *str, /* Unicode string */
  791. Py_ssize_t start,
  792. Py_ssize_t end
  793. );
  794. /* Append an ASCII-encoded byte string.
  795. Return 0 on success, raise an exception and return -1 on error. */
  796. PyAPI_FUNC(int)
  797. _PyUnicodeWriter_WriteASCIIString(_PyUnicodeWriter *writer,
  798. const char *str, /* ASCII-encoded byte string */
  799. Py_ssize_t len /* number of bytes, or -1 if unknown */
  800. );
  801. /* Append a latin1-encoded byte string.
  802. Return 0 on success, raise an exception and return -1 on error. */
  803. PyAPI_FUNC(int)
  804. _PyUnicodeWriter_WriteLatin1String(_PyUnicodeWriter *writer,
  805. const char *str, /* latin1-encoded byte string */
  806. Py_ssize_t len /* length in bytes */
  807. );
  808. /* Get the value of the writer as a Unicode string. Clear the
  809. buffer of the writer. Raise an exception and return NULL
  810. on error. */
  811. PyAPI_FUNC(PyObject *)
  812. _PyUnicodeWriter_Finish(_PyUnicodeWriter *writer);
  813. /* Deallocate memory of a writer (clear its internal buffer). */
  814. PyAPI_FUNC(void)
  815. _PyUnicodeWriter_Dealloc(_PyUnicodeWriter *writer);
  816. #endif
  817. #ifndef Py_LIMITED_API
  818. /* Format the object based on the format_spec, as defined in PEP 3101
  819. (Advanced String Formatting). */
  820. PyAPI_FUNC(int) _PyUnicode_FormatAdvancedWriter(
  821. _PyUnicodeWriter *writer,
  822. PyObject *obj,
  823. PyObject *format_spec,
  824. Py_ssize_t start,
  825. Py_ssize_t end);
  826. #endif
  827. PyAPI_FUNC(void) PyUnicode_InternInPlace(PyObject **);
  828. PyAPI_FUNC(void) PyUnicode_InternImmortal(PyObject **);
  829. PyAPI_FUNC(PyObject *) PyUnicode_InternFromString(
  830. const char *u /* UTF-8 encoded string */
  831. );
  832. #ifndef Py_LIMITED_API
  833. PyAPI_FUNC(void) _Py_ReleaseInternedUnicodeStrings(void);
  834. #endif
  835. /* Use only if you know it's a string */
  836. #define PyUnicode_CHECK_INTERNED(op) \
  837. (((PyASCIIObject *)(op))->state.interned)
  838. /* --- wchar_t support for platforms which support it --------------------- */
  839. #ifdef HAVE_WCHAR_H
  840. /* Create a Unicode Object from the wchar_t buffer w of the given
  841. size.
  842. The buffer is copied into the new object. */
  843. PyAPI_FUNC(PyObject*) PyUnicode_FromWideChar(
  844. const wchar_t *w, /* wchar_t buffer */
  845. Py_ssize_t size /* size of buffer */
  846. );
  847. /* Copies the Unicode Object contents into the wchar_t buffer w. At
  848. most size wchar_t characters are copied.
  849. Note that the resulting wchar_t string may or may not be
  850. 0-terminated. It is the responsibility of the caller to make sure
  851. that the wchar_t string is 0-terminated in case this is required by
  852. the application.
  853. Returns the number of wchar_t characters copied (excluding a
  854. possibly trailing 0-termination character) or -1 in case of an
  855. error. */
  856. PyAPI_FUNC(Py_ssize_t) PyUnicode_AsWideChar(
  857. PyObject *unicode, /* Unicode object */
  858. wchar_t *w, /* wchar_t buffer */
  859. Py_ssize_t size /* size of buffer */
  860. );
  861. /* Convert the Unicode object to a wide character string. The output string
  862. always ends with a nul character. If size is not NULL, write the number of
  863. wide characters (excluding the null character) into *size.
  864. Returns a buffer allocated by PyMem_Malloc() (use PyMem_Free() to free it)
  865. on success. On error, returns NULL, *size is undefined and raises a
  866. MemoryError. */
  867. PyAPI_FUNC(wchar_t*) PyUnicode_AsWideCharString(
  868. PyObject *unicode, /* Unicode object */
  869. Py_ssize_t *size /* number of characters of the result */
  870. );
  871. #ifndef Py_LIMITED_API
  872. PyAPI_FUNC(void*) _PyUnicode_AsKind(PyObject *s, unsigned int kind);
  873. #endif
  874. #endif
  875. /* --- Unicode ordinals --------------------------------------------------- */
  876. /* Create a Unicode Object from the given Unicode code point ordinal.
  877. The ordinal must be in range(0x110000). A ValueError is
  878. raised in case it is not.
  879. */
  880. PyAPI_FUNC(PyObject*) PyUnicode_FromOrdinal(int ordinal);
  881. /* --- Free-list management ----------------------------------------------- */
  882. /* Clear the free list used by the Unicode implementation.
  883. This can be used to release memory used for objects on the free
  884. list back to the Python memory allocator.
  885. */
  886. PyAPI_FUNC(int) PyUnicode_ClearFreeList(void);
  887. /* === Builtin Codecs =====================================================
  888. Many of these APIs take two arguments encoding and errors. These
  889. parameters encoding and errors have the same semantics as the ones
  890. of the builtin str() API.
  891. Setting encoding to NULL causes the default encoding (UTF-8) to be used.
  892. Error handling is set by errors which may also be set to NULL
  893. meaning to use the default handling defined for the codec. Default
  894. error handling for all builtin codecs is "strict" (ValueErrors are
  895. raised).
  896. The codecs all use a similar interface. Only deviation from the
  897. generic ones are documented.
  898. */
  899. /* --- Manage the default encoding ---------------------------------------- */
  900. /* Returns a pointer to the default encoding (UTF-8) of the
  901. Unicode object unicode and the size of the encoded representation
  902. in bytes stored in *size.
  903. In case of an error, no *size is set.
  904. This function caches the UTF-8 encoded string in the unicodeobject
  905. and subsequent calls will return the same string. The memory is released
  906. when the unicodeobject is deallocated.
  907. _PyUnicode_AsStringAndSize is a #define for PyUnicode_AsUTF8AndSize to
  908. support the previous internal function with the same behaviour.
  909. *** This API is for interpreter INTERNAL USE ONLY and will likely
  910. *** be removed or changed in the future.
  911. *** If you need to access the Unicode object as UTF-8 bytes string,
  912. *** please use PyUnicode_AsUTF8String() instead.
  913. */
  914. #ifndef Py_LIMITED_API
  915. PyAPI_FUNC(const char *) PyUnicode_AsUTF8AndSize(
  916. PyObject *unicode,
  917. Py_ssize_t *size);
  918. #define _PyUnicode_AsStringAndSize PyUnicode_AsUTF8AndSize
  919. #endif
  920. /* Returns a pointer to the default encoding (UTF-8) of the
  921. Unicode object unicode.
  922. Like PyUnicode_AsUTF8AndSize(), this also caches the UTF-8 representation
  923. in the unicodeobject.
  924. _PyUnicode_AsString is a #define for PyUnicode_AsUTF8 to
  925. support the previous internal function with the same behaviour.
  926. Use of this API is DEPRECATED since no size information can be
  927. extracted from the returned data.
  928. *** This API is for interpreter INTERNAL USE ONLY and will likely
  929. *** be removed or changed for Python 3.1.
  930. *** If you need to access the Unicode object as UTF-8 bytes string,
  931. *** please use PyUnicode_AsUTF8String() instead.
  932. */
  933. #ifndef Py_LIMITED_API
  934. PyAPI_FUNC(const char *) PyUnicode_AsUTF8(PyObject *unicode);
  935. #define _PyUnicode_AsString PyUnicode_AsUTF8
  936. #endif
  937. /* Returns "utf-8". */
  938. PyAPI_FUNC(const char*) PyUnicode_GetDefaultEncoding(void);
  939. /* --- Generic Codecs ----------------------------------------------------- */
  940. /* Create a Unicode object by decoding the encoded string s of the
  941. given size. */
  942. PyAPI_FUNC(PyObject*) PyUnicode_Decode(
  943. const char *s, /* encoded string */
  944. Py_ssize_t size, /* size of buffer */
  945. const char *encoding, /* encoding */
  946. const char *errors /* error handling */
  947. );
  948. /* Decode a Unicode object unicode and return the result as Python
  949. object.
  950. This API is DEPRECATED. The only supported standard encoding is rot13.
  951. Use PyCodec_Decode() to decode with rot13 and non-standard codecs
  952. that decode from str. */
  953. PyAPI_FUNC(PyObject*) PyUnicode_AsDecodedObject(
  954. PyObject *unicode, /* Unicode object */
  955. const char *encoding, /* encoding */
  956. const char *errors /* error handling */
  957. ) Py_DEPRECATED(3.6);
  958. /* Decode a Unicode object unicode and return the result as Unicode
  959. object.
  960. This API is DEPRECATED. The only supported standard encoding is rot13.
  961. Use PyCodec_Decode() to decode with rot13 and non-standard codecs
  962. that decode from str to str. */
  963. PyAPI_FUNC(PyObject*) PyUnicode_AsDecodedUnicode(
  964. PyObject *unicode, /* Unicode object */
  965. const char *encoding, /* encoding */
  966. const char *errors /* error handling */
  967. ) Py_DEPRECATED(3.6);
  968. /* Encodes a Py_UNICODE buffer of the given size and returns a
  969. Python string object. */
  970. #ifndef Py_LIMITED_API
  971. PyAPI_FUNC(PyObject*) PyUnicode_Encode(
  972. const Py_UNICODE *s, /* Unicode char buffer */
  973. Py_ssize_t size, /* number of Py_UNICODE chars to encode */
  974. const char *encoding, /* encoding */
  975. const char *errors /* error handling */
  976. ) Py_DEPRECATED(3.3);
  977. #endif
  978. /* Encodes a Unicode object and returns the result as Python
  979. object.
  980. This API is DEPRECATED. It is superseded by PyUnicode_AsEncodedString()
  981. since all standard encodings (except rot13) encode str to bytes.
  982. Use PyCodec_Encode() for encoding with rot13 and non-standard codecs
  983. that encode form str to non-bytes. */
  984. PyAPI_FUNC(PyObject*) PyUnicode_AsEncodedObject(
  985. PyObject *unicode, /* Unicode object */
  986. const char *encoding, /* encoding */
  987. const char *errors /* error handling */
  988. ) Py_DEPRECATED(3.6);
  989. /* Encodes a Unicode object and returns the result as Python string
  990. object. */
  991. PyAPI_FUNC(PyObject*) PyUnicode_AsEncodedString(
  992. PyObject *unicode, /* Unicode object */
  993. const char *encoding, /* encoding */
  994. const char *errors /* error handling */
  995. );
  996. /* Encodes a Unicode object and returns the result as Unicode
  997. object.
  998. This API is DEPRECATED. The only supported standard encodings is rot13.
  999. Use PyCodec_Encode() to encode with rot13 and non-standard codecs
  1000. that encode from str to str. */
  1001. PyAPI_FUNC(PyObject*) PyUnicode_AsEncodedUnicode(
  1002. PyObject *unicode, /* Unicode object */
  1003. const char *encoding, /* encoding */
  1004. const char *errors /* error handling */
  1005. ) Py_DEPRECATED(3.6);
  1006. /* Build an encoding map. */
  1007. PyAPI_FUNC(PyObject*) PyUnicode_BuildEncodingMap(
  1008. PyObject* string /* 256 character map */
  1009. );
  1010. /* --- UTF-7 Codecs ------------------------------------------------------- */
  1011. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF7(
  1012. const char *string, /* UTF-7 encoded string */
  1013. Py_ssize_t length, /* size of string */
  1014. const char *errors /* error handling */
  1015. );
  1016. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF7Stateful(
  1017. const char *string, /* UTF-7 encoded string */
  1018. Py_ssize_t length, /* size of string */
  1019. const char *errors, /* error handling */
  1020. Py_ssize_t *consumed /* bytes consumed */
  1021. );
  1022. #ifndef Py_LIMITED_API
  1023. PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF7(
  1024. const Py_UNICODE *data, /* Unicode char buffer */
  1025. Py_ssize_t length, /* number of Py_UNICODE chars to encode */
  1026. int base64SetO, /* Encode RFC2152 Set O characters in base64 */
  1027. int base64WhiteSpace, /* Encode whitespace (sp, ht, nl, cr) in base64 */
  1028. const char *errors /* error handling */
  1029. ) Py_DEPRECATED(3.3);
  1030. PyAPI_FUNC(PyObject*) _PyUnicode_EncodeUTF7(
  1031. PyObject *unicode, /* Unicode object */
  1032. int base64SetO, /* Encode RFC2152 Set O characters in base64 */
  1033. int base64WhiteSpace, /* Encode whitespace (sp, ht, nl, cr) in base64 */
  1034. const char *errors /* error handling */
  1035. );
  1036. #endif
  1037. /* --- UTF-8 Codecs ------------------------------------------------------- */
  1038. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF8(
  1039. const char *string, /* UTF-8 encoded string */
  1040. Py_ssize_t length, /* size of string */
  1041. const char *errors /* error handling */
  1042. );
  1043. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF8Stateful(
  1044. const char *string, /* UTF-8 encoded string */
  1045. Py_ssize_t length, /* size of string */
  1046. const char *errors, /* error handling */
  1047. Py_ssize_t *consumed /* bytes consumed */
  1048. );
  1049. PyAPI_FUNC(PyObject*) PyUnicode_AsUTF8String(
  1050. PyObject *unicode /* Unicode object */
  1051. );
  1052. #ifndef Py_LIMITED_API
  1053. PyAPI_FUNC(PyObject*) _PyUnicode_AsUTF8String(
  1054. PyObject *unicode,
  1055. const char *errors);
  1056. PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF8(
  1057. const Py_UNICODE *data, /* Unicode char buffer */
  1058. Py_ssize_t length, /* number of Py_UNICODE chars to encode */
  1059. const char *errors /* error handling */
  1060. ) Py_DEPRECATED(3.3);
  1061. #endif
  1062. /* --- UTF-32 Codecs ------------------------------------------------------ */
  1063. /* Decodes length bytes from a UTF-32 encoded buffer string and returns
  1064. the corresponding Unicode object.
  1065. errors (if non-NULL) defines the error handling. It defaults
  1066. to "strict".
  1067. If byteorder is non-NULL, the decoder starts decoding using the
  1068. given byte order:
  1069. *byteorder == -1: little endian
  1070. *byteorder == 0: native order
  1071. *byteorder == 1: big endian
  1072. In native mode, the first four bytes of the stream are checked for a
  1073. BOM mark. If found, the BOM mark is analysed, the byte order
  1074. adjusted and the BOM skipped. In the other modes, no BOM mark
  1075. interpretation is done. After completion, *byteorder is set to the
  1076. current byte order at the end of input data.
  1077. If byteorder is NULL, the codec starts in native order mode.
  1078. */
  1079. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF32(
  1080. const char *string, /* UTF-32 encoded string */
  1081. Py_ssize_t length, /* size of string */
  1082. const char *errors, /* error handling */
  1083. int *byteorder /* pointer to byteorder to use
  1084. 0=native;-1=LE,1=BE; updated on
  1085. exit */
  1086. );
  1087. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF32Stateful(
  1088. const char *string, /* UTF-32 encoded string */
  1089. Py_ssize_t length, /* size of string */
  1090. const char *errors, /* error handling */
  1091. int *byteorder, /* pointer to byteorder to use
  1092. 0=native;-1=LE,1=BE; updated on
  1093. exit */
  1094. Py_ssize_t *consumed /* bytes consumed */
  1095. );
  1096. /* Returns a Python string using the UTF-32 encoding in native byte
  1097. order. The string always starts with a BOM mark. */
  1098. PyAPI_FUNC(PyObject*) PyUnicode_AsUTF32String(
  1099. PyObject *unicode /* Unicode object */
  1100. );
  1101. /* Returns a Python string object holding the UTF-32 encoded value of
  1102. the Unicode data.
  1103. If byteorder is not 0, output is written according to the following
  1104. byte order:
  1105. byteorder == -1: little endian
  1106. byteorder == 0: native byte order (writes a BOM mark)
  1107. byteorder == 1: big endian
  1108. If byteorder is 0, the output string will always start with the
  1109. Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark is
  1110. prepended.
  1111. */
  1112. #ifndef Py_LIMITED_API
  1113. PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF32(
  1114. const Py_UNICODE *data, /* Unicode char buffer */
  1115. Py_ssize_t length, /* number of Py_UNICODE chars to encode */
  1116. const char *errors, /* error handling */
  1117. int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
  1118. ) Py_DEPRECATED(3.3);
  1119. PyAPI_FUNC(PyObject*) _PyUnicode_EncodeUTF32(
  1120. PyObject *object, /* Unicode object */
  1121. const char *errors, /* error handling */
  1122. int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
  1123. );
  1124. #endif
  1125. /* --- UTF-16 Codecs ------------------------------------------------------ */
  1126. /* Decodes length bytes from a UTF-16 encoded buffer string and returns
  1127. the corresponding Unicode object.
  1128. errors (if non-NULL) defines the error handling. It defaults
  1129. to "strict".
  1130. If byteorder is non-NULL, the decoder starts decoding using the
  1131. given byte order:
  1132. *byteorder == -1: little endian
  1133. *byteorder == 0: native order
  1134. *byteorder == 1: big endian
  1135. In native mode, the first two bytes of the stream are checked for a
  1136. BOM mark. If found, the BOM mark is analysed, the byte order
  1137. adjusted and the BOM skipped. In the other modes, no BOM mark
  1138. interpretation is done. After completion, *byteorder is set to the
  1139. current byte order at the end of input data.
  1140. If byteorder is NULL, the codec starts in native order mode.
  1141. */
  1142. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF16(
  1143. const char *string, /* UTF-16 encoded string */
  1144. Py_ssize_t length, /* size of string */
  1145. const char *errors, /* error handling */
  1146. int *byteorder /* pointer to byteorder to use
  1147. 0=native;-1=LE,1=BE; updated on
  1148. exit */
  1149. );
  1150. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF16Stateful(
  1151. const char *string, /* UTF-16 encoded string */
  1152. Py_ssize_t length, /* size of string */
  1153. const char *errors, /* error handling */
  1154. int *byteorder, /* pointer to byteorder to use
  1155. 0=native;-1=LE,1=BE; updated on
  1156. exit */
  1157. Py_ssize_t *consumed /* bytes consumed */
  1158. );
  1159. /* Returns a Python string using the UTF-16 encoding in native byte
  1160. order. The string always starts with a BOM mark. */
  1161. PyAPI_FUNC(PyObject*) PyUnicode_AsUTF16String(
  1162. PyObject *unicode /* Unicode object */
  1163. );
  1164. /* Returns a Python string object holding the UTF-16 encoded value of
  1165. the Unicode data.
  1166. If byteorder is not 0, output is written according to the following
  1167. byte order:
  1168. byteorder == -1: little endian
  1169. byteorder == 0: native byte order (writes a BOM mark)
  1170. byteorder == 1: big endian
  1171. If byteorder is 0, the output string will always start with the
  1172. Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark is
  1173. prepended.
  1174. Note that Py_UNICODE data is being interpreted as UTF-16 reduced to
  1175. UCS-2. This trick makes it possible to add full UTF-16 capabilities
  1176. at a later point without compromising the APIs.
  1177. */
  1178. #ifndef Py_LIMITED_API
  1179. PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF16(
  1180. const Py_UNICODE *data, /* Unicode char buffer */
  1181. Py_ssize_t length, /* number of Py_UNICODE chars to encode */
  1182. const char *errors, /* error handling */
  1183. int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
  1184. ) Py_DEPRECATED(3.3);
  1185. PyAPI_FUNC(PyObject*) _PyUnicode_EncodeUTF16(
  1186. PyObject* unicode, /* Unicode object */
  1187. const char *errors, /* error handling */
  1188. int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
  1189. );
  1190. #endif
  1191. /* --- Unicode-Escape Codecs ---------------------------------------------- */
  1192. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUnicodeEscape(
  1193. const char *string, /* Unicode-Escape encoded string */
  1194. Py_ssize_t length, /* size of string */
  1195. const char *errors /* error handling */
  1196. );
  1197. #ifndef Py_LIMITED_API
  1198. /* Helper for PyUnicode_DecodeUnicodeEscape that detects invalid escape
  1199. chars. */
  1200. PyAPI_FUNC(PyObject*) _PyUnicode_DecodeUnicodeEscape(
  1201. const char *string, /* Unicode-Escape encoded string */
  1202. Py_ssize_t length, /* size of string */
  1203. const char *errors, /* error handling */
  1204. const char **first_invalid_escape /* on return, points to first
  1205. invalid escaped char in
  1206. string. */
  1207. );
  1208. #endif
  1209. PyAPI_FUNC(PyObject*) PyUnicode_AsUnicodeEscapeString(
  1210. PyObject *unicode /* Unicode object */
  1211. );
  1212. #ifndef Py_LIMITED_API
  1213. PyAPI_FUNC(PyObject*) PyUnicode_EncodeUnicodeEscape(
  1214. const Py_UNICODE *data, /* Unicode char buffer */
  1215. Py_ssize_t length /* Number of Py_UNICODE chars to encode */
  1216. ) Py_DEPRECATED(3.3);
  1217. #endif
  1218. /* --- Raw-Unicode-Escape Codecs ------------------------------------------ */
  1219. PyAPI_FUNC(PyObject*) PyUnicode_DecodeRawUnicodeEscape(
  1220. const char *string, /* Raw-Unicode-Escape encoded string */
  1221. Py_ssize_t length, /* size of string */
  1222. const char *errors /* error handling */
  1223. );
  1224. PyAPI_FUNC(PyObject*) PyUnicode_AsRawUnicodeEscapeString(
  1225. PyObject *unicode /* Unicode object */
  1226. );
  1227. #ifndef Py_LIMITED_API
  1228. PyAPI_FUNC(PyObject*) PyUnicode_EncodeRawUnicodeEscape(
  1229. const Py_UNICODE *data, /* Unicode char buffer */
  1230. Py_ssize_t length /* Number of Py_UNICODE chars to encode */
  1231. ) Py_DEPRECATED(3.3);
  1232. #endif
  1233. /* --- Unicode Internal Codec ---------------------------------------------
  1234. Only for internal use in _codecsmodule.c */
  1235. #ifndef Py_LIMITED_API
  1236. PyObject *_PyUnicode_DecodeUnicodeInternal(
  1237. const char *string,
  1238. Py_ssize_t length,
  1239. const char *errors
  1240. );
  1241. #endif
  1242. /* --- Latin-1 Codecs -----------------------------------------------------
  1243. Note: Latin-1 corresponds to the first 256 Unicode ordinals.
  1244. */
  1245. PyAPI_FUNC(PyObject*) PyUnicode_DecodeLatin1(
  1246. const char *string, /* Latin-1 encoded string */
  1247. Py_ssize_t length, /* size of string */
  1248. const char *errors /* error handling */
  1249. );
  1250. PyAPI_FUNC(PyObject*) PyUnicode_AsLatin1String(
  1251. PyObject *unicode /* Unicode object */
  1252. );
  1253. #ifndef Py_LIMITED_API
  1254. PyAPI_FUNC(PyObject*) _PyUnicode_AsLatin1String(
  1255. PyObject* unicode,
  1256. const char* errors);
  1257. PyAPI_FUNC(PyObject*) PyUnicode_EncodeLatin1(
  1258. const Py_UNICODE *data, /* Unicode char buffer */
  1259. Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
  1260. const char *errors /* error handling */
  1261. ) Py_DEPRECATED(3.3);
  1262. #endif
  1263. /* --- ASCII Codecs -------------------------------------------------------
  1264. Only 7-bit ASCII data is excepted. All other codes generate errors.
  1265. */
  1266. PyAPI_FUNC(PyObject*) PyUnicode_DecodeASCII(
  1267. const char *string, /* ASCII encoded string */
  1268. Py_ssize_t length, /* size of string */
  1269. const char *errors /* error handling */
  1270. );
  1271. PyAPI_FUNC(PyObject*) PyUnicode_AsASCIIString(
  1272. PyObject *unicode /* Unicode object */
  1273. );
  1274. #ifndef Py_LIMITED_API
  1275. PyAPI_FUNC(PyObject*) _PyUnicode_AsASCIIString(
  1276. PyObject* unicode,
  1277. const char* errors);
  1278. PyAPI_FUNC(PyObject*) PyUnicode_EncodeASCII(
  1279. const Py_UNICODE *data, /* Unicode char buffer */
  1280. Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
  1281. const char *errors /* error handling */
  1282. ) Py_DEPRECATED(3.3);
  1283. #endif
  1284. /* --- Character Map Codecs -----------------------------------------------
  1285. This codec uses mappings to encode and decode characters.
  1286. Decoding mappings must map byte ordinals (integers in the range from 0 to
  1287. 255) to Unicode strings, integers (which are then interpreted as Unicode
  1288. ordinals) or None. Unmapped data bytes (ones which cause a LookupError)
  1289. as well as mapped to None, 0xFFFE or '\ufffe' are treated as "undefined
  1290. mapping" and cause an error.
  1291. Encoding mappings must map Unicode ordinal integers to bytes objects,
  1292. integers in the range from 0 to 255 or None. Unmapped character
  1293. ordinals (ones which cause a LookupError) as well as mapped to
  1294. None are treated as "undefined mapping" and cause an error.
  1295. */
  1296. PyAPI_FUNC(PyObject*) PyUnicode_DecodeCharmap(
  1297. const char *string, /* Encoded string */
  1298. Py_ssize_t length, /* size of string */
  1299. PyObject *mapping, /* decoding mapping */
  1300. const char *errors /* error handling */
  1301. );
  1302. PyAPI_FUNC(PyObject*) PyUnicode_AsCharmapString(
  1303. PyObject *unicode, /* Unicode object */
  1304. PyObject *mapping /* encoding mapping */
  1305. );
  1306. #ifndef Py_LIMITED_API
  1307. PyAPI_FUNC(PyObject*) PyUnicode_EncodeCharmap(
  1308. const Py_UNICODE *data, /* Unicode char buffer */
  1309. Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
  1310. PyObject *mapping, /* encoding mapping */
  1311. const char *errors /* error handling */
  1312. ) Py_DEPRECATED(3.3);
  1313. PyAPI_FUNC(PyObject*) _PyUnicode_EncodeCharmap(
  1314. PyObject *unicode, /* Unicode object */
  1315. PyObject *mapping, /* encoding mapping */
  1316. const char *errors /* error handling */
  1317. );
  1318. #endif
  1319. /* Translate a Py_UNICODE buffer of the given length by applying a
  1320. character mapping table to it and return the resulting Unicode
  1321. object.
  1322. The mapping table must map Unicode ordinal integers to Unicode strings,
  1323. Unicode ordinal integers or None (causing deletion of the character).
  1324. Mapping tables may be dictionaries or sequences. Unmapped character
  1325. ordinals (ones which cause a LookupError) are left untouched and
  1326. are copied as-is.
  1327. */
  1328. #ifndef Py_LIMITED_API
  1329. PyAPI_FUNC(PyObject *) PyUnicode_TranslateCharmap(
  1330. const Py_UNICODE *data, /* Unicode char buffer */
  1331. Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
  1332. PyObject *table, /* Translate table */
  1333. const char *errors /* error handling */
  1334. ) Py_DEPRECATED(3.3);
  1335. #endif
  1336. #ifdef MS_WINDOWS
  1337. /* --- MBCS codecs for Windows -------------------------------------------- */
  1338. PyAPI_FUNC(PyObject*) PyUnicode_DecodeMBCS(
  1339. const char *string, /* MBCS encoded string */
  1340. Py_ssize_t length, /* size of string */
  1341. const char *errors /* error handling */
  1342. );
  1343. PyAPI_FUNC(PyObject*) PyUnicode_DecodeMBCSStateful(
  1344. const char *string, /* MBCS encoded string */
  1345. Py_ssize_t length, /* size of string */
  1346. const char *errors, /* error handling */
  1347. Py_ssize_t *consumed /* bytes consumed */
  1348. );
  1349. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
  1350. PyAPI_FUNC(PyObject*) PyUnicode_DecodeCodePageStateful(
  1351. int code_page, /* code page number */
  1352. const char *string, /* encoded string */
  1353. Py_ssize_t length, /* size of string */
  1354. const char *errors, /* error handling */
  1355. Py_ssize_t *consumed /* bytes consumed */
  1356. );
  1357. #endif
  1358. PyAPI_FUNC(PyObject*) PyUnicode_AsMBCSString(
  1359. PyObject *unicode /* Unicode object */
  1360. );
  1361. #ifndef Py_LIMITED_API
  1362. PyAPI_FUNC(PyObject*) PyUnicode_EncodeMBCS(
  1363. const Py_UNICODE *data, /* Unicode char buffer */
  1364. Py_ssize_t length, /* number of Py_UNICODE chars to encode */
  1365. const char *errors /* error handling */
  1366. ) Py_DEPRECATED(3.3);
  1367. #endif
  1368. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
  1369. PyAPI_FUNC(PyObject*) PyUnicode_EncodeCodePage(
  1370. int code_page, /* code page number */
  1371. PyObject *unicode, /* Unicode object */
  1372. const char *errors /* error handling */
  1373. );
  1374. #endif
  1375. #endif /* MS_WINDOWS */
  1376. #ifndef Py_LIMITED_API
  1377. /* --- Decimal Encoder ---------------------------------------------------- */
  1378. /* Takes a Unicode string holding a decimal value and writes it into
  1379. an output buffer using standard ASCII digit codes.
  1380. The output buffer has to provide at least length+1 bytes of storage
  1381. area. The output string is 0-terminated.
  1382. The encoder converts whitespace to ' ', decimal characters to their
  1383. corresponding ASCII digit and all other Latin-1 characters except
  1384. \0 as-is. Characters outside this range (Unicode ordinals 1-256)
  1385. are treated as errors. This includes embedded NULL bytes.
  1386. Error handling is defined by the errors argument:
  1387. NULL or "strict": raise a ValueError
  1388. "ignore": ignore the wrong characters (these are not copied to the
  1389. output buffer)
  1390. "replace": replaces illegal characters with '?'
  1391. Returns 0 on success, -1 on failure.
  1392. */
  1393. PyAPI_FUNC(int) PyUnicode_EncodeDecimal(
  1394. Py_UNICODE *s, /* Unicode buffer */
  1395. Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
  1396. char *output, /* Output buffer; must have size >= length */
  1397. const char *errors /* error handling */
  1398. ) /* Py_DEPRECATED(3.3) */;
  1399. /* Transforms code points that have decimal digit property to the
  1400. corresponding ASCII digit code points.
  1401. Returns a new Unicode string on success, NULL on failure.
  1402. */
  1403. PyAPI_FUNC(PyObject*) PyUnicode_TransformDecimalToASCII(
  1404. Py_UNICODE *s, /* Unicode buffer */
  1405. Py_ssize_t length /* Number of Py_UNICODE chars to transform */
  1406. ) /* Py_DEPRECATED(3.3) */;
  1407. /* Coverts a Unicode object holding a decimal value to an ASCII string
  1408. for using in int, float and complex parsers.
  1409. Transforms code points that have decimal digit property to the
  1410. corresponding ASCII digit code points. Transforms spaces to ASCII.
  1411. Transforms code points starting from the first non-ASCII code point that
  1412. is neither a decimal digit nor a space to the end into '?'. */
  1413. PyAPI_FUNC(PyObject*) _PyUnicode_TransformDecimalAndSpaceToASCII(
  1414. PyObject *unicode /* Unicode object */
  1415. );
  1416. #endif
  1417. /* --- Locale encoding --------------------------------------------------- */
  1418. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
  1419. /* Decode a string from the current locale encoding. The decoder is strict if
  1420. *surrogateescape* is equal to zero, otherwise it uses the 'surrogateescape'
  1421. error handler (PEP 383) to escape undecodable bytes. If a byte sequence can
  1422. be decoded as a surrogate character and *surrogateescape* is not equal to
  1423. zero, the byte sequence is escaped using the 'surrogateescape' error handler
  1424. instead of being decoded. *str* must end with a null character but cannot
  1425. contain embedded null characters. */
  1426. PyAPI_FUNC(PyObject*) PyUnicode_DecodeLocaleAndSize(
  1427. const char *str,
  1428. Py_ssize_t len,
  1429. const char *errors);
  1430. /* Similar to PyUnicode_DecodeLocaleAndSize(), but compute the string
  1431. length using strlen(). */
  1432. PyAPI_FUNC(PyObject*) PyUnicode_DecodeLocale(
  1433. const char *str,
  1434. const char *errors);
  1435. /* Encode a Unicode object to the current locale encoding. The encoder is
  1436. strict is *surrogateescape* is equal to zero, otherwise the
  1437. "surrogateescape" error handler is used. Return a bytes object. The string
  1438. cannot contain embedded null characters. */
  1439. PyAPI_FUNC(PyObject*) PyUnicode_EncodeLocale(
  1440. PyObject *unicode,
  1441. const char *errors
  1442. );
  1443. #endif
  1444. /* --- File system encoding ---------------------------------------------- */
  1445. /* ParseTuple converter: encode str objects to bytes using
  1446. PyUnicode_EncodeFSDefault(); bytes objects are output as-is. */
  1447. PyAPI_FUNC(int) PyUnicode_FSConverter(PyObject*, void*);
  1448. /* ParseTuple converter: decode bytes objects to unicode using
  1449. PyUnicode_DecodeFSDefaultAndSize(); str objects are output as-is. */
  1450. PyAPI_FUNC(int) PyUnicode_FSDecoder(PyObject*, void*);
  1451. /* Decode a null-terminated string using Py_FileSystemDefaultEncoding
  1452. and the "surrogateescape" error handler.
  1453. If Py_FileSystemDefaultEncoding is not set, fall back to the locale
  1454. encoding.
  1455. Use PyUnicode_DecodeFSDefaultAndSize() if the string length is known.
  1456. */
  1457. PyAPI_FUNC(PyObject*) PyUnicode_DecodeFSDefault(
  1458. const char *s /* encoded string */
  1459. );
  1460. /* Decode a string using Py_FileSystemDefaultEncoding
  1461. and the "surrogateescape" error handler.
  1462. If Py_FileSystemDefaultEncoding is not set, fall back to the locale
  1463. encoding.
  1464. */
  1465. PyAPI_FUNC(PyObject*) PyUnicode_DecodeFSDefaultAndSize(
  1466. const char *s, /* encoded string */
  1467. Py_ssize_t size /* size */
  1468. );
  1469. /* Encode a Unicode object to Py_FileSystemDefaultEncoding with the
  1470. "surrogateescape" error handler, and return bytes.
  1471. If Py_FileSystemDefaultEncoding is not set, fall back to the locale
  1472. encoding.
  1473. */
  1474. PyAPI_FUNC(PyObject*) PyUnicode_EncodeFSDefault(
  1475. PyObject *unicode
  1476. );
  1477. /* --- Methods & Slots ----------------------------------------------------
  1478. These are capable of handling Unicode objects and strings on input
  1479. (we refer to them as strings in the descriptions) and return
  1480. Unicode objects or integers as appropriate. */
  1481. /* Concat two strings giving a new Unicode string. */
  1482. PyAPI_FUNC(PyObject*) PyUnicode_Concat(
  1483. PyObject *left, /* Left string */
  1484. PyObject *right /* Right string */
  1485. );
  1486. /* Concat two strings and put the result in *pleft
  1487. (sets *pleft to NULL on error) */
  1488. PyAPI_FUNC(void) PyUnicode_Append(
  1489. PyObject **pleft, /* Pointer to left string */
  1490. PyObject *right /* Right string */
  1491. );
  1492. /* Concat two strings, put the result in *pleft and drop the right object
  1493. (sets *pleft to NULL on error) */
  1494. PyAPI_FUNC(void) PyUnicode_AppendAndDel(
  1495. PyObject **pleft, /* Pointer to left string */
  1496. PyObject *right /* Right string */
  1497. );
  1498. /* Split a string giving a list of Unicode strings.
  1499. If sep is NULL, splitting will be done at all whitespace
  1500. substrings. Otherwise, splits occur at the given separator.
  1501. At most maxsplit splits will be done. If negative, no limit is set.
  1502. Separators are not included in the resulting list.
  1503. */
  1504. PyAPI_FUNC(PyObject*) PyUnicode_Split(
  1505. PyObject *s, /* String to split */
  1506. PyObject *sep, /* String separator */
  1507. Py_ssize_t maxsplit /* Maxsplit count */
  1508. );
  1509. /* Dito, but split at line breaks.
  1510. CRLF is considered to be one line break. Line breaks are not
  1511. included in the resulting list. */
  1512. PyAPI_FUNC(PyObject*) PyUnicode_Splitlines(
  1513. PyObject *s, /* String to split */
  1514. int keepends /* If true, line end markers are included */
  1515. );
  1516. /* Partition a string using a given separator. */
  1517. PyAPI_FUNC(PyObject*) PyUnicode_Partition(
  1518. PyObject *s, /* String to partition */
  1519. PyObject *sep /* String separator */
  1520. );
  1521. /* Partition a string using a given separator, searching from the end of the
  1522. string. */
  1523. PyAPI_FUNC(PyObject*) PyUnicode_RPartition(
  1524. PyObject *s, /* String to partition */
  1525. PyObject *sep /* String separator */
  1526. );
  1527. /* Split a string giving a list of Unicode strings.
  1528. If sep is NULL, splitting will be done at all whitespace
  1529. substrings. Otherwise, splits occur at the given separator.
  1530. At most maxsplit splits will be done. But unlike PyUnicode_Split
  1531. PyUnicode_RSplit splits from the end of the string. If negative,
  1532. no limit is set.
  1533. Separators are not included in the resulting list.
  1534. */
  1535. PyAPI_FUNC(PyObject*) PyUnicode_RSplit(
  1536. PyObject *s, /* String to split */
  1537. PyObject *sep, /* String separator */
  1538. Py_ssize_t maxsplit /* Maxsplit count */
  1539. );
  1540. /* Translate a string by applying a character mapping table to it and
  1541. return the resulting Unicode object.
  1542. The mapping table must map Unicode ordinal integers to Unicode strings,
  1543. Unicode ordinal integers or None (causing deletion of the character).
  1544. Mapping tables may be dictionaries or sequences. Unmapped character
  1545. ordinals (ones which cause a LookupError) are left untouched and
  1546. are copied as-is.
  1547. */
  1548. PyAPI_FUNC(PyObject *) PyUnicode_Translate(
  1549. PyObject *str, /* String */
  1550. PyObject *table, /* Translate table */
  1551. const char *errors /* error handling */
  1552. );
  1553. /* Join a sequence of strings using the given separator and return
  1554. the resulting Unicode string. */
  1555. PyAPI_FUNC(PyObject*) PyUnicode_Join(
  1556. PyObject *separator, /* Separator string */
  1557. PyObject *seq /* Sequence object */
  1558. );
  1559. #ifndef Py_LIMITED_API
  1560. PyAPI_FUNC(PyObject *) _PyUnicode_JoinArray(
  1561. PyObject *separator,
  1562. PyObject *const *items,
  1563. Py_ssize_t seqlen
  1564. );
  1565. #endif /* Py_LIMITED_API */
  1566. /* Return 1 if substr matches str[start:end] at the given tail end, 0
  1567. otherwise. */
  1568. PyAPI_FUNC(Py_ssize_t) PyUnicode_Tailmatch(
  1569. PyObject *str, /* String */
  1570. PyObject *substr, /* Prefix or Suffix string */
  1571. Py_ssize_t start, /* Start index */
  1572. Py_ssize_t end, /* Stop index */
  1573. int direction /* Tail end: -1 prefix, +1 suffix */
  1574. );
  1575. /* Return the first position of substr in str[start:end] using the
  1576. given search direction or -1 if not found. -2 is returned in case
  1577. an error occurred and an exception is set. */
  1578. PyAPI_FUNC(Py_ssize_t) PyUnicode_Find(
  1579. PyObject *str, /* String */
  1580. PyObject *substr, /* Substring to find */
  1581. Py_ssize_t start, /* Start index */
  1582. Py_ssize_t end, /* Stop index */
  1583. int direction /* Find direction: +1 forward, -1 backward */
  1584. );
  1585. #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
  1586. /* Like PyUnicode_Find, but search for single character only. */
  1587. PyAPI_FUNC(Py_ssize_t) PyUnicode_FindChar(
  1588. PyObject *str,
  1589. Py_UCS4 ch,
  1590. Py_ssize_t start,
  1591. Py_ssize_t end,
  1592. int direction
  1593. );
  1594. #endif
  1595. /* Count the number of occurrences of substr in str[start:end]. */
  1596. PyAPI_FUNC(Py_ssize_t) PyUnicode_Count(
  1597. PyObject *str, /* String */
  1598. PyObject *substr, /* Substring to count */
  1599. Py_ssize_t start, /* Start index */
  1600. Py_ssize_t end /* Stop index */
  1601. );
  1602. /* Replace at most maxcount occurrences of substr in str with replstr
  1603. and return the resulting Unicode object. */
  1604. PyAPI_FUNC(PyObject *) PyUnicode_Replace(
  1605. PyObject *str, /* String */
  1606. PyObject *substr, /* Substring to find */
  1607. PyObject *replstr, /* Substring to replace */
  1608. Py_ssize_t maxcount /* Max. number of replacements to apply;
  1609. -1 = all */
  1610. );
  1611. /* Compare two strings and return -1, 0, 1 for less than, equal,
  1612. greater than resp.
  1613. Raise an exception and return -1 on error. */
  1614. PyAPI_FUNC(int) PyUnicode_Compare(
  1615. PyObject *left, /* Left string */
  1616. PyObject *right /* Right string */
  1617. );
  1618. #ifndef Py_LIMITED_API
  1619. /* Test whether a unicode is equal to ASCII identifier. Return 1 if true,
  1620. 0 otherwise. The right argument must be ASCII identifier.
  1621. Any error occurs inside will be cleared before return. */
  1622. PyAPI_FUNC(int) _PyUnicode_EqualToASCIIId(
  1623. PyObject *left, /* Left string */
  1624. _Py_Identifier *right /* Right identifier */
  1625. );
  1626. #endif
  1627. /* Compare a Unicode object with C string and return -1, 0, 1 for less than,
  1628. equal, and greater than, respectively. It is best to pass only
  1629. ASCII-encoded strings, but the function interprets the input string as
  1630. ISO-8859-1 if it contains non-ASCII characters.
  1631. This function does not raise exceptions. */
  1632. PyAPI_FUNC(int) PyUnicode_CompareWithASCIIString(
  1633. PyObject *left,
  1634. const char *right /* ASCII-encoded string */
  1635. );
  1636. #ifndef Py_LIMITED_API
  1637. /* Test whether a unicode is equal to ASCII string. Return 1 if true,
  1638. 0 otherwise. The right argument must be ASCII-encoded string.
  1639. Any error occurs inside will be cleared before return. */
  1640. PyAPI_FUNC(int) _PyUnicode_EqualToASCIIString(
  1641. PyObject *left,
  1642. const char *right /* ASCII-encoded string */
  1643. );
  1644. #endif
  1645. /* Rich compare two strings and return one of the following:
  1646. - NULL in case an exception was raised
  1647. - Py_True or Py_False for successful comparisons
  1648. - Py_NotImplemented in case the type combination is unknown
  1649. Possible values for op:
  1650. Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE
  1651. */
  1652. PyAPI_FUNC(PyObject *) PyUnicode_RichCompare(
  1653. PyObject *left, /* Left string */
  1654. PyObject *right, /* Right string */
  1655. int op /* Operation: Py_EQ, Py_NE, Py_GT, etc. */
  1656. );
  1657. /* Apply an argument tuple or dictionary to a format string and return
  1658. the resulting Unicode string. */
  1659. PyAPI_FUNC(PyObject *) PyUnicode_Format(
  1660. PyObject *format, /* Format string */
  1661. PyObject *args /* Argument tuple or dictionary */
  1662. );
  1663. /* Checks whether element is contained in container and return 1/0
  1664. accordingly.
  1665. element has to coerce to a one element Unicode string. -1 is
  1666. returned in case of an error. */
  1667. PyAPI_FUNC(int) PyUnicode_Contains(
  1668. PyObject *container, /* Container string */
  1669. PyObject *element /* Element string */
  1670. );
  1671. /* Checks whether argument is a valid identifier. */
  1672. PyAPI_FUNC(int) PyUnicode_IsIdentifier(PyObject *s);
  1673. #ifndef Py_LIMITED_API
  1674. /* Externally visible for str.strip(unicode) */
  1675. PyAPI_FUNC(PyObject *) _PyUnicode_XStrip(
  1676. PyObject *self,
  1677. int striptype,
  1678. PyObject *sepobj
  1679. );
  1680. #endif
  1681. /* Using explicit passed-in values, insert the thousands grouping
  1682. into the string pointed to by buffer. For the argument descriptions,
  1683. see Objects/stringlib/localeutil.h */
  1684. #ifndef Py_LIMITED_API
  1685. PyAPI_FUNC(Py_ssize_t) _PyUnicode_InsertThousandsGrouping(
  1686. _PyUnicodeWriter *writer,
  1687. Py_ssize_t n_buffer,
  1688. PyObject *digits,
  1689. Py_ssize_t d_pos,
  1690. Py_ssize_t n_digits,
  1691. Py_ssize_t min_width,
  1692. const char *grouping,
  1693. PyObject *thousands_sep,
  1694. Py_UCS4 *maxchar);
  1695. #endif
  1696. /* === Characters Type APIs =============================================== */
  1697. /* Helper array used by Py_UNICODE_ISSPACE(). */
  1698. #ifndef Py_LIMITED_API
  1699. PyAPI_DATA(const unsigned char) _Py_ascii_whitespace[];
  1700. /* These should not be used directly. Use the Py_UNICODE_IS* and
  1701. Py_UNICODE_TO* macros instead.
  1702. These APIs are implemented in Objects/unicodectype.c.
  1703. */
  1704. PyAPI_FUNC(int) _PyUnicode_IsLowercase(
  1705. Py_UCS4 ch /* Unicode character */
  1706. );
  1707. PyAPI_FUNC(int) _PyUnicode_IsUppercase(
  1708. Py_UCS4 ch /* Unicode character */
  1709. );
  1710. PyAPI_FUNC(int) _PyUnicode_IsTitlecase(
  1711. Py_UCS4 ch /* Unicode character */
  1712. );
  1713. PyAPI_FUNC(int) _PyUnicode_IsXidStart(
  1714. Py_UCS4 ch /* Unicode character */
  1715. );
  1716. PyAPI_FUNC(int) _PyUnicode_IsXidContinue(
  1717. Py_UCS4 ch /* Unicode character */
  1718. );
  1719. PyAPI_FUNC(int) _PyUnicode_IsWhitespace(
  1720. const Py_UCS4 ch /* Unicode character */
  1721. );
  1722. PyAPI_FUNC(int) _PyUnicode_IsLinebreak(
  1723. const Py_UCS4 ch /* Unicode character */
  1724. );
  1725. PyAPI_FUNC(Py_UCS4) _PyUnicode_ToLowercase(
  1726. Py_UCS4 ch /* Unicode character */
  1727. ) /* Py_DEPRECATED(3.3) */;
  1728. PyAPI_FUNC(Py_UCS4) _PyUnicode_ToUppercase(
  1729. Py_UCS4 ch /* Unicode character */
  1730. ) /* Py_DEPRECATED(3.3) */;
  1731. PyAPI_FUNC(Py_UCS4) _PyUnicode_ToTitlecase(
  1732. Py_UCS4 ch /* Unicode character */
  1733. ) Py_DEPRECATED(3.3);
  1734. PyAPI_FUNC(int) _PyUnicode_ToLowerFull(
  1735. Py_UCS4 ch, /* Unicode character */
  1736. Py_UCS4 *res
  1737. );
  1738. PyAPI_FUNC(int) _PyUnicode_ToTitleFull(
  1739. Py_UCS4 ch, /* Unicode character */
  1740. Py_UCS4 *res
  1741. );
  1742. PyAPI_FUNC(int) _PyUnicode_ToUpperFull(
  1743. Py_UCS4 ch, /* Unicode character */
  1744. Py_UCS4 *res
  1745. );
  1746. PyAPI_FUNC(int) _PyUnicode_ToFoldedFull(
  1747. Py_UCS4 ch, /* Unicode character */
  1748. Py_UCS4 *res
  1749. );
  1750. PyAPI_FUNC(int) _PyUnicode_IsCaseIgnorable(
  1751. Py_UCS4 ch /* Unicode character */
  1752. );
  1753. PyAPI_FUNC(int) _PyUnicode_IsCased(
  1754. Py_UCS4 ch /* Unicode character */
  1755. );
  1756. PyAPI_FUNC(int) _PyUnicode_ToDecimalDigit(
  1757. Py_UCS4 ch /* Unicode character */
  1758. );
  1759. PyAPI_FUNC(int) _PyUnicode_ToDigit(
  1760. Py_UCS4 ch /* Unicode character */
  1761. );
  1762. PyAPI_FUNC(double) _PyUnicode_ToNumeric(
  1763. Py_UCS4 ch /* Unicode character */
  1764. );
  1765. PyAPI_FUNC(int) _PyUnicode_IsDecimalDigit(
  1766. Py_UCS4 ch /* Unicode character */
  1767. );
  1768. PyAPI_FUNC(int) _PyUnicode_IsDigit(
  1769. Py_UCS4 ch /* Unicode character */
  1770. );
  1771. PyAPI_FUNC(int) _PyUnicode_IsNumeric(
  1772. Py_UCS4 ch /* Unicode character */
  1773. );
  1774. PyAPI_FUNC(int) _PyUnicode_IsPrintable(
  1775. Py_UCS4 ch /* Unicode character */
  1776. );
  1777. PyAPI_FUNC(int) _PyUnicode_IsAlpha(
  1778. Py_UCS4 ch /* Unicode character */
  1779. );
  1780. PyAPI_FUNC(size_t) Py_UNICODE_strlen(
  1781. const Py_UNICODE *u
  1782. ) Py_DEPRECATED(3.3);
  1783. PyAPI_FUNC(Py_UNICODE*) Py_UNICODE_strcpy(
  1784. Py_UNICODE *s1,
  1785. const Py_UNICODE *s2) Py_DEPRECATED(3.3);
  1786. PyAPI_FUNC(Py_UNICODE*) Py_UNICODE_strcat(
  1787. Py_UNICODE *s1, const Py_UNICODE *s2) Py_DEPRECATED(3.3);
  1788. PyAPI_FUNC(Py_UNICODE*) Py_UNICODE_strncpy(
  1789. Py_UNICODE *s1,
  1790. const Py_UNICODE *s2,
  1791. size_t n) Py_DEPRECATED(3.3);
  1792. PyAPI_FUNC(int) Py_UNICODE_strcmp(
  1793. const Py_UNICODE *s1,
  1794. const Py_UNICODE *s2
  1795. ) Py_DEPRECATED(3.3);
  1796. PyAPI_FUNC(int) Py_UNICODE_strncmp(
  1797. const Py_UNICODE *s1,
  1798. const Py_UNICODE *s2,
  1799. size_t n
  1800. ) Py_DEPRECATED(3.3);
  1801. PyAPI_FUNC(Py_UNICODE*) Py_UNICODE_strchr(
  1802. const Py_UNICODE *s,
  1803. Py_UNICODE c
  1804. ) Py_DEPRECATED(3.3);
  1805. PyAPI_FUNC(Py_UNICODE*) Py_UNICODE_strrchr(
  1806. const Py_UNICODE *s,
  1807. Py_UNICODE c
  1808. ) Py_DEPRECATED(3.3);
  1809. PyAPI_FUNC(PyObject*) _PyUnicode_FormatLong(PyObject *, int, int, int);
  1810. /* Create a copy of a unicode string ending with a nul character. Return NULL
  1811. and raise a MemoryError exception on memory allocation failure, otherwise
  1812. return a new allocated buffer (use PyMem_Free() to free the buffer). */
  1813. PyAPI_FUNC(Py_UNICODE*) PyUnicode_AsUnicodeCopy(
  1814. PyObject *unicode
  1815. ) Py_DEPRECATED(3.3);
  1816. #endif /* Py_LIMITED_API */
  1817. #if defined(Py_DEBUG) && !defined(Py_LIMITED_API)
  1818. PyAPI_FUNC(int) _PyUnicode_CheckConsistency(
  1819. PyObject *op,
  1820. int check_content);
  1821. #elif !defined(NDEBUG)
  1822. /* For asserts that call _PyUnicode_CheckConsistency(), which would
  1823. * otherwise be a problem when building with asserts but without Py_DEBUG. */
  1824. #define _PyUnicode_CheckConsistency(op, check_content) PyUnicode_Check(op)
  1825. #endif
  1826. #ifndef Py_LIMITED_API
  1827. /* Return an interned Unicode object for an Identifier; may fail if there is no memory.*/
  1828. PyAPI_FUNC(PyObject*) _PyUnicode_FromId(_Py_Identifier*);
  1829. /* Clear all static strings. */
  1830. PyAPI_FUNC(void) _PyUnicode_ClearStaticStrings(void);
  1831. /* Fast equality check when the inputs are known to be exact unicode types
  1832. and where the hash values are equal (i.e. a very probable match) */
  1833. PyAPI_FUNC(int) _PyUnicode_EQ(PyObject *, PyObject *);
  1834. #endif /* !Py_LIMITED_API */
  1835. #ifdef __cplusplus
  1836. }
  1837. #endif
  1838. #endif /* !Py_UNICODEOBJECT_H */