blob: 2c4c4c4fd47fe223105fd41e49d0a809cddc1f78 [file] [log] [blame]
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00001
Georg Brandl0c77a822008-06-10 16:37:50 +00002/* Bytes (String) object interface */
Christian Heimes2c9c7a52008-05-26 13:42:13 +00003
Georg Brandl0c77a822008-06-10 16:37:50 +00004#ifndef Py_BYTESOBJECT_H
5#define Py_BYTESOBJECT_H
Guido van Rossum4dfe8a12006-04-22 23:28:04 +00006#ifdef __cplusplus
7extern "C" {
8#endif
9
10#include <stdarg.h>
11
Christian Heimes2c9c7a52008-05-26 13:42:13 +000012/*
13Type PyBytesObject 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.
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000017
Christian Heimes2c9c7a52008-05-26 13:42:13 +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 This significantly speeds up dict lookups. */
29
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000030#ifndef Py_LIMITED_API
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000031typedef struct {
32 PyObject_VAR_HEAD
Benjamin Peterson8f67d082010-10-17 20:54:53 +000033 Py_hash_t ob_shash;
Christian Heimes2c9c7a52008-05-26 13:42:13 +000034 char ob_sval[1];
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000035
Christian Heimes2c9c7a52008-05-26 13:42:13 +000036 /* Invariants:
37 * ob_sval contains space for 'ob_size+1' elements.
38 * ob_sval[ob_size] == 0.
39 * ob_shash is the hash of the string or -1 if not computed yet.
40 */
41} PyBytesObject;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000042#endif
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000043
Christian Heimes2c9c7a52008-05-26 13:42:13 +000044PyAPI_DATA(PyTypeObject) PyBytes_Type;
45PyAPI_DATA(PyTypeObject) PyBytesIter_Type;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000046
Christian Heimes2c9c7a52008-05-26 13:42:13 +000047#define PyBytes_Check(op) \
48 PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_BYTES_SUBCLASS)
49#define PyBytes_CheckExact(op) (Py_TYPE(op) == &PyBytes_Type)
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000050
Christian Heimes2c9c7a52008-05-26 13:42:13 +000051PyAPI_FUNC(PyObject *) PyBytes_FromStringAndSize(const char *, Py_ssize_t);
52PyAPI_FUNC(PyObject *) PyBytes_FromString(const char *);
Benjamin Petersonc15a0732008-08-26 16:46:47 +000053PyAPI_FUNC(PyObject *) PyBytes_FromObject(PyObject *);
Christian Heimes2c9c7a52008-05-26 13:42:13 +000054PyAPI_FUNC(PyObject *) PyBytes_FromFormatV(const char*, va_list)
55 Py_GCC_ATTRIBUTE((format(printf, 1, 0)));
56PyAPI_FUNC(PyObject *) PyBytes_FromFormat(const char*, ...)
57 Py_GCC_ATTRIBUTE((format(printf, 1, 2)));
58PyAPI_FUNC(Py_ssize_t) PyBytes_Size(PyObject *);
59PyAPI_FUNC(char *) PyBytes_AsString(PyObject *);
60PyAPI_FUNC(PyObject *) PyBytes_Repr(PyObject *, int);
61PyAPI_FUNC(void) PyBytes_Concat(PyObject **, PyObject *);
62PyAPI_FUNC(void) PyBytes_ConcatAndDel(PyObject **, PyObject *);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000063#ifndef Py_LIMITED_API
Christian Heimes2c9c7a52008-05-26 13:42:13 +000064PyAPI_FUNC(int) _PyBytes_Resize(PyObject **, Py_ssize_t);
Ethan Furmanb95b5612015-01-23 20:05:18 -080065PyAPI_FUNC(PyObject *) _PyBytes_Format(PyObject *, PyObject *);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000066#endif
Christian Heimes2c9c7a52008-05-26 13:42:13 +000067PyAPI_FUNC(PyObject *) PyBytes_DecodeEscape(const char *, Py_ssize_t,
68 const char *, Py_ssize_t,
69 const char *);
70
71/* Macro, trading safety for speed */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000072#ifndef Py_LIMITED_API
Christian Heimes2c9c7a52008-05-26 13:42:13 +000073#define PyBytes_AS_STRING(op) (assert(PyBytes_Check(op)), \
74 (((PyBytesObject *)(op))->ob_sval))
75#define PyBytes_GET_SIZE(op) (assert(PyBytes_Check(op)),Py_SIZE(op))
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000076#endif
Christian Heimes2c9c7a52008-05-26 13:42:13 +000077
78/* _PyBytes_Join(sep, x) is like sep.join(x). sep must be PyBytesObject*,
79 x must be an iterable object. */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000080#ifndef Py_LIMITED_API
Christian Heimes2c9c7a52008-05-26 13:42:13 +000081PyAPI_FUNC(PyObject *) _PyBytes_Join(PyObject *sep, PyObject *x);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000082#endif
Christian Heimes2c9c7a52008-05-26 13:42:13 +000083
84/* Provides access to the internal data buffer and size of a string
85 object or the default encoded version of an Unicode object. Passing
86 NULL as *len parameter will force the string buffer to be
87 0-terminated (passing a string with embedded NULL characters will
88 cause an exception). */
89PyAPI_FUNC(int) PyBytes_AsStringAndSize(
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020090 PyObject *obj, /* string or Unicode object */
91 char **s, /* pointer to buffer variable */
92 Py_ssize_t *len /* pointer to length variable or NULL
93 (only possible for 0-terminated
94 strings) */
Christian Heimes2c9c7a52008-05-26 13:42:13 +000095 );
96
97/* Using the current locale, insert the thousands grouping
98 into the string pointed to by buffer. For the argument descriptions,
99 see Objects/stringlib/localeutil.h */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000100#ifndef Py_LIMITED_API
Eric Smith0923d1d2009-04-16 20:16:10 +0000101PyAPI_FUNC(Py_ssize_t) _PyBytes_InsertThousandsGroupingLocale(char *buffer,
102 Py_ssize_t n_buffer,
103 char *digits,
104 Py_ssize_t n_digits,
105 Py_ssize_t min_width);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000106
Eric Smitha3b1ac82009-04-03 14:45:06 +0000107/* Using explicit passed-in values, insert the thousands grouping
108 into the string pointed to by buffer. For the argument descriptions,
109 see Objects/stringlib/localeutil.h */
Eric Smith0923d1d2009-04-16 20:16:10 +0000110PyAPI_FUNC(Py_ssize_t) _PyBytes_InsertThousandsGrouping(char *buffer,
111 Py_ssize_t n_buffer,
112 char *digits,
113 Py_ssize_t n_digits,
114 Py_ssize_t min_width,
115 const char *grouping,
116 const char *thousands_sep);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000117#endif
Eric Smitha3b1ac82009-04-03 14:45:06 +0000118
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000119/* Flags used by string formatting */
120#define F_LJUST (1<<0)
121#define F_SIGN (1<<1)
122#define F_BLANK (1<<2)
123#define F_ALT (1<<3)
124#define F_ZERO (1<<4)
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000125
Victor Stinner00165072015-10-09 01:53:21 +0200126#ifndef Py_LIMITED_API
127/* The _PyBytesWriter structure is big: it contains an embeded "stack buffer".
128 A _PyBytesWriter variable must be declared at the end of variables in a
129 function to optimize the memory allocation on the stack. */
130typedef struct {
131 /* bytes object */
132 PyObject *buffer;
133
134 /* Number of allocated size */
135 Py_ssize_t allocated;
136
Victor Stinner53926a12015-10-09 12:37:03 +0200137 /* Minimum number of allocated bytes,
138 incremented by _PyBytesWriter_Prepare() */
139 Py_ssize_t min_size;
Victor Stinner00165072015-10-09 01:53:21 +0200140
141 /* If non-zero, overallocate the buffer (default: 0). */
142 int overallocate;
143
144 /* Stack buffer */
Victor Stinnerb3653a32015-10-09 03:38:24 +0200145 int use_small_buffer;
146 char small_buffer[512];
Victor Stinner00165072015-10-09 01:53:21 +0200147} _PyBytesWriter;
148
149/* Initialize a bytes writer
150
151 By default, the overallocation is disabled. Set the overallocate attribute
152 to control the allocation of the buffer. */
153PyAPI_FUNC(void) _PyBytesWriter_Init(_PyBytesWriter *writer);
154
155/* Get the buffer content and reset the writer.
156 Return a bytes object.
157 Raise an exception and return NULL on error. */
158PyAPI_FUNC(PyObject *) _PyBytesWriter_Finish(_PyBytesWriter *writer,
159 char *str);
160
161/* Deallocate memory of a writer (clear its internal buffer). */
162PyAPI_FUNC(void) _PyBytesWriter_Dealloc(_PyBytesWriter *writer);
163
164/* Allocate the buffer to write size bytes.
165 Return the pointer to the beginning of buffer data.
166 Raise an exception and return NULL on error. */
167PyAPI_FUNC(char*) _PyBytesWriter_Alloc(_PyBytesWriter *writer,
168 Py_ssize_t size);
169
170/* Add *size* bytes to the buffer.
171 str is the current pointer inside the buffer.
172 Return the updated current pointer inside the buffer.
173 Raise an exception and return NULL on error. */
174PyAPI_FUNC(char*) _PyBytesWriter_Prepare(_PyBytesWriter *writer,
175 char *str,
176 Py_ssize_t size);
Victor Stinnerce179bf2015-10-09 12:57:22 +0200177
178/* Write bytes.
179 Raise an exception and return NULL on error. */
180PyAPI_FUNC(char*) _PyBytesWriter_WriteBytes(_PyBytesWriter *writer,
181 char *str,
182 char *bytes,
183 Py_ssize_t size);
Victor Stinner00165072015-10-09 01:53:21 +0200184#endif /* Py_LIMITED_API */
185
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000186#ifdef __cplusplus
187}
188#endif
Georg Brandl0c77a822008-06-10 16:37:50 +0000189#endif /* !Py_BYTESOBJECT_H */