blob: 7ae6506ed160dc0443248bfd7ee82634c868d83e [file] [log] [blame]
Guido van Rossumd8225182000-03-10 22:33:05 +00001#ifndef Py_UNICODEOBJECT_H
2#define Py_UNICODEOBJECT_H
Guido van Rossumd8225182000-03-10 22:33:05 +00003
Christian Heimesaf98da12008-01-27 15:18:18 +00004#include <stdarg.h>
5
Guido van Rossumd8225182000-03-10 22:33:05 +00006/*
7
8Unicode implementation based on original code by Fredrik Lundh,
9modified by Marc-Andre Lemburg (mal@lemburg.com) according to the
10Unicode Integration Proposal (see file Misc/unicode.txt).
11
Guido van Rossum16b1ad92000-08-03 16:24:25 +000012Copyright (c) Corporation for National Research Initiatives.
Guido van Rossumd8225182000-03-10 22:33:05 +000013
14
15 Original header:
16 --------------------------------------------------------------------
17
18 * Yet another Unicode string type for Python. This type supports the
19 * 16-bit Basic Multilingual Plane (BMP) only.
20 *
21 * Written by Fredrik Lundh, January 1999.
22 *
23 * Copyright (c) 1999 by Secret Labs AB.
24 * Copyright (c) 1999 by Fredrik Lundh.
25 *
26 * fredrik@pythonware.com
27 * http://www.pythonware.com
28 *
29 * --------------------------------------------------------------------
30 * This Unicode String Type is
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000031 *
Guido van Rossumd8225182000-03-10 22:33:05 +000032 * Copyright (c) 1999 by Secret Labs AB
33 * Copyright (c) 1999 by Fredrik Lundh
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000034 *
Guido van Rossumd8225182000-03-10 22:33:05 +000035 * By obtaining, using, and/or copying this software and/or its
36 * associated documentation, you agree that you have read, understood,
37 * and will comply with the following terms and conditions:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 *
Guido van Rossumd8225182000-03-10 22:33:05 +000039 * Permission to use, copy, modify, and distribute this software and its
40 * associated documentation for any purpose and without fee is hereby
41 * granted, provided that the above copyright notice appears in all
42 * copies, and that both that copyright notice and this permission notice
43 * appear in supporting documentation, and that the name of Secret Labs
44 * AB or the author not be used in advertising or publicity pertaining to
45 * distribution of the software without specific, written prior
46 * permission.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 *
Guido van Rossumd8225182000-03-10 22:33:05 +000048 * SECRET LABS AB AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO
49 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
50 * FITNESS. IN NO EVENT SHALL SECRET LABS AB OR THE AUTHOR BE LIABLE FOR
51 * ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
52 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
53 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
54 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
55 * -------------------------------------------------------------------- */
56
Marc-André Lemburg5e6007c2001-09-19 11:21:03 +000057#include <ctype.h>
Guido van Rossumd8225182000-03-10 22:33:05 +000058
59/* === Internal API ======================================================= */
60
61/* --- Internal Unicode Format -------------------------------------------- */
62
Christian Heimes0625e892008-01-07 21:04:21 +000063/* Python 3.x requires unicode */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064#define Py_USING_UNICODE
Christian Heimes0625e892008-01-07 21:04:21 +000065
Fredrik Lundh9b14ab32001-06-26 22:59:49 +000066/* FIXME: MvL's new implementation assumes that Py_UNICODE_SIZE is
67 properly set, but the default rules below doesn't set it. I'll
68 sort this out some other day -- fredrik@pythonware.com */
69
70#ifndef Py_UNICODE_SIZE
71#error Must define Py_UNICODE_SIZE
72#endif
73
Fredrik Lundh8f455852001-06-27 18:59:43 +000074/* Setting Py_UNICODE_WIDE enables UCS-4 storage. Otherwise, Unicode
75 strings are stored as UCS-2 (with limited support for UTF-16) */
76
77#if Py_UNICODE_SIZE >= 4
78#define Py_UNICODE_WIDE
Martin v. Löwis0ba70cc2001-06-26 22:22:37 +000079#endif
Fredrik Lundh1294ad02001-06-26 17:17:07 +000080
Guido van Rossumd8225182000-03-10 22:33:05 +000081/* Set these flags if the platform has "wchar.h", "wctype.h" and the
82 wchar_t type is a 16-bit unsigned type */
83/* #define HAVE_WCHAR_H */
84/* #define HAVE_USABLE_WCHAR_T */
85
86/* Defaults for various platforms */
Martin v. Löwis0ba70cc2001-06-26 22:22:37 +000087#ifndef PY_UNICODE_TYPE
Guido van Rossumd8225182000-03-10 22:33:05 +000088
Fredrik Lundh1294ad02001-06-26 17:17:07 +000089/* Windows has a usable wchar_t type (unless we're using UCS-4) */
Fredrik Lundh8f455852001-06-27 18:59:43 +000090# if defined(MS_WIN32) && Py_UNICODE_SIZE == 2
Guido van Rossumd8225182000-03-10 22:33:05 +000091# define HAVE_USABLE_WCHAR_T
Martin v. Löwis0ba70cc2001-06-26 22:22:37 +000092# define PY_UNICODE_TYPE wchar_t
93# endif
94
Fredrik Lundh8f455852001-06-27 18:59:43 +000095# if defined(Py_UNICODE_WIDE)
Martin v. Löwis0ba70cc2001-06-26 22:22:37 +000096# define PY_UNICODE_TYPE Py_UCS4
Guido van Rossumd8225182000-03-10 22:33:05 +000097# endif
98
99#endif
100
101/* If the compiler provides a wchar_t type we try to support it
102 through the interface functions PyUnicode_FromWideChar() and
103 PyUnicode_AsWideChar(). */
104
105#ifdef HAVE_USABLE_WCHAR_T
Marc-André Lemburg1a731c62000-08-11 11:43:10 +0000106# ifndef HAVE_WCHAR_H
107# define HAVE_WCHAR_H
108# endif
Guido van Rossumd8225182000-03-10 22:33:05 +0000109#endif
110
111#ifdef HAVE_WCHAR_H
Guido van Rossum24bdb042000-03-28 20:29:59 +0000112/* Work around a cosmetic bug in BSDI 4.x wchar.h; thanks to Thomas Wouters */
113# ifdef _HAVE_BSDI
114# include <time.h>
115# endif
Marc-André Lemburg5e6007c2001-09-19 11:21:03 +0000116# include <wchar.h>
Guido van Rossumd8225182000-03-10 22:33:05 +0000117#endif
118
Martin v. Löwis0ba70cc2001-06-26 22:22:37 +0000119/*
120 * Use this typedef when you need to represent a UTF-16 surrogate pair
121 * as single unsigned integer.
122 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000123#if SIZEOF_INT >= 4
124typedef unsigned int Py_UCS4;
Martin v. Löwis0ba70cc2001-06-26 22:22:37 +0000125#elif SIZEOF_LONG >= 4
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126typedef unsigned long Py_UCS4;
Guido van Rossumd8225182000-03-10 22:33:05 +0000127#endif
128
Benjamin Peterson960cf0f2009-01-09 04:11:44 +0000129/* Py_UNICODE is the native Unicode storage format (code unit) used by
130 Python and represents a single Unicode element in the Unicode
131 type. */
132
Martin v. Löwis0ba70cc2001-06-26 22:22:37 +0000133typedef PY_UNICODE_TYPE Py_UNICODE;
Marc-André Lemburg43279102000-07-07 09:01:41 +0000134
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000135/* --- UCS-2/UCS-4 Name Mangling ------------------------------------------ */
136
137/* Unicode API names are mangled to assure that UCS-2 and UCS-4 builds
138 produce different external names and thus cause import errors in
139 case Python interpreters and extensions with mixed compiled in
140 Unicode width assumptions are combined. */
141
142#ifndef Py_UNICODE_WIDE
143
144# define PyUnicode_AsASCIIString PyUnicodeUCS2_AsASCIIString
145# define PyUnicode_AsCharmapString PyUnicodeUCS2_AsCharmapString
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000146# define PyUnicode_AsDecodedObject PyUnicodeUCS2_AsDecodedObject
147# define PyUnicode_AsDecodedUnicode PyUnicodeUCS2_AsDecodedUnicode
Marc-André Lemburgd2d45982004-07-08 17:57:32 +0000148# define PyUnicode_AsEncodedObject PyUnicodeUCS2_AsEncodedObject
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000149# define PyUnicode_AsEncodedString PyUnicodeUCS2_AsEncodedString
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000150# define PyUnicode_AsEncodedUnicode PyUnicodeUCS2_AsEncodedUnicode
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000151# define PyUnicode_AsLatin1String PyUnicodeUCS2_AsLatin1String
152# define PyUnicode_AsRawUnicodeEscapeString PyUnicodeUCS2_AsRawUnicodeEscapeString
Walter Dörwald41980ca2007-08-16 21:55:45 +0000153# define PyUnicode_AsUTF32String PyUnicodeUCS2_AsUTF32String
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000154# define PyUnicode_AsUTF16String PyUnicodeUCS2_AsUTF16String
155# define PyUnicode_AsUTF8String PyUnicodeUCS2_AsUTF8String
156# define PyUnicode_AsUnicode PyUnicodeUCS2_AsUnicode
157# define PyUnicode_AsUnicodeEscapeString PyUnicodeUCS2_AsUnicodeEscapeString
158# define PyUnicode_AsWideChar PyUnicodeUCS2_AsWideChar
Alexandre Vassalotti15fafbe2008-12-28 02:13:22 +0000159# define PyUnicode_ClearFreeList PyUnicodeUCS2_ClearFreelist
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000160# define PyUnicode_Compare PyUnicodeUCS2_Compare
Benjamin Petersonad465f92010-05-07 20:21:26 +0000161# define PyUnicode_CompareWithASCII PyUnicodeUCS2_CompareASCII
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000162# define PyUnicode_Concat PyUnicodeUCS2_Concat
Walter Dörwald1ab83302007-05-18 17:15:44 +0000163# define PyUnicode_Append PyUnicodeUCS2_Append
164# define PyUnicode_AppendAndDel PyUnicodeUCS2_AppendAndDel
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000165# define PyUnicode_Contains PyUnicodeUCS2_Contains
166# define PyUnicode_Count PyUnicodeUCS2_Count
167# define PyUnicode_Decode PyUnicodeUCS2_Decode
168# define PyUnicode_DecodeASCII PyUnicodeUCS2_DecodeASCII
169# define PyUnicode_DecodeCharmap PyUnicodeUCS2_DecodeCharmap
170# define PyUnicode_DecodeLatin1 PyUnicodeUCS2_DecodeLatin1
Guido van Rossum00bc0e02007-10-15 02:52:41 +0000171# define PyUnicode_DecodeFSDefault PyUnicodeUCS2_DecodeFSDefault
Christian Heimes5894ba72007-11-04 11:43:14 +0000172# define PyUnicode_DecodeFSDefaultAndSize PyUnicodeUCS2_DecodeFSDefaultAndSize
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000173# define PyUnicode_DecodeRawUnicodeEscape PyUnicodeUCS2_DecodeRawUnicodeEscape
Walter Dörwald41980ca2007-08-16 21:55:45 +0000174# define PyUnicode_DecodeUTF32 PyUnicodeUCS2_DecodeUTF32
175# define PyUnicode_DecodeUTF32Stateful PyUnicodeUCS2_DecodeUTF32Stateful
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000176# define PyUnicode_DecodeUTF16 PyUnicodeUCS2_DecodeUTF16
Walter Dörwald69652032004-09-07 20:24:22 +0000177# define PyUnicode_DecodeUTF16Stateful PyUnicodeUCS2_DecodeUTF16Stateful
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000178# define PyUnicode_DecodeUTF8 PyUnicodeUCS2_DecodeUTF8
Walter Dörwald69652032004-09-07 20:24:22 +0000179# define PyUnicode_DecodeUTF8Stateful PyUnicodeUCS2_DecodeUTF8Stateful
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000180# define PyUnicode_DecodeUnicodeEscape PyUnicodeUCS2_DecodeUnicodeEscape
181# define PyUnicode_Encode PyUnicodeUCS2_Encode
182# define PyUnicode_EncodeASCII PyUnicodeUCS2_EncodeASCII
183# define PyUnicode_EncodeCharmap PyUnicodeUCS2_EncodeCharmap
184# define PyUnicode_EncodeDecimal PyUnicodeUCS2_EncodeDecimal
185# define PyUnicode_EncodeLatin1 PyUnicodeUCS2_EncodeLatin1
186# define PyUnicode_EncodeRawUnicodeEscape PyUnicodeUCS2_EncodeRawUnicodeEscape
Walter Dörwald41980ca2007-08-16 21:55:45 +0000187# define PyUnicode_EncodeUTF32 PyUnicodeUCS2_EncodeUTF32
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000188# define PyUnicode_EncodeUTF16 PyUnicodeUCS2_EncodeUTF16
189# define PyUnicode_EncodeUTF8 PyUnicodeUCS2_EncodeUTF8
190# define PyUnicode_EncodeUnicodeEscape PyUnicodeUCS2_EncodeUnicodeEscape
191# define PyUnicode_Find PyUnicodeUCS2_Find
192# define PyUnicode_Format PyUnicodeUCS2_Format
193# define PyUnicode_FromEncodedObject PyUnicodeUCS2_FromEncodedObject
Alexandre Vassalotti15fafbe2008-12-28 02:13:22 +0000194# define PyUnicode_FromFormat PyUnicodeUCS2_FromFormat
195# define PyUnicode_FromFormatV PyUnicodeUCS2_FromFormatV
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000196# define PyUnicode_FromObject PyUnicodeUCS2_FromObject
Marc-André Lemburg9c329de2002-08-12 08:19:10 +0000197# define PyUnicode_FromOrdinal PyUnicodeUCS2_FromOrdinal
Walter Dörwaldacaa5a12007-05-05 12:00:46 +0000198# define PyUnicode_FromString PyUnicodeUCS2_FromString
Walter Dörwaldd2034312007-05-18 16:29:38 +0000199# define PyUnicode_FromStringAndSize PyUnicodeUCS2_FromStringAndSize
Alexandre Vassalotti15fafbe2008-12-28 02:13:22 +0000200# define PyUnicode_FromUnicode PyUnicodeUCS2_FromUnicode
Walter Dörwald14176a52007-05-18 17:04:42 +0000201# define PyUnicode_FromWideChar PyUnicodeUCS2_FromWideChar
Martin v. Löwis011e8422009-05-05 04:43:17 +0000202# define PyUnicode_FSConverter PyUnicodeUCS2_FSConverter
Victor Stinner47fcb5b2010-08-13 23:59:58 +0000203# define PyUnicode_FSDecoder PyUnicodeUCS2_FSDecoder
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000204# define PyUnicode_GetDefaultEncoding PyUnicodeUCS2_GetDefaultEncoding
205# define PyUnicode_GetMax PyUnicodeUCS2_GetMax
206# define PyUnicode_GetSize PyUnicodeUCS2_GetSize
Martin v. Löwis47383402007-08-15 07:32:56 +0000207# define PyUnicode_IsIdentifier PyUnicodeUCS2_IsIdentifier
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000208# define PyUnicode_Join PyUnicodeUCS2_Join
Thomas Wouters477c8d52006-05-27 19:21:47 +0000209# define PyUnicode_Partition PyUnicodeUCS2_Partition
210# define PyUnicode_RPartition PyUnicodeUCS2_RPartition
211# define PyUnicode_RSplit PyUnicodeUCS2_RSplit
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000212# define PyUnicode_Replace PyUnicodeUCS2_Replace
213# define PyUnicode_Resize PyUnicodeUCS2_Resize
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000214# define PyUnicode_RichCompare PyUnicodeUCS2_RichCompare
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000215# define PyUnicode_SetDefaultEncoding PyUnicodeUCS2_SetDefaultEncoding
216# define PyUnicode_Split PyUnicodeUCS2_Split
217# define PyUnicode_Splitlines PyUnicodeUCS2_Splitlines
218# define PyUnicode_Tailmatch PyUnicodeUCS2_Tailmatch
219# define PyUnicode_Translate PyUnicodeUCS2_Translate
220# define PyUnicode_TranslateCharmap PyUnicodeUCS2_TranslateCharmap
221# define _PyUnicode_AsDefaultEncodedString _PyUnicodeUCS2_AsDefaultEncodedString
222# define _PyUnicode_Fini _PyUnicodeUCS2_Fini
223# define _PyUnicode_Init _PyUnicodeUCS2_Init
224# define _PyUnicode_IsAlpha _PyUnicodeUCS2_IsAlpha
225# define _PyUnicode_IsDecimalDigit _PyUnicodeUCS2_IsDecimalDigit
226# define _PyUnicode_IsDigit _PyUnicodeUCS2_IsDigit
227# define _PyUnicode_IsLinebreak _PyUnicodeUCS2_IsLinebreak
228# define _PyUnicode_IsLowercase _PyUnicodeUCS2_IsLowercase
229# define _PyUnicode_IsNumeric _PyUnicodeUCS2_IsNumeric
Georg Brandl559e5d72008-06-11 18:37:52 +0000230# define _PyUnicode_IsPrintable _PyUnicodeUCS2_IsPrintable
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000231# define _PyUnicode_IsTitlecase _PyUnicodeUCS2_IsTitlecase
Martin v. Löwis13c3e382007-08-14 22:37:03 +0000232# define _PyUnicode_IsXidStart _PyUnicodeUCS2_IsXidStart
233# define _PyUnicode_IsXidContinue _PyUnicodeUCS2_IsXidContinue
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000234# define _PyUnicode_IsUppercase _PyUnicodeUCS2_IsUppercase
235# define _PyUnicode_IsWhitespace _PyUnicodeUCS2_IsWhitespace
236# define _PyUnicode_ToDecimalDigit _PyUnicodeUCS2_ToDecimalDigit
237# define _PyUnicode_ToDigit _PyUnicodeUCS2_ToDigit
238# define _PyUnicode_ToLowercase _PyUnicodeUCS2_ToLowercase
239# define _PyUnicode_ToNumeric _PyUnicodeUCS2_ToNumeric
240# define _PyUnicode_ToTitlecase _PyUnicodeUCS2_ToTitlecase
241# define _PyUnicode_ToUppercase _PyUnicodeUCS2_ToUppercase
242
243#else
244
245# define PyUnicode_AsASCIIString PyUnicodeUCS4_AsASCIIString
246# define PyUnicode_AsCharmapString PyUnicodeUCS4_AsCharmapString
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000247# define PyUnicode_AsDecodedObject PyUnicodeUCS4_AsDecodedObject
248# define PyUnicode_AsDecodedUnicode PyUnicodeUCS4_AsDecodedUnicode
Marc-André Lemburgd2d45982004-07-08 17:57:32 +0000249# define PyUnicode_AsEncodedObject PyUnicodeUCS4_AsEncodedObject
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000250# define PyUnicode_AsEncodedString PyUnicodeUCS4_AsEncodedString
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000251# define PyUnicode_AsEncodedUnicode PyUnicodeUCS4_AsEncodedUnicode
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000252# define PyUnicode_AsLatin1String PyUnicodeUCS4_AsLatin1String
253# define PyUnicode_AsRawUnicodeEscapeString PyUnicodeUCS4_AsRawUnicodeEscapeString
Walter Dörwald41980ca2007-08-16 21:55:45 +0000254# define PyUnicode_AsUTF32String PyUnicodeUCS4_AsUTF32String
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000255# define PyUnicode_AsUTF16String PyUnicodeUCS4_AsUTF16String
256# define PyUnicode_AsUTF8String PyUnicodeUCS4_AsUTF8String
257# define PyUnicode_AsUnicode PyUnicodeUCS4_AsUnicode
258# define PyUnicode_AsUnicodeEscapeString PyUnicodeUCS4_AsUnicodeEscapeString
259# define PyUnicode_AsWideChar PyUnicodeUCS4_AsWideChar
Alexandre Vassalotti15fafbe2008-12-28 02:13:22 +0000260# define PyUnicode_ClearFreeList PyUnicodeUCS4_ClearFreelist
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000261# define PyUnicode_Compare PyUnicodeUCS4_Compare
Benjamin Petersonad465f92010-05-07 20:21:26 +0000262# define PyUnicode_CompareWithASCII PyUnicodeUCS4_CompareWithASCII
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000263# define PyUnicode_Concat PyUnicodeUCS4_Concat
Walter Dörwald1ab83302007-05-18 17:15:44 +0000264# define PyUnicode_Append PyUnicodeUCS4_Append
265# define PyUnicode_AppendAndDel PyUnicodeUCS4_AppendAndDel
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000266# define PyUnicode_Contains PyUnicodeUCS4_Contains
267# define PyUnicode_Count PyUnicodeUCS4_Count
268# define PyUnicode_Decode PyUnicodeUCS4_Decode
269# define PyUnicode_DecodeASCII PyUnicodeUCS4_DecodeASCII
270# define PyUnicode_DecodeCharmap PyUnicodeUCS4_DecodeCharmap
271# define PyUnicode_DecodeLatin1 PyUnicodeUCS4_DecodeLatin1
Guido van Rossum00bc0e02007-10-15 02:52:41 +0000272# define PyUnicode_DecodeFSDefault PyUnicodeUCS4_DecodeFSDefault
Christian Heimes5894ba72007-11-04 11:43:14 +0000273# define PyUnicode_DecodeFSDefaultAndSize PyUnicodeUCS4_DecodeFSDefaultAndSize
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000274# define PyUnicode_DecodeRawUnicodeEscape PyUnicodeUCS4_DecodeRawUnicodeEscape
Walter Dörwald41980ca2007-08-16 21:55:45 +0000275# define PyUnicode_DecodeUTF32 PyUnicodeUCS4_DecodeUTF32
276# define PyUnicode_DecodeUTF32Stateful PyUnicodeUCS4_DecodeUTF32Stateful
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000277# define PyUnicode_DecodeUTF16 PyUnicodeUCS4_DecodeUTF16
Walter Dörwald69652032004-09-07 20:24:22 +0000278# define PyUnicode_DecodeUTF16Stateful PyUnicodeUCS4_DecodeUTF16Stateful
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000279# define PyUnicode_DecodeUTF8 PyUnicodeUCS4_DecodeUTF8
Walter Dörwald69652032004-09-07 20:24:22 +0000280# define PyUnicode_DecodeUTF8Stateful PyUnicodeUCS4_DecodeUTF8Stateful
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000281# define PyUnicode_DecodeUnicodeEscape PyUnicodeUCS4_DecodeUnicodeEscape
282# define PyUnicode_Encode PyUnicodeUCS4_Encode
283# define PyUnicode_EncodeASCII PyUnicodeUCS4_EncodeASCII
284# define PyUnicode_EncodeCharmap PyUnicodeUCS4_EncodeCharmap
285# define PyUnicode_EncodeDecimal PyUnicodeUCS4_EncodeDecimal
286# define PyUnicode_EncodeLatin1 PyUnicodeUCS4_EncodeLatin1
287# define PyUnicode_EncodeRawUnicodeEscape PyUnicodeUCS4_EncodeRawUnicodeEscape
Walter Dörwald41980ca2007-08-16 21:55:45 +0000288# define PyUnicode_EncodeUTF32 PyUnicodeUCS4_EncodeUTF32
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000289# define PyUnicode_EncodeUTF16 PyUnicodeUCS4_EncodeUTF16
290# define PyUnicode_EncodeUTF8 PyUnicodeUCS4_EncodeUTF8
291# define PyUnicode_EncodeUnicodeEscape PyUnicodeUCS4_EncodeUnicodeEscape
292# define PyUnicode_Find PyUnicodeUCS4_Find
293# define PyUnicode_Format PyUnicodeUCS4_Format
294# define PyUnicode_FromEncodedObject PyUnicodeUCS4_FromEncodedObject
Alexandre Vassalotti15fafbe2008-12-28 02:13:22 +0000295# define PyUnicode_FromFormat PyUnicodeUCS4_FromFormat
296# define PyUnicode_FromFormatV PyUnicodeUCS4_FromFormatV
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000297# define PyUnicode_FromObject PyUnicodeUCS4_FromObject
Marc-André Lemburg9c329de2002-08-12 08:19:10 +0000298# define PyUnicode_FromOrdinal PyUnicodeUCS4_FromOrdinal
Walter Dörwaldacaa5a12007-05-05 12:00:46 +0000299# define PyUnicode_FromString PyUnicodeUCS4_FromString
Walter Dörwaldd2034312007-05-18 16:29:38 +0000300# define PyUnicode_FromStringAndSize PyUnicodeUCS4_FromStringAndSize
Alexandre Vassalotti15fafbe2008-12-28 02:13:22 +0000301# define PyUnicode_FromUnicode PyUnicodeUCS4_FromUnicode
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000302# define PyUnicode_FromWideChar PyUnicodeUCS4_FromWideChar
Martin v. Löwis011e8422009-05-05 04:43:17 +0000303# define PyUnicode_FSConverter PyUnicodeUCS4_FSConverter
Victor Stinner47fcb5b2010-08-13 23:59:58 +0000304# define PyUnicode_FSDecoder PyUnicodeUCS4_FSDecoder
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000305# define PyUnicode_GetDefaultEncoding PyUnicodeUCS4_GetDefaultEncoding
306# define PyUnicode_GetMax PyUnicodeUCS4_GetMax
307# define PyUnicode_GetSize PyUnicodeUCS4_GetSize
Martin v. Löwis47383402007-08-15 07:32:56 +0000308# define PyUnicode_IsIdentifier PyUnicodeUCS4_IsIdentifier
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000309# define PyUnicode_Join PyUnicodeUCS4_Join
Thomas Wouters477c8d52006-05-27 19:21:47 +0000310# define PyUnicode_Partition PyUnicodeUCS4_Partition
311# define PyUnicode_RPartition PyUnicodeUCS4_RPartition
312# define PyUnicode_RSplit PyUnicodeUCS4_RSplit
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000313# define PyUnicode_Replace PyUnicodeUCS4_Replace
314# define PyUnicode_Resize PyUnicodeUCS4_Resize
Thomas Wouters00ee7ba2006-08-21 19:07:27 +0000315# define PyUnicode_RichCompare PyUnicodeUCS4_RichCompare
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000316# define PyUnicode_SetDefaultEncoding PyUnicodeUCS4_SetDefaultEncoding
317# define PyUnicode_Split PyUnicodeUCS4_Split
318# define PyUnicode_Splitlines PyUnicodeUCS4_Splitlines
319# define PyUnicode_Tailmatch PyUnicodeUCS4_Tailmatch
320# define PyUnicode_Translate PyUnicodeUCS4_Translate
321# define PyUnicode_TranslateCharmap PyUnicodeUCS4_TranslateCharmap
322# define _PyUnicode_AsDefaultEncodedString _PyUnicodeUCS4_AsDefaultEncodedString
323# define _PyUnicode_Fini _PyUnicodeUCS4_Fini
324# define _PyUnicode_Init _PyUnicodeUCS4_Init
325# define _PyUnicode_IsAlpha _PyUnicodeUCS4_IsAlpha
326# define _PyUnicode_IsDecimalDigit _PyUnicodeUCS4_IsDecimalDigit
327# define _PyUnicode_IsDigit _PyUnicodeUCS4_IsDigit
328# define _PyUnicode_IsLinebreak _PyUnicodeUCS4_IsLinebreak
329# define _PyUnicode_IsLowercase _PyUnicodeUCS4_IsLowercase
330# define _PyUnicode_IsNumeric _PyUnicodeUCS4_IsNumeric
Georg Brandl559e5d72008-06-11 18:37:52 +0000331# define _PyUnicode_IsPrintable _PyUnicodeUCS4_IsPrintable
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000332# define _PyUnicode_IsTitlecase _PyUnicodeUCS4_IsTitlecase
Martin v. Löwis13c3e382007-08-14 22:37:03 +0000333# define _PyUnicode_IsXidStart _PyUnicodeUCS4_IsXidStart
334# define _PyUnicode_IsXidContinue _PyUnicodeUCS4_IsXidContinue
Marc-André Lemburgb5ac6f62001-07-31 14:30:16 +0000335# define _PyUnicode_IsUppercase _PyUnicodeUCS4_IsUppercase
336# define _PyUnicode_IsWhitespace _PyUnicodeUCS4_IsWhitespace
337# define _PyUnicode_ToDecimalDigit _PyUnicodeUCS4_ToDecimalDigit
338# define _PyUnicode_ToDigit _PyUnicodeUCS4_ToDigit
339# define _PyUnicode_ToLowercase _PyUnicodeUCS4_ToLowercase
340# define _PyUnicode_ToNumeric _PyUnicodeUCS4_ToNumeric
341# define _PyUnicode_ToTitlecase _PyUnicodeUCS4_ToTitlecase
342# define _PyUnicode_ToUppercase _PyUnicodeUCS4_ToUppercase
343
344
345#endif
346
Guido van Rossumd8225182000-03-10 22:33:05 +0000347/* --- Internal Unicode Operations ---------------------------------------- */
348
349/* If you want Python to use the compiler's wctype.h functions instead
Barry Warsaw51ac5802000-03-20 16:36:48 +0000350 of the ones supplied with Python, define WANT_WCTYPE_FUNCTIONS or
Raymond Hettinger57341c32004-10-31 05:46:59 +0000351 configure Python using --with-wctype-functions. This reduces the
Barry Warsaw51ac5802000-03-20 16:36:48 +0000352 interpreter's code size. */
Guido van Rossumd8225182000-03-10 22:33:05 +0000353
354#if defined(HAVE_USABLE_WCHAR_T) && defined(WANT_WCTYPE_FUNCTIONS)
355
Marc-André Lemburg5e6007c2001-09-19 11:21:03 +0000356#include <wctype.h>
Guido van Rossumd8225182000-03-10 22:33:05 +0000357
358#define Py_UNICODE_ISSPACE(ch) iswspace(ch)
359
360#define Py_UNICODE_ISLOWER(ch) iswlower(ch)
361#define Py_UNICODE_ISUPPER(ch) iswupper(ch)
362#define Py_UNICODE_ISTITLE(ch) _PyUnicode_IsTitlecase(ch)
363#define Py_UNICODE_ISLINEBREAK(ch) _PyUnicode_IsLinebreak(ch)
364
365#define Py_UNICODE_TOLOWER(ch) towlower(ch)
366#define Py_UNICODE_TOUPPER(ch) towupper(ch)
367#define Py_UNICODE_TOTITLE(ch) _PyUnicode_ToTitlecase(ch)
368
369#define Py_UNICODE_ISDECIMAL(ch) _PyUnicode_IsDecimalDigit(ch)
370#define Py_UNICODE_ISDIGIT(ch) _PyUnicode_IsDigit(ch)
371#define Py_UNICODE_ISNUMERIC(ch) _PyUnicode_IsNumeric(ch)
Georg Brandl559e5d72008-06-11 18:37:52 +0000372#define Py_UNICODE_ISPRINTABLE(ch) _PyUnicode_IsPrintable(ch)
Guido van Rossumd8225182000-03-10 22:33:05 +0000373
374#define Py_UNICODE_TODECIMAL(ch) _PyUnicode_ToDecimalDigit(ch)
375#define Py_UNICODE_TODIGIT(ch) _PyUnicode_ToDigit(ch)
376#define Py_UNICODE_TONUMERIC(ch) _PyUnicode_ToNumeric(ch)
377
Marc-André Lemburgf03e7412000-07-05 09:45:59 +0000378#define Py_UNICODE_ISALPHA(ch) iswalpha(ch)
379
Guido van Rossumd8225182000-03-10 22:33:05 +0000380#else
381
Benjamin Peterson960cf0f2009-01-09 04:11:44 +0000382/* Since splitting on whitespace is an important use case, and
383 whitespace in most situations is solely ASCII whitespace, we
384 optimize for the common case by using a quick look-up table
385 _Py_ascii_whitespace (see below) with an inlined check.
Christian Heimes190d79e2008-01-30 11:58:22 +0000386
Benjamin Peterson960cf0f2009-01-09 04:11:44 +0000387 */
Christian Heimes190d79e2008-01-30 11:58:22 +0000388#define Py_UNICODE_ISSPACE(ch) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000389 ((ch) < 128U ? _Py_ascii_whitespace[(ch)] : _PyUnicode_IsWhitespace(ch))
Guido van Rossumd8225182000-03-10 22:33:05 +0000390
391#define Py_UNICODE_ISLOWER(ch) _PyUnicode_IsLowercase(ch)
392#define Py_UNICODE_ISUPPER(ch) _PyUnicode_IsUppercase(ch)
393#define Py_UNICODE_ISTITLE(ch) _PyUnicode_IsTitlecase(ch)
394#define Py_UNICODE_ISLINEBREAK(ch) _PyUnicode_IsLinebreak(ch)
395
396#define Py_UNICODE_TOLOWER(ch) _PyUnicode_ToLowercase(ch)
397#define Py_UNICODE_TOUPPER(ch) _PyUnicode_ToUppercase(ch)
398#define Py_UNICODE_TOTITLE(ch) _PyUnicode_ToTitlecase(ch)
399
400#define Py_UNICODE_ISDECIMAL(ch) _PyUnicode_IsDecimalDigit(ch)
401#define Py_UNICODE_ISDIGIT(ch) _PyUnicode_IsDigit(ch)
402#define Py_UNICODE_ISNUMERIC(ch) _PyUnicode_IsNumeric(ch)
Georg Brandl559e5d72008-06-11 18:37:52 +0000403#define Py_UNICODE_ISPRINTABLE(ch) _PyUnicode_IsPrintable(ch)
Guido van Rossumd8225182000-03-10 22:33:05 +0000404
405#define Py_UNICODE_TODECIMAL(ch) _PyUnicode_ToDecimalDigit(ch)
406#define Py_UNICODE_TODIGIT(ch) _PyUnicode_ToDigit(ch)
407#define Py_UNICODE_TONUMERIC(ch) _PyUnicode_ToNumeric(ch)
408
Marc-André Lemburgf03e7412000-07-05 09:45:59 +0000409#define Py_UNICODE_ISALPHA(ch) _PyUnicode_IsAlpha(ch)
Guido van Rossumd8225182000-03-10 22:33:05 +0000410
Marc-André Lemburgf03e7412000-07-05 09:45:59 +0000411#endif
Marc-André Lemburga9c103b2000-07-03 10:52:13 +0000412
413#define Py_UNICODE_ISALNUM(ch) \
414 (Py_UNICODE_ISALPHA(ch) || \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 Py_UNICODE_ISDECIMAL(ch) || \
416 Py_UNICODE_ISDIGIT(ch) || \
417 Py_UNICODE_ISNUMERIC(ch))
Marc-André Lemburga9c103b2000-07-03 10:52:13 +0000418
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000419#define Py_UNICODE_COPY(target, source, length) \
420 Py_MEMCPY((target), (source), (length)*sizeof(Py_UNICODE))
Guido van Rossumd8225182000-03-10 22:33:05 +0000421
Benjamin Peterson960cf0f2009-01-09 04:11:44 +0000422#define Py_UNICODE_FILL(target, value, length) \
423 do {Py_ssize_t i_; Py_UNICODE *t_ = (target); Py_UNICODE v_ = (value);\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000424 for (i_ = 0; i_ < (length); i_++) t_[i_] = v_;\
Thomas Wouters477c8d52006-05-27 19:21:47 +0000425 } while (0)
Guido van Rossumd8225182000-03-10 22:33:05 +0000426
Benjamin Peterson960cf0f2009-01-09 04:11:44 +0000427/* Check if substring matches at given offset. the offset must be
Thomas Wouters477c8d52006-05-27 19:21:47 +0000428 valid, and the substring must not be empty */
Benjamin Peterson960cf0f2009-01-09 04:11:44 +0000429
Thomas Wouters477c8d52006-05-27 19:21:47 +0000430#define Py_UNICODE_MATCH(string, offset, substring) \
431 ((*((string)->str + (offset)) == *((substring)->str)) && \
432 ((*((string)->str + (offset) + (substring)->length-1) == *((substring)->str + (substring)->length-1))) && \
433 !memcmp((string)->str + (offset), (substring)->str, (substring)->length*sizeof(Py_UNICODE)))
Guido van Rossumd8225182000-03-10 22:33:05 +0000434
Barry Warsaw51ac5802000-03-20 16:36:48 +0000435#ifdef __cplusplus
436extern "C" {
437#endif
438
Guido van Rossumd8225182000-03-10 22:33:05 +0000439/* --- Unicode Type ------------------------------------------------------- */
440
441typedef struct {
442 PyObject_HEAD
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443 Py_ssize_t length; /* Length of raw Unicode data in buffer */
444 Py_UNICODE *str; /* Raw Unicode buffer */
445 long hash; /* Hash value; -1 if not set */
446 int state; /* != 0 if interned. In this case the two
447 * references from the dictionary to this object
448 * are *not* counted in ob_refcnt. */
449 PyObject *defenc; /* (Default) Encoded version as Python
450 string, or NULL; this is used for
451 implementing the buffer protocol */
Guido van Rossumd8225182000-03-10 22:33:05 +0000452} PyUnicodeObject;
453
Mark Hammond91a681d2002-08-12 07:21:58 +0000454PyAPI_DATA(PyTypeObject) PyUnicode_Type;
Christian Heimesa22e8bd2007-11-29 22:35:39 +0000455PyAPI_DATA(PyTypeObject) PyUnicodeIter_Type;
Guido van Rossumd8225182000-03-10 22:33:05 +0000456
Walter Dörwald16807132007-05-25 13:52:07 +0000457#define SSTATE_NOT_INTERNED 0
458#define SSTATE_INTERNED_MORTAL 1
459#define SSTATE_INTERNED_IMMORTAL 2
460
Thomas Wouters27d517b2007-02-25 20:39:11 +0000461#define PyUnicode_Check(op) \
Christian Heimes90aa7642007-12-19 02:45:37 +0000462 PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_UNICODE_SUBCLASS)
463#define PyUnicode_CheckExact(op) (Py_TYPE(op) == &PyUnicode_Type)
Guido van Rossumd8225182000-03-10 22:33:05 +0000464
465/* Fast access macros */
466#define PyUnicode_GET_SIZE(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 (assert(PyUnicode_Check(op)),(((PyUnicodeObject *)(op))->length))
Guido van Rossumd8225182000-03-10 22:33:05 +0000468#define PyUnicode_GET_DATA_SIZE(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 (assert(PyUnicode_Check(op)),(((PyUnicodeObject *)(op))->length * sizeof(Py_UNICODE)))
Guido van Rossumd8225182000-03-10 22:33:05 +0000470#define PyUnicode_AS_UNICODE(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 (assert(PyUnicode_Check(op)),(((PyUnicodeObject *)(op))->str))
Guido van Rossumd8225182000-03-10 22:33:05 +0000472#define PyUnicode_AS_DATA(op) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 (assert(PyUnicode_Check(op)),((const char *)((PyUnicodeObject *)(op))->str))
Guido van Rossumd8225182000-03-10 22:33:05 +0000474
475/* --- Constants ---------------------------------------------------------- */
476
477/* This Unicode character will be used as replacement character during
478 decoding if the errors argument is set to "replace". Note: the
479 Unicode character U+FFFD is the official REPLACEMENT CHARACTER in
480 Unicode 3.0. */
481
482#define Py_UNICODE_REPLACEMENT_CHARACTER ((Py_UNICODE) 0xFFFD)
483
484/* === Public API ========================================================= */
485
486/* --- Plain Py_UNICODE --------------------------------------------------- */
487
488/* Create a Unicode Object from the Py_UNICODE buffer u of the given
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 size.
Marc-André Lemburg8155e0e2001-04-23 14:44:21 +0000490
491 u may be NULL which causes the contents to be undefined. It is the
492 user's responsibility to fill in the needed data afterwards. Note
493 that modifying the Unicode object contents after construction is
494 only allowed if u was set to NULL.
Guido van Rossumd8225182000-03-10 22:33:05 +0000495
496 The buffer is copied into the new object. */
497
Mark Hammond91a681d2002-08-12 07:21:58 +0000498PyAPI_FUNC(PyObject*) PyUnicode_FromUnicode(
Guido van Rossumd8225182000-03-10 22:33:05 +0000499 const Py_UNICODE *u, /* Unicode buffer */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000500 Py_ssize_t size /* size of buffer */
Guido van Rossumd8225182000-03-10 22:33:05 +0000501 );
502
Georg Brandl952867a2010-06-27 10:17:12 +0000503/* Similar to PyUnicode_FromUnicode(), but u points to UTF-8 encoded bytes */
Walter Dörwaldd2034312007-05-18 16:29:38 +0000504PyAPI_FUNC(PyObject*) PyUnicode_FromStringAndSize(
505 const char *u, /* char buffer */
506 Py_ssize_t size /* size of buffer */
507 );
508
Walter Dörwaldacaa5a12007-05-05 12:00:46 +0000509/* Similar to PyUnicode_FromUnicode(), but u points to null-terminated
Georg Brandl952867a2010-06-27 10:17:12 +0000510 UTF-8 encoded bytes */
Walter Dörwaldacaa5a12007-05-05 12:00:46 +0000511PyAPI_FUNC(PyObject*) PyUnicode_FromString(
512 const char *u /* string */
513 );
514
Guido van Rossumd8225182000-03-10 22:33:05 +0000515/* Return a read-only pointer to the Unicode object's internal
516 Py_UNICODE buffer. */
517
Mark Hammond91a681d2002-08-12 07:21:58 +0000518PyAPI_FUNC(Py_UNICODE *) PyUnicode_AsUnicode(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 PyObject *unicode /* Unicode object */
Guido van Rossumd8225182000-03-10 22:33:05 +0000520 );
521
522/* Get the length of the Unicode object. */
523
Martin v. Löwis18e16552006-02-15 17:27:45 +0000524PyAPI_FUNC(Py_ssize_t) PyUnicode_GetSize(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 PyObject *unicode /* Unicode object */
Guido van Rossumd8225182000-03-10 22:33:05 +0000526 );
527
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000528/* Get the maximum ordinal for a Unicode character. */
Mark Hammond91a681d2002-08-12 07:21:58 +0000529PyAPI_FUNC(Py_UNICODE) PyUnicode_GetMax(void);
Martin v. Löwisce9b5a52001-06-27 06:28:56 +0000530
Guido van Rossum52c23592000-04-10 13:41:41 +0000531/* Resize an already allocated Unicode object to the new size length.
532
533 *unicode is modified to point to the new (resized) object and 0
534 returned on success.
535
536 This API may only be called by the function which also called the
537 Unicode constructor. The refcount on the object must be 1. Otherwise,
538 an error is returned.
539
540 Error handling is implemented as follows: an exception is set, -1
541 is returned and *unicode left untouched.
542
543*/
544
Mark Hammond91a681d2002-08-12 07:21:58 +0000545PyAPI_FUNC(int) PyUnicode_Resize(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 PyObject **unicode, /* Pointer to the Unicode object */
547 Py_ssize_t length /* New length */
Guido van Rossum52c23592000-04-10 13:41:41 +0000548 );
549
Guido van Rossumd8225182000-03-10 22:33:05 +0000550/* Coerce obj to an Unicode object and return a reference with
551 *incremented* refcount.
552
553 Coercion is done in the following way:
554
Georg Brandl952867a2010-06-27 10:17:12 +0000555 1. bytes, bytearray and other char buffer compatible objects are decoded
Fred Drakecb093fe2000-05-09 19:51:53 +0000556 under the assumptions that they contain data using the current
557 default encoding. Decoding is done in "strict" mode.
Guido van Rossumd8225182000-03-10 22:33:05 +0000558
Guido van Rossumb8c65bc2001-10-19 02:01:31 +0000559 2. All other objects (including Unicode objects) raise an
560 exception.
Guido van Rossumd8225182000-03-10 22:33:05 +0000561
562 The API returns NULL in case of an error. The caller is responsible
563 for decref'ing the returned objects.
564
565*/
566
Mark Hammond91a681d2002-08-12 07:21:58 +0000567PyAPI_FUNC(PyObject*) PyUnicode_FromEncodedObject(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 register PyObject *obj, /* Object */
Marc-André Lemburg5a5c81a2000-07-07 13:46:42 +0000569 const char *encoding, /* encoding */
570 const char *errors /* error handling */
571 );
572
Guido van Rossumb8c65bc2001-10-19 02:01:31 +0000573/* Coerce obj to an Unicode object and return a reference with
Marc-André Lemburg5a5c81a2000-07-07 13:46:42 +0000574 *incremented* refcount.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575
Guido van Rossumb8c65bc2001-10-19 02:01:31 +0000576 Unicode objects are passed back as-is (subclasses are converted to
577 true Unicode objects), all other objects are delegated to
578 PyUnicode_FromEncodedObject(obj, NULL, "strict") which results in
Georg Brandl952867a2010-06-27 10:17:12 +0000579 using UTF-8 encoding as basis for decoding the object.
Marc-André Lemburg5a5c81a2000-07-07 13:46:42 +0000580
581 The API returns NULL in case of an error. The caller is responsible
582 for decref'ing the returned objects.
583
584*/
585
Mark Hammond91a681d2002-08-12 07:21:58 +0000586PyAPI_FUNC(PyObject*) PyUnicode_FromObject(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000587 register PyObject *obj /* Object */
Guido van Rossumd8225182000-03-10 22:33:05 +0000588 );
589
Walter Dörwaldd2034312007-05-18 16:29:38 +0000590PyAPI_FUNC(PyObject *) PyUnicode_FromFormatV(const char*, va_list);
591PyAPI_FUNC(PyObject *) PyUnicode_FromFormat(const char*, ...);
592
Eric Smith4a7d76d2008-05-30 18:10:19 +0000593/* Format the object based on the format_spec, as defined in PEP 3101
594 (Advanced String Formatting). */
595PyAPI_FUNC(PyObject *) _PyUnicode_FormatAdvanced(PyObject *obj,
596 Py_UNICODE *format_spec,
597 Py_ssize_t format_spec_len);
598
Walter Dörwald16807132007-05-25 13:52:07 +0000599PyAPI_FUNC(void) PyUnicode_InternInPlace(PyObject **);
600PyAPI_FUNC(void) PyUnicode_InternImmortal(PyObject **);
601PyAPI_FUNC(PyObject *) PyUnicode_InternFromString(const char *);
602PyAPI_FUNC(void) _Py_ReleaseInternedUnicodeStrings(void);
603
604/* Use only if you know it's a string */
605#define PyUnicode_CHECK_INTERNED(op) (((PyUnicodeObject *)(op))->state)
606
Guido van Rossumd8225182000-03-10 22:33:05 +0000607/* --- wchar_t support for platforms which support it --------------------- */
608
609#ifdef HAVE_WCHAR_H
610
Georg Brandl952867a2010-06-27 10:17:12 +0000611/* Create a Unicode Object from the wchar_t buffer w of the given
Guido van Rossumd8225182000-03-10 22:33:05 +0000612 size.
613
614 The buffer is copied into the new object. */
615
Mark Hammond91a681d2002-08-12 07:21:58 +0000616PyAPI_FUNC(PyObject*) PyUnicode_FromWideChar(
Guido van Rossumd8225182000-03-10 22:33:05 +0000617 register const wchar_t *w, /* wchar_t buffer */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000618 Py_ssize_t size /* size of buffer */
Guido van Rossumd8225182000-03-10 22:33:05 +0000619 );
620
Marc-André Lemburga9cadcd2004-11-22 13:02:31 +0000621/* Copies the Unicode Object contents into the wchar_t buffer w. At
Guido van Rossumd8225182000-03-10 22:33:05 +0000622 most size wchar_t characters are copied.
623
Marc-André Lemburga9cadcd2004-11-22 13:02:31 +0000624 Note that the resulting wchar_t string may or may not be
625 0-terminated. It is the responsibility of the caller to make sure
626 that the wchar_t string is 0-terminated in case this is required by
627 the application.
628
629 Returns the number of wchar_t characters copied (excluding a
630 possibly trailing 0-termination character) or -1 in case of an
Guido van Rossumd8225182000-03-10 22:33:05 +0000631 error. */
632
Martin v. Löwis18e16552006-02-15 17:27:45 +0000633PyAPI_FUNC(Py_ssize_t) PyUnicode_AsWideChar(
Guido van Rossumd8225182000-03-10 22:33:05 +0000634 PyUnicodeObject *unicode, /* Unicode object */
635 register wchar_t *w, /* wchar_t buffer */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000636 Py_ssize_t size /* size of buffer */
Guido van Rossumd8225182000-03-10 22:33:05 +0000637 );
638
639#endif
640
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000641/* --- Unicode ordinals --------------------------------------------------- */
642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643/* Create a Unicode Object from the given Unicode code point ordinal.
644
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000645 The ordinal must be in range(0x10000) on narrow Python builds
646 (UCS2), and range(0x110000) on wide builds (UCS4). A ValueError is
647 raised in case it is not.
648
649*/
650
Marc-André Lemburg9c329de2002-08-12 08:19:10 +0000651PyAPI_FUNC(PyObject*) PyUnicode_FromOrdinal(int ordinal);
Marc-André Lemburgcc8764c2002-08-11 12:23:04 +0000652
Benjamin Peterson960cf0f2009-01-09 04:11:44 +0000653/* --- Free-list management ----------------------------------------------- */
654
655/* Clear the free list used by the Unicode implementation.
656
657 This can be used to release memory used for objects on the free
658 list back to the Python memory allocator.
659
660*/
661
662PyAPI_FUNC(int) PyUnicode_ClearFreeList(void);
663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664/* === Builtin Codecs =====================================================
Guido van Rossumd8225182000-03-10 22:33:05 +0000665
666 Many of these APIs take two arguments encoding and errors. These
667 parameters encoding and errors have the same semantics as the ones
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000668 of the builtin unicode() API.
Guido van Rossumd8225182000-03-10 22:33:05 +0000669
Georg Brandl952867a2010-06-27 10:17:12 +0000670 Setting encoding to NULL causes the default encoding (UTF-8) to be used.
Guido van Rossumd8225182000-03-10 22:33:05 +0000671
672 Error handling is set by errors which may also be set to NULL
673 meaning to use the default handling defined for the codec. Default
674 error handling for all builtin codecs is "strict" (ValueErrors are
675 raised).
676
677 The codecs all use a similar interface. Only deviation from the
678 generic ones are documented.
679
680*/
681
Fred Drakecb093fe2000-05-09 19:51:53 +0000682/* --- Manage the default encoding ---------------------------------------- */
683
Jeremy Hylton3ce45382001-07-30 22:34:24 +0000684/* Return a Python string holding the default encoded value of the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 Unicode object.
Jeremy Hylton3ce45382001-07-30 22:34:24 +0000686
687 The resulting string is cached in the Unicode object for subsequent
688 usage by this function. The cached version is needed to implement
689 the character buffer interface and will live (at least) as long as
690 the Unicode object itself.
691
692 The refcount of the string is *not* incremented.
693
694 *** Exported for internal use by the interpreter only !!! ***
695
696*/
697
Mark Hammond91a681d2002-08-12 07:21:58 +0000698PyAPI_FUNC(PyObject *) _PyUnicode_AsDefaultEncodedString(
Marc-André Lemburg9155aa72008-04-29 11:14:08 +0000699 PyObject *unicode,
700 const char *errors);
Jeremy Hylton3ce45382001-07-30 22:34:24 +0000701
Marc-André Lemburg9155aa72008-04-29 11:14:08 +0000702/* Returns a pointer to the default encoding (normally, UTF-8) of the
703 Unicode object unicode and the size of the encoded representation
704 in bytes stored in *size.
Christian Heimes5894ba72007-11-04 11:43:14 +0000705
Marc-André Lemburg9155aa72008-04-29 11:14:08 +0000706 In case of an error, no *size is set.
Guido van Rossum7d1df6c2007-08-29 13:53:23 +0000707
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000708 *** This API is for interpreter INTERNAL USE ONLY and will likely
709 *** be removed or changed for Python 3.1.
710
711 *** If you need to access the Unicode object as UTF-8 bytes string,
712 *** please use PyUnicode_AsUTF8String() instead.
713
Martin v. Löwis5b222132007-06-10 09:51:05 +0000714*/
715
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000716PyAPI_FUNC(char *) _PyUnicode_AsStringAndSize(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 PyObject *unicode,
Marc-André Lemburg9155aa72008-04-29 11:14:08 +0000718 Py_ssize_t *size);
Guido van Rossum7d1df6c2007-08-29 13:53:23 +0000719
Marc-André Lemburg9155aa72008-04-29 11:14:08 +0000720/* Returns a pointer to the default encoding (normally, UTf-8) of the
721 Unicode object unicode.
Guido van Rossum7d1df6c2007-08-29 13:53:23 +0000722
Marc-André Lemburg9155aa72008-04-29 11:14:08 +0000723 Use of this API is DEPRECATED since no size information can be
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000724 extracted from the returned data.
725
726 *** This API is for interpreter INTERNAL USE ONLY and will likely
727 *** be removed or changed for Python 3.1.
728
729 *** If you need to access the Unicode object as UTF-8 bytes string,
730 *** please use PyUnicode_AsUTF8String() instead.
Guido van Rossum7d1df6c2007-08-29 13:53:23 +0000731
Marc-André Lemburg9155aa72008-04-29 11:14:08 +0000732*/
Martin v. Löwis5b222132007-06-10 09:51:05 +0000733
Marc-André Lemburg4cc0f242008-08-07 18:54:33 +0000734PyAPI_FUNC(char *) _PyUnicode_AsString(PyObject *unicode);
Martin v. Löwis5b222132007-06-10 09:51:05 +0000735
Marc-André Lemburg9155aa72008-04-29 11:14:08 +0000736/* Returns the currently active default encoding.
Fred Drakecb093fe2000-05-09 19:51:53 +0000737
Marc-André Lemburg9155aa72008-04-29 11:14:08 +0000738 The default encoding is currently implemented as run-time settable
739 process global. This may change in future versions of the
740 interpreter to become a parameter which is managed on a per-thread
741 basis.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742
Fred Drakecb093fe2000-05-09 19:51:53 +0000743 */
744
Mark Hammond91a681d2002-08-12 07:21:58 +0000745PyAPI_FUNC(const char*) PyUnicode_GetDefaultEncoding(void);
Fred Drakecb093fe2000-05-09 19:51:53 +0000746
747/* Sets the currently active default encoding.
748
749 Returns 0 on success, -1 in case of an error.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750
Fred Drakecb093fe2000-05-09 19:51:53 +0000751 */
752
Mark Hammond91a681d2002-08-12 07:21:58 +0000753PyAPI_FUNC(int) PyUnicode_SetDefaultEncoding(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000754 const char *encoding /* Encoding name in standard form */
Fred Drakecb093fe2000-05-09 19:51:53 +0000755 );
756
Guido van Rossumd8225182000-03-10 22:33:05 +0000757/* --- Generic Codecs ----------------------------------------------------- */
758
759/* Create a Unicode object by decoding the encoded string s of the
760 given size. */
761
Mark Hammond91a681d2002-08-12 07:21:58 +0000762PyAPI_FUNC(PyObject*) PyUnicode_Decode(
Guido van Rossumd8225182000-03-10 22:33:05 +0000763 const char *s, /* encoded string */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000764 Py_ssize_t size, /* size of buffer */
Guido van Rossumd8225182000-03-10 22:33:05 +0000765 const char *encoding, /* encoding */
766 const char *errors /* error handling */
767 );
768
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000769/* Decode a Unicode object unicode and return the result as Python
770 object. */
771
772PyAPI_FUNC(PyObject*) PyUnicode_AsDecodedObject(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 PyObject *unicode, /* Unicode object */
774 const char *encoding, /* encoding */
775 const char *errors /* error handling */
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000776 );
777
778/* Decode a Unicode object unicode and return the result as Unicode
779 object. */
780
781PyAPI_FUNC(PyObject*) PyUnicode_AsDecodedUnicode(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 PyObject *unicode, /* Unicode object */
783 const char *encoding, /* encoding */
784 const char *errors /* error handling */
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000785 );
786
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000787/* Encodes a Py_UNICODE buffer of the given size and returns a
Guido van Rossumd8225182000-03-10 22:33:05 +0000788 Python string object. */
789
Mark Hammond91a681d2002-08-12 07:21:58 +0000790PyAPI_FUNC(PyObject*) PyUnicode_Encode(
Guido van Rossumd8225182000-03-10 22:33:05 +0000791 const Py_UNICODE *s, /* Unicode char buffer */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000792 Py_ssize_t size, /* number of Py_UNICODE chars to encode */
Guido van Rossumd8225182000-03-10 22:33:05 +0000793 const char *encoding, /* encoding */
794 const char *errors /* error handling */
795 );
796
Marc-André Lemburgd2d45982004-07-08 17:57:32 +0000797/* Encodes a Unicode object and returns the result as Python
798 object. */
799
800PyAPI_FUNC(PyObject*) PyUnicode_AsEncodedObject(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 PyObject *unicode, /* Unicode object */
802 const char *encoding, /* encoding */
803 const char *errors /* error handling */
Marc-André Lemburgd2d45982004-07-08 17:57:32 +0000804 );
805
Guido van Rossumd8225182000-03-10 22:33:05 +0000806/* Encodes a Unicode object and returns the result as Python string
807 object. */
808
Mark Hammond91a681d2002-08-12 07:21:58 +0000809PyAPI_FUNC(PyObject*) PyUnicode_AsEncodedString(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 PyObject *unicode, /* Unicode object */
811 const char *encoding, /* encoding */
812 const char *errors /* error handling */
Guido van Rossumd8225182000-03-10 22:33:05 +0000813 );
814
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000815/* Encodes a Unicode object and returns the result as Unicode
816 object. */
817
818PyAPI_FUNC(PyObject*) PyUnicode_AsEncodedUnicode(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 PyObject *unicode, /* Unicode object */
820 const char *encoding, /* encoding */
821 const char *errors /* error handling */
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000822 );
823
824/* Build an encoding map. */
825
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000826PyAPI_FUNC(PyObject*) PyUnicode_BuildEncodingMap(
827 PyObject* string /* 256 character map */
828 );
829
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000830/* --- UTF-7 Codecs ------------------------------------------------------- */
831
Mark Hammond91a681d2002-08-12 07:21:58 +0000832PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF7(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 const char *string, /* UTF-7 encoded string */
834 Py_ssize_t length, /* size of string */
835 const char *errors /* error handling */
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000836 );
837
Christian Heimes5d14c2b2007-11-20 23:38:09 +0000838PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF7Stateful(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 const char *string, /* UTF-7 encoded string */
840 Py_ssize_t length, /* size of string */
841 const char *errors, /* error handling */
842 Py_ssize_t *consumed /* bytes consumed */
Christian Heimes5d14c2b2007-11-20 23:38:09 +0000843 );
844
Mark Hammond91a681d2002-08-12 07:21:58 +0000845PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF7(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000846 const Py_UNICODE *data, /* Unicode char buffer */
847 Py_ssize_t length, /* number of Py_UNICODE chars to encode */
848 int base64SetO, /* Encode RFC2152 Set O characters in base64 */
849 int base64WhiteSpace, /* Encode whitespace (sp, ht, nl, cr) in base64 */
850 const char *errors /* error handling */
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000851 );
852
Guido van Rossumd8225182000-03-10 22:33:05 +0000853/* --- UTF-8 Codecs ------------------------------------------------------- */
854
Mark Hammond91a681d2002-08-12 07:21:58 +0000855PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF8(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 const char *string, /* UTF-8 encoded string */
857 Py_ssize_t length, /* size of string */
858 const char *errors /* error handling */
Guido van Rossumd8225182000-03-10 22:33:05 +0000859 );
860
Walter Dörwald69652032004-09-07 20:24:22 +0000861PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF8Stateful(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 const char *string, /* UTF-8 encoded string */
863 Py_ssize_t length, /* size of string */
864 const char *errors, /* error handling */
865 Py_ssize_t *consumed /* bytes consumed */
Walter Dörwald69652032004-09-07 20:24:22 +0000866 );
867
Mark Hammond91a681d2002-08-12 07:21:58 +0000868PyAPI_FUNC(PyObject*) PyUnicode_AsUTF8String(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 PyObject *unicode /* Unicode object */
Guido van Rossumd8225182000-03-10 22:33:05 +0000870 );
871
Mark Hammond91a681d2002-08-12 07:21:58 +0000872PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF8(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000873 const Py_UNICODE *data, /* Unicode char buffer */
874 Py_ssize_t length, /* number of Py_UNICODE chars to encode */
875 const char *errors /* error handling */
Guido van Rossumd8225182000-03-10 22:33:05 +0000876 );
877
Walter Dörwald41980ca2007-08-16 21:55:45 +0000878/* --- UTF-32 Codecs ------------------------------------------------------ */
879
880/* Decodes length bytes from a UTF-32 encoded buffer string and returns
881 the corresponding Unicode object.
882
883 errors (if non-NULL) defines the error handling. It defaults
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 to "strict".
Walter Dörwald41980ca2007-08-16 21:55:45 +0000885
886 If byteorder is non-NULL, the decoder starts decoding using the
887 given byte order:
888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 *byteorder == -1: little endian
890 *byteorder == 0: native order
891 *byteorder == 1: big endian
Walter Dörwald41980ca2007-08-16 21:55:45 +0000892
893 In native mode, the first four bytes of the stream are checked for a
894 BOM mark. If found, the BOM mark is analysed, the byte order
895 adjusted and the BOM skipped. In the other modes, no BOM mark
896 interpretation is done. After completion, *byteorder is set to the
897 current byte order at the end of input data.
898
899 If byteorder is NULL, the codec starts in native order mode.
900
901*/
902
903PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF32(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 const char *string, /* UTF-32 encoded string */
905 Py_ssize_t length, /* size of string */
906 const char *errors, /* error handling */
907 int *byteorder /* pointer to byteorder to use
908 0=native;-1=LE,1=BE; updated on
909 exit */
Walter Dörwald41980ca2007-08-16 21:55:45 +0000910 );
911
912PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF32Stateful(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000913 const char *string, /* UTF-32 encoded string */
914 Py_ssize_t length, /* size of string */
915 const char *errors, /* error handling */
916 int *byteorder, /* pointer to byteorder to use
917 0=native;-1=LE,1=BE; updated on
918 exit */
919 Py_ssize_t *consumed /* bytes consumed */
Walter Dörwald41980ca2007-08-16 21:55:45 +0000920 );
921
922/* Returns a Python string using the UTF-32 encoding in native byte
923 order. The string always starts with a BOM mark. */
924
925PyAPI_FUNC(PyObject*) PyUnicode_AsUTF32String(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 PyObject *unicode /* Unicode object */
Walter Dörwald41980ca2007-08-16 21:55:45 +0000927 );
928
929/* Returns a Python string object holding the UTF-32 encoded value of
930 the Unicode data.
931
932 If byteorder is not 0, output is written according to the following
933 byte order:
934
935 byteorder == -1: little endian
936 byteorder == 0: native byte order (writes a BOM mark)
937 byteorder == 1: big endian
938
939 If byteorder is 0, the output string will always start with the
940 Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark is
941 prepended.
942
943*/
944
945PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF32(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000946 const Py_UNICODE *data, /* Unicode char buffer */
947 Py_ssize_t length, /* number of Py_UNICODE chars to encode */
948 const char *errors, /* error handling */
949 int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
Walter Dörwald41980ca2007-08-16 21:55:45 +0000950 );
951
Guido van Rossumd8225182000-03-10 22:33:05 +0000952/* --- UTF-16 Codecs ------------------------------------------------------ */
953
Guido van Rossum9e896b32000-04-05 20:11:21 +0000954/* Decodes length bytes from a UTF-16 encoded buffer string and returns
Guido van Rossumd8225182000-03-10 22:33:05 +0000955 the corresponding Unicode object.
956
957 errors (if non-NULL) defines the error handling. It defaults
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 to "strict".
Guido van Rossumd8225182000-03-10 22:33:05 +0000959
960 If byteorder is non-NULL, the decoder starts decoding using the
961 given byte order:
962
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 *byteorder == -1: little endian
964 *byteorder == 0: native order
965 *byteorder == 1: big endian
Guido van Rossumd8225182000-03-10 22:33:05 +0000966
Marc-André Lemburg489b56e2001-05-21 20:30:15 +0000967 In native mode, the first two bytes of the stream are checked for a
968 BOM mark. If found, the BOM mark is analysed, the byte order
969 adjusted and the BOM skipped. In the other modes, no BOM mark
970 interpretation is done. After completion, *byteorder is set to the
971 current byte order at the end of input data.
Guido van Rossumd8225182000-03-10 22:33:05 +0000972
973 If byteorder is NULL, the codec starts in native order mode.
974
975*/
976
Mark Hammond91a681d2002-08-12 07:21:58 +0000977PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF16(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 const char *string, /* UTF-16 encoded string */
979 Py_ssize_t length, /* size of string */
980 const char *errors, /* error handling */
981 int *byteorder /* pointer to byteorder to use
982 0=native;-1=LE,1=BE; updated on
983 exit */
Guido van Rossumd8225182000-03-10 22:33:05 +0000984 );
985
Walter Dörwald69652032004-09-07 20:24:22 +0000986PyAPI_FUNC(PyObject*) PyUnicode_DecodeUTF16Stateful(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 const char *string, /* UTF-16 encoded string */
988 Py_ssize_t length, /* size of string */
989 const char *errors, /* error handling */
990 int *byteorder, /* pointer to byteorder to use
991 0=native;-1=LE,1=BE; updated on
992 exit */
993 Py_ssize_t *consumed /* bytes consumed */
Walter Dörwald69652032004-09-07 20:24:22 +0000994 );
995
Guido van Rossumd8225182000-03-10 22:33:05 +0000996/* Returns a Python string using the UTF-16 encoding in native byte
997 order. The string always starts with a BOM mark. */
998
Mark Hammond91a681d2002-08-12 07:21:58 +0000999PyAPI_FUNC(PyObject*) PyUnicode_AsUTF16String(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 PyObject *unicode /* Unicode object */
Guido van Rossumd8225182000-03-10 22:33:05 +00001001 );
1002
1003/* Returns a Python string object holding the UTF-16 encoded value of
Guido van Rossum9e896b32000-04-05 20:11:21 +00001004 the Unicode data.
Guido van Rossumd8225182000-03-10 22:33:05 +00001005
1006 If byteorder is not 0, output is written according to the following
1007 byte order:
1008
1009 byteorder == -1: little endian
1010 byteorder == 0: native byte order (writes a BOM mark)
1011 byteorder == 1: big endian
1012
1013 If byteorder is 0, the output string will always start with the
1014 Unicode BOM mark (U+FEFF). In the other two modes, no BOM mark is
1015 prepended.
1016
1017 Note that Py_UNICODE data is being interpreted as UTF-16 reduced to
1018 UCS-2. This trick makes it possible to add full UTF-16 capabilities
Thomas Wouters7e474022000-07-16 12:04:32 +00001019 at a later point without compromising the APIs.
Guido van Rossumd8225182000-03-10 22:33:05 +00001020
1021*/
1022
Mark Hammond91a681d2002-08-12 07:21:58 +00001023PyAPI_FUNC(PyObject*) PyUnicode_EncodeUTF16(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 const Py_UNICODE *data, /* Unicode char buffer */
1025 Py_ssize_t length, /* number of Py_UNICODE chars to encode */
1026 const char *errors, /* error handling */
1027 int byteorder /* byteorder to use 0=BOM+native;-1=LE,1=BE */
Guido van Rossumd8225182000-03-10 22:33:05 +00001028 );
1029
1030/* --- Unicode-Escape Codecs ---------------------------------------------- */
1031
Mark Hammond91a681d2002-08-12 07:21:58 +00001032PyAPI_FUNC(PyObject*) PyUnicode_DecodeUnicodeEscape(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 const char *string, /* Unicode-Escape encoded string */
1034 Py_ssize_t length, /* size of string */
1035 const char *errors /* error handling */
Guido van Rossumd8225182000-03-10 22:33:05 +00001036 );
1037
Mark Hammond91a681d2002-08-12 07:21:58 +00001038PyAPI_FUNC(PyObject*) PyUnicode_AsUnicodeEscapeString(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 PyObject *unicode /* Unicode object */
Guido van Rossumd8225182000-03-10 22:33:05 +00001040 );
1041
Mark Hammond91a681d2002-08-12 07:21:58 +00001042PyAPI_FUNC(PyObject*) PyUnicode_EncodeUnicodeEscape(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 const Py_UNICODE *data, /* Unicode char buffer */
1044 Py_ssize_t length /* Number of Py_UNICODE chars to encode */
Guido van Rossumd8225182000-03-10 22:33:05 +00001045 );
1046
1047/* --- Raw-Unicode-Escape Codecs ------------------------------------------ */
1048
Mark Hammond91a681d2002-08-12 07:21:58 +00001049PyAPI_FUNC(PyObject*) PyUnicode_DecodeRawUnicodeEscape(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 const char *string, /* Raw-Unicode-Escape encoded string */
1051 Py_ssize_t length, /* size of string */
1052 const char *errors /* error handling */
Guido van Rossumd8225182000-03-10 22:33:05 +00001053 );
1054
Mark Hammond91a681d2002-08-12 07:21:58 +00001055PyAPI_FUNC(PyObject*) PyUnicode_AsRawUnicodeEscapeString(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 PyObject *unicode /* Unicode object */
Guido van Rossumd8225182000-03-10 22:33:05 +00001057 );
1058
Mark Hammond91a681d2002-08-12 07:21:58 +00001059PyAPI_FUNC(PyObject*) PyUnicode_EncodeRawUnicodeEscape(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 const Py_UNICODE *data, /* Unicode char buffer */
1061 Py_ssize_t length /* Number of Py_UNICODE chars to encode */
Guido van Rossumd8225182000-03-10 22:33:05 +00001062 );
1063
Walter Dörwalda47d1c02005-08-30 10:23:14 +00001064/* --- Unicode Internal Codec ---------------------------------------------
1065
1066 Only for internal use in _codecsmodule.c */
1067
1068PyObject *_PyUnicode_DecodeUnicodeInternal(
1069 const char *string,
Martin v. Löwis18e16552006-02-15 17:27:45 +00001070 Py_ssize_t length,
Walter Dörwalda47d1c02005-08-30 10:23:14 +00001071 const char *errors
1072 );
1073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074/* --- Latin-1 Codecs -----------------------------------------------------
Guido van Rossumd8225182000-03-10 22:33:05 +00001075
1076 Note: Latin-1 corresponds to the first 256 Unicode ordinals.
1077
1078*/
1079
Mark Hammond91a681d2002-08-12 07:21:58 +00001080PyAPI_FUNC(PyObject*) PyUnicode_DecodeLatin1(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 const char *string, /* Latin-1 encoded string */
1082 Py_ssize_t length, /* size of string */
1083 const char *errors /* error handling */
Guido van Rossumd8225182000-03-10 22:33:05 +00001084 );
1085
Mark Hammond91a681d2002-08-12 07:21:58 +00001086PyAPI_FUNC(PyObject*) PyUnicode_AsLatin1String(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 PyObject *unicode /* Unicode object */
Guido van Rossumd8225182000-03-10 22:33:05 +00001088 );
1089
Mark Hammond91a681d2002-08-12 07:21:58 +00001090PyAPI_FUNC(PyObject*) PyUnicode_EncodeLatin1(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 const Py_UNICODE *data, /* Unicode char buffer */
1092 Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
1093 const char *errors /* error handling */
Guido van Rossumd8225182000-03-10 22:33:05 +00001094 );
1095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096/* --- ASCII Codecs -------------------------------------------------------
Guido van Rossumd8225182000-03-10 22:33:05 +00001097
1098 Only 7-bit ASCII data is excepted. All other codes generate errors.
1099
1100*/
1101
Mark Hammond91a681d2002-08-12 07:21:58 +00001102PyAPI_FUNC(PyObject*) PyUnicode_DecodeASCII(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001103 const char *string, /* ASCII encoded string */
1104 Py_ssize_t length, /* size of string */
1105 const char *errors /* error handling */
Guido van Rossumd8225182000-03-10 22:33:05 +00001106 );
1107
Mark Hammond91a681d2002-08-12 07:21:58 +00001108PyAPI_FUNC(PyObject*) PyUnicode_AsASCIIString(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 PyObject *unicode /* Unicode object */
Guido van Rossumd8225182000-03-10 22:33:05 +00001110 );
1111
Mark Hammond91a681d2002-08-12 07:21:58 +00001112PyAPI_FUNC(PyObject*) PyUnicode_EncodeASCII(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 const Py_UNICODE *data, /* Unicode char buffer */
1114 Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
1115 const char *errors /* error handling */
Guido van Rossumd8225182000-03-10 22:33:05 +00001116 );
1117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118/* --- Character Map Codecs -----------------------------------------------
Guido van Rossumd8225182000-03-10 22:33:05 +00001119
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 This codec uses mappings to encode and decode characters.
Guido van Rossumd8225182000-03-10 22:33:05 +00001121
1122 Decoding mappings must map single string characters to single
1123 Unicode characters, integers (which are then interpreted as Unicode
1124 ordinals) or None (meaning "undefined mapping" and causing an
1125 error).
1126
1127 Encoding mappings must map single Unicode characters to single
1128 string characters, integers (which are then interpreted as Latin-1
1129 ordinals) or None (meaning "undefined mapping" and causing an
1130 error).
1131
1132 If a character lookup fails with a LookupError, the character is
1133 copied as-is meaning that its ordinal value will be interpreted as
1134 Unicode or Latin-1 ordinal resp. Because of this mappings only need
1135 to contain those mappings which map characters to different code
1136 points.
1137
1138*/
1139
Mark Hammond91a681d2002-08-12 07:21:58 +00001140PyAPI_FUNC(PyObject*) PyUnicode_DecodeCharmap(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 const char *string, /* Encoded string */
1142 Py_ssize_t length, /* size of string */
1143 PyObject *mapping, /* character mapping
1144 (char ordinal -> unicode ordinal) */
1145 const char *errors /* error handling */
Guido van Rossumd8225182000-03-10 22:33:05 +00001146 );
1147
Mark Hammond91a681d2002-08-12 07:21:58 +00001148PyAPI_FUNC(PyObject*) PyUnicode_AsCharmapString(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 PyObject *unicode, /* Unicode object */
1150 PyObject *mapping /* character mapping
1151 (unicode ordinal -> char ordinal) */
Guido van Rossumd8225182000-03-10 22:33:05 +00001152 );
1153
Mark Hammond91a681d2002-08-12 07:21:58 +00001154PyAPI_FUNC(PyObject*) PyUnicode_EncodeCharmap(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 const Py_UNICODE *data, /* Unicode char buffer */
1156 Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
1157 PyObject *mapping, /* character mapping
1158 (unicode ordinal -> char ordinal) */
1159 const char *errors /* error handling */
Guido van Rossumd8225182000-03-10 22:33:05 +00001160 );
1161
1162/* Translate a Py_UNICODE buffer of the given length by applying a
1163 character mapping table to it and return the resulting Unicode
1164 object.
1165
1166 The mapping table must map Unicode ordinal integers to Unicode
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 ordinal integers or None (causing deletion of the character).
Guido van Rossumd8225182000-03-10 22:33:05 +00001168
1169 Mapping tables may be dictionaries or sequences. Unmapped character
1170 ordinals (ones which cause a LookupError) are left untouched and
1171 are copied as-is.
1172
1173*/
1174
Mark Hammond91a681d2002-08-12 07:21:58 +00001175PyAPI_FUNC(PyObject *) PyUnicode_TranslateCharmap(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 const Py_UNICODE *data, /* Unicode char buffer */
1177 Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
1178 PyObject *table, /* Translate table */
1179 const char *errors /* error handling */
Guido van Rossumd8225182000-03-10 22:33:05 +00001180 );
1181
Guido van Rossumefec1152000-03-28 02:01:15 +00001182#ifdef MS_WIN32
Guido van Rossum24bdb042000-03-28 20:29:59 +00001183
Guido van Rossumefec1152000-03-28 02:01:15 +00001184/* --- MBCS codecs for Windows -------------------------------------------- */
Guido van Rossum24bdb042000-03-28 20:29:59 +00001185
Mark Hammond91a681d2002-08-12 07:21:58 +00001186PyAPI_FUNC(PyObject*) PyUnicode_DecodeMBCS(
Guido van Rossumefec1152000-03-28 02:01:15 +00001187 const char *string, /* MBCS encoded string */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001188 Py_ssize_t length, /* size of string */
Guido van Rossumefec1152000-03-28 02:01:15 +00001189 const char *errors /* error handling */
1190 );
1191
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001192PyAPI_FUNC(PyObject*) PyUnicode_DecodeMBCSStateful(
1193 const char *string, /* MBCS encoded string */
1194 Py_ssize_t length, /* size of string */
1195 const char *errors, /* error handling */
1196 Py_ssize_t *consumed /* bytes consumed */
1197 );
1198
Mark Hammond91a681d2002-08-12 07:21:58 +00001199PyAPI_FUNC(PyObject*) PyUnicode_AsMBCSString(
Guido van Rossumefec1152000-03-28 02:01:15 +00001200 PyObject *unicode /* Unicode object */
1201 );
1202
Mark Hammond91a681d2002-08-12 07:21:58 +00001203PyAPI_FUNC(PyObject*) PyUnicode_EncodeMBCS(
Guido van Rossumefec1152000-03-28 02:01:15 +00001204 const Py_UNICODE *data, /* Unicode char buffer */
Neal Norwitzd78f6cf2007-08-08 04:49:37 +00001205 Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
Guido van Rossumefec1152000-03-28 02:01:15 +00001206 const char *errors /* error handling */
1207 );
1208
Guido van Rossumefec1152000-03-28 02:01:15 +00001209#endif /* MS_WIN32 */
Guido van Rossum24bdb042000-03-28 20:29:59 +00001210
Guido van Rossum9e896b32000-04-05 20:11:21 +00001211/* --- Decimal Encoder ---------------------------------------------------- */
1212
1213/* Takes a Unicode string holding a decimal value and writes it into
1214 an output buffer using standard ASCII digit codes.
1215
1216 The output buffer has to provide at least length+1 bytes of storage
1217 area. The output string is 0-terminated.
1218
1219 The encoder converts whitespace to ' ', decimal characters to their
1220 corresponding ASCII digit and all other Latin-1 characters except
1221 \0 as-is. Characters outside this range (Unicode ordinals 1-256)
1222 are treated as errors. This includes embedded NULL bytes.
1223
1224 Error handling is defined by the errors argument:
1225
1226 NULL or "strict": raise a ValueError
1227 "ignore": ignore the wrong characters (these are not copied to the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 output buffer)
Guido van Rossum9e896b32000-04-05 20:11:21 +00001229 "replace": replaces illegal characters with '?'
1230
1231 Returns 0 on success, -1 on failure.
1232
1233*/
1234
Mark Hammond91a681d2002-08-12 07:21:58 +00001235PyAPI_FUNC(int) PyUnicode_EncodeDecimal(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 Py_UNICODE *s, /* Unicode buffer */
1237 Py_ssize_t length, /* Number of Py_UNICODE chars to encode */
1238 char *output, /* Output buffer; must have size >= length */
1239 const char *errors /* error handling */
Guido van Rossum9e896b32000-04-05 20:11:21 +00001240 );
1241
Martin v. Löwis011e8422009-05-05 04:43:17 +00001242/* --- File system encoding ---------------------------------------------- */
1243
Victor Stinner47fcb5b2010-08-13 23:59:58 +00001244/* ParseTuple converter: encode str objects to bytes using
1245 PyUnicode_EncodeFSDefault(); bytes objects are output as-is. */
Martin v. Löwis011e8422009-05-05 04:43:17 +00001246
1247PyAPI_FUNC(int) PyUnicode_FSConverter(PyObject*, void*);
1248
Victor Stinner47fcb5b2010-08-13 23:59:58 +00001249/* ParseTuple converter: decode bytes objects to unicode using
1250 PyUnicode_DecodeFSDefaultAndSize(); str objects are output as-is. */
1251
1252PyAPI_FUNC(int) PyUnicode_FSDecoder(PyObject*, void*);
1253
Victor Stinner77c38622010-05-14 15:58:55 +00001254/* Decode a null-terminated string using Py_FileSystemDefaultEncoding
1255 and the "surrogateescape" error handler.
Martin v. Löwis011e8422009-05-05 04:43:17 +00001256
Victor Stinner77c38622010-05-14 15:58:55 +00001257 If Py_FileSystemDefaultEncoding is not set, fall back to UTF-8.
Martin v. Löwis011e8422009-05-05 04:43:17 +00001258
Benjamin Petersonccbd6942010-05-15 17:43:18 +00001259 Use PyUnicode_DecodeFSDefaultAndSize() if the string length is known.
Martin v. Löwis011e8422009-05-05 04:43:17 +00001260*/
1261
1262PyAPI_FUNC(PyObject*) PyUnicode_DecodeFSDefault(
1263 const char *s /* encoded string */
1264 );
1265
Victor Stinner77c38622010-05-14 15:58:55 +00001266/* Decode a string using Py_FileSystemDefaultEncoding
1267 and the "surrogateescape" error handler.
1268
1269 If Py_FileSystemDefaultEncoding is not set, fall back to UTF-8.
1270*/
1271
Martin v. Löwis011e8422009-05-05 04:43:17 +00001272PyAPI_FUNC(PyObject*) PyUnicode_DecodeFSDefaultAndSize(
1273 const char *s, /* encoded string */
1274 Py_ssize_t size /* size */
1275 );
1276
Victor Stinnerae6265f2010-05-15 16:27:27 +00001277/* Encode a Unicode object to Py_FileSystemDefaultEncoding with the
Benjamin Petersonccbd6942010-05-15 17:43:18 +00001278 "surrogateescape" error handler, and return bytes.
Victor Stinnerae6265f2010-05-15 16:27:27 +00001279
1280 If Py_FileSystemDefaultEncoding is not set, fall back to UTF-8.
1281*/
1282
1283PyAPI_FUNC(PyObject*) PyUnicode_EncodeFSDefault(
1284 PyObject *unicode
1285 );
1286
Guido van Rossumd8225182000-03-10 22:33:05 +00001287/* --- Methods & Slots ----------------------------------------------------
1288
1289 These are capable of handling Unicode objects and strings on input
1290 (we refer to them as strings in the descriptions) and return
1291 Unicode objects or integers as apporpriate. */
1292
1293/* Concat two strings giving a new Unicode string. */
1294
Mark Hammond91a681d2002-08-12 07:21:58 +00001295PyAPI_FUNC(PyObject*) PyUnicode_Concat(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001296 PyObject *left, /* Left string */
1297 PyObject *right /* Right string */
Guido van Rossumd8225182000-03-10 22:33:05 +00001298 );
1299
Walter Dörwald1ab83302007-05-18 17:15:44 +00001300/* Concat two strings and put the result in *pleft
1301 (sets *pleft to NULL on error) */
1302
1303PyAPI_FUNC(void) PyUnicode_Append(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 PyObject **pleft, /* Pointer to left string */
1305 PyObject *right /* Right string */
Walter Dörwald1ab83302007-05-18 17:15:44 +00001306 );
1307
1308/* Concat two strings, put the result in *pleft and drop the right object
1309 (sets *pleft to NULL on error) */
1310
1311PyAPI_FUNC(void) PyUnicode_AppendAndDel(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 PyObject **pleft, /* Pointer to left string */
1313 PyObject *right /* Right string */
Walter Dörwald1ab83302007-05-18 17:15:44 +00001314 );
1315
Guido van Rossumd8225182000-03-10 22:33:05 +00001316/* Split a string giving a list of Unicode strings.
1317
1318 If sep is NULL, splitting will be done at all whitespace
1319 substrings. Otherwise, splits occur at the given separator.
1320
1321 At most maxsplit splits will be done. If negative, no limit is set.
1322
1323 Separators are not included in the resulting list.
1324
1325*/
1326
Mark Hammond91a681d2002-08-12 07:21:58 +00001327PyAPI_FUNC(PyObject*) PyUnicode_Split(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001328 PyObject *s, /* String to split */
1329 PyObject *sep, /* String separator */
1330 Py_ssize_t maxsplit /* Maxsplit count */
1331 );
Guido van Rossumd8225182000-03-10 22:33:05 +00001332
1333/* Dito, but split at line breaks.
1334
1335 CRLF is considered to be one line break. Line breaks are not
1336 included in the resulting list. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337
Mark Hammond91a681d2002-08-12 07:21:58 +00001338PyAPI_FUNC(PyObject*) PyUnicode_Splitlines(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 PyObject *s, /* String to split */
1340 int keepends /* If true, line end markers are included */
1341 );
Guido van Rossumd8225182000-03-10 22:33:05 +00001342
Thomas Wouters477c8d52006-05-27 19:21:47 +00001343/* Partition a string using a given separator. */
1344
1345PyAPI_FUNC(PyObject*) PyUnicode_Partition(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 PyObject *s, /* String to partition */
1347 PyObject *sep /* String separator */
1348 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001349
1350/* Partition a string using a given separator, searching from the end of the
1351 string. */
1352
1353PyAPI_FUNC(PyObject*) PyUnicode_RPartition(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 PyObject *s, /* String to partition */
1355 PyObject *sep /* String separator */
1356 );
Thomas Wouters477c8d52006-05-27 19:21:47 +00001357
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +00001358/* Split a string giving a list of Unicode strings.
1359
1360 If sep is NULL, splitting will be done at all whitespace
1361 substrings. Otherwise, splits occur at the given separator.
1362
1363 At most maxsplit splits will be done. But unlike PyUnicode_Split
1364 PyUnicode_RSplit splits from the end of the string. If negative,
1365 no limit is set.
1366
1367 Separators are not included in the resulting list.
1368
1369*/
1370
1371PyAPI_FUNC(PyObject*) PyUnicode_RSplit(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 PyObject *s, /* String to split */
1373 PyObject *sep, /* String separator */
1374 Py_ssize_t maxsplit /* Maxsplit count */
1375 );
Hye-Shik Chang3ae811b2003-12-15 18:49:53 +00001376
Guido van Rossumd8225182000-03-10 22:33:05 +00001377/* Translate a string by applying a character mapping table to it and
1378 return the resulting Unicode object.
1379
1380 The mapping table must map Unicode ordinal integers to Unicode
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 ordinal integers or None (causing deletion of the character).
Guido van Rossumd8225182000-03-10 22:33:05 +00001382
1383 Mapping tables may be dictionaries or sequences. Unmapped character
1384 ordinals (ones which cause a LookupError) are left untouched and
1385 are copied as-is.
1386
1387*/
1388
Mark Hammond91a681d2002-08-12 07:21:58 +00001389PyAPI_FUNC(PyObject *) PyUnicode_Translate(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 PyObject *str, /* String */
1391 PyObject *table, /* Translate table */
1392 const char *errors /* error handling */
Guido van Rossumd8225182000-03-10 22:33:05 +00001393 );
1394
1395/* Join a sequence of strings using the given separator and return
1396 the resulting Unicode string. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397
Mark Hammond91a681d2002-08-12 07:21:58 +00001398PyAPI_FUNC(PyObject*) PyUnicode_Join(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 PyObject *separator, /* Separator string */
1400 PyObject *seq /* Sequence object */
Guido van Rossumd8225182000-03-10 22:33:05 +00001401 );
1402
1403/* Return 1 if substr matches str[start:end] at the given tail end, 0
1404 otherwise. */
1405
Martin v. Löwis18e16552006-02-15 17:27:45 +00001406PyAPI_FUNC(Py_ssize_t) PyUnicode_Tailmatch(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001407 PyObject *str, /* String */
1408 PyObject *substr, /* Prefix or Suffix string */
1409 Py_ssize_t start, /* Start index */
1410 Py_ssize_t end, /* Stop index */
1411 int direction /* Tail end: -1 prefix, +1 suffix */
Guido van Rossumd8225182000-03-10 22:33:05 +00001412 );
1413
1414/* Return the first position of substr in str[start:end] using the
Marc-André Lemburg4da6fd62002-05-29 11:33:13 +00001415 given search direction or -1 if not found. -2 is returned in case
1416 an error occurred and an exception is set. */
Guido van Rossumd8225182000-03-10 22:33:05 +00001417
Martin v. Löwis18e16552006-02-15 17:27:45 +00001418PyAPI_FUNC(Py_ssize_t) PyUnicode_Find(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 PyObject *str, /* String */
1420 PyObject *substr, /* Substring to find */
1421 Py_ssize_t start, /* Start index */
1422 Py_ssize_t end, /* Stop index */
1423 int direction /* Find direction: +1 forward, -1 backward */
Guido van Rossumd8225182000-03-10 22:33:05 +00001424 );
1425
Barry Warsaw51ac5802000-03-20 16:36:48 +00001426/* Count the number of occurrences of substr in str[start:end]. */
Guido van Rossumd8225182000-03-10 22:33:05 +00001427
Martin v. Löwis18e16552006-02-15 17:27:45 +00001428PyAPI_FUNC(Py_ssize_t) PyUnicode_Count(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 PyObject *str, /* String */
1430 PyObject *substr, /* Substring to count */
1431 Py_ssize_t start, /* Start index */
1432 Py_ssize_t end /* Stop index */
Guido van Rossumd8225182000-03-10 22:33:05 +00001433 );
1434
Barry Warsaw51ac5802000-03-20 16:36:48 +00001435/* Replace at most maxcount occurrences of substr in str with replstr
Guido van Rossumd8225182000-03-10 22:33:05 +00001436 and return the resulting Unicode object. */
1437
Mark Hammond91a681d2002-08-12 07:21:58 +00001438PyAPI_FUNC(PyObject *) PyUnicode_Replace(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 PyObject *str, /* String */
1440 PyObject *substr, /* Substring to find */
1441 PyObject *replstr, /* Substring to replace */
1442 Py_ssize_t maxcount /* Max. number of replacements to apply;
1443 -1 = all */
Guido van Rossumd8225182000-03-10 22:33:05 +00001444 );
1445
1446/* Compare two strings and return -1, 0, 1 for less than, equal,
1447 greater than resp. */
1448
Mark Hammond91a681d2002-08-12 07:21:58 +00001449PyAPI_FUNC(int) PyUnicode_Compare(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 PyObject *left, /* Left string */
1451 PyObject *right /* Right string */
Guido van Rossumd8225182000-03-10 22:33:05 +00001452 );
1453
Martin v. Löwis5b222132007-06-10 09:51:05 +00001454PyAPI_FUNC(int) PyUnicode_CompareWithASCIIString(
1455 PyObject *left,
1456 const char *right
1457 );
1458
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001459/* Rich compare two strings and return one of the following:
1460
1461 - NULL in case an exception was raised
1462 - Py_True or Py_False for successfuly comparisons
1463 - Py_NotImplemented in case the type combination is unknown
1464
1465 Note that Py_EQ and Py_NE comparisons can cause a UnicodeWarning in
1466 case the conversion of the arguments to Unicode fails with a
1467 UnicodeDecodeError.
1468
1469 Possible values for op:
1470
1471 Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE
1472
1473*/
1474
1475PyAPI_FUNC(PyObject *) PyUnicode_RichCompare(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001476 PyObject *left, /* Left string */
1477 PyObject *right, /* Right string */
1478 int op /* Operation: Py_EQ, Py_NE, Py_GT, etc. */
Thomas Wouters00ee7ba2006-08-21 19:07:27 +00001479 );
1480
Thomas Wouters7e474022000-07-16 12:04:32 +00001481/* Apply a argument tuple or dictionary to a format string and return
Guido van Rossumd8225182000-03-10 22:33:05 +00001482 the resulting Unicode string. */
1483
Mark Hammond91a681d2002-08-12 07:21:58 +00001484PyAPI_FUNC(PyObject *) PyUnicode_Format(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001485 PyObject *format, /* Format string */
1486 PyObject *args /* Argument tuple or dictionary */
Guido van Rossumd8225182000-03-10 22:33:05 +00001487 );
1488
Guido van Rossumd0d366b2000-03-13 23:22:24 +00001489/* Checks whether element is contained in container and return 1/0
1490 accordingly.
1491
1492 element has to coerce to an one element Unicode string. -1 is
1493 returned in case of an error. */
1494
Mark Hammond91a681d2002-08-12 07:21:58 +00001495PyAPI_FUNC(int) PyUnicode_Contains(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 PyObject *container, /* Container string */
1497 PyObject *element /* Element string */
Guido van Rossumd0d366b2000-03-13 23:22:24 +00001498 );
1499
Martin v. Löwis47383402007-08-15 07:32:56 +00001500/* Checks whether argument is a valid identifier. */
1501
1502PyAPI_FUNC(int) PyUnicode_IsIdentifier(PyObject *s);
1503
Walter Dörwaldde02bcb2002-04-22 17:42:37 +00001504/* Externally visible for str.strip(unicode) */
Mark Hammond91a681d2002-08-12 07:21:58 +00001505PyAPI_FUNC(PyObject *) _PyUnicode_XStrip(
Walter Dörwaldde02bcb2002-04-22 17:42:37 +00001506 PyUnicodeObject *self,
1507 int striptype,
1508 PyObject *sepobj
1509 );
1510
Eric Smith5807c412008-05-11 21:00:57 +00001511/* Using the current locale, insert the thousands grouping
1512 into the string pointed to by buffer. For the argument descriptions,
1513 see Objects/stringlib/localeutil.h */
1514
Eric Smith0923d1d2009-04-16 20:16:10 +00001515PyAPI_FUNC(Py_ssize_t) _PyUnicode_InsertThousandsGroupingLocale(Py_UNICODE *buffer,
1516 Py_ssize_t n_buffer,
1517 Py_UNICODE *digits,
1518 Py_ssize_t n_digits,
1519 Py_ssize_t min_width);
Eric Smith5807c412008-05-11 21:00:57 +00001520
Eric Smitha3b1ac82009-04-03 14:45:06 +00001521/* Using explicit passed-in values, insert the thousands grouping
1522 into the string pointed to by buffer. For the argument descriptions,
1523 see Objects/stringlib/localeutil.h */
Eric Smith0923d1d2009-04-16 20:16:10 +00001524PyAPI_FUNC(Py_ssize_t) _PyUnicode_InsertThousandsGrouping(Py_UNICODE *buffer,
1525 Py_ssize_t n_buffer,
1526 Py_UNICODE *digits,
1527 Py_ssize_t n_digits,
1528 Py_ssize_t min_width,
1529 const char *grouping,
1530 const char *thousands_sep);
Guido van Rossumd8225182000-03-10 22:33:05 +00001531/* === Characters Type APIs =============================================== */
1532
Benjamin Peterson960cf0f2009-01-09 04:11:44 +00001533/* Helper array used by Py_UNICODE_ISSPACE(). */
1534
1535PyAPI_DATA(const unsigned char) _Py_ascii_whitespace[];
1536
Guido van Rossumd8225182000-03-10 22:33:05 +00001537/* These should not be used directly. Use the Py_UNICODE_IS* and
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 Py_UNICODE_TO* macros instead.
Guido van Rossumd8225182000-03-10 22:33:05 +00001539
1540 These APIs are implemented in Objects/unicodectype.c.
1541
1542*/
1543
Mark Hammond91a681d2002-08-12 07:21:58 +00001544PyAPI_FUNC(int) _PyUnicode_IsLowercase(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 Py_UNICODE ch /* Unicode character */
Guido van Rossumd8225182000-03-10 22:33:05 +00001546 );
1547
Mark Hammond91a681d2002-08-12 07:21:58 +00001548PyAPI_FUNC(int) _PyUnicode_IsUppercase(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 Py_UNICODE ch /* Unicode character */
Guido van Rossumd8225182000-03-10 22:33:05 +00001550 );
1551
Mark Hammond91a681d2002-08-12 07:21:58 +00001552PyAPI_FUNC(int) _PyUnicode_IsTitlecase(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001553 Py_UNICODE ch /* Unicode character */
Guido van Rossumd8225182000-03-10 22:33:05 +00001554 );
1555
Martin v. Löwis13c3e382007-08-14 22:37:03 +00001556PyAPI_FUNC(int) _PyUnicode_IsXidStart(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001557 Py_UNICODE ch /* Unicode character */
Martin v. Löwis13c3e382007-08-14 22:37:03 +00001558 );
1559
1560PyAPI_FUNC(int) _PyUnicode_IsXidContinue(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001561 Py_UNICODE ch /* Unicode character */
Martin v. Löwis13c3e382007-08-14 22:37:03 +00001562 );
1563
Mark Hammond91a681d2002-08-12 07:21:58 +00001564PyAPI_FUNC(int) _PyUnicode_IsWhitespace(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 const Py_UNICODE ch /* Unicode character */
Guido van Rossumd8225182000-03-10 22:33:05 +00001566 );
1567
Mark Hammond91a681d2002-08-12 07:21:58 +00001568PyAPI_FUNC(int) _PyUnicode_IsLinebreak(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569 const Py_UNICODE ch /* Unicode character */
Guido van Rossumd8225182000-03-10 22:33:05 +00001570 );
1571
Mark Hammond91a681d2002-08-12 07:21:58 +00001572PyAPI_FUNC(Py_UNICODE) _PyUnicode_ToLowercase(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 Py_UNICODE ch /* Unicode character */
Guido van Rossumd8225182000-03-10 22:33:05 +00001574 );
1575
Mark Hammond91a681d2002-08-12 07:21:58 +00001576PyAPI_FUNC(Py_UNICODE) _PyUnicode_ToUppercase(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 Py_UNICODE ch /* Unicode character */
Guido van Rossumd8225182000-03-10 22:33:05 +00001578 );
1579
Mark Hammond91a681d2002-08-12 07:21:58 +00001580PyAPI_FUNC(Py_UNICODE) _PyUnicode_ToTitlecase(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 Py_UNICODE ch /* Unicode character */
Guido van Rossumd8225182000-03-10 22:33:05 +00001582 );
1583
Mark Hammond91a681d2002-08-12 07:21:58 +00001584PyAPI_FUNC(int) _PyUnicode_ToDecimalDigit(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 Py_UNICODE ch /* Unicode character */
Guido van Rossumd8225182000-03-10 22:33:05 +00001586 );
1587
Mark Hammond91a681d2002-08-12 07:21:58 +00001588PyAPI_FUNC(int) _PyUnicode_ToDigit(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 Py_UNICODE ch /* Unicode character */
Guido van Rossumd8225182000-03-10 22:33:05 +00001590 );
1591
Mark Hammond91a681d2002-08-12 07:21:58 +00001592PyAPI_FUNC(double) _PyUnicode_ToNumeric(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 Py_UNICODE ch /* Unicode character */
Guido van Rossumd8225182000-03-10 22:33:05 +00001594 );
1595
Mark Hammond91a681d2002-08-12 07:21:58 +00001596PyAPI_FUNC(int) _PyUnicode_IsDecimalDigit(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 Py_UNICODE ch /* Unicode character */
Guido van Rossumd8225182000-03-10 22:33:05 +00001598 );
1599
Mark Hammond91a681d2002-08-12 07:21:58 +00001600PyAPI_FUNC(int) _PyUnicode_IsDigit(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 Py_UNICODE ch /* Unicode character */
Guido van Rossumd8225182000-03-10 22:33:05 +00001602 );
1603
Mark Hammond91a681d2002-08-12 07:21:58 +00001604PyAPI_FUNC(int) _PyUnicode_IsNumeric(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 Py_UNICODE ch /* Unicode character */
Guido van Rossumd8225182000-03-10 22:33:05 +00001606 );
1607
Georg Brandl559e5d72008-06-11 18:37:52 +00001608PyAPI_FUNC(int) _PyUnicode_IsPrintable(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609 Py_UNICODE ch /* Unicode character */
Georg Brandl559e5d72008-06-11 18:37:52 +00001610 );
1611
Mark Hammond91a681d2002-08-12 07:21:58 +00001612PyAPI_FUNC(int) _PyUnicode_IsAlpha(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 Py_UNICODE ch /* Unicode character */
Marc-André Lemburgf03e7412000-07-05 09:45:59 +00001614 );
1615
Victor Stinneref8d95c2010-08-16 22:03:11 +00001616PyAPI_FUNC(size_t) Py_UNICODE_strlen(
1617 const Py_UNICODE *u
1618 );
Martin v. Löwis5b222132007-06-10 09:51:05 +00001619
1620PyAPI_FUNC(Py_UNICODE*) Py_UNICODE_strcpy(
Victor Stinneref8d95c2010-08-16 22:03:11 +00001621 Py_UNICODE *s1,
1622 const Py_UNICODE *s2);
Martin v. Löwis5b222132007-06-10 09:51:05 +00001623
1624PyAPI_FUNC(Py_UNICODE*) Py_UNICODE_strncpy(
Victor Stinneref8d95c2010-08-16 22:03:11 +00001625 Py_UNICODE *s1,
1626 const Py_UNICODE *s2,
1627 size_t n);
Martin v. Löwis5b222132007-06-10 09:51:05 +00001628
1629PyAPI_FUNC(int) Py_UNICODE_strcmp(
Victor Stinneref8d95c2010-08-16 22:03:11 +00001630 const Py_UNICODE *s1,
1631 const Py_UNICODE *s2
1632 );
1633
1634PyAPI_FUNC(int) Py_UNICODE_strncmp(
1635 const Py_UNICODE *s1,
1636 const Py_UNICODE *s2,
1637 size_t n
1638 );
Martin v. Löwis5b222132007-06-10 09:51:05 +00001639
1640PyAPI_FUNC(Py_UNICODE*) Py_UNICODE_strchr(
Victor Stinneref8d95c2010-08-16 22:03:11 +00001641 const Py_UNICODE *s,
1642 Py_UNICODE c
Martin v. Löwis5b222132007-06-10 09:51:05 +00001643 );
1644
Victor Stinner331ea922010-08-10 16:37:20 +00001645PyAPI_FUNC(Py_UNICODE*) Py_UNICODE_strrchr(
Victor Stinneref8d95c2010-08-16 22:03:11 +00001646 const Py_UNICODE *s,
1647 Py_UNICODE c
Victor Stinner331ea922010-08-10 16:37:20 +00001648 );
1649
Guido van Rossumd8225182000-03-10 22:33:05 +00001650#ifdef __cplusplus
1651}
1652#endif
Guido van Rossumd8225182000-03-10 22:33:05 +00001653#endif /* !Py_UNICODEOBJECT_H */