blob: 916e3f7ce883d280bf82b20bfaf16b81687b6a92 [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
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000030typedef struct {
31 PyObject_VAR_HEAD
Christian Heimes2c9c7a52008-05-26 13:42:13 +000032 long ob_shash;
33 char ob_sval[1];
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000034
Christian Heimes2c9c7a52008-05-26 13:42:13 +000035 /* Invariants:
36 * ob_sval contains space for 'ob_size+1' elements.
37 * ob_sval[ob_size] == 0.
38 * ob_shash is the hash of the string or -1 if not computed yet.
39 */
40} PyBytesObject;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000041
Christian Heimes2c9c7a52008-05-26 13:42:13 +000042PyAPI_DATA(PyTypeObject) PyBytes_Type;
43PyAPI_DATA(PyTypeObject) PyBytesIter_Type;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000044
Christian Heimes2c9c7a52008-05-26 13:42:13 +000045#define PyBytes_Check(op) \
46 PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_BYTES_SUBCLASS)
47#define PyBytes_CheckExact(op) (Py_TYPE(op) == &PyBytes_Type)
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000048
Christian Heimes2c9c7a52008-05-26 13:42:13 +000049PyAPI_FUNC(PyObject *) PyBytes_FromStringAndSize(const char *, Py_ssize_t);
50PyAPI_FUNC(PyObject *) PyBytes_FromString(const char *);
Benjamin Petersonc15a0732008-08-26 16:46:47 +000051PyAPI_FUNC(PyObject *) PyBytes_FromObject(PyObject *);
Christian Heimes2c9c7a52008-05-26 13:42:13 +000052PyAPI_FUNC(PyObject *) PyBytes_FromFormatV(const char*, va_list)
53 Py_GCC_ATTRIBUTE((format(printf, 1, 0)));
54PyAPI_FUNC(PyObject *) PyBytes_FromFormat(const char*, ...)
55 Py_GCC_ATTRIBUTE((format(printf, 1, 2)));
56PyAPI_FUNC(Py_ssize_t) PyBytes_Size(PyObject *);
57PyAPI_FUNC(char *) PyBytes_AsString(PyObject *);
58PyAPI_FUNC(PyObject *) PyBytes_Repr(PyObject *, int);
59PyAPI_FUNC(void) PyBytes_Concat(PyObject **, PyObject *);
60PyAPI_FUNC(void) PyBytes_ConcatAndDel(PyObject **, PyObject *);
61PyAPI_FUNC(int) _PyBytes_Resize(PyObject **, Py_ssize_t);
Christian Heimes2c9c7a52008-05-26 13:42:13 +000062PyAPI_FUNC(PyObject *) _PyBytes_FormatLong(PyObject*, int, int,
63 int, char**, int*);
64PyAPI_FUNC(PyObject *) PyBytes_DecodeEscape(const char *, Py_ssize_t,
65 const char *, Py_ssize_t,
66 const char *);
67
68/* Macro, trading safety for speed */
69#define PyBytes_AS_STRING(op) (assert(PyBytes_Check(op)), \
70 (((PyBytesObject *)(op))->ob_sval))
71#define PyBytes_GET_SIZE(op) (assert(PyBytes_Check(op)),Py_SIZE(op))
72
73/* _PyBytes_Join(sep, x) is like sep.join(x). sep must be PyBytesObject*,
74 x must be an iterable object. */
75PyAPI_FUNC(PyObject *) _PyBytes_Join(PyObject *sep, PyObject *x);
76
77/* Provides access to the internal data buffer and size of a string
78 object or the default encoded version of an Unicode object. Passing
79 NULL as *len parameter will force the string buffer to be
80 0-terminated (passing a string with embedded NULL characters will
81 cause an exception). */
82PyAPI_FUNC(int) PyBytes_AsStringAndSize(
83 register PyObject *obj, /* string or Unicode object */
84 register char **s, /* pointer to buffer variable */
85 register Py_ssize_t *len /* pointer to length variable or NULL
86 (only possible for 0-terminated
87 strings) */
88 );
89
90/* Using the current locale, insert the thousands grouping
91 into the string pointed to by buffer. For the argument descriptions,
92 see Objects/stringlib/localeutil.h */
93
Eric Smith0923d1d2009-04-16 20:16:10 +000094PyAPI_FUNC(Py_ssize_t) _PyBytes_InsertThousandsGroupingLocale(char *buffer,
95 Py_ssize_t n_buffer,
96 char *digits,
97 Py_ssize_t n_digits,
98 Py_ssize_t min_width);
Christian Heimes2c9c7a52008-05-26 13:42:13 +000099
Eric Smitha3b1ac82009-04-03 14:45:06 +0000100/* Using explicit passed-in values, insert the thousands grouping
101 into the string pointed to by buffer. For the argument descriptions,
102 see Objects/stringlib/localeutil.h */
Eric Smith0923d1d2009-04-16 20:16:10 +0000103PyAPI_FUNC(Py_ssize_t) _PyBytes_InsertThousandsGrouping(char *buffer,
104 Py_ssize_t n_buffer,
105 char *digits,
106 Py_ssize_t n_digits,
107 Py_ssize_t min_width,
108 const char *grouping,
109 const char *thousands_sep);
Eric Smitha3b1ac82009-04-03 14:45:06 +0000110
Christian Heimes2c9c7a52008-05-26 13:42:13 +0000111/* Flags used by string formatting */
112#define F_LJUST (1<<0)
113#define F_SIGN (1<<1)
114#define F_BLANK (1<<2)
115#define F_ALT (1<<3)
116#define F_ZERO (1<<4)
Guido van Rossum4dfe8a12006-04-22 23:28:04 +0000117
118#ifdef __cplusplus
119}
120#endif
Georg Brandl0c77a822008-06-10 16:37:50 +0000121#endif /* !Py_BYTESOBJECT_H */