blob: c0a1fca29738b4034ec3aa1ac5a58580a3304ea9 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Gregory P. Smithe0b92612008-06-11 03:40:10 +00002/* String (str/bytes) object interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00003
Gregory P. Smithe0b92612008-06-11 03:40:10 +00004#ifndef Py_STRINGOBJECT_H
5#define Py_STRINGOBJECT_H
6#ifdef __cplusplus
7extern "C" {
8#endif
9
10#include <stdarg.h>
11
12/*
13Type PyStringObject represents a character string. An extra zero byte is
14reserved 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
27/* Caching the hash (ob_shash) saves recalculation of a string's hash value.
28 Interning strings (ob_sstate) tries to ensure that only one string
29 object with a given value exists, so equality tests can be one pointer
30 comparison. This is generally restricted to strings that "look like"
31 Python identifiers, although the intern() builtin can be used to force
32 interning of any string.
33 Together, these sped the interpreter by up to 20%. */
34
35typedef struct {
36 PyObject_VAR_HEAD
37 long ob_shash;
38 int ob_sstate;
39 char ob_sval[1];
40
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 */
49} PyStringObject;
50
51#define SSTATE_NOT_INTERNED 0
52#define SSTATE_INTERNED_MORTAL 1
53#define SSTATE_INTERNED_IMMORTAL 2
54
55PyAPI_DATA(PyTypeObject) PyBaseString_Type;
56PyAPI_DATA(PyTypeObject) PyString_Type;
57
58#define PyString_Check(op) \
59 PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_STRING_SUBCLASS)
60#define PyString_CheckExact(op) (Py_TYPE(op) == &PyString_Type)
61
62PyAPI_FUNC(PyObject *) PyString_FromStringAndSize(const char *, Py_ssize_t);
63PyAPI_FUNC(PyObject *) PyString_FromString(const char *);
64PyAPI_FUNC(PyObject *) PyString_FromFormatV(const char*, va_list)
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000065 Py_GCC_ATTRIBUTE((format(printf, 1, 0)));
Gregory P. Smithe0b92612008-06-11 03:40:10 +000066PyAPI_FUNC(PyObject *) PyString_FromFormat(const char*, ...)
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000067 Py_GCC_ATTRIBUTE((format(printf, 1, 2)));
Gregory P. Smithe0b92612008-06-11 03:40:10 +000068PyAPI_FUNC(Py_ssize_t) PyString_Size(PyObject *);
69PyAPI_FUNC(char *) PyString_AsString(PyObject *);
70PyAPI_FUNC(PyObject *) PyString_Repr(PyObject *, int);
71PyAPI_FUNC(void) PyString_Concat(PyObject **, PyObject *);
72PyAPI_FUNC(void) PyString_ConcatAndDel(PyObject **, PyObject *);
73PyAPI_FUNC(int) _PyString_Resize(PyObject **, Py_ssize_t);
74PyAPI_FUNC(int) _PyString_Eq(PyObject *, PyObject*);
75PyAPI_FUNC(PyObject *) PyString_Format(PyObject *, PyObject *);
76PyAPI_FUNC(PyObject *) _PyString_FormatLong(PyObject*, int, int,
Antoine Pitrouc7c96a92010-05-09 15:15:40 +000077 int, char**, int*);
78PyAPI_FUNC(PyObject *) PyString_DecodeEscape(const char *, Py_ssize_t,
79 const char *, Py_ssize_t,
80 const char *);
Gregory P. Smithe0b92612008-06-11 03:40:10 +000081
82PyAPI_FUNC(void) PyString_InternInPlace(PyObject **);
83PyAPI_FUNC(void) PyString_InternImmortal(PyObject **);
84PyAPI_FUNC(PyObject *) PyString_InternFromString(const char *);
85PyAPI_FUNC(void) _Py_ReleaseInternedStrings(void);
86
87/* Use only if you know it's a string */
88#define PyString_CHECK_INTERNED(op) (((PyStringObject *)(op))->ob_sstate)
89
90/* Macro, trading safety for speed */
91#define PyString_AS_STRING(op) (((PyStringObject *)(op))->ob_sval)
92#define PyString_GET_SIZE(op) Py_SIZE(op)
93
94/* _PyString_Join(sep, x) is like sep.join(x). sep must be PyStringObject*,
95 x must be an iterable object. */
96PyAPI_FUNC(PyObject *) _PyString_Join(PyObject *sep, PyObject *x);
97
98/* --- Generic Codecs ----------------------------------------------------- */
99
100/* Create an object by decoding the encoded string s of the
101 given size. */
102
103PyAPI_FUNC(PyObject*) PyString_Decode(
104 const char *s, /* encoded string */
105 Py_ssize_t size, /* size of buffer */
106 const char *encoding, /* encoding */
107 const char *errors /* error handling */
108 );
109
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000110/* Encodes a char buffer of the given size and returns a
Gregory P. Smithe0b92612008-06-11 03:40:10 +0000111 Python object. */
112
113PyAPI_FUNC(PyObject*) PyString_Encode(
114 const char *s, /* string char buffer */
115 Py_ssize_t size, /* number of chars to encode */
116 const char *encoding, /* encoding */
117 const char *errors /* error handling */
118 );
119
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000120/* Encodes a string object and returns the result as Python
Gregory P. Smithe0b92612008-06-11 03:40:10 +0000121 object. */
122
123PyAPI_FUNC(PyObject*) PyString_AsEncodedObject(
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000124 PyObject *str, /* string object */
125 const char *encoding, /* encoding */
126 const char *errors /* error handling */
Gregory P. Smithe0b92612008-06-11 03:40:10 +0000127 );
128
129/* Encodes a string object and returns the result as Python string
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000130 object.
131
Gregory P. Smithe0b92612008-06-11 03:40:10 +0000132 If the codec returns an Unicode object, the object is converted
133 back to a string using the default encoding.
134
135 DEPRECATED - use PyString_AsEncodedObject() instead. */
136
137PyAPI_FUNC(PyObject*) PyString_AsEncodedString(
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000138 PyObject *str, /* string object */
139 const char *encoding, /* encoding */
140 const char *errors /* error handling */
Gregory P. Smithe0b92612008-06-11 03:40:10 +0000141 );
142
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000143/* Decodes a string object and returns the result as Python
Gregory P. Smithe0b92612008-06-11 03:40:10 +0000144 object. */
145
146PyAPI_FUNC(PyObject*) PyString_AsDecodedObject(
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000147 PyObject *str, /* string object */
148 const char *encoding, /* encoding */
149 const char *errors /* error handling */
Gregory P. Smithe0b92612008-06-11 03:40:10 +0000150 );
151
152/* Decodes a string object and returns the result as Python string
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000153 object.
154
Gregory P. Smithe0b92612008-06-11 03:40:10 +0000155 If the codec returns an Unicode object, the object is converted
156 back to a string using the default encoding.
157
158 DEPRECATED - use PyString_AsDecodedObject() instead. */
159
160PyAPI_FUNC(PyObject*) PyString_AsDecodedString(
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000161 PyObject *str, /* string object */
162 const char *encoding, /* encoding */
163 const char *errors /* error handling */
Gregory P. Smithe0b92612008-06-11 03:40:10 +0000164 );
165
166/* Provides access to the internal data buffer and size of a string
167 object or the default encoded version of an Unicode object. Passing
168 NULL as *len parameter will force the string buffer to be
169 0-terminated (passing a string with embedded NULL characters will
170 cause an exception). */
171
172PyAPI_FUNC(int) PyString_AsStringAndSize(
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000173 register PyObject *obj, /* string or Unicode object */
174 register char **s, /* pointer to buffer variable */
175 register Py_ssize_t *len /* pointer to length variable or NULL
176 (only possible for 0-terminated
177 strings) */
Gregory P. Smithe0b92612008-06-11 03:40:10 +0000178 );
179
180/* Using the current locale, insert the thousands grouping
181 into the string pointed to by buffer. For the argument descriptions,
182 see Objects/stringlib/localeutil.h */
183
184PyAPI_FUNC(int) _PyString_InsertThousandsGrouping(char *buffer,
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000185 Py_ssize_t n_buffer,
186 Py_ssize_t n_digits,
187 Py_ssize_t buf_size,
188 Py_ssize_t *count,
189 int append_zero_char);
Gregory P. Smithe0b92612008-06-11 03:40:10 +0000190
191/* Format the object based on the format_spec, as defined in PEP 3101
192 (Advanced String Formatting). */
193PyAPI_FUNC(PyObject *) _PyBytes_FormatAdvanced(PyObject *obj,
Antoine Pitrouc7c96a92010-05-09 15:15:40 +0000194 char *format_spec,
195 Py_ssize_t format_spec_len);
Gregory P. Smithe0b92612008-06-11 03:40:10 +0000196
197#ifdef __cplusplus
198}
199#endif
200#endif /* !Py_STRINGOBJECT_H */