| 1 | /* A copy of pypy2's PyStringObject */ |
| 2 | |
| 3 | #ifndef Py_BYTESOBJECT_H |
| 4 | #define Py_BYTESOBJECT_H |
| 5 | #ifdef __cplusplus |
| 6 | extern "C" { |
| 7 | #endif |
| 8 | |
| 9 | #include <stdarg.h> |
| 10 | |
| 11 | #define PyBytes_GET_SIZE(op) PyBytes_Size(op) |
| 12 | |
| 13 | /* |
| 14 | Type PyStringObject represents a character string. An extra zero byte is |
| 15 | reserved at the end to ensure it is zero-terminated, but a size is |
| 16 | present so strings with null bytes in them can be represented. This |
| 17 | is an immutable object type. |
| 18 | |
| 19 | There are functions to create new string objects, to test |
| 20 | an object for string-ness, and to get the |
| 21 | string value. The latter function returns a null pointer |
| 22 | if the object is not of the proper type. |
| 23 | There is a variant that takes an explicit size as well as a |
| 24 | variant that assumes a zero-terminated string. Note that none of the |
| 25 | functions should be applied to nil objects. |
| 26 | */ |
| 27 | |
| 28 | /* Caching the hash (ob_shash) saves recalculation of a string's hash value. |
| 29 | Interning strings (ob_sstate) tries to ensure that only one string |
| 30 | object with a given value exists, so equality tests can be one pointer |
| 31 | comparison. This is generally restricted to strings that "look like" |
| 32 | Python identifiers, although the intern() builtin can be used to force |
| 33 | interning of any string. |
| 34 | Together, these sped cpython up by up to 20%, and since they are part of the |
| 35 | "public" interface PyPy must reimpliment them. */ |
| 36 | |
| 37 | typedef struct { |
| 38 | PyObject_VAR_HEAD |
| 39 | Py_hash_t ob_shash; |
| 40 | int ob_sstate; |
| 41 | char ob_sval[1]; |
| 42 | |
| 43 | /* Invariants |
| 44 | * ob_sval contains space for 'ob_size+1' elements. |
| 45 | * ob_sval[ob_size] == 0. |
| 46 | * ob_shash is the hash of the string or -1 if not computed yet. |
| 47 | * ob_sstate != 0 iff the string object is in stringobject.c's |
| 48 | * 'interned' dictionary; in this case the two references |
| 49 | * from 'interned' to this object are *not counted* in ob_refcnt. |
| 50 | */ |
| 51 | } PyBytesObject; |
| 52 | |
| 53 | #define SSTATE_NOT_INTERNED 0 |
| 54 | #define SSTATE_INTERNED_MORTAL 1 |
| 55 | #define SSTATE_INTERNED_IMMORTAL 2 |
| 56 | #define PyString_CHECK_INTERNED(op) (((PyStringObject *)(op))->ob_sstate) |
| 57 | |
| 58 | #define PyBytes_Check(op) \ |
| 59 | PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_BYTES_SUBCLASS) |
| 60 | #define PyBytes_CheckExact(op) (Py_TYPE(op) == &PyBytes_Type) |
| 61 | |
| 62 | PyAPI_FUNC(PyObject *) PyBytes_FromFormatV(const char*, va_list); |
| 63 | PyAPI_FUNC(PyObject *) PyBytes_FromFormat(const char*, ...); |
| 64 | |
| 65 | #ifdef __cplusplus |
| 66 | } |
| 67 | #endif |
| 68 | #endif |