source: trunk/essentials/dev-lang/python/Include/stringobject.h@ 3326

Last change on this file since 3326 was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 6.5 KB
Line 
1
2/* String object interface */
3
4#ifndef Py_STRINGOBJECT_H
5#define Py_STRINGOBJECT_H
6#ifdef __cplusplus
7extern "C" {
8#endif
9
10#include <stdarg.h>
11
12/*
13Type PyStringObject represents a character string. An extra zero byte is
14reserved at the end to ensure it is zero-terminated, but a size is
15present so strings with null bytes in them can be represented. This
16is an immutable object type.
17
18There are functions to create new string objects, to test
19an object for string-ness, and to get the
20string value. The latter function returns a null pointer
21if the object is not of the proper type.
22There is a variant that takes an explicit size as well as a
23variant that assumes a zero-terminated string. Note that none of the
24functions should be applied to nil objects.
25*/
26
27/* Caching the hash (ob_shash) saves recalculation of a string's hash value.
28 Interning strings (ob_sstate) tries to ensure that only one string
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"
31 Python identifiers, although the intern() builtin can be used to force
32 interning of any string.
33 Together, these sped the interpreter by up to 20%. */
34
35typedef struct {
36 PyObject_VAR_HEAD
37 long ob_shash;
38 int ob_sstate;
39 char ob_sval[1];
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 */
49} PyStringObject;
50
51#define SSTATE_NOT_INTERNED 0
52#define SSTATE_INTERNED_MORTAL 1
53#define SSTATE_INTERNED_IMMORTAL 2
54
55PyAPI_DATA(PyTypeObject) PyBaseString_Type;
56PyAPI_DATA(PyTypeObject) PyString_Type;
57
58#define PyString_Check(op) PyObject_TypeCheck(op, &PyString_Type)
59#define PyString_CheckExact(op) ((op)->ob_type == &PyString_Type)
60
61PyAPI_FUNC(PyObject *) PyString_FromStringAndSize(const char *, Py_ssize_t);
62PyAPI_FUNC(PyObject *) PyString_FromString(const char *);
63PyAPI_FUNC(PyObject *) PyString_FromFormatV(const char*, va_list)
64 Py_GCC_ATTRIBUTE((format(printf, 1, 0)));
65PyAPI_FUNC(PyObject *) PyString_FromFormat(const char*, ...)
66 Py_GCC_ATTRIBUTE((format(printf, 1, 2)));
67PyAPI_FUNC(Py_ssize_t) PyString_Size(PyObject *);
68PyAPI_FUNC(char *) PyString_AsString(PyObject *);
69PyAPI_FUNC(PyObject *) PyString_Repr(PyObject *, int);
70PyAPI_FUNC(void) PyString_Concat(PyObject **, PyObject *);
71PyAPI_FUNC(void) PyString_ConcatAndDel(PyObject **, PyObject *);
72PyAPI_FUNC(int) _PyString_Resize(PyObject **, Py_ssize_t);
73PyAPI_FUNC(int) _PyString_Eq(PyObject *, PyObject*);
74PyAPI_FUNC(PyObject *) PyString_Format(PyObject *, PyObject *);
75PyAPI_FUNC(PyObject *) _PyString_FormatLong(PyObject*, int, int,
76 int, char**, int*);