- Got rid of newmodule.c
[python/dscho.git] / Include / stringobject.h
blob2a56ef639bf08b4b056d2bb65372d316225cd55c
2 /* String object interface */
4 #ifndef Py_STRINGOBJECT_H
5 #define Py_STRINGOBJECT_H
6 #ifdef __cplusplus
7 extern "C" {
8 #endif
10 #include <stdarg.h>
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.
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.
27 /* Caching the hash (ob_shash) saves recalculation of a string's hash value.
28 Interning strings (ob_sinterned) 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%. */
35 typedef struct {
36 PyObject_VAR_HEAD
37 long ob_shash;
38 PyObject *ob_sinterned;
39 char ob_sval[1];
40 } PyStringObject;
42 extern DL_IMPORT(PyTypeObject) PyBaseString_Type;
43 extern DL_IMPORT(PyTypeObject) PyString_Type;
45 #define PyString_Check(op) PyObject_TypeCheck(op, &PyString_Type)
46 #define PyString_CheckExact(op) ((op)->ob_type == &PyString_Type)
48 extern DL_IMPORT(PyObject *) PyString_FromStringAndSize(const char *, int);
49 extern DL_IMPORT(PyObject *) PyString_FromString(const char *);
50 extern DL_IMPORT(PyObject *) PyString_FromFormatV(const char*, va_list)
51 __attribute__((format(printf, 1, 0)));
52 extern DL_IMPORT(PyObject *) PyString_FromFormat(const char*, ...)
53 __attribute__((format(printf, 1, 2)));
54 extern DL_IMPORT(int) PyString_Size(PyObject *);
55 extern DL_IMPORT(char *) PyString_AsString(PyObject *);
56 extern DL_IMPORT(void) PyString_Concat(PyObject **, PyObject *);
57 extern DL_IMPORT(void) PyString_ConcatAndDel(PyObject **, PyObject *);
58 extern DL_IMPORT(int) _PyString_Resize(PyObject **, int);
59 extern DL_IMPORT(int) _PyString_Eq(PyObject *, PyObject*);
60 extern DL_IMPORT(PyObject *) PyString_Format(PyObject *, PyObject *);
61 extern DL_IMPORT(PyObject *) _PyString_FormatLong(PyObject*, int, int,
62 int, char**, int*);
64 extern DL_IMPORT(void) PyString_InternInPlace(PyObject **);
65 extern DL_IMPORT(PyObject *) PyString_InternFromString(const char *);
66 extern DL_IMPORT(void) _Py_ReleaseInternedStrings(void);
68 /* Macro, trading safety for speed */
69 #define PyString_AS_STRING(op) (((PyStringObject *)(op))->ob_sval)
70 #define PyString_GET_SIZE(op) (((PyStringObject *)(op))->ob_size)
72 /* _PyString_Join(sep, x) is like sep.join(x). sep must be PyStringObject*,
73 x must be an iterable object. */
74 extern DL_IMPORT(PyObject *) _PyString_Join(PyObject *sep, PyObject *x);
76 /* --- Generic Codecs ----------------------------------------------------- */
78 /* Create an object by decoding the encoded string s of the
79 given size. */
81 extern DL_IMPORT(PyObject*) PyString_Decode(
82 const char *s, /* encoded string */
83 int size, /* size of buffer */
84 const char *encoding, /* encoding */
85 const char *errors /* error handling */
88 /* Encodes a char buffer of the given size and returns a
89 Python object. */
91 extern DL_IMPORT(PyObject*) PyString_Encode(
92 const char *s, /* string char buffer */
93 int size, /* number of chars to encode */
94 const char *encoding, /* encoding */
95 const char *errors /* error handling */
98 /* Encodes a string object and returns the result as Python
99 object. */
101 extern DL_IMPORT(PyObject*) PyString_AsEncodedObject(
102 PyObject *str, /* string object */
103 const char *encoding, /* encoding */
104 const char *errors /* error handling */
107 /* Encodes a string object and returns the result as Python string
108 object.
110 If the codec returns an Unicode object, the object is converted
111 back to a string using the default encoding.
113 DEPRECATED - use PyString_AsEncodedObject() instead. */
115 extern DL_IMPORT(PyObject*) PyString_AsEncodedString(
116 PyObject *str, /* string object */
117 const char *encoding, /* encoding */
118 const char *errors /* error handling */
121 /* Decodes a string object and returns the result as Python
122 object. */
124 extern DL_IMPORT(PyObject*) PyString_AsDecodedObject(
125 PyObject *str, /* string object */
126 const char *encoding, /* encoding */
127 const char *errors /* error handling */
130 /* Decodes a string object and returns the result as Python string
131 object.
133 If the codec returns an Unicode object, the object is converted
134 back to a string using the default encoding.
136 DEPRECATED - use PyString_AsDecodedObject() instead. */
138 extern DL_IMPORT(PyObject*) PyString_AsDecodedString(
139 PyObject *str, /* string object */
140 const char *encoding, /* encoding */
141 const char *errors /* error handling */
144 /* Provides access to the internal data buffer and size of a string
145 object or the default encoded version of an Unicode object. Passing
146 NULL as *len parameter will force the string buffer to be
147 0-terminated (passing a string with embedded NULL characters will
148 cause an exception). */
150 extern DL_IMPORT(int) PyString_AsStringAndSize(
151 register PyObject *obj, /* string or Unicode object */
152 register char **s, /* pointer to buffer variable */
153 register int *len /* pointer to length variable or NULL
154 (only possible for 0-terminated
155 strings) */
159 #ifdef __cplusplus
161 #endif
162 #endif /* !Py_STRINGOBJECT_H */