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 | |
Guido van Rossum | 1e6e9a2 | 1997-01-18 07:53:23 +0000 | [diff] [blame] | 27 | /* Two speedup hacks. Caching the hash saves recalculation of a |
| 28 | string's hash value. Interning strings (which requires hash |
| 29 | caching) tries to ensure that only one string object with a given |
| 30 | value exists, so equality tests are one pointer comparison. |
| 31 | Together, these can speed the interpreter up by as much as 20%. |
| 32 | Each costs the size of a long or pointer per string object. In |
| 33 | addition, interned strings live until the end of times. If you are |
| 34 | concerned about memory footprint, simply comment the #define out |
| 35 | here (and rebuild everything!). */ |
Guido van Rossum | fdebf25 | 1996-07-30 16:42:03 +0000 | [diff] [blame] | 36 | #define CACHE_HASH |
Guido van Rossum | 1e6e9a2 | 1997-01-18 07:53:23 +0000 | [diff] [blame] | 37 | #ifdef CACHE_HASH |
| 38 | #define INTERN_STRINGS |
| 39 | #endif |
Guido van Rossum | fdebf25 | 1996-07-30 16:42:03 +0000 | [diff] [blame] | 40 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 41 | typedef struct { |
Fred Drake | 3cf4d2b | 2000-07-09 00:55:06 +0000 | [diff] [blame] | 42 | PyObject_VAR_HEAD |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 43 | #ifdef CACHE_HASH |
Fred Drake | 3cf4d2b | 2000-07-09 00:55:06 +0000 | [diff] [blame] | 44 | long ob_shash; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 45 | #endif |
Guido van Rossum | 1e6e9a2 | 1997-01-18 07:53:23 +0000 | [diff] [blame] | 46 | #ifdef INTERN_STRINGS |
Fred Drake | 3cf4d2b | 2000-07-09 00:55:06 +0000 | [diff] [blame] | 47 | PyObject *ob_sinterned; |
Guido van Rossum | 1e6e9a2 | 1997-01-18 07:53:23 +0000 | [diff] [blame] | 48 | #endif |
Fred Drake | 3cf4d2b | 2000-07-09 00:55:06 +0000 | [diff] [blame] | 49 | char ob_sval[1]; |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 50 | } PyStringObject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 51 | |
Guido van Rossum | 051ab12 | 1995-02-27 10:17:52 +0000 | [diff] [blame] | 52 | extern DL_IMPORT(PyTypeObject) PyString_Type; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | 5eef77a | 2001-08-30 03:08:07 +0000 | [diff] [blame] | 54 | #define PyString_Check(op) PyObject_TypeCheck(op, &PyString_Type) |
Tim Peters | 5a49ade | 2001-09-11 01:41:59 +0000 | [diff] [blame] | 55 | #define PyString_CheckExact(op) ((op)->ob_type == &PyString_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 56 | |
Fred Drake | 3cf4d2b | 2000-07-09 00:55:06 +0000 | [diff] [blame] | 57 | extern DL_IMPORT(PyObject *) PyString_FromStringAndSize(const char *, int); |
| 58 | extern DL_IMPORT(PyObject *) PyString_FromString(const char *); |
Neil Schemenauer | 90b6890 | 2001-10-23 02:21:22 +0000 | [diff] [blame] | 59 | extern DL_IMPORT(PyObject *) PyString_FromFormatV(const char*, va_list) |
| 60 | __attribute__((format(printf, 1, 0))); |
| 61 | extern DL_IMPORT(PyObject *) PyString_FromFormat(const char*, ...) |
| 62 | __attribute__((format(printf, 1, 2))); |
Fred Drake | 3cf4d2b | 2000-07-09 00:55:06 +0000 | [diff] [blame] | 63 | extern DL_IMPORT(int) PyString_Size(PyObject *); |
| 64 | extern DL_IMPORT(char *) PyString_AsString(PyObject *); |
| 65 | extern DL_IMPORT(void) PyString_Concat(PyObject **, PyObject *); |
| 66 | extern DL_IMPORT(void) PyString_ConcatAndDel(PyObject **, PyObject *); |
| 67 | extern DL_IMPORT(int) _PyString_Resize(PyObject **, int); |
Martin v. Löwis | cd35306 | 2001-05-24 16:56:35 +0000 | [diff] [blame] | 68 | extern DL_IMPORT(int) _PyString_Eq(PyObject *, PyObject*); |
Fred Drake | 3cf4d2b | 2000-07-09 00:55:06 +0000 | [diff] [blame] | 69 | extern DL_IMPORT(PyObject *) PyString_Format(PyObject *, PyObject *); |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 70 | extern DL_IMPORT(PyObject *) _PyString_FormatLong(PyObject*, int, int, |
| 71 | int, char**, int*); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 72 | |
Guido van Rossum | 1e6e9a2 | 1997-01-18 07:53:23 +0000 | [diff] [blame] | 73 | #ifdef INTERN_STRINGS |
Fred Drake | 3cf4d2b | 2000-07-09 00:55:06 +0000 | [diff] [blame] | 74 | extern DL_IMPORT(void) PyString_InternInPlace(PyObject **); |
| 75 | extern DL_IMPORT(PyObject *) PyString_InternFromString(const char *); |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 76 | extern DL_IMPORT(void) _Py_ReleaseInternedStrings(void); |
Guido van Rossum | 1e6e9a2 | 1997-01-18 07:53:23 +0000 | [diff] [blame] | 77 | #else |
| 78 | #define PyString_InternInPlace(p) |
| 79 | #define PyString_InternFromString(cp) PyString_FromString(cp) |
Barry Warsaw | a903ad98 | 2001-02-23 16:40:48 +0000 | [diff] [blame] | 80 | #define _Py_ReleaseInternedStrings() |
Guido van Rossum | 1e6e9a2 | 1997-01-18 07:53:23 +0000 | [diff] [blame] | 81 | #endif |
| 82 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 83 | /* Macro, trading safety for speed */ |
Barry Warsaw | accfb84 | 1997-01-06 22:42:50 +0000 | [diff] [blame] | 84 | #define PyString_AS_STRING(op) (((PyStringObject *)(op))->ob_sval) |
| 85 | #define PyString_GET_SIZE(op) (((PyStringObject *)(op))->ob_size) |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 86 | |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 87 | /* _PyString_Join(sep, x) is like sep.join(x). sep must be PyStringObject*, |
| 88 | x must be an iterable object. */ |
| 89 | extern DL_IMPORT(PyObject *) _PyString_Join(PyObject *sep, PyObject *x); |
| 90 | |
Marc-André Lemburg | 3d1a1d7 | 2000-07-06 11:25:40 +0000 | [diff] [blame] | 91 | /* --- Generic Codecs ----------------------------------------------------- */ |
| 92 | |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 93 | /* Create an object by decoding the encoded string s of the |
Marc-André Lemburg | 3d1a1d7 | 2000-07-06 11:25:40 +0000 | [diff] [blame] | 94 | given size. */ |
| 95 | |
| 96 | extern DL_IMPORT(PyObject*) PyString_Decode( |
| 97 | const char *s, /* encoded string */ |
| 98 | int size, /* size of buffer */ |
| 99 | const char *encoding, /* encoding */ |
| 100 | const char *errors /* error handling */ |
| 101 | ); |
| 102 | |
| 103 | /* Encodes a char buffer of the given size and returns a |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 104 | Python object. */ |
Marc-André Lemburg | 3d1a1d7 | 2000-07-06 11:25:40 +0000 | [diff] [blame] | 105 | |
| 106 | extern DL_IMPORT(PyObject*) PyString_Encode( |
| 107 | const char *s, /* string char buffer */ |
| 108 | int size, /* number of chars to encode */ |
| 109 | const char *encoding, /* encoding */ |
| 110 | const char *errors /* error handling */ |
| 111 | ); |
| 112 | |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 113 | /* Encodes a string object and returns the result as Python |
Marc-André Lemburg | 3d1a1d7 | 2000-07-06 11:25:40 +0000 | [diff] [blame] | 114 | object. */ |
| 115 | |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 116 | extern DL_IMPORT(PyObject*) PyString_AsEncodedObject( |
| 117 | PyObject *str, /* string object */ |
| 118 | const char *encoding, /* encoding */ |
| 119 | const char *errors /* error handling */ |
| 120 | ); |
| 121 | |
| 122 | /* Encodes a string object and returns the result as Python string |
| 123 | object. |
| 124 | |
| 125 | If the codec returns an Unicode object, the object is converted |
| 126 | back to a string using the default encoding. |
| 127 | |
| 128 | DEPRECATED - use PyString_AsEncodedObject() instead. */ |
| 129 | |
Marc-André Lemburg | 3d1a1d7 | 2000-07-06 11:25:40 +0000 | [diff] [blame] | 130 | extern DL_IMPORT(PyObject*) PyString_AsEncodedString( |
| 131 | PyObject *str, /* string object */ |
| 132 | const char *encoding, /* encoding */ |
| 133 | const char *errors /* error handling */ |
| 134 | ); |
| 135 | |
Marc-André Lemburg | 2d92041 | 2001-05-15 12:00:02 +0000 | [diff] [blame] | 136 | /* Decodes a string object and returns the result as Python |
| 137 | object. */ |
| 138 | |
| 139 | extern DL_IMPORT(PyObject*) PyString_AsDecodedObject( |
| 140 | PyObject *str, /* string object */ |
| 141 | const char *encoding, /* encoding */ |
| 142 | const char *errors /* error handling */ |
| 143 | ); |
| 144 | |
| 145 | /* Decodes a string object and returns the result as Python string |
| 146 | object. |
| 147 | |
| 148 | If the codec returns an Unicode object, the object is converted |
| 149 | back to a string using the default encoding. |
| 150 | |
| 151 | DEPRECATED - use PyString_AsDecodedObject() instead. */ |
| 152 | |
| 153 | extern DL_IMPORT(PyObject*) PyString_AsDecodedString( |
| 154 | PyObject *str, /* string object */ |
| 155 | const char *encoding, /* encoding */ |
| 156 | const char *errors /* error handling */ |
| 157 | ); |
| 158 | |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 159 | /* Provides access to the internal data buffer and size of a string |
| 160 | object or the default encoded version of an Unicode object. Passing |
| 161 | NULL as *len parameter will force the string buffer to be |
| 162 | 0-terminated (passing a string with embedded NULL characters will |
| 163 | cause an exception). */ |
| 164 | |
| 165 | extern DL_IMPORT(int) PyString_AsStringAndSize( |
| 166 | register PyObject *obj, /* string or Unicode object */ |
| 167 | register char **s, /* pointer to buffer variable */ |
| 168 | register int *len /* pointer to length variable or NULL |
| 169 | (only possible for 0-terminated |
| 170 | strings) */ |
| 171 | ); |
| 172 | |
| 173 | |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 174 | #ifdef __cplusplus |
| 175 | } |
| 176 | #endif |
| 177 | #endif /* !Py_STRINGOBJECT_H */ |