unicodeobject.h 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  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 file Misc/unicode.txt).
  8. Copyright (c) Corporation for National Research Initiatives.
  9. Original header:
  10. --------------------------------------------------------------------
  11. * Yet another Unicode string type for Python. This type supports the
  12. * 16-bit Basic Multilingual Plane (BMP) only.
  13. *
  14. * Written by Fredrik Lundh, January 1999.
  15. *
  16. * Copyright (c) 1999 by Secret Labs AB.
  17. * Copyright (c) 1999 by Fredrik Lundh.
  18. *
  19. * fredrik@pythonware.com
  20. * http://www.pythonware.com
  21. *
  22. * --------------------------------------------------------------------
  23. * This Unicode String Type is
  24. *
  25. * Copyright (c) 1999 by Secret Labs AB
  26. * Copyright (c) 1999 by Fredrik Lundh
  27. *
  28. * By obtaining, using, and/or copying this software and/or its
  29. * associated documentation, you agree that you have read, understood,
  30. * and will comply with the following terms and conditions:
  31. *
  32. * Permission to use, copy, modify, and distribute this software and its
  33. * associated documentation for any purpose and without fee is hereby
  34. * granted, provided that the above copyright notice appears in all
  35. * copies, and that both that copyright notice and this permission notice
  36. * appear in supporting documentation, and that the name of Secret Labs
  37. * AB or the author not be used in advertising or publicity pertaining to
  38. * distribution of the software without specific, written prior
  39. * permission.
  40. *
  41. * SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO
  42. * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  43. * FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR
  44. * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  45. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  46. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  47. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  48. * -------------------------------------------------------------------- */
  49. #include <ctype.h>
  50. /* === Internal API ======================================================= */
  51. /* --- Internal Unicode Format -------------------------------------------- */
  52. #ifndef Py_USING_UNICODE
  53. #define PyUnicode_Check(op) 0
  54. #define PyUnicode_CheckExact(op) 0
  55. #else
  56. /* FIXME: MvL's new implementation assumes that Py_UNICODE_SIZE is
  57. properly set, but the default rules below doesn't set it. I'll
  58. sort this out some other day -- fredrik@pythonware.com */
  59. #ifndef Py_UNICODE_SIZE
  60. #error Must define Py_UNICODE_SIZE
  61. #endif
  62. /* Setting Py_UNICODE_WIDE enables UCS-4 storage. Otherwise, Unicode
  63. strings are stored as UCS-2 (with limited support for UTF-16) */
  64. #if Py_UNICODE_SIZE >= 4
  65. #define Py_UNICODE_WIDE
  66. #endif
  67. /* Set these flags if the platform has "wchar.h", "wctype.h" and the
  68. wchar_t type is a 16-bit unsigned type */
  69. /* #define HAVE_WCHAR_H */
  70. /* #define HAVE_USABLE_WCHAR_T */
  71. /* Defaults for various platforms */
  72. #ifndef PY_UNICODE_TYPE
  73. /* Windows has a usable wchar_t type (unless we're using UCS-4) */
  74. # if defined(MS_WIN32) && Py_UNICODE_SIZE == 2
  75. # define HAVE_USABLE_WCHAR_T
  76. # define PY_UNICODE_TYPE wchar_t
  77. # endif
  78. # if defined(Py_UNICODE_WIDE)
  79. # define PY_UNICODE_TYPE Py_UCS4
  80. # endif
  81. #endif
  82. /* If the compiler provides a wchar_t type we try to support it
  83. through the interface functions PyUnicode_FromWideChar() and
  84. PyUnicode_AsWideChar(). */
  85. #ifdef HAVE_USABLE_WCHAR_T
  86. # ifndef HAVE_WCHAR_H
  87. # define HAVE_WCHAR_H
  88. # endif
  89. #endif
  90. #ifdef HAVE_WCHAR_H
  91. /* Work around a cosmetic bug in BSDI 4.x wchar.h; thanks to Thomas Wouters */
  92. # ifdef _HAVE_BSDI
  93. # include <time.h>
  94. # endif
  95. # include <wchar.h>
  96. #endif
  97. /*
  98. * Use this typedef when you need to represent a UTF-16 surrogate pair
  99. * as single unsigned integer.
  100. */
  101. #if SIZEOF_INT >= 4
  102. typedef unsigned int Py_UCS4;
  103. #elif SIZEOF_LONG >= 4
  104. typedef unsigned long Py_UCS4;
  105. #endif
  106. /* Py_UNICODE is the native Unicode storage format (code unit) used by
  107. Python and represents a single Unicode element in the Unicode
  108. type. */
  109. typedef PY_UNICODE_TYPE Py_UNICODE;
  110. /* --- UCS-2/UCS-4 Name Mangling ------------------------------------------ */
  111. /* Unicode API names are mangled to assure that UCS-2 and UCS-4 builds
  112. produce different external names and thus cause import errors in
  113. case Python interpreters and extensions with mixed compiled in
  114. Unicode width assumptions are combined. */
  115. #ifndef Py_UNICODE_WIDE
  116. # define PyUnicode_AsASCIIString PyUnicodeUCS2_AsASCIIString
  117. # define PyUnicode_AsCharmapString PyUnicodeUCS2_AsCharmapString
  118. # define PyUnicode_AsEncodedObject PyUnicodeUCS2_AsEncodedObject
  119. # define PyUnicode_AsEncodedString PyUnicodeUCS2_AsEncodedString
  120. # define PyUnicode_AsLatin1String PyUnicodeUCS2_AsLatin1String
  121. # define PyUnicode_AsRawUnicodeEscapeString PyUnicodeUCS2_AsRawUnicodeEscapeString
  122. # define PyUnicode_AsUTF32String PyUnicodeUCS2_AsUTF32String
  123. # define PyUnicode_AsUTF16String PyUnicodeUCS2_AsUTF16String
  124. # define PyUnicode_AsUTF8String PyUnicodeUCS2_AsUTF8String
  125. # define PyUnicode_AsUnicode PyUnicodeUCS2_AsUnicode
  126. # define PyUnicode_AsUnicodeEscapeString PyUnicodeUCS2_AsUnicodeEscapeString
  127. # define PyUnicode_AsWideChar PyUnicodeUCS2_AsWideChar
  128. # define PyUnicode_ClearFreeList PyUnicodeUCS2_ClearFreelist
  129. # define PyUnicode_Compare PyUnicodeUCS2_Compare
  130. # define PyUnicode_Concat PyUnicodeUCS2_Concat
  131. # define PyUnicode_Contains PyUnicodeUCS2_Contains
  132. # define PyUnicode_Count PyUnicodeUCS2_Count
  133. # define PyUnicode_Decode PyUnicodeUCS2_Decode
  134. # define PyUnicode_DecodeASCII PyUnicodeUCS2_DecodeASCII
  135. # define PyUnicode_DecodeCharmap PyUnicodeUCS2_DecodeCharmap
  136. # define PyUnicode_DecodeLatin1 PyUnicodeUCS2_DecodeLatin1
  137. # define PyUnicode_DecodeRawUnicodeEscape PyUnicodeUCS2_DecodeRawUnicodeEscape
  138. # define PyUnicode_DecodeUTF32 PyUnicodeUCS2_DecodeUTF32
  139. # define PyUnicode_DecodeUTF32Stateful PyUnicodeUCS2_DecodeUTF32Stateful
  140. # define PyUnicode_DecodeUTF16 PyUnicodeUCS2_DecodeUTF16
  141. # define PyUnicode_DecodeUTF16Stateful PyUnicodeUCS2_DecodeUTF16Stateful
  142. # define PyUnicode_DecodeUTF8 PyUnicodeUCS2_DecodeUTF8
  143. # define PyUnicode_DecodeUTF8Stateful PyUnicodeUCS2_DecodeUTF8Stateful
  144. # define PyUnicode_DecodeUnicodeEscape PyUnicodeUCS2_DecodeUnicodeEscape
  145. # define PyUnicode_Encode PyUnicodeUCS2_Encode
  146. # define PyUnicode_EncodeASCII PyUnicodeUCS2_EncodeASCII
  147. # define PyUnicode_EncodeCharmap PyUnicodeUCS2_EncodeCharmap
  148. # define PyUnicode_EncodeDecimal PyUnicodeUCS2_EncodeDecimal
  149. # define PyUnicode_EncodeLatin1 PyUnicodeUCS2_EncodeLatin1
  150. # define PyUnicode_EncodeRawUnicodeEscape PyUnicodeUCS2_EncodeRawUnicodeEscape
  151. # define PyUnicode_EncodeUTF32 PyUnicodeUCS2_EncodeUTF32
  152. # define PyUnicode_EncodeUTF16 PyUnicodeUCS2_EncodeUTF16
  153. # define PyUnicode_EncodeUTF8 PyUnicodeUCS2_EncodeUTF8
  154. # define PyUnicode_EncodeUnicodeEscape PyUnicodeUCS2_EncodeUnicodeEscape
  155. # define PyUnicode_Find PyUnicodeUCS2_Find
  156. # define PyUnicode_Format PyUnicodeUCS2_Format
  157. # define PyUnicode_FromEncodedObject PyUnicodeUCS2_FromEncodedObject
  158. # define PyUnicode_FromFormat PyUnicodeUCS2_FromFormat
  159. # define PyUnicode_FromFormatV PyUnicodeUCS2_FromFormatV
  160. # define PyUnicode_FromObject PyUnicodeUCS2_FromObject
  161. # define PyUnicode_FromOrdinal PyUnicodeUCS2_FromOrdinal
  162. # define PyUnicode_FromString PyUnicodeUCS2_FromString
  163. # define PyUnicode_FromStringAndSize PyUnicodeUCS2_FromStringAndSize
  164. # define PyUnicode_FromUnicode PyUnicodeUCS2_FromUnicode
  165. # define PyUnicode_FromWideChar PyUnicodeUCS2_FromWideChar
  166. # define PyUnicode_GetDefaultEncoding PyUnicodeUCS2_GetDefaultEncoding
  167. # define PyUnicode_GetMax PyUnicodeUCS2_GetMax
  168. # define PyUnicode_GetSize PyUnicodeUCS2_GetSize
  169. # define PyUnicode_Join PyUnicodeUCS2_Join
  170. # define PyUnicode_Partition PyUnicodeUCS2_Partition
  171. # define PyUnicode_RPartition PyUnicodeUCS2_RPartition
  172. # define PyUnicode_RSplit PyUnicodeUCS2_RSplit
  173. # define PyUnicode_Replace PyUnicodeUCS2_Replace
  174. # define PyUnicode_Resize PyUnicodeUCS2_Resize
  175. # define PyUnicode_RichCompare PyUnicodeUCS2_RichCompare
  176. # define PyUnicode_SetDefaultEncoding PyUnicodeUCS2_SetDefaultEncoding
  177. # define PyUnicode_Split PyUnicodeUCS2_Split
  178. # define PyUnicode_Splitlines PyUnicodeUCS2_Splitlines
  179. # define PyUnicode_Tailmatch PyUnicodeUCS2_Tailmatch
  180. # define PyUnicode_Translate PyUnicodeUCS2_Translate
  181. # define PyUnicode_TranslateCharmap PyUnicodeUCS2_TranslateCharmap
  182. # define _PyUnicode_AsDefaultEncodedString _PyUnicodeUCS2_AsDefaultEncodedString
  183. # define _PyUnicode_Fini _PyUnicodeUCS2_Fini
  184. # define _PyUnicode_Init _PyUnicodeUCS2_Init
  185. # define _PyUnicode_IsAlpha _PyUnicodeUCS2_IsAlpha
  186. # define _PyUnicode_IsDecimalDigit _PyUnicodeUCS2_IsDecimalDigit
  187. # define _PyUnicode_IsDigit _PyUnicodeUCS2_IsDigit
  188. # define _PyUnicode_IsLinebreak _PyUnicodeUCS2_IsLinebreak
  189. # define _PyUnicode_IsLowercase _PyUnicodeUCS2_IsLowercase
  190. # define _PyUnicode_IsNumeric _PyUnicodeUCS2_IsNumeric
  191. # define _PyUnicode_IsTitlecase _PyUnicodeUCS2_IsTitlecase
  192. # define _PyUnicode_IsUppercase _PyUnicodeUCS2_IsUppercase
  193. # define _PyUnicode_IsWhitespace _PyUnicodeUCS2_IsWhitespace
  194. # define _PyUnicode_ToDecimalDigit _PyUnicodeUCS2_ToDecimalDigit
  195. # define _PyUnicode_ToDigit _PyUnicodeUCS2_ToDigit
  196. # define _PyUnicode_ToLowercase _PyUnicodeUCS2_ToLowercase
  197. # define _PyUnicode_ToNumeric _PyUnicodeUCS2_ToNumeric
  198. # define _PyUnicode_ToTitlecase _PyUnicodeUCS2_ToTitlecase
  199. # define _PyUnicode_ToUppercase _PyUnicodeUCS2_ToUppercase
  200. #else
  201. # define PyUnicode_AsASCIIString PyUnicodeUCS4_AsASCIIString
  202. # define PyUnicode_AsCharmapString PyUnicodeUCS4_AsCharmapString
  203. # define PyUnicode_AsEncodedObject PyUnicodeUCS4_AsEncodedObject
  204. # define PyUnicode_AsEncodedString PyUnicodeUCS4_AsEncodedString
  205. # define PyUnicode_AsLatin1String PyUnicodeUCS4_AsLatin1String
  206. # define PyUnicode_AsRawUnicodeEscapeString PyUnicodeUCS4_AsRawUnicodeEscapeString
  207. # define PyUnicode_AsUTF32String PyUnicodeUCS4_AsUTF32String
  208. # define PyUnicode_AsUTF16String PyUnicodeUCS4_AsUTF16String
  209. # define PyUnicode_AsUTF8String PyUnicodeUCS4_AsUTF8String
  210. # define PyUnicode_AsUnicode PyUnicodeUCS4_AsUnicode
  211. # define PyUnicode_AsUnicodeEscapeString PyUnicodeUCS4_AsUnicodeEscapeString
  212. # define PyUnicode_AsWideChar PyUnicodeUCS4_AsWideChar
  213. # define PyUnicode_ClearFreeList PyUnicodeUCS4_ClearFreelist
  214. # define PyUnicode_Compare PyUnicodeUCS4_Compare
  215. # define PyUnicode_Concat PyUnicodeUCS4_Concat
  216. # define PyUnicode_Contains PyUnicodeUCS4_Contains
  217. # define PyUnicode_Count PyUnicodeUCS4_Count
  218. # define PyUnicode_Decode PyUnicodeUCS4_Decode
  219. # define PyUnicode_DecodeASCII PyUnicodeUCS4_DecodeASCII
  220. # define PyUnicode_DecodeCharmap PyUnicodeUCS4_DecodeCharmap
  221. # define PyUnicode_DecodeLatin1 PyUnicodeUCS4_DecodeLatin1
  222. # define PyUnicode_DecodeRawUnicodeEscape PyUnicodeUCS4_DecodeRawUnicodeEscape
  223. # define PyUnicode_DecodeUTF32 PyUnicodeUCS4_DecodeUTF32
  224. # define PyUnicode_DecodeUTF32Stateful PyUnicodeUCS4_DecodeUTF32Stateful
  225. # define PyUnicode_DecodeUTF16 PyUnicodeUCS4_DecodeUTF16
  226. # define PyUnicode_DecodeUTF16Stateful PyUnicodeUCS4_DecodeUTF16Stateful
  227. # define PyUnicode_DecodeUTF8 PyUnicodeUCS4_DecodeUTF8
  228. # define PyUnicode_DecodeUTF8Stateful PyUnicodeUCS4_DecodeUTF8Stateful
  229. # define PyUnicode_DecodeUnicodeEscape PyUnicodeUCS4_DecodeUnicodeEscape
  230. # define PyUnicode_Encode PyUnicodeUCS4_Encode
  231. # define PyUnicode_EncodeASCII PyUnicodeUCS4_EncodeASCII
  232. # define PyUnicode_EncodeCharmap PyUnicodeUCS4_EncodeCharmap
  233. # define PyUnicode_EncodeDecimal PyUnicodeUCS4_EncodeDecimal
  234. # define PyUnicode_EncodeLatin1 PyUnicodeUCS4_EncodeLatin1
  235. # define PyUnicode_EncodeRawUnicodeEscape PyUnicodeUCS4_EncodeRawUnicodeEscape
  236. # define PyUnicode_EncodeUTF32 PyUnicodeUCS4_EncodeUTF32
  237. # define PyUnicode_EncodeUTF16 PyUnicodeUCS4_EncodeUTF16
  238. # define PyUnicode_EncodeUTF8 PyUnicodeUCS4_EncodeUTF8
  239. # define PyUnicode_EncodeUnicodeEscape PyUnicodeUCS4_EncodeUnicodeEscape
  240. # define PyUnicode_Find PyUnicodeUCS4_Find
  241. # define PyUnicode_Format PyUnicodeUCS4_Format
  242. # define PyUnicode_FromEncodedObject PyUnicodeUCS4_FromEncodedObject
  243. # define PyUnicode_FromFormat PyUnicodeUCS4_FromFormat
  244. # define PyUnicode_FromFormatV PyUnicodeUCS4_FromFormatV
  245. # define PyUnicode_FromObject PyUnicodeUCS4_FromObject
  246. # define PyUnicode_FromOrdinal PyUnicodeUCS4_FromOrdinal
  247. # define PyUnicode_FromString PyUnicodeUCS4_FromString
  248. # define PyUnicode_FromStringAndSize PyUnicodeUCS4_FromStringAndSize
  249. # define PyUnicode_FromUnicode PyUnicodeUCS4_FromUnicode
  250. # define PyUnicode_FromWideChar PyUnicodeUCS4_FromWideChar
  251. # define PyUnicode_GetDefaultEncoding PyUnicodeUCS4_GetDefaultEncoding
  252. # define PyUnicode_GetMax PyUnicodeUCS4_GetMax
  253. # define PyUnicode_GetSize PyUnicodeUCS4_GetSize
  254. # define PyUnicode_Join PyUnicodeUCS4_Join
  255. # define PyUnicode_Partition PyUnicodeUCS4_Partition
  256. # define PyUnicode_RPartition PyUnicodeUCS4_RPartition
  257. # define PyUnicode_RSplit PyUnicodeUCS4_RSplit
  258. # define PyUnicode_Replace PyUnicodeUCS4_Replace
  259. # define PyUnicode_Resize PyUnicodeUCS4_Resize
  260. # define PyUnicode_RichCompare PyUnicodeUCS4_RichCompare
  261. # define PyUnicode_SetDefaultEncoding PyUnicodeUCS4_SetDefaultEncoding
  262. # define PyUnicode_Split PyUnicodeUCS4_Split
  263. # define PyUnicode_Splitlines PyUnicodeUCS4_Splitlines
  264. # define PyUnicode_Tailmatch PyUnicodeUCS4_Tailmatch
  265. # define PyUnicode_Translate PyUnicodeUCS4_Translate
  266. # define PyUnicode_TranslateCharmap PyUnicodeUCS4_TranslateCharmap
  267. # define _PyUnicode_AsDefaultEncodedString _PyUnicodeUCS4_AsDefaultEncodedString
  268. # define _PyUnicode_Fini _PyUnicodeUCS4_Fini
  269. # define _PyUnicode_Init _PyUnicodeUCS4_Init
  270. # define _PyUnicode_IsAlpha _PyUnicodeUCS4_IsAlpha
  271. # define _PyUnicode_IsDecimalDigit _PyUnicodeUCS4_IsDecimalDigit
  272. # define _PyUnicode_IsDigit _PyUnicodeUCS4_IsDigit
  273. # define _PyUnicode_IsLinebreak _PyUnicodeUCS4_IsLinebreak
  274. # define _PyUnicode_IsLowercase _PyUnicodeUCS4_IsLowercase
  275. # define _PyUnicode_IsNumeric _PyUnicodeUCS4_IsNumeric
  276. # define _PyUnicode_IsTitlecase _PyUnicodeUCS4_IsTitlecase
  277. # define _PyUnicode_IsUppercase _PyUnicodeUCS4_IsUppercase
  278. # define _PyUnicode_IsWhitespace _PyUnicodeUCS4_IsWhitespace
  279. # define _PyUnicode_ToDecimalDigit _PyUnicodeUCS4_ToDecimalDigit
  280. # define _PyUnicode_ToDigit _PyUnicodeUCS4_ToDigit
  281. # define _PyUnicode_ToLowercase _PyUnicodeUCS4_ToLowercase
  282. # define _PyUnicode_ToNumeric _PyUnicodeUCS4_ToNumeric
  283. # define _PyUnicode_ToTitlecase _PyUnicodeUCS4_ToTitlecase
  284. # define _PyUnicode_ToUppercase _PyUnicodeUCS4_ToUppercase
  285. #endif
  286. /* --- Internal Unicode Operations ---------------------------------------- */
  287. /* If you want Python to use the compiler's wctype.h functions instead
  288. of the ones supplied with Python, define WANT_WCTYPE_FUNCTIONS or
  289. configure Python using --with-wctype-functions. This reduces the
  290. interpreter's code size. */
  291. #if defined(HAVE_USABLE_WCHAR_T) && defined(WANT_WCTYPE_FUNCTIONS)
  292. #include <wctype.h>
  293. #define Py_UNICODE_ISSPACE(ch) iswspace(ch)
  294. #define Py_UNICODE_ISLOWER(ch) iswlower(ch)
  295. #define Py_UNICODE_ISUPPER(ch) iswupper(ch)
  296. #define Py_UNICODE_ISTITLE(ch) _PyUnicode_IsTitlecase(ch)
  297. #define Py_UNICODE_ISLINEBREAK(ch) _PyUnicode_IsLinebreak(ch)
  298. #define Py_UNICODE_TOLOWER(ch) towlower(ch)
  299. #define Py_UNICODE_TOUPPER(ch) towupper(ch)
  300. #define Py_UNICODE_TOTITLE(ch) _PyUnicode_ToTitlecase(ch)
  301. #define Py_UNICODE_ISDECIMAL(ch) _PyUnicode_IsDecimalDigit(ch)
  302. #define Py_UNICODE_ISDIGIT(ch) _PyUnicode_IsDigit(ch)
  303. #define Py_UNICODE_ISNUMERIC(ch) _PyUnicode_IsNumeric(ch)
  304. #define Py_UNICODE_TODECIMAL(ch) _PyUnicode_ToDecimalDigit(ch)
  305. #define Py_UNICODE_TODIGIT(ch) _PyUnicode_ToDigit(ch)
  306. #define Py_UNICODE_TONUMERIC(ch) _PyUnicode_ToNumeric(ch)
  307. #define Py_UNICODE_ISALPHA(ch) iswalpha(ch)
  308. #else
  309. /* Since splitting on whitespace is an important use case, and
  310. whitespace in most situations is solely ASCII whitespace, we
  311. optimize for the common case by using a quick look-up table
  312. _Py_ascii_whitespace (see below) with an inlined check.
  313. */
  314. #define Py_UNICODE_ISSPACE(ch) \
  315. ((ch) < 128U ? _Py_ascii_whitespace[(ch)] : _PyUnicode_IsWhitespace(ch))
  316. #define Py_UNICODE_ISLOWER(ch) _PyUnicode_IsLowercase(ch)
  317. #define Py_UNICODE_ISUPPER(ch) _PyUnicode_IsUppercase(ch)
  318. #define Py_UNICODE_ISTITLE(ch) _PyUnicode_IsTitlecase(ch)
  319. #define Py_UNICODE_ISLINEBREAK(ch) _PyUnicode_IsLinebreak(ch)
  320. #define Py_UNICODE_TOLOWER(ch) _PyUnicode_ToLowercase(ch)
  321. #define Py_UNICODE_TOUPPER(ch) _PyUnicode_ToUppercase(ch)
  322. #define Py_UNICODE_TOTITLE(ch) _PyUnicode_ToTitlecase(ch)
  323. #define Py_UNICODE_ISDECIMAL(ch) _PyUnicode_IsDecimalDigit(ch)
  324. #define Py_UNICODE_ISDIGIT(ch) _PyUnicode_IsDigit(ch)
  325. #define Py_UNICODE_ISNUMERIC(ch) _PyUnicode_IsNumeric(ch)
  326. #define Py_UNICODE_TODECIMAL(ch) _PyUnicode_ToDecimalDigit(ch)
  327. #define Py_UNICODE_TODIGIT(ch) _PyUnicode_ToDigit(ch)
  328. #define Py_UNICODE_TONUMERIC(ch) _PyUnicode_ToNumeric(ch)
  329. #define Py_UNICODE_ISALPHA(ch) _PyUnicode_IsAlpha(ch)
  330. #endif
  331. #define Py_UNICODE_ISALNUM(ch) \
  332. (Py_UNICODE_ISALPHA(ch) || \
  333. Py_UNICODE_ISDECIMAL(ch) || \
  334. Py_UNICODE_ISDIGIT(ch) || \
  335. Py_UNICODE_ISNUMERIC(ch))
  336. #define Py_UNICODE_COPY(target, source, length) \
  337. Py_MEMCPY((target), (source), (length)*sizeof(Py_UNICODE))
  338. #define Py_UNICODE_FILL(target, value, length) \
  339. do {Py_ssize_t i_; Py_UNICODE *t_ = (target); Py_UNICODE v_ = (value);\
  340. for (i_ = 0; i_ < (length); i_++) t_[i_] = v_;\
  341. } while (0)
  342. /* Check if substring matches at given offset. the offset must be
  343. valid, and the substring must not be empty */
  344. #define Py_UNICODE_MATCH(string, offset, substring) \
  345. ((*((string)->str + (offset)) == *((substring)->str)) && \
  346. ((*((string)->str + (offset) + (substring)->length-1) == *((substring)->str + (substring)->length-1))) && \
  347. !memcmp((string)->str + (offset), (substring)->str, (substring)->length*sizeof(Py_UNICODE)))
  348. #ifdef __cplusplus
  349. extern "C" {
  350. #endif
  351. /* --- Unicode Type ------------------------------------------------------- */
  352. typedef struct {
  353. PyObject_HEAD
  354. Py_ssize_t length; /* Length of raw Unicode data in buffer */
  355. Py_UNICODE *str; /* Raw Unicode buffer */
  356. long hash; /* Hash value; -1 if not set */
  357. PyObject *defenc; /* (Default) Encoded version as Python
  358. string, or NULL; this is used for
  359. implementing the buffer protocol */
  360. } PyUnicodeObject;
  361. PyAPI_DATA(PyTypeObject) PyUnicode_Type;
  362. #define PyUnicode_Check(op) \
  363. PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_UNICODE_SUBCLASS)
  364. #define PyUnicode_CheckExact(op) (Py_TYPE(op) == &PyUnicode_Type)
  365. /* Fast access macros */
  366. #define PyUnicode_GET_SIZE(op) \
  367. (((PyUnicodeObject *)(op))->length)
  368. #define PyUnicode_GET_DATA_SIZE(op) \
  369. (((PyUnicodeObject *)(op))->length * sizeof(Py_UNICODE))
  370. #define PyUnicode_AS_UNICODE(op) \
  371. (((PyUnicodeObject *)(op))->str)
  372. #define PyUnicode_AS_DATA(op) \
  373. ((const char *)((PyUnicodeObject *)(op))->str)
  374. /* --- Constants ---------------------------------------------------------- */
  375. /* This Unicode character will be used as replacement character during
  376. decoding if the errors argument is set to "replace". Note: the
  377. Unicode character U+FFFD is the official REPLACEMENT CHARACTER in
  378. Unicode 3.0. */
  379. #define Py_UNICODE_REPLACEMENT_CHARACTER ((Py_UNICODE) 0xFFFD)
  380. /* === Public API ========================================================= */
  381. /* --- Plain Py_UNICODE --------------------------------------------------- */
  382. /* Create a Unicode Object from the Py_UNICODE buffer u of the given
  383. size.
  384. u may be NULL which causes the contents to be undefined. It is the
  385. user's responsibility to fill in the needed data afterwards. Note
  386. that modifying the Unicode object contents after construction is
  387. only allowed if u was set to NULL.
  388. The buffer is copied into the new object. */
  389. PyAPI_FUNC(PyObject*) PyUnicode_FromUnicode(
  390. const Py_UNICODE *u, /* Unicode buffer */
  391. Py_ssize_t size /* size of buffer */
  392. );
  393. /* Similar to PyUnicode_FromUnicode(), but u points to Latin-1 encoded bytes */
  394. PyAPI_FUNC(PyObject*) PyUnicode_FromStringAndSize(
  395. const char *u, /* char buffer */
  396. Py_ssize_t size /* size of buffer */
  397. );
  398. /* Similar to PyUnicode_FromUnicode(), but u points to null-terminated
  399. Latin-1 encoded bytes */
  400. PyAPI_FUNC(PyObject*) PyUnicode_FromString(
  401. const char *u /* string */
  402. );
  403. /* Return a read-only pointer to the Unicode object's internal
  404. Py_UNICODE buffer. */
  405. PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicode(
  406. PyObject *unicode /* Unicode object */
  407. );
  408. /* Get the length of the Unicode object. */
  409. PyAPI_FUNC(Py_ssize_t) PyUnicode_GetSize(
  410. PyObject *unicode /* Unicode object */
  411. );
  412. /* Get the maximum ordinal for a Unicode character. */
  413. PyAPI_FUNC(Py_UNICODE) PyUnicode_GetMax(void);
  414. /* Resize an already allocated Unicode object to the new size length.
  415. *unicode is modified to point to the new (resized) object and 0
  416. returned on success.
  417. This API may only be called by the function which also called the
  418. Unicode constructor. The refcount on the object must be 1. Otherwise,
  419. an error is returned.
  420. Error handling is implemented as follows: an exception is set, -1
  421. is returned and *unicode left untouched.
  422. */
  423. PyAPI_FUNC(int) PyUnicode_Resize(
  424. PyObject **unicode, /* Pointer to the Unicode object */
  425. Py_ssize_t length /* New length */
  426. );
  427. /* Coerce obj to a Unicode object and return a reference with
  428. *incremented* refcount.
  429. Coercion is done in the following way:
  430. 1. String and other char buffer compatible objects are decoded
  431. under the assumptions that they contain data using the current
  432. default encoding. Decoding is done in "strict" mode.
  433. 2. All other objects (including Unicode objects) raise an
  434. exception.
  435. The API returns NULL in case of an error. The caller is responsible
  436. for decref'ing the returned objects.
  437. */
  438. PyAPI_FUNC(PyObject*) PyUnicode_FromEncodedObject(
  439. register PyObject *obj, /* Object */
  440. const char *encoding, /* encoding */
  441. const char *errors /* error handling */
  442. );
  443. /* Coerce obj to a Unicode object and return a reference with
  444. *incremented* refcount.
  445. Unicode objects are passed back as-is (subclasses are converted to
  446. true Unicode objects), all other objects are delegated to
  447. PyUnicode_FromEncodedObject(obj, NULL, "strict") which results in
  448. using the default encoding as basis for decoding the object.
  449. The API returns NULL in case of an error. The caller is responsible
  450. for decref'ing the returned objects.
  451. */
  452. PyAPI_FUNC(PyObject*) PyUnicode_FromObject(
  453. register PyObject *obj /* Object */
  454. );
  455. PyAPI_FUNC(PyObject *) PyUnicode_FromFormatV(const char*, va_list);
  456. PyAPI_FUNC(PyObject *) PyUnicode_FromFormat(const char*, ...);
  457. /* Format the object based on the format_spec, as defined in PEP 3101
  458. (Advanced String Formatting). */
  459. PyAPI_FUNC(PyObject *) _PyUnicode_FormatAdvanced(PyObject *obj,
  460. Py_UNICODE *format_spec,
  461. Py_ssize_t format_spec_len);
  462. /* --- wchar_t support for platforms which support it --------------------- */
  463. #ifdef HAVE_WCHAR_H
  464. /* Create a Unicode Object from the whcar_t buffer w of the given
  465. size.
  466. The buffer is copied into the new object. */
  467. PyAPI_FUNC(PyObject*) PyUnicode_FromWideChar(
  468. register const wchar_t *w, /* wchar_t buffer */
  469. Py_ssize_t size /* size of buffer */
  470. );
  471. /* Copies the Unicode Object contents into the wchar_t buffer w. At
  472. most size wchar_t characters are copied.
  473. Note that the resulting wchar_t string may or may not be
  474. 0-terminated. It is the responsibility of the caller to make sure
  475. that the wchar_t string is 0-terminated in case this is required by
  476. the application.
  477. Returns the number of wchar_t characters copied (excluding a
  478. possibly trailing 0-termination character) or -1 in case of an
  479. error. */
  480. PyAPI_FUNC(Py_ssize_t) PyUnicode_AsWideChar(
  481. PyUnicodeObject *unicode, /* Unicode object */
  482. register wchar_t *w, /* wchar_t buffer */
  483. Py_ssize_t size /* size of buffer */
  484. );
  485. #endif
  486. /* --- Unicode ordinals --------------------------------------------------- */
  487. /* Create a Unicode Object from the given Unicode code point ordinal.
  488. The ordinal must be in range(0x10000) on narrow Python builds
  489. (UCS2), and range(0x110000) on wide builds (UCS4). A ValueError is
  490. raised in case it is not.
  491. */
  492. PyAPI_FUNC(PyObject*) PyUnicode_FromOrdinal(int ordinal);
  493. /* --- Free-list management ----------------------------------------------- */
  494. /* Clear the free list used by the Unicode implementation.
  495. This can be used to release memory used for objects on the free
  496. list back to the Python memory allocator.
  497. */
  498. PyAPI_FUNC(int) PyUnicode_ClearFreeList(void);
  499. /* === Builtin Codecs =====================================================
  500. Many of these APIs take two arguments encoding and errors. These
  501. parameters encoding and errors have the same semantics as the ones
  502. of the builtin unicode() API.
  503. Setting encoding to NULL causes the default encoding to be used.
  504. Error handling is set by errors which may also be set to NULL
  505. meaning to use the default handling defined for the codec. Default
  506. error handling for all builtin codecs is "strict" (ValueErrors are
  507. raised).
  508. The codecs all use a similar interface. Only deviation from the
  509. generic ones are documented.
  510. */
  511. /* --- Manage the default encoding ---------------------------------------- */
  512. /* Return a Python string holding the default encoded value of the
  513. Unicode object.
  514. The resulting string is cached in the Unicode object for subsequent
  515. usage by this function. The cached version is needed to implement
  516. the character buffer interface and will live (at least) as long as
  517. the Unicode object itself.
  518. The refcount of the string is *not* incremented.
  519. *** Exported for internal use by the interpreter only !!! ***
  520. */
  521. PyAPI_FUNC(PyObject *) _PyUnicode_AsDefaultEncodedString(
  522. PyObject *, const char *);
  523. /* Returns the currently active default encoding.
  524. The default encoding is currently implemented as run-time settable
  525. process global. This may change in future versions of the
  526. interpreter to become a parameter which is managed on a per-thread
  527. basis.
  528. */
  529. PyAPI_FUNC(const char*) PyUnicode_GetDefaultEncoding(void);
  530. /* Sets the currently active default encoding.
  531. Returns 0 on success, -1 in case of an error.
  532. */
  533. PyAPI_FUNC(int) PyUnicode_SetDefaultEncoding(
  534. const char *encoding /* Encoding name in standard form */
  535. );
  536. /* --- Generic Codecs ----------------------------------------------------- */
  537. /* Create a Unicode object by decoding the encoded string s of the
  538. given size. */
  539. PyAPI_FUNC(PyObject*) PyUnicode_Decode(
  540. const char *s, /* encoded string */
  541. Py_ssize_t size, /* size of buffer */
  542. const char *encoding, /* encoding */
  543. const char *errors /* error handling */
  544. );
  545. /* Encodes a Py_UNICODE buffer of the given size and returns a
  546. Python string object. */
  547. PyAPI_FUNC(PyObject*) PyUnicode_Encode(
  548. const Py_UNICODE *s, /* Unicode char buffer */
  549. Py_ssize_t size, /* number of Py_UNICODE chars to encode */
  550. const char *encoding, /* encoding */
  551. const char *errors /* error handling */
  552. );
  553. /* Encodes a Unicode object and returns the result as Python
  554. object. */
  555. PyAPI_FUNC(PyObject*) PyUnicode_AsEncodedObject(
  556. PyObject *unicode, /* Unicode object */
  557. const char *encoding, /* encoding */
  558. const char *errors /* error handling */
  559. );
  560. /* Encodes a Unicode object and returns the result as Python string
  561. object. */
  562. PyAPI_FUNC(PyObject*) PyUnicode_AsEncodedString(
  563. PyObject *unicode, /* Unicode object */
  564. const char *encoding, /* encoding */
  565. const char *errors /* error handling */
  566. );
  567. PyAPI_FUNC(PyObject*) PyUnicode_BuildEncodingMap(
  568. PyObject* string /* 256 character map */
  569. );
  570. /* --- UTF-7 Codecs ------------------------------------------------------- */
  571. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF7(
  572. const char *string, /* UTF-7 encoded string */
  573. Py_ssize_t length, /* size of string */
  574. const char *errors /* error handling */
  575. );
  576. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF7Stateful(
  577. const char *string, /* UTF-7 encoded string */
  578. Py_ssize_t length, /* size of string */
  579. const char *errors, /* error handling */
  580. Py_ssize_t *consumed /* bytes consumed */
  581. );
  582. PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF7(
  583. const Py_UNICODE *data, /* Unicode char buffer */
  584. Py_ssize_t length, /* number of Py_UNICODE chars to encode */
  585. int base64SetO, /* Encode RFC2152 Set O characters in base64 */
  586. int base64WhiteSpace, /* Encode whitespace (sp, ht, nl, cr) in base64 */
  587. const char *errors /* error handling */
  588. );
  589. /* --- UTF-8 Codecs ------------------------------------------------------- */
  590. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF8(
  591. const char *string, /* UTF-8 encoded string */
  592. Py_ssize_t length, /* size of string */
  593. const char *errors /* error handling */
  594. );
  595. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF8Stateful(
  596. const char *string, /* UTF-8 encoded string */
  597. Py_ssize_t length, /* size of string */
  598. const char *errors, /* error handling */
  599. Py_ssize_t *consumed /* bytes consumed */
  600. );
  601. PyAPI_FUNC(PyObject*) PyUnicode_AsUTF8String(
  602. PyObject *unicode /* Unicode object */
  603. );
  604. PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF8(
  605. const Py_UNICODE *data, /* Unicode char buffer */
  606. Py_ssize_t length, /* number of Py_UNICODE chars to encode */
  607. const char *errors /* error handling */
  608. );
  609. /* --- UTF-32 Codecs ------------------------------------------------------ */
  610. /* Decodes length bytes from a UTF-32 encoded buffer string and returns
  611. the corresponding Unicode object.
  612. errors (if non-NULL) defines the error handling. It defaults
  613. to "strict".
  614. If byteorder is non-NULL, the decoder starts decoding using the
  615. given byte order:
  616. *byteorder == -1: little endian
  617. *byteorder == 0: native order
  618. *byteorder == 1: big endian
  619. In native mode, the first four bytes of the stream are checked for a
  620. BOM mark. If found, the BOM mark is analysed, the byte order
  621. adjusted and the BOM skipped. In the other modes, no BOM mark
  622. interpretation is done. After completion, *byteorder is set to the
  623. current byte order at the end of input data.
  624. If byteorder is NULL, the codec starts in native order mode.
  625. */
  626. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF32(
  627. const char *string, /* UTF-32 encoded string */
  628. Py_ssize_t length, /* size of string */
  629. const char *errors, /* error handling */
  630. int *byteorder /* pointer to byteorder to use
  631. 0=native;-1=LE,1=BE; updated on
  632. exit */
  633. );
  634. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF32Stateful(
  635. const char *string, /* UTF-32 encoded string */
  636. Py_ssize_t length, /* size of string */
  637. const char *errors, /* error handling */
  638. int *byteorder, /* pointer to byteorder to use
  639. 0=native;-1=LE,1=BE; updated on
  640. exit */
  641. Py_ssize_t *consumed /* bytes consumed */
  642. );
  643. /* Returns a Python string using the UTF-32 encoding in native byte
  644. order. The string always starts with a BOM mark. */
  645. PyAPI_FUNC(PyObject*) PyUnicode_AsUTF32String(
  646. PyObject *unicode /* Unicode object */
  647. );
  648. /* Returns a Python string object holding the UTF-32 encoded value of
  649. the Unicode data.
  650. If byteorder is not 0, output is written according to the following
  651. byte order:
  652. byteorder == -1: little endian
  653. byteorder == 0: native byte order (writes a BOM mark)
  654. byteorder == 1: big endian
  655. If byteorder is 0, the output string will always start with the
  656. Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark is
  657. prepended.
  658. */
  659. PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF32(
  660. const Py_UNICODE *data, /* Unicode char buffer */
  661. Py_ssize_t length, /* number of Py_UNICODE chars to encode */
  662. const char *errors, /* error handling */
  663. int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
  664. );
  665. /* --- UTF-16 Codecs ------------------------------------------------------ */
  666. /* Decodes length bytes from a UTF-16 encoded buffer string and returns
  667. the corresponding Unicode object.
  668. errors (if non-NULL) defines the error handling. It defaults
  669. to "strict".
  670. If byteorder is non-NULL, the decoder starts decoding using the
  671. given byte order:
  672. *byteorder == -1: little endian
  673. *byteorder == 0: native order
  674. *byteorder == 1: big endian
  675. In native mode, the first two bytes of the stream are checked for a
  676. BOM mark. If found, the BOM mark is analysed, the byte order
  677. adjusted and the BOM skipped. In the other modes, no BOM mark
  678. interpretation is done. After completion, *byteorder is set to the
  679. current byte order at the end of input data.
  680. If byteorder is NULL, the codec starts in native order mode.
  681. */
  682. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF16(
  683. const char *string, /* UTF-16 encoded string */
  684. Py_ssize_t length, /* size of string */
  685. const char *errors, /* error handling */
  686. int *byteorder /* pointer to byteorder to use
  687. 0=native;-1=LE,1=BE; updated on
  688. exit */
  689. );
  690. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF16Stateful(
  691. const char *string, /* UTF-16 encoded string */
  692. Py_ssize_t length, /* size of string */
  693. const char *errors, /* error handling */
  694. int *byteorder, /* pointer to byteorder to use
  695. 0=native;-1=LE,1=BE; updated on
  696. exit */
  697. Py_ssize_t *consumed /* bytes consumed */
  698. );
  699. /* Returns a Python string using the UTF-16 encoding in native byte
  700. order. The string always starts with a BOM mark. */
  701. PyAPI_FUNC(PyObject*) PyUnicode_AsUTF16String(
  702. PyObject *unicode /* Unicode object */
  703. );
  704. /* Returns a Python string object holding the UTF-16 encoded value of
  705. the Unicode data.
  706. If byteorder is not 0, output is written according to the following
  707. byte order:
  708. byteorder == -1: little endian
  709. byteorder == 0: native byte order (writes a BOM mark)
  710. byteorder == 1: big endian
  711. If byteorder is 0, the output string will always start with the
  712. Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark is
  713. prepended.
  714. Note that Py_UNICODE data is being interpreted as UTF-16 reduced to
  715. UCS-2. This trick makes it possible to add full UTF-16 capabilities
  716. at a later point without compromising the APIs.
  717. */
  718. PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF16(
  719. const Py_UNICODE *data, /* Unicode char buffer */
  720. Py_ssize_t length, /* number of Py_UNICODE chars to encode */
  721. const char *errors, /* error handling */
  722. int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
  723. );
  724. /* --- Unicode-Escape Codecs ---------------------------------------------- */
  725. PyAPI_FUNC(PyObject*) PyUnicode_DecodeUnicodeEscape(
  726. const char *string, /* Unicode-Escape encoded string */
  727. Py_ssize_t length, /* size of string */
  728. const char *errors /* error handling */
  729. );
  730. PyAPI_FUNC(PyObject*) PyUnicode_AsUnicodeEscapeString(
  731. PyObject *unicode /* Unicode object */
  732. );
  733. PyAPI_FUNC(PyObject*) PyUnicode_EncodeUnicodeEscape(
  734. const Py_UNICODE *data, /* Unicode char buffer */
  735. Py_ssize_t length /* Number of Py_UNICODE chars to encode */
  736. );
  737. /* --- Raw-Unicode-Escape Codecs ------------------------------------------ */
  738. PyAPI_FUNC(PyObject*) PyUnicode_DecodeRawUnicodeEscape(
  739. const char *string, /* Raw-Unicode-Escape encoded string */
  740. Py_ssize_t length, /* size of string */
  741. const char *errors /* error handling */
  742. );
  743. PyAPI_FUNC(PyObject*) PyUnicode_AsRawUnicodeEscapeString(
  744. PyObject *unicode /* Unicode object */
  745. );
  746. PyAPI_FUNC(PyObject*) PyUnicode_EncodeRawUnicodeEscape(
  747. const Py_UNICODE *data, /* Unicode char buffer */
  748. Py_ssize_t length /* Number of Py_UNICODE chars to encode */
  749. );
  750. /* --- Unicode Internal Codec ---------------------------------------------
  751. Only for internal use in _codecsmodule.c */
  752. PyObject *_PyUnicode_DecodeUnicodeInternal(
  753. const char *string,
  754. Py_ssize_t length,
  755. const char *errors
  756. );
  757. /* --- Latin-1 Codecs -----------------------------------------------------
  758. Note: Latin-1 corresponds to the first 256 Unicode ordinals.
  759. */
  760. PyAPI_FUNC(PyObject*) PyUnicode_DecodeLatin1(
  761. const char *string, /* Latin-1 encoded string */
  762. Py_ssize_t length, /* size of string */
  763. const char *errors /* error handling */
  764. );
  765. PyAPI_FUNC(PyObject*) PyUnicode_AsLatin1String(
  766. PyObject *unicode /* Unicode object */
  767. );
  768. PyAPI_FUNC(PyObject*) PyUnicode_EncodeLatin1(
  769. const Py_UNICODE *data, /* Unicode char buffer */
  770. Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
  771. const char *errors /* error handling */
  772. );
  773. /* --- ASCII Codecs -------------------------------------------------------
  774. Only 7-bit ASCII data is excepted. All other codes generate errors.
  775. */
  776. PyAPI_FUNC(PyObject*) PyUnicode_DecodeASCII(
  777. const char *string, /* ASCII encoded string */
  778. Py_ssize_t length, /* size of string */
  779. const char *errors /* error handling */
  780. );
  781. PyAPI_FUNC(PyObject*) PyUnicode_AsASCIIString(
  782. PyObject *unicode /* Unicode object */
  783. );
  784. PyAPI_FUNC(PyObject*) PyUnicode_EncodeASCII(
  785. const Py_UNICODE *data, /* Unicode char buffer */
  786. Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
  787. const char *errors /* error handling */
  788. );
  789. /* --- Character Map Codecs -----------------------------------------------
  790. This codec uses mappings to encode and decode characters.
  791. Decoding mappings must map single string characters to single
  792. Unicode characters, integers (which are then interpreted as Unicode
  793. ordinals) or None (meaning "undefined mapping" and causing an
  794. error).
  795. Encoding mappings must map single Unicode characters to single
  796. string characters, integers (which are then interpreted as Latin-1
  797. ordinals) or None (meaning "undefined mapping" and causing an
  798. error).
  799. If a character lookup fails with a LookupError, the character is
  800. copied as-is meaning that its ordinal value will be interpreted as
  801. Unicode or Latin-1 ordinal resp. Because of this mappings only need
  802. to contain those mappings which map characters to different code
  803. points.
  804. */
  805. PyAPI_FUNC(PyObject*) PyUnicode_DecodeCharmap(
  806. const char *string, /* Encoded string */
  807. Py_ssize_t length, /* size of string */
  808. PyObject *mapping, /* character mapping
  809. (char ordinal -> unicode ordinal) */
  810. const char *errors /* error handling */
  811. );
  812. PyAPI_FUNC(PyObject*) PyUnicode_AsCharmapString(
  813. PyObject *unicode, /* Unicode object */
  814. PyObject *mapping /* character mapping
  815. (unicode ordinal -> char ordinal) */
  816. );
  817. PyAPI_FUNC(PyObject*) PyUnicode_EncodeCharmap(
  818. const Py_UNICODE *data, /* Unicode char buffer */
  819. Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
  820. PyObject *mapping, /* character mapping
  821. (unicode ordinal -> char ordinal) */
  822. const char *errors /* error handling */
  823. );
  824. /* Translate a Py_UNICODE buffer of the given length by applying a
  825. character mapping table to it and return the resulting Unicode
  826. object.
  827. The mapping table must map Unicode ordinal integers to Unicode
  828. ordinal integers or None (causing deletion of the character).
  829. Mapping tables may be dictionaries or sequences. Unmapped character
  830. ordinals (ones which cause a LookupError) are left untouched and
  831. are copied as-is.
  832. */
  833. PyAPI_FUNC(PyObject *) PyUnicode_TranslateCharmap(
  834. const Py_UNICODE *data, /* Unicode char buffer */
  835. Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
  836. PyObject *table, /* Translate table */
  837. const char *errors /* error handling */
  838. );
  839. #ifdef MS_WIN32
  840. /* --- MBCS codecs for Windows -------------------------------------------- */
  841. PyAPI_FUNC(PyObject*) PyUnicode_DecodeMBCS(
  842. const char *string, /* MBCS encoded string */
  843. Py_ssize_t length, /* size of string */
  844. const char *errors /* error handling */
  845. );
  846. PyAPI_FUNC(PyObject*) PyUnicode_DecodeMBCSStateful(
  847. const char *string, /* MBCS encoded string */
  848. Py_ssize_t length, /* size of string */
  849. const char *errors, /* error handling */
  850. Py_ssize_t *consumed /* bytes consumed */
  851. );
  852. PyAPI_FUNC(PyObject*) PyUnicode_AsMBCSString(
  853. PyObject *unicode /* Unicode object */
  854. );
  855. PyAPI_FUNC(PyObject*) PyUnicode_EncodeMBCS(
  856. const Py_UNICODE *data, /* Unicode char buffer */
  857. Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
  858. const char *errors /* error handling */
  859. );
  860. #endif /* MS_WIN32 */
  861. /* --- Decimal Encoder ---------------------------------------------------- */
  862. /* Takes a Unicode string holding a decimal value and writes it into
  863. an output buffer using standard ASCII digit codes.
  864. The output buffer has to provide at least length+1 bytes of storage
  865. area. The output string is 0-terminated.
  866. The encoder converts whitespace to ' ', decimal characters to their
  867. corresponding ASCII digit and all other Latin-1 characters except
  868. \0 as-is. Characters outside this range (Unicode ordinals 1-256)
  869. are treated as errors. This includes embedded NULL bytes.
  870. Error handling is defined by the errors argument:
  871. NULL or "strict": raise a ValueError
  872. "ignore": ignore the wrong characters (these are not copied to the
  873. output buffer)
  874. "replace": replaces illegal characters with '?'
  875. Returns 0 on success, -1 on failure.
  876. */
  877. PyAPI_FUNC(int) PyUnicode_EncodeDecimal(
  878. Py_UNICODE *s, /* Unicode buffer */
  879. Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
  880. char *output, /* Output buffer; must have size >= length */
  881. const char *errors /* error handling */
  882. );
  883. /* --- Methods & Slots ----------------------------------------------------
  884. These are capable of handling Unicode objects and strings on input
  885. (we refer to them as strings in the descriptions) and return
  886. Unicode objects or integers as apporpriate. */
  887. /* Concat two strings giving a new Unicode string. */
  888. PyAPI_FUNC(PyObject*) PyUnicode_Concat(
  889. PyObject *left, /* Left string */
  890. PyObject *right /* Right string */
  891. );
  892. /* Split a string giving a list of Unicode strings.
  893. If sep is NULL, splitting will be done at all whitespace
  894. substrings. Otherwise, splits occur at the given separator.
  895. At most maxsplit splits will be done. If negative, no limit is set.
  896. Separators are not included in the resulting list.
  897. */
  898. PyAPI_FUNC(PyObject*) PyUnicode_Split(
  899. PyObject *s, /* String to split */
  900. PyObject *sep, /* String separator */
  901. Py_ssize_t maxsplit /* Maxsplit count */
  902. );
  903. /* Dito, but split at line breaks.
  904. CRLF is considered to be one line break. Line breaks are not
  905. included in the resulting list. */
  906. PyAPI_FUNC(PyObject*) PyUnicode_Splitlines(
  907. PyObject *s, /* String to split */
  908. int keepends /* If true, line end markers are included */
  909. );
  910. /* Partition a string using a given separator. */
  911. PyAPI_FUNC(PyObject*) PyUnicode_Partition(
  912. PyObject *s, /* String to partition */
  913. PyObject *sep /* String separator */
  914. );
  915. /* Partition a string using a given separator, searching from the end of the
  916. string. */
  917. PyAPI_FUNC(PyObject*) PyUnicode_RPartition(
  918. PyObject *s, /* String to partition */
  919. PyObject *sep /* String separator */
  920. );
  921. /* Split a string giving a list of Unicode strings.
  922. If sep is NULL, splitting will be done at all whitespace
  923. substrings. Otherwise, splits occur at the given separator.
  924. At most maxsplit splits will be done. But unlike PyUnicode_Split
  925. PyUnicode_RSplit splits from the end of the string. If negative,
  926. no limit is set.
  927. Separators are not included in the resulting list.
  928. */
  929. PyAPI_FUNC(PyObject*) PyUnicode_RSplit(
  930. PyObject *s, /* String to split */
  931. PyObject *sep, /* String separator */
  932. Py_ssize_t maxsplit /* Maxsplit count */
  933. );
  934. /* Translate a string by applying a character mapping table to it and
  935. return the resulting Unicode object.
  936. The mapping table must map Unicode ordinal integers to Unicode
  937. ordinal integers or None (causing deletion of the character).
  938. Mapping tables may be dictionaries or sequences. Unmapped character
  939. ordinals (ones which cause a LookupError) are left untouched and
  940. are copied as-is.
  941. */
  942. PyAPI_FUNC(PyObject *) PyUnicode_Translate(
  943. PyObject *str, /* String */
  944. PyObject *table, /* Translate table */
  945. const char *errors /* error handling */
  946. );
  947. /* Join a sequence of strings using the given separator and return
  948. the resulting Unicode string. */
  949. PyAPI_FUNC(PyObject*) PyUnicode_Join(
  950. PyObject *separator, /* Separator string */
  951. PyObject *seq /* Sequence object */
  952. );
  953. /* Return 1 if substr matches str[start:end] at the given tail end, 0
  954. otherwise. */
  955. PyAPI_FUNC(Py_ssize_t) PyUnicode_Tailmatch(
  956. PyObject *str, /* String */
  957. PyObject *substr, /* Prefix or Suffix string */
  958. Py_ssize_t start, /* Start index */
  959. Py_ssize_t end, /* Stop index */
  960. int direction /* Tail end: -1 prefix, +1 suffix */
  961. );
  962. /* Return the first position of substr in str[start:end] using the
  963. given search direction or -1 if not found. -2 is returned in case
  964. an error occurred and an exception is set. */
  965. PyAPI_FUNC(Py_ssize_t) PyUnicode_Find(
  966. PyObject *str, /* String */
  967. PyObject *substr, /* Substring to find */
  968. Py_ssize_t start, /* Start index */
  969. Py_ssize_t end, /* Stop index */
  970. int direction /* Find direction: +1 forward, -1 backward */
  971. );
  972. /* Count the number of occurrences of substr in str[start:end]. */
  973. PyAPI_FUNC(Py_ssize_t) PyUnicode_Count(
  974. PyObject *str, /* String */
  975. PyObject *substr, /* Substring to count */
  976. Py_ssize_t start, /* Start index */
  977. Py_ssize_t end /* Stop index */
  978. );
  979. /* Replace at most maxcount occurrences of substr in str with replstr
  980. and return the resulting Unicode object. */
  981. PyAPI_FUNC(PyObject *) PyUnicode_Replace(
  982. PyObject *str, /* String */
  983. PyObject *substr, /* Substring to find */
  984. PyObject *replstr, /* Substring to replace */
  985. Py_ssize_t maxcount /* Max. number of replacements to apply;
  986. -1 = all */
  987. );
  988. /* Compare two strings and return -1, 0, 1 for less than, equal,
  989. greater than resp. */
  990. PyAPI_FUNC(int) PyUnicode_Compare(
  991. PyObject *left, /* Left string */
  992. PyObject *right /* Right string */
  993. );
  994. /* Rich compare two strings and return one of the following:
  995. - NULL in case an exception was raised
  996. - Py_True or Py_False for successfuly comparisons
  997. - Py_NotImplemented in case the type combination is unknown
  998. Note that Py_EQ and Py_NE comparisons can cause a UnicodeWarning in
  999. case the conversion of the arguments to Unicode fails with a
  1000. UnicodeDecodeError.
  1001. Possible values for op:
  1002. Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE
  1003. */
  1004. PyAPI_FUNC(PyObject *) PyUnicode_RichCompare(
  1005. PyObject *left, /* Left string */
  1006. PyObject *right, /* Right string */
  1007. int op /* Operation: Py_EQ, Py_NE, Py_GT, etc. */
  1008. );
  1009. /* Apply an argument tuple or dictionary to a format string and return
  1010. the resulting Unicode string. */
  1011. PyAPI_FUNC(PyObject *) PyUnicode_Format(
  1012. PyObject *format, /* Format string */
  1013. PyObject *args /* Argument tuple or dictionary */
  1014. );
  1015. /* Checks whether element is contained in container and return 1/0
  1016. accordingly.
  1017. element has to coerce to a one element Unicode string. -1 is
  1018. returned in case of an error. */
  1019. PyAPI_FUNC(int) PyUnicode_Contains(
  1020. PyObject *container, /* Container string */
  1021. PyObject *element /* Element string */
  1022. );
  1023. /* Externally visible for str.strip(unicode) */
  1024. PyAPI_FUNC(PyObject *) _PyUnicode_XStrip(
  1025. PyUnicodeObject *self,
  1026. int striptype,
  1027. PyObject *sepobj
  1028. );
  1029. /* === Characters Type APIs =============================================== */
  1030. /* Helper array used by Py_UNICODE_ISSPACE(). */
  1031. PyAPI_DATA(const unsigned char) _Py_ascii_whitespace[];
  1032. /* These should not be used directly. Use the Py_UNICODE_IS* and
  1033. Py_UNICODE_TO* macros instead.
  1034. These APIs are implemented in Objects/unicodectype.c.
  1035. */
  1036. PyAPI_FUNC(int) _PyUnicode_IsLowercase(
  1037. Py_UNICODE ch /* Unicode character */
  1038. );
  1039. PyAPI_FUNC(int) _PyUnicode_IsUppercase(
  1040. Py_UNICODE ch /* Unicode character */
  1041. );
  1042. PyAPI_FUNC(int) _PyUnicode_IsTitlecase(
  1043. Py_UNICODE ch /* Unicode character */
  1044. );
  1045. PyAPI_FUNC(int) _PyUnicode_IsWhitespace(
  1046. const Py_UNICODE ch /* Unicode character */
  1047. );
  1048. PyAPI_FUNC(int) _PyUnicode_IsLinebreak(
  1049. const Py_UNICODE ch /* Unicode character */
  1050. );
  1051. PyAPI_FUNC(Py_UNICODE) _PyUnicode_ToLowercase(
  1052. Py_UNICODE ch /* Unicode character */
  1053. );
  1054. PyAPI_FUNC(Py_UNICODE) _PyUnicode_ToUppercase(
  1055. Py_UNICODE ch /* Unicode character */
  1056. );
  1057. PyAPI_FUNC(Py_UNICODE) _PyUnicode_ToTitlecase(
  1058. Py_UNICODE ch /* Unicode character */
  1059. );
  1060. PyAPI_FUNC(int) _PyUnicode_ToDecimalDigit(
  1061. Py_UNICODE ch /* Unicode character */
  1062. );
  1063. PyAPI_FUNC(int) _PyUnicode_ToDigit(
  1064. Py_UNICODE ch /* Unicode character */
  1065. );
  1066. PyAPI_FUNC(double) _PyUnicode_ToNumeric(
  1067. Py_UNICODE ch /* Unicode character */
  1068. );
  1069. PyAPI_FUNC(int) _PyUnicode_IsDecimalDigit(
  1070. Py_UNICODE ch /* Unicode character */
  1071. );
  1072. PyAPI_FUNC(int) _PyUnicode_IsDigit(
  1073. Py_UNICODE ch /* Unicode character */
  1074. );
  1075. PyAPI_FUNC(int) _PyUnicode_IsNumeric(
  1076. Py_UNICODE ch /* Unicode character */
  1077. );
  1078. PyAPI_FUNC(int) _PyUnicode_IsAlpha(
  1079. Py_UNICODE ch /* Unicode character */
  1080. );
  1081. #ifdef __cplusplus
  1082. }
  1083. #endif
  1084. #endif /* Py_USING_UNICODE */
  1085. #endif /* !Py_UNICODEOBJECT_H */