blob: acb943b5f92661777ad21d05cd44de1a689f64af [file] [log] [blame]
Christian Heimes1a6387e2008-03-26 12:49:49 +00001
Gregory P. Smith1bd52d72008-06-01 22:57:47 +00002/* Bytes (String) object interface */
Christian Heimes3497f942008-05-26 12:29:14 +00003
Gregory P. Smith1bd52d72008-06-01 22:57:47 +00004#ifndef Py_BYTESOBJECT_H
5#define Py_BYTESOBJECT_H
Christian Heimes1a6387e2008-03-26 12:49:49 +00006#ifdef __cplusplus
7extern "C" {
8#endif
9
10#include <stdarg.h>
11
Christian Heimes3497f942008-05-26 12:29:14 +000012/*
Christian Heimes593daf52008-05-26 12:51:38 +000013Type PyBytesObject represents a character string. An extra zero byte is
Christian Heimes3497f942008-05-26 12:29:14 +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.
Christian Heimes1a6387e2008-03-26 12:49:49 +000017
Christian Heimes3497f942008-05-26 12:29:14 +000018There 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
Christian Heimes1a6387e2008-03-26 12:49:49 +000035typedef struct {
36 PyObject_VAR_HEAD
Christian Heimes3497f942008-05-26 12:29:14 +000037 long ob_shash;
38 int ob_sstate;
39 char ob_sval[1];
Christian Heimes1a6387e2008-03-26 12:49:49 +000040
Christian Heimes3497f942008-05-26 12:29:14 +000041 /* 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 */
Christian Heimes593daf52008-05-26 12:51:38 +000049} PyBytesObject;
Christian Heimes1a6387e2008-03-26 12:49:49 +000050
Christian Heimes3497f942008-05-26 12:29:14 +000051#define SSTATE_NOT_INTERNED 0
52#define SSTATE_INTERNED_MORTAL 1
53#define SSTATE_INTERNED_IMMORTAL 2
Christian Heimes1a6387e2008-03-26 12:49:49 +000054
Christian Heimes3497f942008-05-26 12:29:14 +000055PyAPI_DATA(PyTypeObject) PyBaseString_Type;
Christian Heimes593daf52008-05-26 12:51:38 +000056PyAPI_DATA(PyTypeObject) PyBytes_Type;
Christian Heimes1a6387e2008-03-26 12:49:49 +000057
Christian Heimes593daf52008-05-26 12:51:38 +000058#define PyBytes_Check(op) \
Christian Heimes3497f942008-05-26 12:29:14 +000059 PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_STRING_SUBCLASS)
Christian Heimes593daf52008-05-26 12:51:38 +000060#define PyBytes_CheckExact(op) (Py_TYPE(op) == &PyBytes_Type)
Christian Heimes3497f942008-05-26 12:29:14 +000061
Christian Heimes593daf52008-05-26 12:51:38 +000062PyAPI_FUNC(PyObject *) PyBytes_FromStringAndSize(const char *, Py_ssize_t);
63PyAPI_FUNC(PyObject *) PyBytes_FromString(const char *);
64PyAPI_FUNC(PyObject *) PyBytes_FromFormatV(const char*, va_list)
Christian Heimes3497f942008-05-26 12:29:14 +000065 Py_GCC_ATTRIBUTE((format(printf, 1, 0)));
Christian Heimes593daf52008-05-26 12:51:38 +000066PyAPI_FUNC(PyObject *) PyBytes_FromFormat(const char*, ...)
Christian Heimes3497f942008-05-26 12:29:14 +000067 Py_GCC_ATTRIBUTE((format(printf, 1, 2)));
Christian Heimes593daf52008-05-26 12:51:38 +000068PyAPI_FUNC(Py_ssize_t) PyBytes_Size(PyObject *);
69PyAPI_FUNC(char *) PyBytes_AsString(PyObject *);
70PyAPI_FUNC(PyObject *) PyBytes_Repr(PyObject *, int);
71PyAPI_FUNC(void) PyBytes_Concat(PyObject **, PyObject *);
72PyAPI_FUNC(void) PyBytes_ConcatAndDel(PyObject **, PyObject *);
73PyAPI_FUNC(int) _PyBytes_Resize(PyObject **, Py_ssize_t);
74PyAPI_FUNC(int) _PyBytes_Eq(PyObject *, PyObject*);
75PyAPI_FUNC(PyObject *) PyBytes_Format(PyObject *, PyObject *);
76PyAPI_FUNC(PyObject *) _PyBytes_FormatLong(PyObject*, int, int,
Christian Heimes3497f942008-05-26 12:29:14 +000077 int, char**, int*);
Christian Heimes593daf52008-05-26 12:51:38 +000078PyAPI_FUNC(PyObject *) PyBytes_DecodeEscape(const char *, Py_ssize_t,
Christian Heimes3497f942008-05-26 12:29:14 +000079 const char *, Py_ssize_t,
80 const char *);
81
Christian Heimes593daf52008-05-26 12:51:38 +000082PyAPI_FUNC(void) PyBytes_InternInPlace(PyObject **);
83PyAPI_FUNC(void) PyBytes_InternImmortal(PyObject **);
84PyAPI_FUNC(PyObject *) PyBytes_InternFromString(const char *);
Christian Heimes3497f942008-05-26 12:29:14 +000085PyAPI_FUNC(void) _Py_ReleaseInternedStrings(void);
86
87/* Use only if you know it's a string */
Christian Heimes593daf52008-05-26 12:51:38 +000088#define PyBytes_CHECK_INTERNED(op) (((PyBytesObject *)(op))->ob_sstate)
Christian Heimes3497f942008-05-26 12:29:14 +000089
90/* Macro, trading safety for speed */
Christian Heimes593daf52008-05-26 12:51:38 +000091#define PyBytes_AS_STRING(op) (((PyBytesObject *)(op))->ob_sval)
92#define PyBytes_GET_SIZE(op) Py_SIZE(op)
Christian Heimes3497f942008-05-26 12:29:14 +000093
Christian Heimes593daf52008-05-26 12:51:38 +000094/* _PyBytes_Join(sep, x) is like sep.join(x). sep must be PyBytesObject*,
Christian Heimes3497f942008-05-26 12:29:14 +000095 x must be an iterable object. */
Christian Heimes593daf52008-05-26 12:51:38 +000096PyAPI_FUNC(PyObject *) _PyBytes_Join(PyObject *sep, PyObject *x);
Christian Heimes3497f942008-05-26 12:29:14 +000097
98/* --- Generic Codecs ----------------------------------------------------- */
99
100/* Create an object by decoding the encoded string s of the
101 given size. */
102
Christian Heimes593daf52008-05-26 12:51:38 +0000103PyAPI_FUNC(PyObject*) PyBytes_Decode(
Christian Heimes3497f942008-05-26 12:29:14 +0000104 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
110/* Encodes a char buffer of the given size and returns a
111 Python object. */
112
Christian Heimes593daf52008-05-26 12:51:38 +0000113PyAPI_FUNC(PyObject*) PyBytes_Encode(
Christian Heimes3497f942008-05-26 12:29:14 +0000114 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
120/* Encodes a string object and returns the result as Python
121 object. */
122
Christian Heimes593daf52008-05-26 12:51:38 +0000123PyAPI_FUNC(PyObject*) PyBytes_AsEncodedObject(
Christian Heimes3497f942008-05-26 12:29:14 +0000124 PyObject *str, /* string object */
125 const char *encoding, /* encoding */
126 const char *errors /* error handling */
127 );
128
129/* Encodes a string object and returns the result as Python string
130 object.
131
132 If the codec returns an Unicode object, the object is converted
133 back to a string using the default encoding.
134
Christian Heimes593daf52008-05-26 12:51:38 +0000135 DEPRECATED - use PyBytes_AsEncodedObject() instead. */
Christian Heimes3497f942008-05-26 12:29:14 +0000136
Christian Heimes593daf52008-05-26 12:51:38 +0000137PyAPI_FUNC(PyObject*) PyBytes_AsEncodedString(
Christian Heimes3497f942008-05-26 12:29:14 +0000138 PyObject *str, /* string object */
139 const char *encoding, /* encoding */
140 const char *errors /* error handling */
141 );
142
143/* Decodes a string object and returns the result as Python
144 object. */
145
Christian Heimes593daf52008-05-26 12:51:38 +0000146PyAPI_FUNC(PyObject*) PyBytes_AsDecodedObject(
Christian Heimes3497f942008-05-26 12:29:14 +0000147 PyObject *str, /* string object */
148 const char *encoding, /* encoding */
149 const char *errors /* error handling */
150 );
151
152/* Decodes a string object and returns the result as Python string
153 object.
154
155 If the codec returns an Unicode object, the object is converted
156 back to a string using the default encoding.
157
Christian Heimes593daf52008-05-26 12:51:38 +0000158 DEPRECATED - use PyBytes_AsDecodedObject() instead. */
Christian Heimes3497f942008-05-26 12:29:14 +0000159
Christian Heimes593daf52008-05-26 12:51:38 +0000160PyAPI_FUNC(PyObject*) PyBytes_AsDecodedString(
Christian Heimes3497f942008-05-26 12:29:14 +0000161 PyObject *str, /* string object */
162 const char *encoding, /* encoding */
163 const char *errors /* error handling */
164 );
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
Christian Heimes593daf52008-05-26 12:51:38 +0000172PyAPI_FUNC(int) PyBytes_AsStringAndSize(
Christian Heimes3497f942008-05-26 12:29:14 +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) */
178 );
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
Christian Heimes593daf52008-05-26 12:51:38 +0000184PyAPI_FUNC(int) _PyBytes_InsertThousandsGrouping(char *buffer,
Christian Heimes3497f942008-05-26 12:29:14 +0000185 Py_ssize_t len,
186 char *plast,
187 Py_ssize_t buf_size,
188 Py_ssize_t *count,
189 int append_zero_char);
Christian Heimes1a6387e2008-03-26 12:49:49 +0000190
Eric Smithdc13b792008-05-30 18:10:04 +0000191/* Format the object based on the format_spec, as defined in PEP 3101
192 (Advanced String Formatting). */
193PyAPI_FUNC(PyObject *) _PyBytes_FormatAdvanced(PyObject *obj,
194 char *format_spec,
195 Py_ssize_t format_spec_len);
196
Christian Heimes1a6387e2008-03-26 12:49:49 +0000197#ifdef __cplusplus
198}
199#endif
Gregory P. Smith1bd52d72008-06-01 22:57:47 +0000200#endif /* !Py_BYTESOBJECT_H */