blob: fc9981e56d277416da6828635d3e8f0ad8037fb2 [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)
Serhiy Storchaka598ceae2017-11-28 17:56:10 +020055 Py_GCC_ATTRIBUTE((format(printf, 1, 0)));
Christian Heimes2c9c7a52008-05-26 13:42:13 +000056PyAPI_FUNC(PyObject *) PyBytes_FromFormat(const char*, ...)
Serhiy Storchaka598ceae2017-11-28 17:56:10 +020057 Py_GCC_ATTRIBUTE((format(printf, 1, 2)));
Christian Heimes2c9c7a52008-05-26 13:42:13 +000058PyAPI_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);
Victor Stinner772b2b02015-10-14 09:56:53 +020065PyAPI_FUNC(PyObject*) _PyBytes_FormatEx(
66 const char *format,
67 Py_ssize_t format_len,
68 PyObject *args,
69 int use_bytearray);
Victor Stinner2bf89932015-10-14 11:25:33 +020070PyAPI_FUNC(PyObject*) _PyBytes_FromHex(
71 PyObject *string,
72 int use_bytearray);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000073#endif
Christian Heimes2c9c7a52008-05-26 13:42:13 +000074PyAPI_FUNC(PyObject *) PyBytes_DecodeEscape(const char *, Py_ssize_t,
Serhiy Storchaka598ceae2017-11-28 17:56:10 +020075 const char *, Py_ssize_t,
76 const char *);
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +020077#ifndef Py_LIMITED_API
Eric V. Smith56466482016-10-31 14:46:26 -040078/* Helper for PyBytes_DecodeEscape that detects invalid escape chars. */
79PyAPI_FUNC(PyObject *) _PyBytes_DecodeEscape(const char *, Py_ssize_t,
Greg Price3a4f6672019-09-12 11:12:22 -070080 const char *, const char **);
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +020081#endif
Christian Heimes2c9c7a52008-05-26 13:42:13 +000082
83/* Macro, trading safety for speed */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000084#ifndef Py_LIMITED_API
Christian Heimes2c9c7a52008-05-26 13:42:13 +000085#define PyBytes_AS_STRING(op) (assert(PyBytes_Check(op)), \
86 (((PyBytesObject *)(op))->ob_sval))
87#define PyBytes_GET_SIZE(op) (assert(PyBytes_Check(op)),Py_SIZE(op))
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000088#endif
Christian Heimes2c9c7a52008-05-26 13:42:13 +000089
90/* _PyBytes_Join(sep, x) is like sep.join(x). sep must be PyBytesObject*,
91 x must be an iterable object. */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000092#ifndef Py_LIMITED_API
Christian Heimes2c9c7a52008-05-26 13:42:13 +000093PyAPI_FUNC(PyObject *) _PyBytes_Join(PyObject *sep, PyObject *x);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000094#endif
Christian Heimes2c9c7a52008-05-26 13:42:13 +000095
96/* Provides access to the internal data buffer and size of a string
Martin Panter6245cb32016-04-15 02:14:19 +000097 object or the default encoded version of a Unicode object. Passing
Christian Heimes2c9c7a52008-05-26 13:42:13 +000098 NULL as *len parameter will force the string buffer to be
99 0-terminated (passing a string with embedded NULL characters will
100 cause an exception). */
101PyAPI_FUNC(int) PyBytes_AsStringAndSize(
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200102 PyObject *obj, /* string or Unicode object */
103 char **s, /* pointer to buffer variable */
104 Py_ssize_t *len /* pointer to length variable or NULL
105 (only possible for 0-terminated
106 strings) */
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000107 );
108
109/* Using the current locale, insert the thousands grouping
110 into the string pointed to by buffer. For the argument descriptions,
111 see Objects/stringlib/localeutil.h */
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000112#ifndef Py_LIMITED_API
Eric Smith0923d1d2009-04-16 20:16:10 +0000113PyAPI_FUNC(Py_ssize_t) _PyBytes_InsertThousandsGroupingLocale(char *buffer,
114 Py_ssize_t n_buffer,
115 char *digits,
116 Py_ssize_t n_digits,
117 Py_ssize_t min_width);
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000118
Eric Smitha3b1ac82009-04-03 14:45:06 +0000119/* Using explicit passed-in values, insert the thousands grouping
120 into the string pointed to by buffer. For the argument descriptions,
121 see Objects/stringlib/localeutil.h */
Eric Smith0923d1d2009-04-16 20:16:10 +0000122PyAPI_FUNC(Py_ssize_t) _PyBytes_InsertThousandsGrouping(char *buffer,
123 Py_ssize_t n_buffer,
124 char *digits,
125 Py_ssize_t n_digits,
126 Py_ssize_t min_width,
127 const char *grouping,
128 const char *thousands_sep);
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000129#endif
Eric Smitha3b1ac82009-04-03 14:45:06 +0000130
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000131/* Flags used by string formatting */
132#define F_LJUST (1<<0)
Serhiy Storchaka598ceae2017-11-28 17:56:10 +0200133#define F_SIGN (1<<1)
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000134#define F_BLANK (1<<2)
Serhiy Storchaka598ceae2017-11-28 17:56:10 +0200135#define F_ALT (1<<3)
136#define F_ZERO (1<<4)
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000137
Victor Stinner00165072015-10-09 01:53:21 +0200138#ifndef Py_LIMITED_API
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700139/* The _PyBytesWriter structure is big: it contains an embedded "stack buffer".
Victor Stinner00165072015-10-09 01:53:21 +0200140 A _PyBytesWriter variable must be declared at the end of variables in a
141 function to optimize the memory allocation on the stack. */
142typedef struct {
Victor Stinner661aacc2015-10-14 09:41:48 +0200143 /* bytes, bytearray or NULL (when the small buffer is used) */
Victor Stinner00165072015-10-09 01:53:21 +0200144 PyObject *buffer;
145
Victor Stinner661aacc2015-10-14 09:41:48 +0200146 /* Number of allocated size. */
Victor Stinner00165072015-10-09 01:53:21 +0200147 Py_ssize_t allocated;
148
Victor Stinner53926a12015-10-09 12:37:03 +0200149 /* Minimum number of allocated bytes,
150 incremented by _PyBytesWriter_Prepare() */
151 Py_ssize_t min_size;
Victor Stinner00165072015-10-09 01:53:21 +0200152
Victor Stinner661aacc2015-10-14 09:41:48 +0200153 /* If non-zero, use a bytearray instead of a bytes object for buffer. */
154 int use_bytearray;
155
156 /* If non-zero, overallocate the buffer (default: 0).
157 This flag must be zero if use_bytearray is non-zero. */
Victor Stinner00165072015-10-09 01:53:21 +0200158 int overallocate;
159
160 /* Stack buffer */
Victor Stinnerb3653a32015-10-09 03:38:24 +0200161 int use_small_buffer;
162 char small_buffer[512];
Victor Stinner00165072015-10-09 01:53:21 +0200163} _PyBytesWriter;
164
165/* Initialize a bytes writer
166
167 By default, the overallocation is disabled. Set the overallocate attribute
168 to control the allocation of the buffer. */
169PyAPI_FUNC(void) _PyBytesWriter_Init(_PyBytesWriter *writer);
170
171/* Get the buffer content and reset the writer.
Victor Stinner661aacc2015-10-14 09:41:48 +0200172 Return a bytes object, or a bytearray object if use_bytearray is non-zero.
Victor Stinner00165072015-10-09 01:53:21 +0200173 Raise an exception and return NULL on error. */
174PyAPI_FUNC(PyObject *) _PyBytesWriter_Finish(_PyBytesWriter *writer,
Victor Stinnerc29e29b2015-10-12 13:12:54 +0200175 void *str);
Victor Stinner00165072015-10-09 01:53:21 +0200176
177/* Deallocate memory of a writer (clear its internal buffer). */
178PyAPI_FUNC(void) _PyBytesWriter_Dealloc(_PyBytesWriter *writer);
179
180/* Allocate the buffer to write size bytes.
181 Return the pointer to the beginning of buffer data.
182 Raise an exception and return NULL on error. */
Victor Stinnerc29e29b2015-10-12 13:12:54 +0200183PyAPI_FUNC(void*) _PyBytesWriter_Alloc(_PyBytesWriter *writer,
Victor Stinner00165072015-10-09 01:53:21 +0200184 Py_ssize_t size);
185
Victor Stinnerc5c3ba42015-10-14 13:56:47 +0200186/* Ensure that the buffer is large enough to write *size* bytes.
187 Add size to the writer minimum size (min_size attribute).
188
Victor Stinner00165072015-10-09 01:53:21 +0200189 str is the current pointer inside the buffer.
190 Return the updated current pointer inside the buffer.
191 Raise an exception and return NULL on error. */
Victor Stinnerc29e29b2015-10-12 13:12:54 +0200192PyAPI_FUNC(void*) _PyBytesWriter_Prepare(_PyBytesWriter *writer,
193 void *str,
Victor Stinner00165072015-10-09 01:53:21 +0200194 Py_ssize_t size);
Victor Stinnerce179bf2015-10-09 12:57:22 +0200195
Victor Stinnerc5c3ba42015-10-14 13:56:47 +0200196/* Resize the buffer to make it larger.
197 The new buffer may be larger than size bytes because of overallocation.
198 Return the updated current pointer inside the buffer.
199 Raise an exception and return NULL on error.
200
201 Note: size must be greater than the number of allocated bytes in the writer.
202
203 This function doesn't use the writer minimum size (min_size attribute).
204
205 See also _PyBytesWriter_Prepare().
206 */
207PyAPI_FUNC(void*) _PyBytesWriter_Resize(_PyBytesWriter *writer,
208 void *str,
209 Py_ssize_t size);
210
Victor Stinnerce179bf2015-10-09 12:57:22 +0200211/* Write bytes.
212 Raise an exception and return NULL on error. */
Victor Stinnerc29e29b2015-10-12 13:12:54 +0200213PyAPI_FUNC(void*) _PyBytesWriter_WriteBytes(_PyBytesWriter *writer,
214 void *str,
215 const void *bytes,
Victor Stinnerce179bf2015-10-09 12:57:22 +0200216 Py_ssize_t size);
Victor Stinner00165072015-10-09 01:53:21 +0200217#endif /* Py_LIMITED_API */
218
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000219#ifdef __cplusplus
220}
221#endif
Georg Brandl0c77a822008-06-10 16:37:50 +0000222#endif /* !Py_BYTESOBJECT_H */