blob: a67e33e3a8363af71eefbf70c2c62c716d639b1a [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
Guido van Rossum1e6e9a21997-01-18 07:53:23 +000027/* Two speedup hacks. Caching the hash saves recalculation of a
28 string's hash value. Interning strings (which requires hash
29 caching) tries to ensure that only one string object with a given
30 value exists, so equality tests are one pointer comparison.
31 Together, these can speed the interpreter up by as much as 20%.
32 Each costs the size of a long or pointer per string object. In
33 addition, interned strings live until the end of times. If you are
34 concerned about memory footprint, simply comment the #define out
35 here (and rebuild everything!). */
Guido van Rossumfdebf251996-07-30 16:42:03 +000036#define CACHE_HASH
Guido van Rossum1e6e9a21997-01-18 07:53:23 +000037#ifdef CACHE_HASH
38#define INTERN_STRINGS
39#endif
Guido van Rossumfdebf251996-07-30 16:42:03 +000040
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041typedef struct {
Fred Drake3cf4d2b2000-07-09 00:55:06 +000042 PyObject_VAR_HEAD
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000043#ifdef CACHE_HASH
Fred Drake3cf4d2b2000-07-09 00:55:06 +000044 long ob_shash;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000045#endif
Guido van Rossum1e6e9a21997-01-18 07:53:23 +000046#ifdef INTERN_STRINGS
Fred Drake3cf4d2b2000-07-09 00:55:06 +000047 PyObject *ob_sinterned;
Guido van Rossum1e6e9a21997-01-18 07:53:23 +000048#endif
Fred Drake3cf4d2b2000-07-09 00:55:06 +000049 char ob_sval[1];
Guido van Rossumcaa63801995-01-12 11:45:45 +000050} PyStringObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051
Guido van Rossum051ab121995-02-27 10:17:52 +000052extern DL_IMPORT(PyTypeObject) PyString_Type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053
Guido van Rossum5eef77a2001-08-30 03:08:07 +000054#define PyString_Check(op) PyObject_TypeCheck(op, &PyString_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055
Fred Drake3cf4d2b2000-07-09 00:55:06 +000056extern DL_IMPORT(PyObject *) PyString_FromStringAndSize(const char *, int);
57extern DL_IMPORT(PyObject *) PyString_FromString(const char *);
Barry Warsawdadace02001-08-24 18:32:06 +000058extern DL_IMPORT(PyObject *) PyString_FromFormatV(const char*, va_list);
59extern DL_IMPORT(PyObject *) PyString_FromFormat(const char*, ...);
Fred Drake3cf4d2b2000-07-09 00:55:06 +000060extern DL_IMPORT(int) PyString_Size(PyObject *);
61extern DL_IMPORT(char *) PyString_AsString(PyObject *);
62extern DL_IMPORT(void) PyString_Concat(PyObject **, PyObject *);
63extern DL_IMPORT(void) PyString_ConcatAndDel(PyObject **, PyObject *);
64extern DL_IMPORT(int) _PyString_Resize(PyObject **, int);
Martin v. Löwiscd353062001-05-24 16:56:35 +000065extern DL_IMPORT(int) _PyString_Eq(PyObject *, PyObject*);
Fred Drake3cf4d2b2000-07-09 00:55:06 +000066extern DL_IMPORT(PyObject *) PyString_Format(PyObject *, PyObject *);
Tim Peters38fd5b62000-09-21 05:43:11 +000067extern DL_IMPORT(PyObject *) _PyString_FormatLong(PyObject*, int, int,
68 int, char**, int*);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000069
Guido van Rossum1e6e9a21997-01-18 07:53:23 +000070#ifdef INTERN_STRINGS
Fred Drake3cf4d2b2000-07-09 00:55:06 +000071extern DL_IMPORT(void) PyString_InternInPlace(PyObject **);
72extern DL_IMPORT(PyObject *) PyString_InternFromString(const char *);
Barry Warsawa903ad982001-02-23 16:40:48 +000073extern DL_IMPORT(void) _Py_ReleaseInternedStrings(void);
Guido van Rossum1e6e9a21997-01-18 07:53:23 +000074#else
75#define PyString_InternInPlace(p)
76#define PyString_InternFromString(cp) PyString_FromString(cp)
Barry Warsawa903ad982001-02-23 16:40:48 +000077#define _Py_ReleaseInternedStrings()
Guido van Rossum1e6e9a21997-01-18 07:53:23 +000078#endif
79
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000080/* Macro, trading safety for speed */
Barry Warsawaccfb841997-01-06 22:42:50 +000081#define PyString_AS_STRING(op) (((PyStringObject *)(op))->ob_sval)
82#define PyString_GET_SIZE(op) (((PyStringObject *)(op))->ob_size)
Guido van Rossuma3309961993-07-28 09:05:47 +000083
Tim Petersa7259592001-06-16 05:11:17 +000084/* _PyString_Join(sep, x) is like sep.join(x). sep must be PyStringObject*,
85 x must be an iterable object. */
86extern DL_IMPORT(PyObject *) _PyString_Join(PyObject *sep, PyObject *x);
87
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +000088/* --- Generic Codecs ----------------------------------------------------- */
89
Marc-André Lemburg2d920412001-05-15 12:00:02 +000090/* Create an object by decoding the encoded string s of the
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +000091 given size. */
92
93extern DL_IMPORT(PyObject*) PyString_Decode(
94 const char *s, /* encoded string */
95 int size, /* size of buffer */
96 const char *encoding, /* encoding */
97 const char *errors /* error handling */
98 );
99
100/* Encodes a char buffer of the given size and returns a
Marc-André Lemburg2d920412001-05-15 12:00:02 +0000101 Python object. */
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +0000102
103extern DL_IMPORT(PyObject*) PyString_Encode(
104 const char *s, /* string char buffer */
105 int size, /* number of chars to encode */
106 const char *encoding, /* encoding */
107 const char *errors /* error handling */
108 );
109
Marc-André Lemburg2d920412001-05-15 12:00:02 +0000110/* Encodes a string object and returns the result as Python
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +0000111 object. */
112
Marc-André Lemburg2d920412001-05-15 12:00:02 +0000113extern DL_IMPORT(PyObject*) PyString_AsEncodedObject(
114 PyObject *str, /* string object */
115 const char *encoding, /* encoding */
116 const char *errors /* error handling */
117 );
118
119/* Encodes a string object and returns the result as Python string
120 object.
121
122 If the codec returns an Unicode object, the object is converted
123 back to a string using the default encoding.
124
125 DEPRECATED - use PyString_AsEncodedObject() instead. */
126
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +0000127extern DL_IMPORT(PyObject*) PyString_AsEncodedString(
128 PyObject *str, /* string object */
129 const char *encoding, /* encoding */
130 const char *errors /* error handling */
131 );
132
Marc-André Lemburg2d920412001-05-15 12:00:02 +0000133/* Decodes a string object and returns the result as Python
134 object. */
135
136extern DL_IMPORT(PyObject*) PyString_AsDecodedObject(
137 PyObject *str, /* string object */
138 const char *encoding, /* encoding */
139 const char *errors /* error handling */
140 );
141
142/* Decodes a string object and returns the result as Python string
143 object.
144
145 If the codec returns an Unicode object, the object is converted
146 back to a string using the default encoding.
147
148 DEPRECATED - use PyString_AsDecodedObject() instead. */
149
150extern DL_IMPORT(PyObject*) PyString_AsDecodedString(
151 PyObject *str, /* string object */
152 const char *encoding, /* encoding */
153 const char *errors /* error handling */
154 );
155
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000156/* Provides access to the internal data buffer and size of a string
157 object or the default encoded version of an Unicode object. Passing
158 NULL as *len parameter will force the string buffer to be
159 0-terminated (passing a string with embedded NULL characters will
160 cause an exception). */
161
162extern DL_IMPORT(int) PyString_AsStringAndSize(
163 register PyObject *obj, /* string or Unicode object */
164 register char **s, /* pointer to buffer variable */
165 register int *len /* pointer to length variable or NULL
166 (only possible for 0-terminated
167 strings) */
168 );
169
170
Guido van Rossuma3309961993-07-28 09:05:47 +0000171#ifdef __cplusplus
172}
173#endif
174#endif /* !Py_STRINGOBJECT_H */