blob: c1a51144bcca3156d022ef853a64b02af102284d [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/*
Guido van Rossumcaa63801995-01-12 11:45:45 +000013Type PyStringObject 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 */
Guido van Rossumcaa63801995-01-12 11:45:45 +000040} PyStringObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041
Mark Hammond91a681d2002-08-12 07:21:58 +000042PyAPI_DATA(PyTypeObject) PyString_Type;
Christian Heimesa22e8bd2007-11-29 22:35:39 +000043PyAPI_DATA(PyTypeObject) PyStringIter_Type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044
Thomas Wouters27d517b2007-02-25 20:39:11 +000045#define PyString_Check(op) \
Christian Heimes90aa7642007-12-19 02:45:37 +000046 PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_STRING_SUBCLASS)
47#define PyString_CheckExact(op) (Py_TYPE(op) == &PyString_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048
Martin v. Löwis18e16552006-02-15 17:27:45 +000049PyAPI_FUNC(PyObject *) PyString_FromStringAndSize(const char *, Py_ssize_t);
Mark Hammond91a681d2002-08-12 07:21:58 +000050PyAPI_FUNC(PyObject *) PyString_FromString(const char *);
51PyAPI_FUNC(PyObject *) PyString_FromFormatV(const char*, va_list)
Neil Schemenauer96aa0ac2002-09-15 14:09:54 +000052 Py_GCC_ATTRIBUTE((format(printf, 1, 0)));
Mark Hammond91a681d2002-08-12 07:21:58 +000053PyAPI_FUNC(PyObject *) PyString_FromFormat(const char*, ...)
Neil Schemenauer96aa0ac2002-09-15 14:09:54 +000054 Py_GCC_ATTRIBUTE((format(printf, 1, 2)));
Martin v. Löwis18e16552006-02-15 17:27:45 +000055PyAPI_FUNC(Py_ssize_t) PyString_Size(PyObject *);
Mark Hammond91a681d2002-08-12 07:21:58 +000056PyAPI_FUNC(char *) PyString_AsString(PyObject *);
Martin v. Löwis8a8da792002-08-14 07:46:28 +000057PyAPI_FUNC(PyObject *) PyString_Repr(PyObject *, int);
Mark Hammond91a681d2002-08-12 07:21:58 +000058PyAPI_FUNC(void) PyString_Concat(PyObject **, PyObject *);
59PyAPI_FUNC(void) PyString_ConcatAndDel(PyObject **, PyObject *);
Martin v. Löwis18e16552006-02-15 17:27:45 +000060PyAPI_FUNC(int) _PyString_Resize(PyObject **, Py_ssize_t);
Mark Hammond91a681d2002-08-12 07:21:58 +000061PyAPI_FUNC(PyObject *) PyString_Format(PyObject *, PyObject *);
62PyAPI_FUNC(PyObject *) _PyString_FormatLong(PyObject*, int, int,
Tim Peters38fd5b62000-09-21 05:43:11 +000063 int, char**, int*);
Guido van Rossuma2074f02007-10-26 19:34:40 +000064PyAPI_FUNC(PyObject *) PyString_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 */
Guido van Rossum98297ee2007-11-06 21:34:58 +000069#define PyString_AS_STRING(op) (assert(PyString_Check(op)), \
70 (((PyStringObject *)(op))->ob_sval))
Christian Heimes90aa7642007-12-19 02:45:37 +000071#define PyString_GET_SIZE(op) (assert(PyString_Check(op)),Py_SIZE(op))
Guido van Rossuma3309961993-07-28 09:05:47 +000072
Tim Petersa7259592001-06-16 05:11:17 +000073/* _PyString_Join(sep, x) is like sep.join(x). sep must be PyStringObject*,
74 x must be an iterable object. */
Mark Hammond91a681d2002-08-12 07:21:58 +000075PyAPI_FUNC(PyObject *) _PyString_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). */
Mark Hammond91a681d2002-08-12 07:21:58 +000082PyAPI_FUNC(int) PyString_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
Guido van Rossum98297ee2007-11-06 21:34:58 +000090/* Flags used by string formatting */
91#define F_LJUST (1<<0)
92#define F_SIGN (1<<1)
93#define F_BLANK (1<<2)
94#define F_ALT (1<<3)
95#define F_ZERO (1<<4)
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +000096
Guido van Rossuma3309961993-07-28 09:05:47 +000097#ifdef __cplusplus
98}
99#endif
100#endif /* !Py_STRINGOBJECT_H */