blob: 5062d8d123ad3e6bdca0311ff821dc3134ea04a2 [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
Christian Heimes2c9c7a52008-05-26 13:42:13 +000030PyAPI_DATA(PyTypeObject) PyBytes_Type;
31PyAPI_DATA(PyTypeObject) PyBytesIter_Type;
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000032
Christian Heimes2c9c7a52008-05-26 13:42:13 +000033#define PyBytes_Check(op) \
34 PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_BYTES_SUBCLASS)
Dong-hee Nad905df72020-02-14 02:37:17 +090035#define PyBytes_CheckExact(op) Py_IS_TYPE(op, &PyBytes_Type)
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000036
Christian Heimes2c9c7a52008-05-26 13:42:13 +000037PyAPI_FUNC(PyObject *) PyBytes_FromStringAndSize(const char *, Py_ssize_t);
38PyAPI_FUNC(PyObject *) PyBytes_FromString(const char *);
Benjamin Petersonc15a0732008-08-26 16:46:47 +000039PyAPI_FUNC(PyObject *) PyBytes_FromObject(PyObject *);
Christian Heimes2c9c7a52008-05-26 13:42:13 +000040PyAPI_FUNC(PyObject *) PyBytes_FromFormatV(const char*, va_list)
Serhiy Storchaka598ceae2017-11-28 17:56:10 +020041 Py_GCC_ATTRIBUTE((format(printf, 1, 0)));
Christian Heimes2c9c7a52008-05-26 13:42:13 +000042PyAPI_FUNC(PyObject *) PyBytes_FromFormat(const char*, ...)
Serhiy Storchaka598ceae2017-11-28 17:56:10 +020043 Py_GCC_ATTRIBUTE((format(printf, 1, 2)));
Christian Heimes2c9c7a52008-05-26 13:42:13 +000044PyAPI_FUNC(Py_ssize_t) PyBytes_Size(PyObject *);
45PyAPI_FUNC(char *) PyBytes_AsString(PyObject *);
46PyAPI_FUNC(PyObject *) PyBytes_Repr(PyObject *, int);
47PyAPI_FUNC(void) PyBytes_Concat(PyObject **, PyObject *);
48PyAPI_FUNC(void) PyBytes_ConcatAndDel(PyObject **, PyObject *);
Christian Heimes2c9c7a52008-05-26 13:42:13 +000049PyAPI_FUNC(PyObject *) PyBytes_DecodeEscape(const char *, Py_ssize_t,
Serhiy Storchaka598ceae2017-11-28 17:56:10 +020050 const char *, Py_ssize_t,
51 const char *);
Christian Heimes2c9c7a52008-05-26 13:42:13 +000052
53/* Provides access to the internal data buffer and size of a string
Martin Panter6245cb32016-04-15 02:14:19 +000054 object or the default encoded version of a Unicode object. Passing
Christian Heimes2c9c7a52008-05-26 13:42:13 +000055 NULL as *len parameter will force the string buffer to be
56 0-terminated (passing a string with embedded NULL characters will
57 cause an exception). */
58PyAPI_FUNC(int) PyBytes_AsStringAndSize(
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020059 PyObject *obj, /* string or Unicode object */
60 char **s, /* pointer to buffer variable */
61 Py_ssize_t *len /* pointer to length variable or NULL
62 (only possible for 0-terminated
63 strings) */
Christian Heimes2c9c7a52008-05-26 13:42:13 +000064 );
65
Christian Heimes2c9c7a52008-05-26 13:42:13 +000066/* Flags used by string formatting */
67#define F_LJUST (1<<0)
Serhiy Storchaka598ceae2017-11-28 17:56:10 +020068#define F_SIGN (1<<1)
Christian Heimes2c9c7a52008-05-26 13:42:13 +000069#define F_BLANK (1<<2)
Serhiy Storchaka598ceae2017-11-28 17:56:10 +020070#define F_ALT (1<<3)
71#define F_ZERO (1<<4)
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000072
Victor Stinner00165072015-10-09 01:53:21 +020073#ifndef Py_LIMITED_API
Victor Stinner98921ae2020-02-12 23:54:31 +010074# define Py_CPYTHON_BYTESOBJECT_H
75# include "cpython/bytesobject.h"
76# undef Py_CPYTHON_BYTESOBJECT_H
77#endif
Victor Stinner00165072015-10-09 01:53:21 +020078
Guido van Rossum4dfe8a12006-04-22 23:28:04 +000079#ifdef __cplusplus
80}
81#endif
Georg Brandl0c77a822008-06-10 16:37:50 +000082#endif /* !Py_BYTESOBJECT_H */