blob: abc8fad625eda2f847bb3fb6a0236e11f5749671 [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.
28 Interning strings (ob_sinterned) tries to ensure that only one string
29 object with a given value exists, so equality tests can be one pointer
30 comparison. This is generally restricted to strings that "look like"
31 Python identifiers, although the intern() builtin can be used to force
32 interning of any string.
33 Together, these sped the interpreter by up to 20%. */
Guido van Rossumfdebf251996-07-30 16:42:03 +000034
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000035typedef struct {
Fred Drake3cf4d2b2000-07-09 00:55:06 +000036 PyObject_VAR_HEAD
Fred Drake3cf4d2b2000-07-09 00:55:06 +000037 long ob_shash;
Fred Drake3cf4d2b2000-07-09 00:55:06 +000038 PyObject *ob_sinterned;
Fred Drake3cf4d2b2000-07-09 00:55:06 +000039 char ob_sval[1];
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) PyBaseString_Type;
43PyAPI_DATA(PyTypeObject) PyString_Type;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044
Guido van Rossum5eef77a2001-08-30 03:08:07 +000045#define PyString_Check(op) PyObject_TypeCheck(op, &PyString_Type)
Tim Peters5a49ade2001-09-11 01:41:59 +000046#define PyString_CheckExact(op) ((op)->ob_type == &PyString_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047
Mark Hammond91a681d2002-08-12 07:21:58 +000048PyAPI_FUNC(PyObject *) PyString_FromStringAndSize(const char *, int);
49PyAPI_FUNC(PyObject *) PyString_FromString(const char *);
50PyAPI_FUNC(PyObject *) PyString_FromFormatV(const char*, va_list)
Neil Schemenauer90b68902001-10-23 02:21:22 +000051 __attribute__((format(printf, 1, 0)));
Mark Hammond91a681d2002-08-12 07:21:58 +000052PyAPI_FUNC(PyObject *) PyString_FromFormat(const char*, ...)
Neil Schemenauer90b68902001-10-23 02:21:22 +000053 __attribute__((format(printf, 1, 2)));
Mark Hammond91a681d2002-08-12 07:21:58 +000054PyAPI_FUNC(int) PyString_Size(PyObject *);
55PyAPI_FUNC(char *) PyString_AsString(PyObject *);
56PyAPI_FUNC(void) PyString_Concat(PyObject **, PyObject *);
57PyAPI_FUNC(void) PyString_ConcatAndDel(PyObject **, PyObject *);
58PyAPI_FUNC(int) _PyString_Resize(PyObject **, int);
59PyAPI_FUNC(int) _PyString_Eq(PyObject *, PyObject*);
60PyAPI_FUNC(PyObject *) PyString_Format(PyObject *, PyObject *);
61PyAPI_FUNC(PyObject *) _PyString_FormatLong(PyObject*, int, int,
Tim Peters38fd5b62000-09-21 05:43:11 +000062 int, char**, int*);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000063
Mark Hammond91a681d2002-08-12 07:21:58 +000064PyAPI_FUNC(void) PyString_InternInPlace(PyObject **);
65PyAPI_FUNC(PyObject *) PyString_InternFromString(const char *);
66PyAPI_FUNC(void) _Py_ReleaseInternedStrings(void);
Guido van Rossum1e6e9a21997-01-18 07:53:23 +000067
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000068/* Macro, trading safety for speed */
Barry Warsawaccfb841997-01-06 22:42:50 +000069#define PyString_AS_STRING(op) (((PyStringObject *)(op))->ob_sval)
70#define PyString_GET_SIZE(op) (((PyStringObject *)(op))->ob_size)
Guido van Rossuma3309961993-07-28 09:05:47 +000071
Tim Petersa7259592001-06-16 05:11:17 +000072/* _PyString_Join(sep, x) is like sep.join(x). sep must be PyStringObject*,
73 x must be an iterable object. */
Mark Hammond91a681d2002-08-12 07:21:58 +000074PyAPI_FUNC(PyObject *) _PyString_Join(PyObject *sep, PyObject *x);
Tim Petersa7259592001-06-16 05:11:17 +000075
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +000076/* --- Generic Codecs ----------------------------------------------------- */
77
Marc-André Lemburg2d920412001-05-15 12:00:02 +000078/* Create an object by decoding the encoded string s of the
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +000079 given size. */
80
Mark Hammond91a681d2002-08-12 07:21:58 +000081PyAPI_FUNC(PyObject*) PyString_Decode(
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +000082 const char *s, /* encoded string */
83 int size, /* size of buffer */
84 const char *encoding, /* encoding */
85 const char *errors /* error handling */
86 );
87
88/* Encodes a char buffer of the given size and returns a
Marc-André Lemburg2d920412001-05-15 12:00:02 +000089 Python object. */
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +000090
Mark Hammond91a681d2002-08-12 07:21:58 +000091PyAPI_FUNC(PyObject*) PyString_Encode(
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +000092 const char *s, /* string char buffer */
93 int size, /* number of chars to encode */
94 const char *encoding, /* encoding */
95 const char *errors /* error handling */
96 );
97
Marc-André Lemburg2d920412001-05-15 12:00:02 +000098/* Encodes a string object and returns the result as Python
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +000099 object. */
100
Mark Hammond91a681d2002-08-12 07:21:58 +0000101PyAPI_FUNC(PyObject*) PyString_AsEncodedObject(
Marc-André Lemburg2d920412001-05-15 12:00:02 +0000102 PyObject *str, /* string object */
103 const char *encoding, /* encoding */
104 const char *errors /* error handling */
105 );
106
107/* Encodes a string object and returns the result as Python string
108 object.
109
110 If the codec returns an Unicode object, the object is converted
111 back to a string using the default encoding.
112
113 DEPRECATED - use PyString_AsEncodedObject() instead. */
114
Mark Hammond91a681d2002-08-12 07:21:58 +0000115PyAPI_FUNC(PyObject*) PyString_AsEncodedString(
Marc-André Lemburg3d1a1d72000-07-06 11:25:40 +0000116 PyObject *str, /* string object */
117 const char *encoding, /* encoding */
118 const char *errors /* error handling */
119 );
120
Marc-André Lemburg2d920412001-05-15 12:00:02 +0000121/* Decodes a string object and returns the result as Python
122 object. */
123
Mark Hammond91a681d2002-08-12 07:21:58 +0000124PyAPI_FUNC(PyObject*) PyString_AsDecodedObject(
Marc-André Lemburg2d920412001-05-15 12:00:02 +0000125 PyObject *str, /* string object */
126 const char *encoding, /* encoding */
127 const char *errors /* error handling */
128 );
129
130/* Decodes a string object and returns the result as Python string
131 object.
132
133 If the codec returns an Unicode object, the object is converted
134 back to a string using the default encoding.
135
136 DEPRECATED - use PyString_AsDecodedObject() instead. */
137
Mark Hammond91a681d2002-08-12 07:21:58 +0000138PyAPI_FUNC(PyObject*) PyString_AsDecodedString(
Marc-André Lemburg2d920412001-05-15 12:00:02 +0000139 PyObject *str, /* string object */
140 const char *encoding, /* encoding */
141 const char *errors /* error handling */
142 );
143
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000144/* Provides access to the internal data buffer and size of a string
145 object or the default encoded version of an Unicode object. Passing
146 NULL as *len parameter will force the string buffer to be
147 0-terminated (passing a string with embedded NULL characters will
148 cause an exception). */
149
Mark Hammond91a681d2002-08-12 07:21:58 +0000150PyAPI_FUNC(int) PyString_AsStringAndSize(
Marc-André Lemburgd1ba4432000-09-19 21:04:18 +0000151 register PyObject *obj, /* string or Unicode object */
152 register char **s, /* pointer to buffer variable */
153 register int *len /* pointer to length variable or NULL
154 (only possible for 0-terminated
155 strings) */
156 );
157
158
Guido van Rossuma3309961993-07-28 09:05:47 +0000159#ifdef __cplusplus
160}
161#endif
162#endif /* !Py_STRINGOBJECT_H */