Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 1 | #ifndef Py_STRINGOBJECT_H |
| 2 | #define Py_STRINGOBJECT_H |
| 3 | #ifdef __cplusplus |
| 4 | extern "C" { |
| 5 | #endif |
| 6 | |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 7 | /*********************************************************** |
Guido van Rossum | 5799b52 | 1995-01-04 19:06:22 +0000 | [diff] [blame] | 8 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 9 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 10 | |
| 11 | All Rights Reserved |
| 12 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame^] | 13 | Copyright (c) 2000, BeOpen.com. |
| 14 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 15 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 16 | All rights reserved. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 17 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame^] | 18 | See the file "Misc/COPYRIGHT" for information on usage and |
| 19 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 20 | |
| 21 | ******************************************************************/ |
| 22 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 23 | /* String object interface */ |
| 24 | |
| 25 | /* |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 26 | Type PyStringObject represents a character string. An extra zero byte is |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 27 | reserved at the end to ensure it is zero-terminated, but a size is |
| 28 | present so strings with null bytes in them can be represented. This |
| 29 | is an immutable object type. |
| 30 | |
| 31 | There are functions to create new string objects, to test |
| 32 | an object for string-ness, and to get the |
| 33 | string value. The latter function returns a null pointer |
| 34 | if the object is not of the proper type. |
| 35 | There is a variant that takes an explicit size as well as a |
| 36 | variant that assumes a zero-terminated string. Note that none of the |
| 37 | functions should be applied to nil objects. |
| 38 | */ |
| 39 | |
Guido van Rossum | 1e6e9a2 | 1997-01-18 07:53:23 +0000 | [diff] [blame] | 40 | /* Two speedup hacks. Caching the hash saves recalculation of a |
| 41 | string's hash value. Interning strings (which requires hash |
| 42 | caching) tries to ensure that only one string object with a given |
| 43 | value exists, so equality tests are one pointer comparison. |
| 44 | Together, these can speed the interpreter up by as much as 20%. |
| 45 | Each costs the size of a long or pointer per string object. In |
| 46 | addition, interned strings live until the end of times. If you are |
| 47 | concerned about memory footprint, simply comment the #define out |
| 48 | here (and rebuild everything!). */ |
Guido van Rossum | fdebf25 | 1996-07-30 16:42:03 +0000 | [diff] [blame] | 49 | #define CACHE_HASH |
Guido van Rossum | 1e6e9a2 | 1997-01-18 07:53:23 +0000 | [diff] [blame] | 50 | #ifdef CACHE_HASH |
| 51 | #define INTERN_STRINGS |
| 52 | #endif |
Guido van Rossum | fdebf25 | 1996-07-30 16:42:03 +0000 | [diff] [blame] | 53 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 54 | typedef struct { |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 55 | PyObject_VAR_HEAD |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 56 | #ifdef CACHE_HASH |
| 57 | long ob_shash; |
| 58 | #endif |
Guido van Rossum | 1e6e9a2 | 1997-01-18 07:53:23 +0000 | [diff] [blame] | 59 | #ifdef INTERN_STRINGS |
| 60 | PyObject *ob_sinterned; |
| 61 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 62 | char ob_sval[1]; |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 63 | } PyStringObject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 64 | |
Guido van Rossum | 051ab12 | 1995-02-27 10:17:52 +0000 | [diff] [blame] | 65 | extern DL_IMPORT(PyTypeObject) PyString_Type; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 66 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 67 | #define PyString_Check(op) ((op)->ob_type == &PyString_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 68 | |
Guido van Rossum | 43466ec | 1998-12-04 18:48:25 +0000 | [diff] [blame] | 69 | extern DL_IMPORT(PyObject *) PyString_FromStringAndSize Py_PROTO((const char *, int)); |
| 70 | extern DL_IMPORT(PyObject *) PyString_FromString Py_PROTO((const char *)); |
| 71 | extern DL_IMPORT(int) PyString_Size Py_PROTO((PyObject *)); |
| 72 | extern DL_IMPORT(char *) PyString_AsString Py_PROTO((PyObject *)); |
| 73 | extern DL_IMPORT(void) PyString_Concat Py_PROTO((PyObject **, PyObject *)); |
| 74 | extern DL_IMPORT(void) PyString_ConcatAndDel Py_PROTO((PyObject **, PyObject *)); |
| 75 | extern DL_IMPORT(int) _PyString_Resize Py_PROTO((PyObject **, int)); |
| 76 | extern DL_IMPORT(PyObject *) PyString_Format Py_PROTO((PyObject *, PyObject *)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 77 | |
Guido van Rossum | 1e6e9a2 | 1997-01-18 07:53:23 +0000 | [diff] [blame] | 78 | #ifdef INTERN_STRINGS |
Guido van Rossum | 43466ec | 1998-12-04 18:48:25 +0000 | [diff] [blame] | 79 | extern DL_IMPORT(void) PyString_InternInPlace Py_PROTO((PyObject **)); |
| 80 | extern DL_IMPORT(PyObject *) PyString_InternFromString Py_PROTO((const char *)); |
Guido van Rossum | 1e6e9a2 | 1997-01-18 07:53:23 +0000 | [diff] [blame] | 81 | #else |
| 82 | #define PyString_InternInPlace(p) |
| 83 | #define PyString_InternFromString(cp) PyString_FromString(cp) |
| 84 | #endif |
| 85 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 86 | /* Macro, trading safety for speed */ |
Barry Warsaw | accfb84 | 1997-01-06 22:42:50 +0000 | [diff] [blame] | 87 | #define PyString_AS_STRING(op) (((PyStringObject *)(op))->ob_sval) |
| 88 | #define PyString_GET_SIZE(op) (((PyStringObject *)(op))->ob_size) |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 89 | |
| 90 | #ifdef __cplusplus |
| 91 | } |
| 92 | #endif |
| 93 | #endif /* !Py_STRINGOBJECT_H */ |