blob: 103a9bee092865ce527739b793713aca7df4833a [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* String object interface */
3
Fred Drake3cf4d2b2000-07-09 00:55:06 +00004#ifndef Py_STRINGOBJECT_H
5#define Py_STRINGOBJECT_H
6#ifdef __cplusplus
7extern "C" {
8#endif
9
Barry Warsawdadace02001-08-24 18:32:06 +000010#include <stdarg.h>
11
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000012/*
Guido van Rossumcaa63801995-01-12 11:45:45 +000013Type PyStringObject represents a character string. An extra zero byte is
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014reserved at the end to ensure it is zero-terminated, but a size is
15present so strings with null bytes in them can be represented. This
16is an immutable object type.
17
18There are functions to create new string objects, to test
19an object for string-ness, and to get the
20string value. The latter function returns a null pointer
21if the object is not of the proper type.
22There is a variant that takes an explicit size as well as a
23variant that assumes a zero-terminated string. Note that none of the
24functions should be applied to nil objects.
25*/
26
Tim Peters1f7df352002-03-29 03:29:08 +000027/* Caching the hash (ob_shash) saves recalculation of a string's hash value.
Guido van Rossum45ec02a2002-08-19 21:43:18 +000028 Interning strings (ob_sstate) tries to ensure that only one string
Tim Peters1f7df352002-03-29 03:29:08 +000029 object with a given value exists, so equality tests can be one pointer
30 comparison. This is generally restricted to strings that "look like"
Georg Brandl66a796e2006-12-19 20:50:34 +000031 Python identifiers, although the sys.intern() function can be used to force
Tim Peters1f7df352002-03-29 03:29:08 +000032 interning of any string.
33 Together, these sped the interpreter by up to 20%. */
Guido van Rossumfdebf251996-07-30 16:42:03 +000034
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035typedef struct {
Fred Drake3cf4d2b2000-07-09 00:55:06 +000036 PyObject_VAR_HEAD
Fred Drake3cf4d2b2000-07-09 00:55:06 +000037 long ob_shash;
Guido van Rossum45ec02a2002-08-19 21:43:18 +000038 int ob_sstate;
Fred Drake3cf4d2b2000-07-09 00:55:06 +000039 char ob_sval[1];
Armin Rigo89a39462004-10-28 16:32:00 +000040
41 /* Invariants:
42 * ob_sval contains space for 'ob_size+1' elements.
43 * ob_sval[ob_size] == 0.
44 * ob_shash is the hash of the string or -1 if not computed yet.
45 * ob_sstate != 0 iff the string object is in stringobject.c's
46 * 'interned' dictionary; in this case the two references
47 * from 'interned' to this object are *not counted* in ob_refcnt.
48 */
Guido van Rossumcaa63801995-01-12 11:45:45 +000049} PyStringObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000050
Mark Hammond91a681d2002-08-12 07:21:58 +000051PyAPI_DATA(PyTypeObject) PyString_Type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000052
Thomas Wouters27d517b2007-02-25 20:39:11 +000053#define PyString_Check(op) \
Martin v. Löwis9f2e3462007-07-21 17:22:18 +000054 PyType_FastSubclass(Py_Type(op), Py_TPFLAGS_STRING_SUBCLASS)
55#define PyString_CheckExact(op) (Py_Type(op) == &PyString_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056
Martin v. Löwis18e16552006-02-15 17:27:45 +000057PyAPI_FUNC(PyObject *) PyString_FromStringAndSize(const char *, Py_ssize_t);
Mark Hammond91a681d2002-08-12 07:21:58 +000058PyAPI_FUNC(PyObject *) PyString_FromString(const char *);
59PyAPI_FUNC(PyObject *) PyString_FromFormatV(const char*, va_list)
Neil Schemenauer96aa0ac2002-09-15 14:09:54 +000060 Py_GCC_ATTRIBUTE((format(printf, 1, 0)));
Mark Hammond91a681d2002-08-12 07:21:58 +000061PyAPI_FUNC(PyObject *) PyString_FromFormat(const char*, ...)
Neil Schemenauer96aa0ac2002-09-15 14:09:54 +000062 Py_GCC_ATTRIBUTE((format(printf, 1, 2)));
Martin v. Löwis18e16552006-02-15 17:27:45 +000063PyAPI_FUNC(Py_ssize_t) PyString_Size(PyObject *);
Mark Hammond91a681d2002-08-12 07:21:58 +000064PyAPI_FUNC(char *) PyString_AsString(PyObject *);
Martin v. Löwis8a8da792002-08-14 07:46:28 +000065PyAPI_FUNC(PyObject *) PyString_Repr(PyObject *, int);
Mark Hammond91a681d2002-08-12 07:21:58 +000066PyAPI_FUNC(void) PyString_Concat(PyObject **, PyObject *);
67PyAPI_FUNC(void) PyString_ConcatAndDel(PyObject **, PyObject *);
Martin v. Löwis18e16552006-02-15 17:27:45 +000068PyAPI_FUNC(int) _PyString_Resize(PyObject **, Py_ssize_t);
Mark Hammond91a681d2002-08-12 07:21:58 +000069PyAPI_FUNC(int) _PyString_Eq(PyObject *, PyObject*);
70PyAPI_FUNC(PyObject *) PyString_Format(PyObject *, PyObject *);
71PyAPI_FUNC(PyObject *) _PyString_FormatLong(PyObject*, int, int,
Tim Peters38fd5b62000-09-21 05:43:11 +000072 int, char**, int*);
Martin v. Löwis18e16552006-02-15 17:27:45 +000073PyAPI_FUNC(PyObject *) PyString_DecodeEscape(const char *, Py_ssize_t,
74 const char *, Py_ssize_t,
Martin v. Löwis8a8da792002-08-14 07:46:28 +000075 const char *);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076
Mark Hammond91a681d2002-08-12 07:21:58 +000077PyAPI_FUNC(void) PyString_InternInPlace(PyObject **);
Guido van Rossum45ec02a2002-08-19 21:43:18 +000078PyAPI_FUNC(void) PyString_InternImmortal(PyObject **);
Mark Hammond91a681d2002-08-12 07:21:58 +000079PyAPI_FUNC(PyObject *) PyString_InternFromString(const char *);
80PyAPI_FUNC(void) _Py_ReleaseInternedStrings(void);
Guido van Rossum1e6e9a21997-01-18 07:53:23 +000081
Guido van Rossum45ec02a2002-08-19 21:43:18 +000082/* Use only if you know it's a string */
83#define PyString_CHECK_INTERNED(op) (((PyStringObject *)(op))->ob_sstate)
84
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000085/* Macro, trading safety for speed */
Martin v. Löwis5b222132007-06-10 09:51:05 +000086#define PyString_AS_STRING(op) (assert(PyString_Check(op)),(((PyStringObject *)(op))->ob_sval))
Martin v. Löwis5d7428b2007-07-21 18:47:48 +000087#define PyString_GET_SIZE(op) (assert(PyString_Check(op)),Py_Size(op))
Guido van Rossuma3309961993-07-28 09:05:47 +000088
Tim Petersa7259592001-06-16 05:11:17 +000089/* _PyString_Join(sep, x) is like sep.join(x). sep must be PyStringObject*,
90 x must be an iterable object. */
Mark Hammond91a681d2002-08-12 07:21:58 +000091PyAPI_FUNC(PyObject *) _PyString_Join(PyObject *sep, PyObject *x);
Tim Petersa7259592001-06-16 05:11:17 +000092
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +000093/* --- Generic Codecs ----------------------------------------------------- */
94
Marc-André Lemburg2d920412001-05-15 12:00:02 +000095/* Create an object by decoding the encoded string s of the
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +000096 given size. */
97
Mark Hammond91a681d2002-08-12 07:21:58 +000098PyAPI_FUNC(PyObject*) PyString_Decode(
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +000099 const char *s, /* encoded string */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000100 Py_ssize_t size, /* size of buffer */
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +0000101 const char *encoding, /* encoding */
102 const char *errors /* error handling */
103 );
104
105/* Encodes a char buffer of the given size and returns a
Marc-André Lemburg2d920412001-05-15 12:00:02 +0000106 Python object. */
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +0000107
Mark Hammond91a681d2002-08-12 07:21:58 +0000108PyAPI_FUNC(PyObject*) PyString_Encode(
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +0000109 const char *s, /* string char buffer */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000110 Py_ssize_t size, /* number of chars to encode */
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +0000111 const char *encoding, /* encoding */
112 const char *errors /* error handling */
113 );
114
Marc-André Lemburg2d920412001-05-15 12:00:02 +0000115/* Encodes a string object and returns the result as Python
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +0000116 object. */
117
Mark Hammond91a681d2002-08-12 07:21:58 +0000118PyAPI_FUNC(PyObject*) PyString_AsEncodedObject(
Marc-André Lemburg2d920412001-05-15 12:00:02 +0000119 PyObject *str, /* string object */
120 const char *encoding, /* encoding */
121 const char *errors /* error handling */
122 );
123
124/* Encodes a string object and returns the result as Python string
125 object.
126
127 If the codec returns an Unicode object, the object is converted
128 back to a string using the default encoding.
129
130 DEPRECATED - use PyString_AsEncodedObject() instead. */
131
Mark Hammond91a681d2002-08-12 07:21:58 +0000132PyAPI_FUNC(PyObject*) PyString_AsEncodedString(
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +0000133 PyObject *str, /* string object */
134 const char *encoding, /* encoding */
135 const char *errors /* error handling */
136 );
137
Marc-André Lemburg2d920412001-05-15 12:00:02 +0000138/* Decodes a string object and returns the result as Python
139 object. */
140
Mark Hammond91a681d2002-08-12 07:21:58 +0000141PyAPI_FUNC(PyObject*) PyString_AsDecodedObject(
Marc-André Lemburg2d920412001-05-15 12:00:02 +0000142 PyObject *str, /* string object */
143 const char *encoding, /* encoding */
144 const char *errors /* error handling */
145 );
146
147/* Decodes a string object and returns the result as Python string
148 object.
149
150 If the codec returns an Unicode object, the object is converted
151 back to a string using the default encoding.
152
153 DEPRECATED - use PyString_AsDecodedObject() instead. */
154
Mark Hammond91a681d2002-08-12 07:21:58 +0000155PyAPI_FUNC(PyObject*) PyString_AsDecodedString(
Marc-André Lemburg2d920412001-05-15 12:00:02 +0000156 PyObject *str, /* string object */
157 const char *encoding, /* encoding */
158 const char *errors /* error handling */
159 );
160
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000161/* Provides access to the internal data buffer and size of a string
162 object or the default encoded version of an Unicode object. Passing
163 NULL as *len parameter will force the string buffer to be
164 0-terminated (passing a string with embedded NULL characters will
165 cause an exception). */
166
Mark Hammond91a681d2002-08-12 07:21:58 +0000167PyAPI_FUNC(int) PyString_AsStringAndSize(
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000168 register PyObject *obj, /* string or Unicode object */
169 register char **s, /* pointer to buffer variable */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000170 register Py_ssize_t *len /* pointer to length variable or NULL
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000171 (only possible for 0-terminated
172 strings) */
173 );
174
175
Guido van Rossuma3309961993-07-28 09:05:47 +0000176#ifdef __cplusplus
177}
178#endif
179#endif /* !Py_STRINGOBJECT_H */