blob: 1c73fdeb80561ae54ca52989716336c94a5af877 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* String object interface */
3
Fred Drake3cf4d2b2000-07-09 00:55:06 +00004#ifndef Py_STRINGOBJECT_H
5#define Py_STRINGOBJECT_H
6#ifdef __cplusplus
7extern "C" {
8#endif
9
Barry Warsawdadace02001-08-24 18:32:06 +000010#include <stdarg.h>
11
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000012/*
Christian Heimes72b710a2008-05-26 13:28:38 +000013Type PyBytesObject represents a character string. An extra zero byte is
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000014reserved 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.
17
18There 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
Tim Peters1f7df352002-03-29 03:29:08 +000027/* Caching the hash (ob_shash) saves recalculation of a string's hash value.
Guido van Rossum98297ee2007-11-06 21:34:58 +000028 This significantly speeds up dict lookups. */
Guido van Rossumfdebf251996-07-30 16:42:03 +000029
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000030typedef struct {
Fred Drake3cf4d2b2000-07-09 00:55:06 +000031 PyObject_VAR_HEAD
Fred Drake3cf4d2b2000-07-09 00:55:06 +000032 long ob_shash;
Fred Drake3cf4d2b2000-07-09 00:55:06 +000033 char ob_sval[1];
Armin Rigo89a39462004-10-28 16:32:00 +000034
35 /* 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.
Armin Rigo89a39462004-10-28 16:32:00 +000039 */
Christian Heimes72b710a2008-05-26 13:28:38 +000040} PyBytesObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041
Christian Heimes72b710a2008-05-26 13:28:38 +000042PyAPI_DATA(PyTypeObject) PyBytes_Type;
43PyAPI_DATA(PyTypeObject) PyBytesIter_Type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044
Christian Heimes72b710a2008-05-26 13:28:38 +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 Rossum85a5fbb1990-10-14 12:07:46 +000048
Christian Heimes72b710a2008-05-26 13:28:38 +000049PyAPI_FUNC(PyObject *) PyBytes_FromStringAndSize(const char *, Py_ssize_t);
50PyAPI_FUNC(PyObject *) PyBytes_FromString(const char *);
51PyAPI_FUNC(PyObject *) PyBytes_FromFormatV(const char*, va_list)
Neil Schemenauer96aa0ac2002-09-15 14:09:54 +000052 Py_GCC_ATTRIBUTE((format(printf, 1, 0)));
Christian Heimes72b710a2008-05-26 13:28:38 +000053PyAPI_FUNC(PyObject *) PyBytes_FromFormat(const char*, ...)
Neil Schemenauer96aa0ac2002-09-15 14:09:54 +000054 Py_GCC_ATTRIBUTE((format(printf, 1, 2)));
Christian Heimes72b710a2008-05-26 13:28:38 +000055PyAPI_FUNC(Py_ssize_t) PyBytes_Size(PyObject *);
56PyAPI_FUNC(char *) PyBytes_AsString(PyObject *);
57PyAPI_FUNC(PyObject *) PyBytes_Repr(PyObject *, int);
58PyAPI_FUNC(void) PyBytes_Concat(PyObject **, PyObject *);
59PyAPI_FUNC(void) PyBytes_ConcatAndDel(PyObject **, PyObject *);
60PyAPI_FUNC(int) _PyBytes_Resize(PyObject **, Py_ssize_t);
61PyAPI_FUNC(PyObject *) PyBytes_Format(PyObject *, PyObject *);
62PyAPI_FUNC(PyObject *) _PyBytes_FormatLong(PyObject*, int, int,
Tim Peters38fd5b62000-09-21 05:43:11 +000063 int, char**, int*);
Christian Heimes72b710a2008-05-26 13:28:38 +000064PyAPI_FUNC(PyObject *) PyBytes_DecodeEscape(const char *, Py_ssize_t,
Martin v. Löwis18e16552006-02-15 17:27:45 +000065 const char *, Py_ssize_t,
Martin v. Löwis8a8da792002-08-14 07:46:28 +000066 const char *);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000067
68/* Macro, trading safety for speed */
Christian Heimes72b710a2008-05-26 13:28:38 +000069#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))
Guido van Rossuma3309961993-07-28 09:05:47 +000072
Christian Heimes72b710a2008-05-26 13:28:38 +000073/* _PyBytes_Join(sep, x) is like sep.join(x). sep must be PyBytesObject*,
Tim Petersa7259592001-06-16 05:11:17 +000074 x must be an iterable object. */
Christian Heimes72b710a2008-05-26 13:28:38 +000075PyAPI_FUNC(PyObject *) _PyBytes_Join(PyObject *sep, PyObject *x);
Tim Petersa7259592001-06-16 05:11:17 +000076
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +000077/* 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). */
Christian Heimes72b710a2008-05-26 13:28:38 +000082PyAPI_FUNC(int) PyBytes_AsStringAndSize(
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +000083 register PyObject *obj, /* string or Unicode object */
84 register char **s, /* pointer to buffer variable */
Martin v. Löwis18e16552006-02-15 17:27:45 +000085 register Py_ssize_t *len /* pointer to length variable or NULL
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +000086 (only possible for 0-terminated
87 strings) */
88 );
Guido van Rossuma2074f02007-10-26 19:34:40 +000089
Eric Smith5807c412008-05-11 21:00:57 +000090/* 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
Christian Heimes72b710a2008-05-26 13:28:38 +000094PyAPI_FUNC(int) _PyBytes_InsertThousandsGrouping(char *buffer,
Eric Smith5807c412008-05-11 21:00:57 +000095 Py_ssize_t len,
96 char *plast,
97 Py_ssize_t buf_size,
98 Py_ssize_t *count,
99 int append_zero_char);
100
Guido van Rossum98297ee2007-11-06 21:34:58 +0000101/* Flags used by string formatting */
102#define F_LJUST (1<<0)
103#define F_SIGN (1<<1)
104#define F_BLANK (1<<2)
105#define F_ALT (1<<3)
106#define F_ZERO (1<<4)
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000107
Guido van Rossuma3309961993-07-28 09:05:47 +0000108#ifdef __cplusplus
109}
110#endif
111#endif /* !Py_STRINGOBJECT_H */