Chris@87: Chris@87: /* String (str/bytes) object interface */ Chris@87: Chris@87: #ifndef Py_STRINGOBJECT_H Chris@87: #define Py_STRINGOBJECT_H Chris@87: #ifdef __cplusplus Chris@87: extern "C" { Chris@87: #endif Chris@87: Chris@87: #include Chris@87: Chris@87: /* Chris@87: Type PyStringObject represents a character string. An extra zero byte is Chris@87: reserved at the end to ensure it is zero-terminated, but a size is Chris@87: present so strings with null bytes in them can be represented. This Chris@87: is an immutable object type. Chris@87: Chris@87: There are functions to create new string objects, to test Chris@87: an object for string-ness, and to get the Chris@87: string value. The latter function returns a null pointer Chris@87: if the object is not of the proper type. Chris@87: There is a variant that takes an explicit size as well as a Chris@87: variant that assumes a zero-terminated string. Note that none of the Chris@87: functions should be applied to nil objects. Chris@87: */ Chris@87: Chris@87: /* Caching the hash (ob_shash) saves recalculation of a string's hash value. Chris@87: Interning strings (ob_sstate) tries to ensure that only one string Chris@87: object with a given value exists, so equality tests can be one pointer Chris@87: comparison. This is generally restricted to strings that "look like" Chris@87: Python identifiers, although the intern() builtin can be used to force Chris@87: interning of any string. Chris@87: Together, these sped the interpreter by up to 20%. */ Chris@87: Chris@87: typedef struct { Chris@87: PyObject_VAR_HEAD Chris@87: long ob_shash; Chris@87: int ob_sstate; Chris@87: char ob_sval[1]; Chris@87: Chris@87: /* Invariants: Chris@87: * ob_sval contains space for 'ob_size+1' elements. Chris@87: * ob_sval[ob_size] == 0. Chris@87: * ob_shash is the hash of the string or -1 if not computed yet. Chris@87: * ob_sstate != 0 iff the string object is in stringobject.c's Chris@87: * 'interned' dictionary; in this case the two references Chris@87: * from 'interned' to this object are *not counted* in ob_refcnt. Chris@87: */ Chris@87: } PyStringObject; Chris@87: Chris@87: #define SSTATE_NOT_INTERNED 0 Chris@87: #define SSTATE_INTERNED_MORTAL 1 Chris@87: #define SSTATE_INTERNED_IMMORTAL 2 Chris@87: Chris@87: PyAPI_DATA(PyTypeObject) PyBaseString_Type; Chris@87: PyAPI_DATA(PyTypeObject) PyString_Type; Chris@87: Chris@87: #define PyString_Check(op) \ Chris@87: PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_STRING_SUBCLASS) Chris@87: #define PyString_CheckExact(op) (Py_TYPE(op) == &PyString_Type) Chris@87: Chris@87: PyAPI_FUNC(PyObject *) PyString_FromStringAndSize(const char *, Py_ssize_t); Chris@87: PyAPI_FUNC(PyObject *) PyString_FromString(const char *); Chris@87: PyAPI_FUNC(PyObject *) PyString_FromFormatV(const char*, va_list) Chris@87: Py_GCC_ATTRIBUTE((format(printf, 1, 0))); Chris@87: PyAPI_FUNC(PyObject *) PyString_FromFormat(const char*, ...) Chris@87: Py_GCC_ATTRIBUTE((format(printf, 1, 2))); Chris@87: PyAPI_FUNC(Py_ssize_t) PyString_Size(PyObject *); Chris@87: PyAPI_FUNC(char *) PyString_AsString(PyObject *); Chris@87: PyAPI_FUNC(PyObject *) PyString_Repr(PyObject *, int); Chris@87: PyAPI_FUNC(void) PyString_Concat(PyObject **, PyObject *); Chris@87: PyAPI_FUNC(void) PyString_ConcatAndDel(PyObject **, PyObject *); Chris@87: PyAPI_FUNC(int) _PyString_Resize(PyObject **, Py_ssize_t); Chris@87: PyAPI_FUNC(int) _PyString_Eq(PyObject *, PyObject*); Chris@87: PyAPI_FUNC(PyObject *) PyString_Format(PyObject *, PyObject *); Chris@87: PyAPI_FUNC(PyObject *) _PyString_FormatLong(PyObject*, int, int, Chris@87: int, char**, int*); Chris@87: PyAPI_FUNC(PyObject *) PyString_DecodeEscape(const char *, Py_ssize_t, Chris@87: const char *, Py_ssize_t, Chris@87: const char *); Chris@87: Chris@87: PyAPI_FUNC(void) PyString_InternInPlace(PyObject **); Chris@87: PyAPI_FUNC(void) PyString_InternImmortal(PyObject **); Chris@87: PyAPI_FUNC(PyObject *) PyString_InternFromString(const char *); Chris@87: PyAPI_FUNC(void) _Py_ReleaseInternedStrings(void); Chris@87: Chris@87: /* Use only if you know it's a string */ Chris@87: #define PyString_CHECK_INTERNED(op) (((PyStringObject *)(op))->ob_sstate) Chris@87: Chris@87: /* Macro, trading safety for speed */ Chris@87: #define PyString_AS_STRING(op) (((PyStringObject *)(op))->ob_sval) Chris@87: #define PyString_GET_SIZE(op) Py_SIZE(op) Chris@87: Chris@87: /* _PyString_Join(sep, x) is like sep.join(x). sep must be PyStringObject*, Chris@87: x must be an iterable object. */ Chris@87: PyAPI_FUNC(PyObject *) _PyString_Join(PyObject *sep, PyObject *x); Chris@87: Chris@87: /* --- Generic Codecs ----------------------------------------------------- */ Chris@87: Chris@87: /* Create an object by decoding the encoded string s of the Chris@87: given size. */ Chris@87: Chris@87: PyAPI_FUNC(PyObject*) PyString_Decode( Chris@87: const char *s, /* encoded string */ Chris@87: Py_ssize_t size, /* size of buffer */ Chris@87: const char *encoding, /* encoding */ Chris@87: const char *errors /* error handling */ Chris@87: ); Chris@87: Chris@87: /* Encodes a char buffer of the given size and returns a Chris@87: Python object. */ Chris@87: Chris@87: PyAPI_FUNC(PyObject*) PyString_Encode( Chris@87: const char *s, /* string char buffer */ Chris@87: Py_ssize_t size, /* number of chars to encode */ Chris@87: const char *encoding, /* encoding */ Chris@87: const char *errors /* error handling */ Chris@87: ); Chris@87: Chris@87: /* Encodes a string object and returns the result as Python Chris@87: object. */ Chris@87: Chris@87: PyAPI_FUNC(PyObject*) PyString_AsEncodedObject( Chris@87: PyObject *str, /* string object */ Chris@87: const char *encoding, /* encoding */ Chris@87: const char *errors /* error handling */ Chris@87: ); Chris@87: Chris@87: /* Encodes a string object and returns the result as Python string Chris@87: object. Chris@87: Chris@87: If the codec returns an Unicode object, the object is converted Chris@87: back to a string using the default encoding. Chris@87: Chris@87: DEPRECATED - use PyString_AsEncodedObject() instead. */ Chris@87: Chris@87: PyAPI_FUNC(PyObject*) PyString_AsEncodedString( Chris@87: PyObject *str, /* string object */ Chris@87: const char *encoding, /* encoding */ Chris@87: const char *errors /* error handling */ Chris@87: ); Chris@87: Chris@87: /* Decodes a string object and returns the result as Python Chris@87: object. */ Chris@87: Chris@87: PyAPI_FUNC(PyObject*) PyString_AsDecodedObject( Chris@87: PyObject *str, /* string object */ Chris@87: const char *encoding, /* encoding */ Chris@87: const char *errors /* error handling */ Chris@87: ); Chris@87: Chris@87: /* Decodes a string object and returns the result as Python string Chris@87: object. Chris@87: Chris@87: If the codec returns an Unicode object, the object is converted Chris@87: back to a string using the default encoding. Chris@87: Chris@87: DEPRECATED - use PyString_AsDecodedObject() instead. */ Chris@87: Chris@87: PyAPI_FUNC(PyObject*) PyString_AsDecodedString( Chris@87: PyObject *str, /* string object */ Chris@87: const char *encoding, /* encoding */ Chris@87: const char *errors /* error handling */ Chris@87: ); Chris@87: Chris@87: /* Provides access to the internal data buffer and size of a string Chris@87: object or the default encoded version of an Unicode object. Passing Chris@87: NULL as *len parameter will force the string buffer to be Chris@87: 0-terminated (passing a string with embedded NULL characters will Chris@87: cause an exception). */ Chris@87: Chris@87: PyAPI_FUNC(int) PyString_AsStringAndSize( Chris@87: register PyObject *obj, /* string or Unicode object */ Chris@87: register char **s, /* pointer to buffer variable */ Chris@87: register Py_ssize_t *len /* pointer to length variable or NULL Chris@87: (only possible for 0-terminated Chris@87: strings) */ Chris@87: ); Chris@87: Chris@87: Chris@87: /* Using the current locale, insert the thousands grouping Chris@87: into the string pointed to by buffer. For the argument descriptions, Chris@87: see Objects/stringlib/localeutil.h */ Chris@87: PyAPI_FUNC(Py_ssize_t) _PyString_InsertThousandsGroupingLocale(char *buffer, Chris@87: Py_ssize_t n_buffer, Chris@87: char *digits, Chris@87: Py_ssize_t n_digits, Chris@87: Py_ssize_t min_width); Chris@87: Chris@87: /* Using explicit passed-in values, insert the thousands grouping Chris@87: into the string pointed to by buffer. For the argument descriptions, Chris@87: see Objects/stringlib/localeutil.h */ Chris@87: PyAPI_FUNC(Py_ssize_t) _PyString_InsertThousandsGrouping(char *buffer, Chris@87: Py_ssize_t n_buffer, Chris@87: char *digits, Chris@87: Py_ssize_t n_digits, Chris@87: Py_ssize_t min_width, Chris@87: const char *grouping, Chris@87: const char *thousands_sep); Chris@87: Chris@87: /* Format the object based on the format_spec, as defined in PEP 3101 Chris@87: (Advanced String Formatting). */ Chris@87: PyAPI_FUNC(PyObject *) _PyBytes_FormatAdvanced(PyObject *obj, Chris@87: char *format_spec, Chris@87: Py_ssize_t format_spec_len); Chris@87: Chris@87: #ifdef __cplusplus Chris@87: } Chris@87: #endif Chris@87: #endif /* !Py_STRINGOBJECT_H */