| Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 1 |  | 
| Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 2 | /* Bytes (String) object interface */ | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 3 |  | 
| Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 4 | #ifndef Py_BYTESOBJECT_H | 
 | 5 | #define Py_BYTESOBJECT_H | 
| Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 6 | #ifdef __cplusplus | 
 | 7 | extern "C" { | 
 | 8 | #endif | 
 | 9 |  | 
 | 10 | #include <stdarg.h> | 
 | 11 |  | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 12 | /* | 
 | 13 | Type PyBytesObject represents a character string.  An extra zero byte is | 
 | 14 | reserved at the end to ensure it is zero-terminated, but a size is | 
 | 15 | present so strings with null bytes in them can be represented.  This | 
 | 16 | is an immutable object type. | 
| Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 17 |  | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 18 | There are functions to create new string objects, to test | 
 | 19 | an object for string-ness, and to get the | 
 | 20 | string value.  The latter function returns a null pointer | 
 | 21 | if the object is not of the proper type. | 
 | 22 | There is a variant that takes an explicit size as well as a | 
 | 23 | variant that assumes a zero-terminated string.  Note that none of the | 
 | 24 | functions 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öwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 30 | #ifndef Py_LIMITED_API | 
| Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 31 | typedef struct { | 
 | 32 |     PyObject_VAR_HEAD | 
| Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 33 |     Py_hash_t ob_shash; | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 34 |     char ob_sval[1]; | 
| Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 35 |  | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 36 |     /* 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öwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 42 | #endif | 
| Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 43 |  | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 44 | PyAPI_DATA(PyTypeObject) PyBytes_Type; | 
 | 45 | PyAPI_DATA(PyTypeObject) PyBytesIter_Type; | 
| Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 46 |  | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 47 | #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 Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 50 |  | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 51 | PyAPI_FUNC(PyObject *) PyBytes_FromStringAndSize(const char *, Py_ssize_t); | 
 | 52 | PyAPI_FUNC(PyObject *) PyBytes_FromString(const char *); | 
| Benjamin Peterson | c15a073 | 2008-08-26 16:46:47 +0000 | [diff] [blame] | 53 | PyAPI_FUNC(PyObject *) PyBytes_FromObject(PyObject *); | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 54 | PyAPI_FUNC(PyObject *) PyBytes_FromFormatV(const char*, va_list) | 
 | 55 | 				Py_GCC_ATTRIBUTE((format(printf, 1, 0))); | 
 | 56 | PyAPI_FUNC(PyObject *) PyBytes_FromFormat(const char*, ...) | 
 | 57 | 				Py_GCC_ATTRIBUTE((format(printf, 1, 2))); | 
 | 58 | PyAPI_FUNC(Py_ssize_t) PyBytes_Size(PyObject *); | 
 | 59 | PyAPI_FUNC(char *) PyBytes_AsString(PyObject *); | 
 | 60 | PyAPI_FUNC(PyObject *) PyBytes_Repr(PyObject *, int); | 
 | 61 | PyAPI_FUNC(void) PyBytes_Concat(PyObject **, PyObject *); | 
 | 62 | PyAPI_FUNC(void) PyBytes_ConcatAndDel(PyObject **, PyObject *); | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 63 | #ifndef Py_LIMITED_API | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 64 | PyAPI_FUNC(int) _PyBytes_Resize(PyObject **, Py_ssize_t); | 
| Victor Stinner | 772b2b0 | 2015-10-14 09:56:53 +0200 | [diff] [blame] | 65 | PyAPI_FUNC(PyObject*) _PyBytes_FormatEx( | 
 | 66 |     const char *format, | 
 | 67 |     Py_ssize_t format_len, | 
 | 68 |     PyObject *args, | 
 | 69 |     int use_bytearray); | 
| Victor Stinner | 2bf8993 | 2015-10-14 11:25:33 +0200 | [diff] [blame] | 70 | PyAPI_FUNC(PyObject*) _PyBytes_FromHex( | 
 | 71 |     PyObject *string, | 
 | 72 |     int use_bytearray); | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 73 | #endif | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 74 | PyAPI_FUNC(PyObject *) PyBytes_DecodeEscape(const char *, Py_ssize_t, | 
 | 75 | 						   const char *, Py_ssize_t, | 
 | 76 | 						   const char *); | 
 | 77 |  | 
 | 78 | /* Macro, trading safety for speed */ | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 79 | #ifndef Py_LIMITED_API | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 80 | #define PyBytes_AS_STRING(op) (assert(PyBytes_Check(op)), \ | 
 | 81 |                                 (((PyBytesObject *)(op))->ob_sval)) | 
 | 82 | #define PyBytes_GET_SIZE(op)  (assert(PyBytes_Check(op)),Py_SIZE(op)) | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 83 | #endif | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 84 |  | 
 | 85 | /* _PyBytes_Join(sep, x) is like sep.join(x).  sep must be PyBytesObject*, | 
 | 86 |    x must be an iterable object. */ | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 87 | #ifndef Py_LIMITED_API | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 88 | PyAPI_FUNC(PyObject *) _PyBytes_Join(PyObject *sep, PyObject *x); | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 89 | #endif | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 90 |  | 
 | 91 | /* Provides access to the internal data buffer and size of a string | 
| Martin Panter | 6245cb3 | 2016-04-15 02:14:19 +0000 | [diff] [blame] | 92 |    object or the default encoded version of a Unicode object. Passing | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 93 |    NULL as *len parameter will force the string buffer to be | 
 | 94 |    0-terminated (passing a string with embedded NULL characters will | 
 | 95 |    cause an exception).  */ | 
 | 96 | PyAPI_FUNC(int) PyBytes_AsStringAndSize( | 
| Antoine Pitrou | 9ed5f27 | 2013-08-13 20:18:52 +0200 | [diff] [blame] | 97 |     PyObject *obj,      /* string or Unicode object */ | 
 | 98 |     char **s,           /* pointer to buffer variable */ | 
 | 99 |     Py_ssize_t *len     /* pointer to length variable or NULL | 
 | 100 |                            (only possible for 0-terminated | 
 | 101 |                            strings) */ | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 102 |     ); | 
 | 103 |  | 
 | 104 | /* Using the current locale, insert the thousands grouping | 
 | 105 |    into the string pointed to by buffer.  For the argument descriptions, | 
 | 106 |    see Objects/stringlib/localeutil.h */ | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 107 | #ifndef Py_LIMITED_API | 
| Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 108 | PyAPI_FUNC(Py_ssize_t) _PyBytes_InsertThousandsGroupingLocale(char *buffer, | 
 | 109 |                                                    Py_ssize_t n_buffer, | 
 | 110 |                                                    char *digits, | 
 | 111 |                                                    Py_ssize_t n_digits, | 
 | 112 |                                                    Py_ssize_t min_width); | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 113 |  | 
| Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 114 | /* Using explicit passed-in values, insert the thousands grouping | 
 | 115 |    into the string pointed to by buffer.  For the argument descriptions, | 
 | 116 |    see Objects/stringlib/localeutil.h */ | 
| Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 117 | PyAPI_FUNC(Py_ssize_t) _PyBytes_InsertThousandsGrouping(char *buffer, | 
 | 118 |                                                    Py_ssize_t n_buffer, | 
 | 119 |                                                    char *digits, | 
 | 120 |                                                    Py_ssize_t n_digits, | 
 | 121 |                                                    Py_ssize_t min_width, | 
 | 122 |                                                    const char *grouping, | 
 | 123 |                                                    const char *thousands_sep); | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 124 | #endif | 
| Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 125 |  | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 126 | /* Flags used by string formatting */ | 
 | 127 | #define F_LJUST (1<<0) | 
 | 128 | #define F_SIGN	(1<<1) | 
 | 129 | #define F_BLANK (1<<2) | 
 | 130 | #define F_ALT	(1<<3) | 
 | 131 | #define F_ZERO	(1<<4) | 
| Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 132 |  | 
| Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 133 | #ifndef Py_LIMITED_API | 
 | 134 | /* The _PyBytesWriter structure is big: it contains an embeded "stack buffer". | 
 | 135 |    A _PyBytesWriter variable must be declared at the end of variables in a | 
 | 136 |    function to optimize the memory allocation on the stack. */ | 
 | 137 | typedef struct { | 
| Victor Stinner | 661aacc | 2015-10-14 09:41:48 +0200 | [diff] [blame] | 138 |     /* bytes, bytearray or NULL (when the small buffer is used) */ | 
| Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 139 |     PyObject *buffer; | 
 | 140 |  | 
| Victor Stinner | 661aacc | 2015-10-14 09:41:48 +0200 | [diff] [blame] | 141 |     /* Number of allocated size. */ | 
| Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 142 |     Py_ssize_t allocated; | 
 | 143 |  | 
| Victor Stinner | 53926a1 | 2015-10-09 12:37:03 +0200 | [diff] [blame] | 144 |     /* Minimum number of allocated bytes, | 
 | 145 |        incremented by _PyBytesWriter_Prepare() */ | 
 | 146 |     Py_ssize_t min_size; | 
| Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 147 |  | 
| Victor Stinner | 661aacc | 2015-10-14 09:41:48 +0200 | [diff] [blame] | 148 |     /* If non-zero, use a bytearray instead of a bytes object for buffer. */ | 
 | 149 |     int use_bytearray; | 
 | 150 |  | 
 | 151 |     /* If non-zero, overallocate the buffer (default: 0). | 
 | 152 |        This flag must be zero if use_bytearray is non-zero. */ | 
| Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 153 |     int overallocate; | 
 | 154 |  | 
 | 155 |     /* Stack buffer */ | 
| Victor Stinner | b3653a3 | 2015-10-09 03:38:24 +0200 | [diff] [blame] | 156 |     int use_small_buffer; | 
 | 157 |     char small_buffer[512]; | 
| Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 158 | } _PyBytesWriter; | 
 | 159 |  | 
 | 160 | /* Initialize a bytes writer | 
 | 161 |  | 
 | 162 |    By default, the overallocation is disabled. Set the overallocate attribute | 
 | 163 |    to control the allocation of the buffer. */ | 
 | 164 | PyAPI_FUNC(void) _PyBytesWriter_Init(_PyBytesWriter *writer); | 
 | 165 |  | 
 | 166 | /* Get the buffer content and reset the writer. | 
| Victor Stinner | 661aacc | 2015-10-14 09:41:48 +0200 | [diff] [blame] | 167 |    Return a bytes object, or a bytearray object if use_bytearray is non-zero. | 
| Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 168 |    Raise an exception and return NULL on error. */ | 
 | 169 | PyAPI_FUNC(PyObject *) _PyBytesWriter_Finish(_PyBytesWriter *writer, | 
| Victor Stinner | c29e29b | 2015-10-12 13:12:54 +0200 | [diff] [blame] | 170 |     void *str); | 
| Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 171 |  | 
 | 172 | /* Deallocate memory of a writer (clear its internal buffer). */ | 
 | 173 | PyAPI_FUNC(void) _PyBytesWriter_Dealloc(_PyBytesWriter *writer); | 
 | 174 |  | 
 | 175 | /* Allocate the buffer to write size bytes. | 
 | 176 |    Return the pointer to the beginning of buffer data. | 
 | 177 |    Raise an exception and return NULL on error. */ | 
| Victor Stinner | c29e29b | 2015-10-12 13:12:54 +0200 | [diff] [blame] | 178 | PyAPI_FUNC(void*) _PyBytesWriter_Alloc(_PyBytesWriter *writer, | 
| Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 179 |     Py_ssize_t size); | 
 | 180 |  | 
| Victor Stinner | c5c3ba4 | 2015-10-14 13:56:47 +0200 | [diff] [blame] | 181 | /* Ensure that the buffer is large enough to write *size* bytes. | 
 | 182 |    Add size to the writer minimum size (min_size attribute). | 
 | 183 |  | 
| Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 184 |    str is the current pointer inside the buffer. | 
 | 185 |    Return the updated current pointer inside the buffer. | 
 | 186 |    Raise an exception and return NULL on error. */ | 
| Victor Stinner | c29e29b | 2015-10-12 13:12:54 +0200 | [diff] [blame] | 187 | PyAPI_FUNC(void*) _PyBytesWriter_Prepare(_PyBytesWriter *writer, | 
 | 188 |     void *str, | 
| Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 189 |     Py_ssize_t size); | 
| Victor Stinner | ce179bf | 2015-10-09 12:57:22 +0200 | [diff] [blame] | 190 |  | 
| Victor Stinner | c5c3ba4 | 2015-10-14 13:56:47 +0200 | [diff] [blame] | 191 | /* Resize the buffer to make it larger. | 
 | 192 |    The new buffer may be larger than size bytes because of overallocation. | 
 | 193 |    Return the updated current pointer inside the buffer. | 
 | 194 |    Raise an exception and return NULL on error. | 
 | 195 |  | 
 | 196 |    Note: size must be greater than the number of allocated bytes in the writer. | 
 | 197 |  | 
 | 198 |    This function doesn't use the writer minimum size (min_size attribute). | 
 | 199 |  | 
 | 200 |    See also _PyBytesWriter_Prepare(). | 
 | 201 |    */ | 
 | 202 | PyAPI_FUNC(void*) _PyBytesWriter_Resize(_PyBytesWriter *writer, | 
 | 203 |     void *str, | 
 | 204 |     Py_ssize_t size); | 
 | 205 |  | 
| Victor Stinner | ce179bf | 2015-10-09 12:57:22 +0200 | [diff] [blame] | 206 | /* Write bytes. | 
 | 207 |    Raise an exception and return NULL on error. */ | 
| Victor Stinner | c29e29b | 2015-10-12 13:12:54 +0200 | [diff] [blame] | 208 | PyAPI_FUNC(void*) _PyBytesWriter_WriteBytes(_PyBytesWriter *writer, | 
 | 209 |     void *str, | 
 | 210 |     const void *bytes, | 
| Victor Stinner | ce179bf | 2015-10-09 12:57:22 +0200 | [diff] [blame] | 211 |     Py_ssize_t size); | 
| Victor Stinner | 0016507 | 2015-10-09 01:53:21 +0200 | [diff] [blame] | 212 | #endif   /* Py_LIMITED_API */ | 
 | 213 |  | 
| Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 214 | #ifdef __cplusplus | 
 | 215 | } | 
 | 216 | #endif | 
| Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 217 | #endif /* !Py_BYTESOBJECT_H */ |