Mercurial > cpython-lru_cache-weakref
annotate Include/stringobject.h @ 13778:f6e4a9dd67cd legacy-trunk
Rename the "parser" package to "parsers" to be compatible with the PyXML
project.
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Mon, 31 Jul 2000 23:59:54 +0000 |
| parents | 091ca6b9348f |
| children | 6f6f965c5e2d |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
13255
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
9586
diff
changeset
|
2 Copyright (c) 2000, BeOpen.com. |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
9586
diff
changeset
|
3 Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
9586
diff
changeset
|
4 Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
9586
diff
changeset
|
5 All rights reserved. |
| 145 | 6 |
|
13255
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
9586
diff
changeset
|
7 See the file "Misc/COPYRIGHT" for information on usage and |
|
916fb024ce94
Change copyright notice.
Guido van Rossum <guido@python.org>
parents:
9586
diff
changeset
|
8 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 145 | 9 ******************************************************************/ |
| 10 | |
| 6 | 11 /* String object interface */ |
| 12 | |
|
13445
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13376
diff
changeset
|
13 #ifndef Py_STRINGOBJECT_H |
|
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13376
diff
changeset
|
14 #define Py_STRINGOBJECT_H |
|
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13376
diff
changeset
|
15 #ifdef __cplusplus |
|
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13376
diff
changeset
|
16 extern "C" { |
|
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13376
diff
changeset
|
17 #endif |
|
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13376
diff
changeset
|
18 |
| 6 | 19 /* |
|
2129
05360cd616ae
The great renaming, phase two: all header files have been updated to
Guido van Rossum <guido@python.org>
parents:
2058
diff
changeset
|
20 Type PyStringObject represents a character string. An extra zero byte is |
| 6 | 21 reserved at the end to ensure it is zero-terminated, but a size is |
| 22 present so strings with null bytes in them can be represented. This | |
| 23 is an immutable object type. | |
| 24 | |
| 25 There are functions to create new string objects, to test | |
| 26 an object for string-ness, and to get the | |
| 27 string value. The latter function returns a null pointer | |
| 28 if the object is not of the proper type. | |
| 29 There is a variant that takes an explicit size as well as a | |
| 30 variant that assumes a zero-terminated string. Note that none of the | |
| 31 functions should be applied to nil objects. | |
| 32 */ | |
| 33 | |
|
5003
65aace2012e5
Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents:
4897
diff
changeset
|
34 /* Two speedup hacks. Caching the hash saves recalculation of a |
|
65aace2012e5
Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents:
4897
diff
changeset
|
35 string's hash value. Interning strings (which requires hash |
|
65aace2012e5
Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents:
4897
diff
changeset
|
36 caching) tries to ensure that only one string object with a given |
|
65aace2012e5
Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents:
4897
diff
changeset
|
37 value exists, so equality tests are one pointer comparison. |
|
65aace2012e5
Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents:
4897
diff
changeset
|
38 Together, these can speed the interpreter up by as much as 20%. |
|
65aace2012e5
Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents:
4897
diff
changeset
|
39 Each costs the size of a long or pointer per string object. In |
|
65aace2012e5
Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents:
4897
diff
changeset
|
40 addition, interned strings live until the end of times. If you are |
|
65aace2012e5
Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents:
4897
diff
changeset
|
41 concerned about memory footprint, simply comment the #define out |
|
65aace2012e5
Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents:
4897
diff
changeset
|
42 here (and rebuild everything!). */ |
|
3833
f0efff28a24d
Turn on CACHE_HASH, for 2% speedier dict lookups
Guido van Rossum <guido@python.org>
parents:
2436
diff
changeset
|
43 #define CACHE_HASH |
|
5003
65aace2012e5
Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents:
4897
diff
changeset
|
44 #ifdef CACHE_HASH |
|
65aace2012e5
Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents:
4897
diff
changeset
|
45 #define INTERN_STRINGS |
|
65aace2012e5
Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents:
4897
diff
changeset
|
46 #endif |
|
3833
f0efff28a24d
Turn on CACHE_HASH, for 2% speedier dict lookups
Guido van Rossum <guido@python.org>
parents:
2436
diff
changeset
|
47 |
| 6 | 48 typedef struct { |
|
13445
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13376
diff
changeset
|
49 PyObject_VAR_HEAD |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
50 #ifdef CACHE_HASH |
|
13445
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13376
diff
changeset
|
51 long ob_shash; |
|
1356
043da19302ef
Several optimizations and speed improvements.
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
52 #endif |
|
5003
65aace2012e5
Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents:
4897
diff
changeset
|
53 #ifdef INTERN_STRINGS |
|
13445
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13376
diff
changeset
|
54 PyObject *ob_sinterned; |
|
5003
65aace2012e5
Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents:
4897
diff
changeset
|
55 #endif |
|
13445
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13376
diff
changeset
|
56 char ob_sval[1]; |
|
2129
05360cd616ae
The great renaming, phase two: all header files have been updated to
Guido van Rossum <guido@python.org>
parents:
2058
diff
changeset
|
57 } PyStringObject; |
| 6 | 58 |
|
2436
fa98dda07877
make the type a parameter of the DL_IMPORT macro, for Borland C
Guido van Rossum <guido@python.org>
parents:
2146
diff
changeset
|
59 extern DL_IMPORT(PyTypeObject) PyString_Type; |
| 6 | 60 |
|
2129
05360cd616ae
The great renaming, phase two: all header files have been updated to
Guido van Rossum <guido@python.org>
parents:
2058
diff
changeset
|
61 #define PyString_Check(op) ((op)->ob_type == &PyString_Type) |
| 6 | 62 |
|
13445
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13376
diff
changeset
|
63 extern DL_IMPORT(PyObject *) PyString_FromStringAndSize(const char *, int); |
|
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13376
diff
changeset
|
64 extern DL_IMPORT(PyObject *) PyString_FromString(const char *); |
|
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13376
diff
changeset
|
65 extern DL_IMPORT(int) PyString_Size(PyObject *); |
|
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13376
diff
changeset
|
66 extern DL_IMPORT(char *) PyString_AsString(PyObject *); |
|
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13376
diff
changeset
|
67 extern DL_IMPORT(void) PyString_Concat(PyObject **, PyObject *); |
|
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13376
diff
changeset
|
68 extern DL_IMPORT(void) PyString_ConcatAndDel(PyObject **, PyObject *); |
|
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13376
diff
changeset
|
69 extern DL_IMPORT(int) _PyString_Resize(PyObject **, int); |
|
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13376
diff
changeset
|
70 extern DL_IMPORT(PyObject *) PyString_Format(PyObject *, PyObject *); |
| 6 | 71 |
|
5003
65aace2012e5
Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents:
4897
diff
changeset
|
72 #ifdef INTERN_STRINGS |
|
13445
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13376
diff
changeset
|
73 extern DL_IMPORT(void) PyString_InternInPlace(PyObject **); |
|
091ca6b9348f
ANSI-fication and Py_PROTO extermination.
Fred Drake <fdrake@acm.org>
parents:
13376
diff
changeset
|
74 extern DL_IMPORT(PyObject *) PyString_InternFromString(const char *); |
|
5003
65aace2012e5
Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents:
4897
diff
changeset
|
75 #else |
|
65aace2012e5
Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents:
4897
diff
changeset
|
76 #define PyString_InternInPlace(p) |
|
65aace2012e5
Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents:
4897
diff
changeset
|
77 #define PyString_InternFromString(cp) PyString_FromString(cp) |
|
65aace2012e5
Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents:
4897
diff
changeset
|
78 #endif |
|
65aace2012e5
Two speedup hacks. Caching the hash saves recalculation of a string's
Guido van Rossum <guido@python.org>
parents:
4897
diff
changeset
|
79 |
| 6 | 80 /* Macro, trading safety for speed */ |
|
4897
90c21dde4284
added PyString_GET_SIZE macro
Barry Warsaw <barry@python.org>
parents:
4654
diff
changeset
|
81 #define PyString_AS_STRING(op) (((PyStringObject *)(op))->ob_sval) |
|
90c21dde4284
added PyString_GET_SIZE macro
Barry Warsaw <barry@python.org>
parents:
4654
diff
changeset
|
82 #define PyString_GET_SIZE(op) (((PyStringObject *)(op))->ob_size) |
|
1329
519c25039c6a
* Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
83 |
|
13376
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
84 /* --- Generic Codecs ----------------------------------------------------- */ |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
85 |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
86 /* Create a string object by decoding the encoded string s of the |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
87 given size. */ |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
88 |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
89 extern DL_IMPORT(PyObject*) PyString_Decode( |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
90 const char *s, /* encoded string */ |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
91 int size, /* size of buffer */ |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
92 const char *encoding, /* encoding */ |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
93 const char *errors /* error handling */ |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
94 ); |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
95 |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
96 /* Encodes a char buffer of the given size and returns a |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
97 Python string object. */ |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
98 |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
99 extern DL_IMPORT(PyObject*) PyString_Encode( |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
100 const char *s, /* string char buffer */ |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
101 int size, /* number of chars to encode */ |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
102 const char *encoding, /* encoding */ |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
103 const char *errors /* error handling */ |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
104 ); |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
105 |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
106 /* Encodes a string object and returns the result as Python string |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
107 object. */ |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
108 |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
109 extern DL_IMPORT(PyObject*) PyString_AsEncodedString( |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
110 PyObject *str, /* string object */ |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
111 const char *encoding, /* encoding */ |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
112 const char *errors /* error handling */ |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
113 ); |
|
bfe22cf6e770
Added prototypes for the new codec APIs for strings. These APIs
Marc-André Lemburg <mal@egenix.com>
parents:
13256
diff
changeset
|
114 |
|
1329
519c25039c6a
* Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
115 #ifdef __cplusplus |
|
519c25039c6a
* Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
116 } |
|
519c25039c6a
* Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
117 #endif |
|
519c25039c6a
* Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents:
1252
diff
changeset
|
118 #endif /* !Py_STRINGOBJECT_H */ |
