blob: 96f371ee7edf27831717edbb22e666fe69a09059 [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
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000010/*
Guido van Rossumcaa63801995-01-12 11:45:45 +000011Type PyStringObject represents a character string. An extra zero byte is
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000012reserved at the end to ensure it is zero-terminated, but a size is
13present so strings with null bytes in them can be represented. This
14is an immutable object type.
15
16There are functions to create new string objects, to test
17an object for string-ness, and to get the
18string value. The latter function returns a null pointer
19if the object is not of the proper type.
20There is a variant that takes an explicit size as well as a
21variant that assumes a zero-terminated string. Note that none of the
22functions should be applied to nil objects.
23*/
24
Guido van Rossum1e6e9a21997-01-18 07:53:23 +000025/* Two speedup hacks. Caching the hash saves recalculation of a
26 string's hash value. Interning strings (which requires hash
27 caching) tries to ensure that only one string object with a given
28 value exists, so equality tests are one pointer comparison.
29 Together, these can speed the interpreter up by as much as 20%.
30 Each costs the size of a long or pointer per string object. In
31 addition, interned strings live until the end of times. If you are
32 concerned about memory footprint, simply comment the #define out
33 here (and rebuild everything!). */
Guido van Rossumfdebf251996-07-30 16:42:03 +000034#define CACHE_HASH
Guido van Rossum1e6e9a21997-01-18 07:53:23 +000035#ifdef CACHE_HASH
36#define INTERN_STRINGS
37#endif
Guido van Rossumfdebf251996-07-30 16:42:03 +000038
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039typedef struct {
Fred Drake3cf4d2b2000-07-09 00:55:06 +000040 PyObject_VAR_HEAD
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000041#ifdef CACHE_HASH
Fred Drake3cf4d2b2000-07-09 00:55:06 +000042 long ob_shash;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +000043#endif
Guido van Rossum1e6e9a21997-01-18 07:53:23 +000044#ifdef INTERN_STRINGS
Fred Drake3cf4d2b2000-07-09 00:55:06 +000045 PyObject *ob_sinterned;
Guido van Rossum1e6e9a21997-01-18 07:53:23 +000046#endif
Fred Drake3cf4d2b2000-07-09 00:55:06 +000047 char ob_sval[1];
Guido van Rossumcaa63801995-01-12 11:45:45 +000048} PyStringObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000049
Guido van Rossum051ab121995-02-27 10:17:52 +000050extern DL_IMPORT(PyTypeObject) PyString_Type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000051
Guido van Rossumcaa63801995-01-12 11:45:45 +000052#define PyString_Check(op) ((op)->ob_type == &PyString_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053
Fred Drake3cf4d2b2000-07-09 00:55:06 +000054extern DL_IMPORT(PyObject *) PyString_FromStringAndSize(const char *, int);
55extern DL_IMPORT(PyObject *) PyString_FromString(const char *);
56extern DL_IMPORT(int) PyString_Size(PyObject *);
57extern DL_IMPORT(char *) PyString_AsString(PyObject *);
58extern DL_IMPORT(void) PyString_Concat(PyObject **, PyObject *);
59extern DL_IMPORT(void) PyString_ConcatAndDel(PyObject **, PyObject *);
60extern DL_IMPORT(int) _PyString_Resize(PyObject **, int);
Martin v. Löwiscd353062001-05-24 16:56:35 +000061extern DL_IMPORT(int) _PyString_Eq(PyObject *, PyObject*);
Fred Drake3cf4d2b2000-07-09 00:55:06 +000062extern DL_IMPORT(PyObject *) PyString_Format(PyObject *, PyObject *);
Tim Peters38fd5b62000-09-21 05:43:11 +000063extern DL_IMPORT(PyObject *) _PyString_FormatLong(PyObject*, int, int,
64 int, char**, int*);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000065
Guido van Rossum1e6e9a21997-01-18 07:53:23 +000066#ifdef INTERN_STRINGS
Fred Drake3cf4d2b2000-07-09 00:55:06 +000067extern DL_IMPORT(void) PyString_InternInPlace(PyObject **);
68extern DL_IMPORT(PyObject *) PyString_InternFromString(const char *);
Barry Warsawa903ad982001-02-23 16:40:48 +000069extern DL_IMPORT(void) _Py_ReleaseInternedStrings(void);
Guido van Rossum1e6e9a21997-01-18 07:53:23 +000070#else
71#define PyString_InternInPlace(p)
72#define PyString_InternFromString(cp) PyString_FromString(cp)
Barry Warsawa903ad982001-02-23 16:40:48 +000073#define _Py_ReleaseInternedStrings()
Guido van Rossum1e6e9a21997-01-18 07:53:23 +000074#endif
75
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076/* Macro, trading safety for speed */
Barry Warsawaccfb841997-01-06 22:42:50 +000077#define PyString_AS_STRING(op) (((PyStringObject *)(op))->ob_sval)
78#define PyString_GET_SIZE(op) (((PyStringObject *)(op))->ob_size)
Guido van Rossuma3309961993-07-28 09:05:47 +000079
Tim Petersa7259592001-06-16 05:11:17 +000080/* _PyString_Join(sep, x) is like sep.join(x). sep must be PyStringObject*,
81 x must be an iterable object. */
82extern DL_IMPORT(PyObject *) _PyString_Join(PyObject *sep, PyObject *x);
83
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +000084/* --- Generic Codecs ----------------------------------------------------- */
85
Marc-André Lemburg2d920412001-05-15 12:00:02 +000086/* Create an object by decoding the encoded string s of the
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +000087 given size. */
88
89extern DL_IMPORT(PyObject*) PyString_Decode(
90 const char *s, /* encoded string */
91 int size, /* size of buffer */
92 const char *encoding, /* encoding */
93 const char *errors /* error handling */
94 );
95
96/* Encodes a char buffer of the given size and returns a
Marc-André Lemburg2d920412001-05-15 12:00:02 +000097 Python object. */
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +000098
99extern DL_IMPORT(PyObject*) PyString_Encode(
100 const char *s, /* string char buffer */
101 int size, /* number of chars to encode */
102 const char *encoding, /* encoding */
103 const char *errors /* error handling */
104 );
105
Marc-André Lemburg2d920412001-05-15 12:00:02 +0000106/* Encodes a string object and returns the result as Python
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +0000107 object. */
108
Marc-André Lemburg2d920412001-05-15 12:00:02 +0000109extern DL_IMPORT(PyObject*) PyString_AsEncodedObject(
110 PyObject *str, /* string object */
111 const char *encoding, /* encoding */
112 const char *errors /* error handling */
113 );
114
115/* Encodes a string object and returns the result as Python string
116 object.
117
118 If the codec returns an Unicode object, the object is converted
119 back to a string using the default encoding.
120
121 DEPRECATED - use PyString_AsEncodedObject() instead. */
122
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +0000123extern DL_IMPORT(PyObject*) PyString_AsEncodedString(
124 PyObject *str, /* string object */
125 const char *encoding, /* encoding */
126 const char *errors /* error handling */
127 );
128
Marc-André Lemburg2d920412001-05-15 12:00:02 +0000129/* Decodes a string object and returns the result as Python
130 object. */
131
132extern DL_IMPORT(PyObject*) PyString_AsDecodedObject(
133 PyObject *str, /* string object */
134 const char *encoding, /* encoding */
135 const char *errors /* error handling */
136 );
137
138/* Decodes a string object and returns the result as Python string
139 object.
140
141 If the codec returns an Unicode object, the object is converted
142 back to a string using the default encoding.
143
144 DEPRECATED - use PyString_AsDecodedObject() instead. */
145
146extern DL_IMPORT(PyObject*) PyString_AsDecodedString(
147 PyObject *str, /* string object */
148 const char *encoding, /* encoding */
149 const char *errors /* error handling */
150 );
151
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000152/* Provides access to the internal data buffer and size of a string
153 object or the default encoded version of an Unicode object. Passing
154 NULL as *len parameter will force the string buffer to be
155 0-terminated (passing a string with embedded NULL characters will
156 cause an exception). */
157
158extern DL_IMPORT(int) PyString_AsStringAndSize(
159 register PyObject *obj, /* string or Unicode object */
160 register char **s, /* pointer to buffer variable */
161 register int *len /* pointer to length variable or NULL
162 (only possible for 0-terminated
163 strings) */
164 );
165
166
Guido van Rossuma3309961993-07-28 09:05:47 +0000167#ifdef __cplusplus
168}
169#endif
170#endif /* !Py_STRINGOBJECT_H */