| 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); | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 65 | PyAPI_FUNC(PyObject *) _PyBytes_FormatLong(PyObject*, int, int, | 
 | 66 | 						  int, char**, int*); | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 67 | #endif | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 68 | PyAPI_FUNC(PyObject *) PyBytes_DecodeEscape(const char *, Py_ssize_t, | 
 | 69 | 						   const char *, Py_ssize_t, | 
 | 70 | 						   const char *); | 
 | 71 |  | 
 | 72 | /* Macro, trading safety for speed */ | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 73 | #ifndef Py_LIMITED_API | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 74 | #define PyBytes_AS_STRING(op) (assert(PyBytes_Check(op)), \ | 
 | 75 |                                 (((PyBytesObject *)(op))->ob_sval)) | 
 | 76 | #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] | 77 | #endif | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 78 |  | 
 | 79 | /* _PyBytes_Join(sep, x) is like sep.join(x).  sep must be PyBytesObject*, | 
 | 80 |    x must be an iterable object. */ | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 81 | #ifndef Py_LIMITED_API | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 82 | PyAPI_FUNC(PyObject *) _PyBytes_Join(PyObject *sep, PyObject *x); | 
| 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 | /* Provides access to the internal data buffer and size of a string | 
 | 86 |    object or the default encoded version of an Unicode object. Passing | 
 | 87 |    NULL as *len parameter will force the string buffer to be | 
 | 88 |    0-terminated (passing a string with embedded NULL characters will | 
 | 89 |    cause an exception).  */ | 
 | 90 | PyAPI_FUNC(int) PyBytes_AsStringAndSize( | 
 | 91 |     register PyObject *obj,	/* string or Unicode object */ | 
 | 92 |     register char **s,		/* pointer to buffer variable */ | 
 | 93 |     register Py_ssize_t *len	/* pointer to length variable or NULL | 
 | 94 | 				   (only possible for 0-terminated | 
 | 95 | 				   strings) */ | 
 | 96 |     ); | 
 | 97 |  | 
 | 98 | /* Using the current locale, insert the thousands grouping | 
 | 99 |    into the string pointed to by buffer.  For the argument descriptions, | 
 | 100 |    see Objects/stringlib/localeutil.h */ | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 101 | #ifndef Py_LIMITED_API | 
| Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 102 | PyAPI_FUNC(Py_ssize_t) _PyBytes_InsertThousandsGroupingLocale(char *buffer, | 
 | 103 |                                                    Py_ssize_t n_buffer, | 
 | 104 |                                                    char *digits, | 
 | 105 |                                                    Py_ssize_t n_digits, | 
 | 106 |                                                    Py_ssize_t min_width); | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 107 |  | 
| Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 108 | /* Using explicit passed-in values, insert the thousands grouping | 
 | 109 |    into the string pointed to by buffer.  For the argument descriptions, | 
 | 110 |    see Objects/stringlib/localeutil.h */ | 
| Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 111 | PyAPI_FUNC(Py_ssize_t) _PyBytes_InsertThousandsGrouping(char *buffer, | 
 | 112 |                                                    Py_ssize_t n_buffer, | 
 | 113 |                                                    char *digits, | 
 | 114 |                                                    Py_ssize_t n_digits, | 
 | 115 |                                                    Py_ssize_t min_width, | 
 | 116 |                                                    const char *grouping, | 
 | 117 |                                                    const char *thousands_sep); | 
| Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 118 | #endif | 
| Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame] | 119 |  | 
| Christian Heimes | 2c9c7a5 | 2008-05-26 13:42:13 +0000 | [diff] [blame] | 120 | /* Flags used by string formatting */ | 
 | 121 | #define F_LJUST (1<<0) | 
 | 122 | #define F_SIGN	(1<<1) | 
 | 123 | #define F_BLANK (1<<2) | 
 | 124 | #define F_ALT	(1<<3) | 
 | 125 | #define F_ZERO	(1<<4) | 
| Guido van Rossum | 4dfe8a1 | 2006-04-22 23:28:04 +0000 | [diff] [blame] | 126 |  | 
 | 127 | #ifdef __cplusplus | 
 | 128 | } | 
 | 129 | #endif | 
| Georg Brandl | 0c77a82 | 2008-06-10 16:37:50 +0000 | [diff] [blame] | 130 | #endif /* !Py_BYTESOBJECT_H */ |