Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* String object interface */ |
| 3 | |
Fred Drake | 3cf4d2b | 2000-07-09 00:55:06 +0000 | [diff] [blame] | 4 | #ifndef Py_STRINGOBJECT_H |
| 5 | #define Py_STRINGOBJECT_H |
| 6 | #ifdef __cplusplus |
| 7 | extern "C" { |
| 8 | #endif |
| 9 | |
Barry Warsaw | dadace0 | 2001-08-24 18:32:06 +0000 | [diff] [blame] | 10 | #include <stdarg.h> |
| 11 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 12 | /* |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 13 | Type PyStringObject represents a character string. An extra zero byte is |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 14 | reserved at the end to ensure it is zero-terminated, but a size is |
| 15 | present so strings with null bytes in them can be represented. This |
| 16 | is an immutable object type. |
| 17 | |
| 18 | There are functions to create new string objects, to test |
| 19 | an object for string-ness, and to get the |
| 20 | string value. The latter function returns a null pointer |
| 21 | if the object is not of the proper type. |
| 22 | There is a variant that takes an explicit size as well as a |
| 23 | variant that assumes a zero-terminated string. Note that none of the |
| 24 | functions should be applied to nil objects. |
| 25 | */ |
| 26 | |
Tim Peters | 1f7df35 | 2002-03-29 03:29:08 +0000 | [diff] [blame] | 27 | /* Caching the hash (ob_shash) saves recalculation of a string's hash value. |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 28 | Interning strings (ob_sstate) tries to ensure that only one string |
Tim Peters | 1f7df35 | 2002-03-29 03:29:08 +0000 | [diff] [blame] | 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" |
Georg Brandl | 66a796e | 2006-12-19 20:50:34 +0000 | [diff] [blame] | 31 | Python identifiers, although the sys.intern() function can be used to force |
Tim Peters | 1f7df35 | 2002-03-29 03:29:08 +0000 | [diff] [blame] | 32 | interning of any string. |
| 33 | Together, these sped the interpreter by up to 20%. */ |
Guido van Rossum | fdebf25 | 1996-07-30 16:42:03 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 35 | typedef struct { |
Fred Drake | 3cf4d2b | 2000-07-09 00:55:06 +0000 | [diff] [blame] | 36 | PyObject_VAR_HEAD |
Fred Drake | 3cf4d2b | 2000-07-09 00:55:06 +0000 | [diff] [blame] | 37 | long ob_shash; |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 38 | int ob_sstate; |
Fred Drake | 3cf4d2b | 2000-07-09 00:55:06 +0000 | [diff] [blame] | 39 | char ob_sval[1]; |
Armin Rigo | 89a3946 | 2004-10-28 16:32:00 +0000 | [diff] [blame] | 40 | |
| 41 | /* Invariants: |
| 42 | * ob_sval contains space for 'ob_size+1' elements. |
| 43 | * ob_sval[ob_size] == 0. |
| 44 | * ob_shash is the hash of the string or -1 if not computed yet. |
| 45 | * ob_sstate != 0 iff the string object is in stringobject.c's |
| 46 | * 'interned' dictionary; in this case the two references |
| 47 | * from 'interned' to this object are *not counted* in ob_refcnt. |
| 48 | */ |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 49 | } PyStringObject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 50 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 51 | PyAPI_DATA(PyTypeObject) PyBaseString_Type; |
| 52 | PyAPI_DATA(PyTypeObject) PyString_Type; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 53 | |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 54 | #define PyString_Check(op) \ |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 55 | PyType_FastSubclass(Py_Type(op), Py_TPFLAGS_STRING_SUBCLASS) |
| 56 | #define PyString_CheckExact(op) (Py_Type(op) == &PyString_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 57 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 58 | PyAPI_FUNC(PyObject *) PyString_FromStringAndSize(const char *, Py_ssize_t); |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 59 | PyAPI_FUNC(PyObject *) PyString_FromString(const char *); |
| 60 | PyAPI_FUNC(PyObject *) PyString_FromFormatV(const char*, va_list) |
Neil Schemenauer | 96aa0ac | 2002-09-15 14:09:54 +0000 | [diff] [blame] | 61 | Py_GCC_ATTRIBUTE((format(printf, 1, 0))); |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 62 | PyAPI_FUNC(PyObject *) PyString_FromFormat(const char*, ...) |
Neil Schemenauer | 96aa0ac | 2002-09-15 14:09:54 +0000 | [diff] [blame] | 63 | Py_GCC_ATTRIBUTE((format(printf, 1, 2))); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 64 | PyAPI_FUNC(Py_ssize_t) PyString_Size(PyObject *); |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 65 | PyAPI_FUNC(char *) PyString_AsString(PyObject *); |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 66 | PyAPI_FUNC(PyObject *) PyString_Repr(PyObject *, int); |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 67 | PyAPI_FUNC(void) PyString_Concat(PyObject **, PyObject *); |
| 68 | PyAPI_FUNC(void) PyString_ConcatAndDel(PyObject **, PyObject *); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 69 | PyAPI_FUNC(int) _PyString_Resize(PyObject **, Py_ssize_t); |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 70 | PyAPI_FUNC(int) _PyString_Eq(PyObject *, PyObject*); |
| 71 | PyAPI_FUNC(PyObject *) PyString_Format(PyObject *, PyObject *); |
| 72 | PyAPI_FUNC(PyObject *) _PyString_FormatLong(PyObject*, int, int, |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 73 | int, char**, int*); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 74 | PyAPI_FUNC(PyObject *) PyString_DecodeEscape(const char *, Py_ssize_t, |
| 75 | const char *, Py_ssize_t, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 76 | const char *); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 77 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 78 | PyAPI_FUNC(void) PyString_InternInPlace(PyObject **); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 79 | PyAPI_FUNC(void) PyString_InternImmortal(PyObject **); |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 80 | PyAPI_FUNC(PyObject *) PyString_InternFromString(const char *); |
| 81 | PyAPI_FUNC(void) _Py_ReleaseInternedStrings(void); |
Guido van Rossum | 1e6e9a2 | 1997-01-18 07:53:23 +0000 | [diff] [blame] | 82 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 83 | /* Use only if you know it's a string */ |
| 84 | #define PyString_CHECK_INTERNED(op) (((PyStringObject *)(op))->ob_sstate) |
| 85 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 86 | /* Macro, trading safety for speed */ |
Martin v. Löwis | 5b22213 | 2007-06-10 09:51:05 +0000 | [diff] [blame] | 87 | #define PyString_AS_STRING(op) (assert(PyString_Check(op)),(((PyStringObject *)(op))->ob_sval)) |
Martin v. Löwis | 5d7428b | 2007-07-21 18:47:48 +0000 | [diff] [blame] | 88 | #define PyString_GET_SIZE(op) (assert(PyString_Check(op)),Py_Size(op)) |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 89 | |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 90 | /* _PyString_Join(sep, x) is like sep.join(x). sep must be PyStringObject*, |
| 91 | x must be an iterable object. */ |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 92 | PyAPI_FUNC(PyObject *) _PyString_Join(PyObject *sep, PyObject *x); |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 93 | |
Marc-André Lemburg | 3d1a1d7 | 2000-07-06 11:25:40 +0000 | [diff] [blame] | 94 | /* --- Generic Codecs ----------------------------------------------------- */ |
| 95 | |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 96 | /* Create an object by decoding the encoded string s of the |
Marc-André Lemburg | 3d1a1d7 | 2000-07-06 11:25:40 +0000 | [diff] [blame] | 97 | given size. */ |
| 98 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 99 | PyAPI_FUNC(PyObject*) PyString_Decode( |
Marc-André Lemburg | 3d1a1d7 | 2000-07-06 11:25:40 +0000 | [diff] [blame] | 100 | const char *s, /* encoded string */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 101 | Py_ssize_t size, /* size of buffer */ |
Marc-André Lemburg | 3d1a1d7 | 2000-07-06 11:25:40 +0000 | [diff] [blame] | 102 | const char *encoding, /* encoding */ |
| 103 | const char *errors /* error handling */ |
| 104 | ); |
| 105 | |
| 106 | /* Encodes a char buffer of the given size and returns a |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 107 | Python object. */ |
Marc-André Lemburg | 3d1a1d7 | 2000-07-06 11:25:40 +0000 | [diff] [blame] | 108 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 109 | PyAPI_FUNC(PyObject*) PyString_Encode( |
Marc-André Lemburg | 3d1a1d7 | 2000-07-06 11:25:40 +0000 | [diff] [blame] | 110 | const char *s, /* string char buffer */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 111 | Py_ssize_t size, /* number of chars to encode */ |
Marc-André Lemburg | 3d1a1d7 | 2000-07-06 11:25:40 +0000 | [diff] [blame] | 112 | const char *encoding, /* encoding */ |
| 113 | const char *errors /* error handling */ |
| 114 | ); |
| 115 | |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 116 | /* Encodes a string object and returns the result as Python |
Marc-André Lemburg | 3d1a1d7 | 2000-07-06 11:25:40 +0000 | [diff] [blame] | 117 | object. */ |
| 118 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 119 | PyAPI_FUNC(PyObject*) PyString_AsEncodedObject( |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 120 | PyObject *str, /* string object */ |
| 121 | const char *encoding, /* encoding */ |
| 122 | const char *errors /* error handling */ |
| 123 | ); |
| 124 | |
| 125 | /* Encodes a string object and returns the result as Python string |
| 126 | object. |
| 127 | |
| 128 | If the codec returns an Unicode object, the object is converted |
| 129 | back to a string using the default encoding. |
| 130 | |
| 131 | DEPRECATED - use PyString_AsEncodedObject() instead. */ |
| 132 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 133 | PyAPI_FUNC(PyObject*) PyString_AsEncodedString( |
Marc-André Lemburg | 3d1a1d7 | 2000-07-06 11:25:40 +0000 | [diff] [blame] | 134 | PyObject *str, /* string object */ |
| 135 | const char *encoding, /* encoding */ |
| 136 | const char *errors /* error handling */ |
| 137 | ); |
| 138 | |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 139 | /* Decodes a string object and returns the result as Python |
| 140 | object. */ |
| 141 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 142 | PyAPI_FUNC(PyObject*) PyString_AsDecodedObject( |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 143 | PyObject *str, /* string object */ |
| 144 | const char *encoding, /* encoding */ |
| 145 | const char *errors /* error handling */ |
| 146 | ); |
| 147 | |
| 148 | /* Decodes a string object and returns the result as Python string |
| 149 | object. |
| 150 | |
| 151 | If the codec returns an Unicode object, the object is converted |
| 152 | back to a string using the default encoding. |
| 153 | |
| 154 | DEPRECATED - use PyString_AsDecodedObject() instead. */ |
| 155 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 156 | PyAPI_FUNC(PyObject*) PyString_AsDecodedString( |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 157 | PyObject *str, /* string object */ |
| 158 | const char *encoding, /* encoding */ |
| 159 | const char *errors /* error handling */ |
| 160 | ); |
| 161 | |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 162 | /* Provides access to the internal data buffer and size of a string |
| 163 | object or the default encoded version of an Unicode object. Passing |
| 164 | NULL as *len parameter will force the string buffer to be |
| 165 | 0-terminated (passing a string with embedded NULL characters will |
| 166 | cause an exception). */ |
| 167 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 168 | PyAPI_FUNC(int) PyString_AsStringAndSize( |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 169 | register PyObject *obj, /* string or Unicode object */ |
| 170 | register char **s, /* pointer to buffer variable */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 171 | register Py_ssize_t *len /* pointer to length variable or NULL |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 172 | (only possible for 0-terminated |
| 173 | strings) */ |
| 174 | ); |
| 175 | |
| 176 | |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 177 | #ifdef __cplusplus |
| 178 | } |
| 179 | #endif |
| 180 | #endif /* !Py_STRINGOBJECT_H */ |