| 1 |
|
|---|
| 2 | /* String object interface */
|
|---|
| 3 |
|
|---|
| 4 | #ifndef Py_STRINGOBJECT_H
|
|---|
| 5 | #define Py_STRINGOBJECT_H
|
|---|
| 6 | #ifdef __cplusplus
|
|---|
| 7 | extern "C" {
|
|---|
| 8 | #endif
|
|---|
| 9 |
|
|---|
| 10 | #include <stdarg.h>
|
|---|
| 11 |
|
|---|
| 12 | /*
|
|---|
| 13 | Type PyStringObject represents a character string. An extra zero byte is
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 35 | typedef 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 |
|
|---|
| 55 | PyAPI_DATA(PyTypeObject) PyBaseString_Type;
|
|---|
| 56 | PyAPI_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 |
|
|---|
| 61 | PyAPI_FUNC(PyObject *) PyString_FromStringAndSize(const char *, Py_ssize_t);
|
|---|
| 62 | PyAPI_FUNC(PyObject *) PyString_FromString(const char *);
|
|---|
| 63 | PyAPI_FUNC(PyObject *) PyString_FromFormatV(const char*, va_list)
|
|---|
| 64 | Py_GCC_ATTRIBUTE((format(printf, 1, 0)));
|
|---|
| 65 | PyAPI_FUNC(PyObject *) PyString_FromFormat(const char*, ...)
|
|---|
| 66 | Py_GCC_ATTRIBUTE((format(printf, 1, 2)));
|
|---|
| 67 | PyAPI_FUNC(Py_ssize_t) PyString_Size(PyObject *);
|
|---|
| 68 | PyAPI_FUNC(char *) PyString_AsString(PyObject *);
|
|---|
| 69 | PyAPI_FUNC(PyObject *) PyString_Repr(PyObject *, int);
|
|---|
| 70 | PyAPI_FUNC(void) PyString_Concat(PyObject **, PyObject *);
|
|---|
| 71 | PyAPI_FUNC(void) PyString_ConcatAndDel(PyObject **, PyObject *);
|
|---|
| 72 | PyAPI_FUNC(int) _PyString_Resize(PyObject **, Py_ssize_t);
|
|---|
| 73 | PyAPI_FUNC(int) _PyString_Eq(PyObject *, PyObject*);
|
|---|
| 74 | PyAPI_FUNC(PyObject *) PyString_Format(PyObject *, PyObject *);
|
|---|
| 75 | PyAPI_FUNC(PyObject *) _PyString_FormatLong(PyObject*, int, int,
|
|---|
| 76 | int, char**, int*);
|
|---|
|
|---|