1991-02-19 08:39:46 -04:00
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* String object interface */
|
|
|
|
|
2000-07-08 21:55:06 -03:00
|
|
|
#ifndef Py_STRINGOBJECT_H
|
|
|
|
#define Py_STRINGOBJECT_H
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2001-08-24 15:32:06 -03:00
|
|
|
#include <stdarg.h>
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/*
|
1995-01-12 07:45:45 -04:00
|
|
|
Type PyStringObject represents a character string. An extra zero byte is
|
1990-10-14 09:07:46 -03:00
|
|
|
reserved at the end to ensure it is zero-terminated, but a size is
|
|
|
|
present so strings with null bytes in them can be represented. This
|
|
|
|
is an immutable object type.
|
|
|
|
|
|
|
|
There are functions to create new string objects, to test
|
|
|
|
an object for string-ness, and to get the
|
|
|
|
string value. The latter function returns a null pointer
|
|
|
|
if the object is not of the proper type.
|
|
|
|
There is a variant that takes an explicit size as well as a
|
|
|
|
variant that assumes a zero-terminated string. Note that none of the
|
|
|
|
functions should be applied to nil objects.
|
|
|
|
*/
|
|
|
|
|
2002-03-28 23:29:08 -04:00
|
|
|
/* Caching the hash (ob_shash) saves recalculation of a string's hash value.
|
2002-08-19 18:43:18 -03:00
|
|
|
Interning strings (ob_sstate) tries to ensure that only one string
|
2002-03-28 23:29:08 -04:00
|
|
|
object with a given value exists, so equality tests can be one pointer
|
|
|
|
comparison. This is generally restricted to strings that "look like"
|
2006-12-19 16:50:34 -04:00
|
|
|
Python identifiers, although the sys.intern() function can be used to force
|
2002-03-28 23:29:08 -04:00
|
|
|
interning of any string.
|
|
|
|
Together, these sped the interpreter by up to 20%. */
|
1996-07-30 13:42:03 -03:00
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
typedef struct {
|
2000-07-08 21:55:06 -03:00
|
|
|
PyObject_VAR_HEAD
|
|
|
|
long ob_shash;
|
2002-08-19 18:43:18 -03:00
|
|
|
int ob_sstate;
|
2000-07-08 21:55:06 -03:00
|
|
|
char ob_sval[1];
|
2004-10-28 13:32:00 -03:00
|
|
|
|
|
|
|
/* Invariants:
|
|
|
|
* ob_sval contains space for 'ob_size+1' elements.
|
|
|
|
* ob_sval[ob_size] == 0.
|
|
|
|
* ob_shash is the hash of the string or -1 if not computed yet.
|
|
|
|
* ob_sstate != 0 iff the string object is in stringobject.c's
|
|
|
|
* 'interned' dictionary; in this case the two references
|
|
|
|
* from 'interned' to this object are *not counted* in ob_refcnt.
|
|
|
|
*/
|
1995-01-12 07:45:45 -04:00
|
|
|
} PyStringObject;
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_DATA(PyTypeObject) PyBaseString_Type;
|
|
|
|
PyAPI_DATA(PyTypeObject) PyString_Type;
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2007-02-25 16:39:11 -04:00
|
|
|
#define PyString_Check(op) \
|
Merged revisions 56467-56482 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/p3yk
................
r56477 | martin.v.loewis | 2007-07-21 09:04:38 +0200 (Sa, 21 Jul 2007) | 11 lines
Merged revisions 56466-56476 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r56476 | martin.v.loewis | 2007-07-21 08:55:02 +0200 (Sa, 21 Jul 2007) | 4 lines
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
backwards compatibility. Add Py_Refcnt, Py_Type, Py_Size, and
PyVarObject_HEAD_INIT.
........
................
r56478 | martin.v.loewis | 2007-07-21 09:47:23 +0200 (Sa, 21 Jul 2007) | 2 lines
PEP 3123: Use proper C inheritance for PyObject.
................
r56479 | martin.v.loewis | 2007-07-21 10:06:55 +0200 (Sa, 21 Jul 2007) | 3 lines
Add longintrepr.h to Python.h, so that the compiler can
see that PyFalse is really some kind of PyObject*.
................
r56480 | martin.v.loewis | 2007-07-21 10:47:18 +0200 (Sa, 21 Jul 2007) | 2 lines
Qualify SHIFT, MASK, BASE.
................
r56482 | martin.v.loewis | 2007-07-21 19:10:57 +0200 (Sa, 21 Jul 2007) | 2 lines
Correctly refer to _ob_next.
................
2007-07-21 14:22:18 -03:00
|
|
|
PyType_FastSubclass(Py_Type(op), Py_TPFLAGS_STRING_SUBCLASS)
|
|
|
|
#define PyString_CheckExact(op) (Py_Type(op) == &PyString_Type)
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2006-02-15 13:27:45 -04:00
|
|
|
PyAPI_FUNC(PyObject *) PyString_FromStringAndSize(const char *, Py_ssize_t);
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(PyObject *) PyString_FromString(const char *);
|
|
|
|
PyAPI_FUNC(PyObject *) PyString_FromFormatV(const char*, va_list)
|
2002-09-15 11:09:54 -03:00
|
|
|
Py_GCC_ATTRIBUTE((format(printf, 1, 0)));
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(PyObject *) PyString_FromFormat(const char*, ...)
|
2002-09-15 11:09:54 -03:00
|
|
|
Py_GCC_ATTRIBUTE((format(printf, 1, 2)));
|
2006-02-15 13:27:45 -04:00
|
|
|
PyAPI_FUNC(Py_ssize_t) PyString_Size(PyObject *);
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(char *) PyString_AsString(PyObject *);
|
2002-08-14 04:46:28 -03:00
|
|
|
PyAPI_FUNC(PyObject *) PyString_Repr(PyObject *, int);
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(void) PyString_Concat(PyObject **, PyObject *);
|
|
|
|
PyAPI_FUNC(void) PyString_ConcatAndDel(PyObject **, PyObject *);
|
2006-02-15 13:27:45 -04:00
|
|
|
PyAPI_FUNC(int) _PyString_Resize(PyObject **, Py_ssize_t);
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(int) _PyString_Eq(PyObject *, PyObject*);
|
|
|
|
PyAPI_FUNC(PyObject *) PyString_Format(PyObject *, PyObject *);
|
|
|
|
PyAPI_FUNC(PyObject *) _PyString_FormatLong(PyObject*, int, int,
|
2000-09-21 02:43:11 -03:00
|
|
|
int, char**, int*);
|
2006-02-15 13:27:45 -04:00
|
|
|
PyAPI_FUNC(PyObject *) PyString_DecodeEscape(const char *, Py_ssize_t,
|
|
|
|
const char *, Py_ssize_t,
|
2002-08-14 04:46:28 -03:00
|
|
|
const char *);
|
1990-10-14 09:07:46 -03:00
|
|
|
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(void) PyString_InternInPlace(PyObject **);
|
2002-08-19 18:43:18 -03:00
|
|
|
PyAPI_FUNC(void) PyString_InternImmortal(PyObject **);
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(PyObject *) PyString_InternFromString(const char *);
|
|
|
|
PyAPI_FUNC(void) _Py_ReleaseInternedStrings(void);
|
1997-01-18 03:53:23 -04:00
|
|
|
|
2002-08-19 18:43:18 -03:00
|
|
|
/* Use only if you know it's a string */
|
|
|
|
#define PyString_CHECK_INTERNED(op) (((PyStringObject *)(op))->ob_sstate)
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Macro, trading safety for speed */
|
2007-06-10 06:51:05 -03:00
|
|
|
#define PyString_AS_STRING(op) (assert(PyString_Check(op)),(((PyStringObject *)(op))->ob_sval))
|
2007-07-21 15:47:48 -03:00
|
|
|
#define PyString_GET_SIZE(op) (assert(PyString_Check(op)),Py_Size(op))
|
1993-07-28 06:05:47 -03:00
|
|
|
|
2001-06-16 02:11:17 -03:00
|
|
|
/* _PyString_Join(sep, x) is like sep.join(x). sep must be PyStringObject*,
|
|
|
|
x must be an iterable object. */
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(PyObject *) _PyString_Join(PyObject *sep, PyObject *x);
|
2001-06-16 02:11:17 -03:00
|
|
|
|
2000-07-06 08:25:40 -03:00
|
|
|
/* --- Generic Codecs ----------------------------------------------------- */
|
|
|
|
|
2001-05-15 09:00:02 -03:00
|
|
|
/* Create an object by decoding the encoded string s of the
|
2000-07-06 08:25:40 -03:00
|
|
|
given size. */
|
|
|
|
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(PyObject*) PyString_Decode(
|
2000-07-06 08:25:40 -03:00
|
|
|
const char *s, /* encoded string */
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t size, /* size of buffer */
|
2000-07-06 08:25:40 -03:00
|
|
|
const char *encoding, /* encoding */
|
|
|
|
const char *errors /* error handling */
|
|
|
|
);
|
|
|
|
|
|
|
|
/* Encodes a char buffer of the given size and returns a
|
2001-05-15 09:00:02 -03:00
|
|
|
Python object. */
|
2000-07-06 08:25:40 -03:00
|
|
|
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(PyObject*) PyString_Encode(
|
2000-07-06 08:25:40 -03:00
|
|
|
const char *s, /* string char buffer */
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t size, /* number of chars to encode */
|
2000-07-06 08:25:40 -03:00
|
|
|
const char *encoding, /* encoding */
|
|
|
|
const char *errors /* error handling */
|
|
|
|
);
|
|
|
|
|
2001-05-15 09:00:02 -03:00
|
|
|
/* Encodes a string object and returns the result as Python
|
2000-07-06 08:25:40 -03:00
|
|
|
object. */
|
|
|
|
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(PyObject*) PyString_AsEncodedObject(
|
2001-05-15 09:00:02 -03:00
|
|
|
PyObject *str, /* string object */
|
|
|
|
const char *encoding, /* encoding */
|
|
|
|
const char *errors /* error handling */
|
|
|
|
);
|
|
|
|
|
|
|
|
/* Encodes a string object and returns the result as Python string
|
|
|
|
object.
|
|
|
|
|
|
|
|
If the codec returns an Unicode object, the object is converted
|
|
|
|
back to a string using the default encoding.
|
|
|
|
|
|
|
|
DEPRECATED - use PyString_AsEncodedObject() instead. */
|
|
|
|
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(PyObject*) PyString_AsEncodedString(
|
2000-07-06 08:25:40 -03:00
|
|
|
PyObject *str, /* string object */
|
|
|
|
const char *encoding, /* encoding */
|
|
|
|
const char *errors /* error handling */
|
|
|
|
);
|
|
|
|
|
2001-05-15 09:00:02 -03:00
|
|
|
/* Decodes a string object and returns the result as Python
|
|
|
|
object. */
|
|
|
|
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(PyObject*) PyString_AsDecodedObject(
|
2001-05-15 09:00:02 -03:00
|
|
|
PyObject *str, /* string object */
|
|
|
|
const char *encoding, /* encoding */
|
|
|
|
const char *errors /* error handling */
|
|
|
|
);
|
|
|
|
|
|
|
|
/* Decodes a string object and returns the result as Python string
|
|
|
|
object.
|
|
|
|
|
|
|
|
If the codec returns an Unicode object, the object is converted
|
|
|
|
back to a string using the default encoding.
|
|
|
|
|
|
|
|
DEPRECATED - use PyString_AsDecodedObject() instead. */
|
|
|
|
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(PyObject*) PyString_AsDecodedString(
|
2001-05-15 09:00:02 -03:00
|
|
|
PyObject *str, /* string object */
|
|
|
|
const char *encoding, /* encoding */
|
|
|
|
const char *errors /* error handling */
|
|
|
|
);
|
|
|
|
|
2000-09-19 18:04:18 -03:00
|
|
|
/* Provides access to the internal data buffer and size of a string
|
|
|
|
object or the default encoded version of an Unicode object. Passing
|
|
|
|
NULL as *len parameter will force the string buffer to be
|
|
|
|
0-terminated (passing a string with embedded NULL characters will
|
|
|
|
cause an exception). */
|
|
|
|
|
2002-08-12 04:21:58 -03:00
|
|
|
PyAPI_FUNC(int) PyString_AsStringAndSize(
|
2000-09-19 18:04:18 -03:00
|
|
|
register PyObject *obj, /* string or Unicode object */
|
|
|
|
register char **s, /* pointer to buffer variable */
|
2006-02-15 13:27:45 -04:00
|
|
|
register Py_ssize_t *len /* pointer to length variable or NULL
|
2000-09-19 18:04:18 -03:00
|
|
|
(only possible for 0-terminated
|
|
|
|
strings) */
|
|
|
|
);
|
|
|
|
|
|
|
|
|
1993-07-28 06:05:47 -03:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif /* !Py_STRINGOBJECT_H */
|