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 | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 28 | This significantly speeds up dict lookups. */ |
Guido van Rossum | fdebf25 | 1996-07-30 16:42:03 +0000 | [diff] [blame] | 29 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 30 | typedef struct { |
Fred Drake | 3cf4d2b | 2000-07-09 00:55:06 +0000 | [diff] [blame] | 31 | PyObject_VAR_HEAD |
Fred Drake | 3cf4d2b | 2000-07-09 00:55:06 +0000 | [diff] [blame] | 32 | long ob_shash; |
Fred Drake | 3cf4d2b | 2000-07-09 00:55:06 +0000 | [diff] [blame] | 33 | char ob_sval[1]; |
Armin Rigo | 89a3946 | 2004-10-28 16:32:00 +0000 | [diff] [blame] | 34 | |
| 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 Rigo | 89a3946 | 2004-10-28 16:32:00 +0000 | [diff] [blame] | 39 | */ |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 40 | } PyStringObject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 41 | |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 42 | PyAPI_DATA(PyTypeObject) PyString_Type; |
Christian Heimes | a22e8bd | 2007-11-29 22:35:39 +0000 | [diff] [blame] | 43 | PyAPI_DATA(PyTypeObject) PyStringIter_Type; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 45 | #define PyString_Check(op) \ |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 46 | PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_STRING_SUBCLASS) |
| 47 | #define PyString_CheckExact(op) (Py_TYPE(op) == &PyString_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 48 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 49 | PyAPI_FUNC(PyObject *) PyString_FromStringAndSize(const char *, Py_ssize_t); |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 50 | PyAPI_FUNC(PyObject *) PyString_FromString(const char *); |
| 51 | PyAPI_FUNC(PyObject *) PyString_FromFormatV(const char*, va_list) |
Neil Schemenauer | 96aa0ac | 2002-09-15 14:09:54 +0000 | [diff] [blame] | 52 | Py_GCC_ATTRIBUTE((format(printf, 1, 0))); |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 53 | PyAPI_FUNC(PyObject *) PyString_FromFormat(const char*, ...) |
Neil Schemenauer | 96aa0ac | 2002-09-15 14:09:54 +0000 | [diff] [blame] | 54 | Py_GCC_ATTRIBUTE((format(printf, 1, 2))); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 55 | PyAPI_FUNC(Py_ssize_t) PyString_Size(PyObject *); |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 56 | PyAPI_FUNC(char *) PyString_AsString(PyObject *); |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 57 | PyAPI_FUNC(PyObject *) PyString_Repr(PyObject *, int); |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 58 | PyAPI_FUNC(void) PyString_Concat(PyObject **, PyObject *); |
| 59 | PyAPI_FUNC(void) PyString_ConcatAndDel(PyObject **, PyObject *); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 60 | PyAPI_FUNC(int) _PyString_Resize(PyObject **, Py_ssize_t); |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 61 | PyAPI_FUNC(PyObject *) PyString_Format(PyObject *, PyObject *); |
| 62 | PyAPI_FUNC(PyObject *) _PyString_FormatLong(PyObject*, int, int, |
Tim Peters | 38fd5b6 | 2000-09-21 05:43:11 +0000 | [diff] [blame] | 63 | int, char**, int*); |
Guido van Rossum | a2074f0 | 2007-10-26 19:34:40 +0000 | [diff] [blame] | 64 | PyAPI_FUNC(PyObject *) PyString_DecodeEscape(const char *, Py_ssize_t, |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 65 | const char *, Py_ssize_t, |
Martin v. Löwis | 8a8da79 | 2002-08-14 07:46:28 +0000 | [diff] [blame] | 66 | const char *); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 67 | |
| 68 | /* Macro, trading safety for speed */ |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 69 | #define PyString_AS_STRING(op) (assert(PyString_Check(op)), \ |
| 70 | (((PyStringObject *)(op))->ob_sval)) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 71 | #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] | 72 | |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 73 | /* _PyString_Join(sep, x) is like sep.join(x). sep must be PyStringObject*, |
| 74 | x must be an iterable object. */ |
Mark Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 75 | PyAPI_FUNC(PyObject *) _PyString_Join(PyObject *sep, PyObject *x); |
Tim Peters | a725959 | 2001-06-16 05:11:17 +0000 | [diff] [blame] | 76 | |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 77 | /* 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 Hammond | 91a681d | 2002-08-12 07:21:58 +0000 | [diff] [blame] | 82 | PyAPI_FUNC(int) PyString_AsStringAndSize( |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 83 | register PyObject *obj, /* string or Unicode object */ |
| 84 | register char **s, /* pointer to buffer variable */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 85 | register Py_ssize_t *len /* pointer to length variable or NULL |
Marc-André Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 86 | (only possible for 0-terminated |
| 87 | strings) */ |
| 88 | ); |
Guido van Rossum | a2074f0 | 2007-10-26 19:34:40 +0000 | [diff] [blame] | 89 | |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 90 | /* 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é Lemburg | d1ba443 | 2000-09-19 21:04:18 +0000 | [diff] [blame] | 96 | |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 97 | #ifdef __cplusplus |
| 98 | } |
| 99 | #endif |
| 100 | #endif /* !Py_STRINGOBJECT_H */ |