Chris@87
|
1
|
Chris@87
|
2 /* String (str/bytes) object interface */
|
Chris@87
|
3
|
Chris@87
|
4 #ifndef Py_STRINGOBJECT_H
|
Chris@87
|
5 #define Py_STRINGOBJECT_H
|
Chris@87
|
6 #ifdef __cplusplus
|
Chris@87
|
7 extern "C" {
|
Chris@87
|
8 #endif
|
Chris@87
|
9
|
Chris@87
|
10 #include <stdarg.h>
|
Chris@87
|
11
|
Chris@87
|
12 /*
|
Chris@87
|
13 Type PyStringObject represents a character string. An extra zero byte is
|
Chris@87
|
14 reserved at the end to ensure it is zero-terminated, but a size is
|
Chris@87
|
15 present so strings with null bytes in them can be represented. This
|
Chris@87
|
16 is an immutable object type.
|
Chris@87
|
17
|
Chris@87
|
18 There are functions to create new string objects, to test
|
Chris@87
|
19 an object for string-ness, and to get the
|
Chris@87
|
20 string value. The latter function returns a null pointer
|
Chris@87
|
21 if the object is not of the proper type.
|
Chris@87
|
22 There is a variant that takes an explicit size as well as a
|
Chris@87
|
23 variant that assumes a zero-terminated string. Note that none of the
|
Chris@87
|
24 functions should be applied to nil objects.
|
Chris@87
|
25 */
|
Chris@87
|
26
|
Chris@87
|
27 /* Caching the hash (ob_shash) saves recalculation of a string's hash value.
|
Chris@87
|
28 Interning strings (ob_sstate) tries to ensure that only one string
|
Chris@87
|
29 object with a given value exists, so equality tests can be one pointer
|
Chris@87
|
30 comparison. This is generally restricted to strings that "look like"
|
Chris@87
|
31 Python identifiers, although the intern() builtin can be used to force
|
Chris@87
|
32 interning of any string.
|
Chris@87
|
33 Together, these sped the interpreter by up to 20%. */
|
Chris@87
|
34
|
Chris@87
|
35 typedef struct {
|
Chris@87
|
36 PyObject_VAR_HEAD
|
Chris@87
|
37 long ob_shash;
|
Chris@87
|
38 int ob_sstate;
|
Chris@87
|
39 char ob_sval[1];
|
Chris@87
|
40
|
Chris@87
|
41 /* Invariants:
|
Chris@87
|
42 * ob_sval contains space for 'ob_size+1' elements.
|
Chris@87
|
43 * ob_sval[ob_size] == 0.
|
Chris@87
|
44 * ob_shash is the hash of the string or -1 if not computed yet.
|
Chris@87
|
45 * ob_sstate != 0 iff the string object is in stringobject.c's
|
Chris@87
|
46 * 'interned' dictionary; in this case the two references
|
Chris@87
|
47 * from 'interned' to this object are *not counted* in ob_refcnt.
|
Chris@87
|
48 */
|
Chris@87
|
49 } PyStringObject;
|
Chris@87
|
50
|
Chris@87
|
51 #define SSTATE_NOT_INTERNED 0
|
Chris@87
|
52 #define SSTATE_INTERNED_MORTAL 1
|
Chris@87
|
53 #define SSTATE_INTERNED_IMMORTAL 2
|
Chris@87
|
54
|
Chris@87
|
55 PyAPI_DATA(PyTypeObject) PyBaseString_Type;
|
Chris@87
|
56 PyAPI_DATA(PyTypeObject) PyString_Type;
|
Chris@87
|
57
|
Chris@87
|
58 #define PyString_Check(op) \
|
Chris@87
|
59 PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_STRING_SUBCLASS)
|
Chris@87
|
60 #define PyString_CheckExact(op) (Py_TYPE(op) == &PyString_Type)
|
Chris@87
|
61
|
Chris@87
|
62 PyAPI_FUNC(PyObject *) PyString_FromStringAndSize(const char *, Py_ssize_t);
|
Chris@87
|
63 PyAPI_FUNC(PyObject *) PyString_FromString(const char *);
|
Chris@87
|
64 PyAPI_FUNC(PyObject *) PyString_FromFormatV(const char*, va_list)
|
Chris@87
|
65 Py_GCC_ATTRIBUTE((format(printf, 1, 0)));
|
Chris@87
|
66 PyAPI_FUNC(PyObject *) PyString_FromFormat(const char*, ...)
|
Chris@87
|
67 Py_GCC_ATTRIBUTE((format(printf, 1, 2)));
|
Chris@87
|
68 PyAPI_FUNC(Py_ssize_t) PyString_Size(PyObject *);
|
Chris@87
|
69 PyAPI_FUNC(char *) PyString_AsString(PyObject *);
|
Chris@87
|
70 PyAPI_FUNC(PyObject *) PyString_Repr(PyObject *, int);
|
Chris@87
|
71 PyAPI_FUNC(void) PyString_Concat(PyObject **, PyObject *);
|
Chris@87
|
72 PyAPI_FUNC(void) PyString_ConcatAndDel(PyObject **, PyObject *);
|
Chris@87
|
73 PyAPI_FUNC(int) _PyString_Resize(PyObject **, Py_ssize_t);
|
Chris@87
|
74 PyAPI_FUNC(int) _PyString_Eq(PyObject *, PyObject*);
|
Chris@87
|
75 PyAPI_FUNC(PyObject *) PyString_Format(PyObject *, PyObject *);
|
Chris@87
|
76 PyAPI_FUNC(PyObject *) _PyString_FormatLong(PyObject*, int, int,
|
Chris@87
|
77 int, char**, int*);
|
Chris@87
|
78 PyAPI_FUNC(PyObject *) PyString_DecodeEscape(const char *, Py_ssize_t,
|
Chris@87
|
79 const char *, Py_ssize_t,
|
Chris@87
|
80 const char *);
|
Chris@87
|
81
|
Chris@87
|
82 PyAPI_FUNC(void) PyString_InternInPlace(PyObject **);
|
Chris@87
|
83 PyAPI_FUNC(void) PyString_InternImmortal(PyObject **);
|
Chris@87
|
84 PyAPI_FUNC(PyObject *) PyString_InternFromString(const char *);
|
Chris@87
|
85 PyAPI_FUNC(void) _Py_ReleaseInternedStrings(void);
|
Chris@87
|
86
|
Chris@87
|
87 /* Use only if you know it's a string */
|
Chris@87
|
88 #define PyString_CHECK_INTERNED(op) (((PyStringObject *)(op))->ob_sstate)
|
Chris@87
|
89
|
Chris@87
|
90 /* Macro, trading safety for speed */
|
Chris@87
|
91 #define PyString_AS_STRING(op) (((PyStringObject *)(op))->ob_sval)
|
Chris@87
|
92 #define PyString_GET_SIZE(op) Py_SIZE(op)
|
Chris@87
|
93
|
Chris@87
|
94 /* _PyString_Join(sep, x) is like sep.join(x). sep must be PyStringObject*,
|
Chris@87
|
95 x must be an iterable object. */
|
Chris@87
|
96 PyAPI_FUNC(PyObject *) _PyString_Join(PyObject *sep, PyObject *x);
|
Chris@87
|
97
|
Chris@87
|
98 /* --- Generic Codecs ----------------------------------------------------- */
|
Chris@87
|
99
|
Chris@87
|
100 /* Create an object by decoding the encoded string s of the
|
Chris@87
|
101 given size. */
|
Chris@87
|
102
|
Chris@87
|
103 PyAPI_FUNC(PyObject*) PyString_Decode(
|
Chris@87
|
104 const char *s, /* encoded string */
|
Chris@87
|
105 Py_ssize_t size, /* size of buffer */
|
Chris@87
|
106 const char *encoding, /* encoding */
|
Chris@87
|
107 const char *errors /* error handling */
|
Chris@87
|
108 );
|
Chris@87
|
109
|
Chris@87
|
110 /* Encodes a char buffer of the given size and returns a
|
Chris@87
|
111 Python object. */
|
Chris@87
|
112
|
Chris@87
|
113 PyAPI_FUNC(PyObject*) PyString_Encode(
|
Chris@87
|
114 const char *s, /* string char buffer */
|
Chris@87
|
115 Py_ssize_t size, /* number of chars to encode */
|
Chris@87
|
116 const char *encoding, /* encoding */
|
Chris@87
|
117 const char *errors /* error handling */
|
Chris@87
|
118 );
|
Chris@87
|
119
|
Chris@87
|
120 /* Encodes a string object and returns the result as Python
|
Chris@87
|
121 object. */
|
Chris@87
|
122
|
Chris@87
|
123 PyAPI_FUNC(PyObject*) PyString_AsEncodedObject(
|
Chris@87
|
124 PyObject *str, /* string object */
|
Chris@87
|
125 const char *encoding, /* encoding */
|
Chris@87
|
126 const char *errors /* error handling */
|
Chris@87
|
127 );
|
Chris@87
|
128
|
Chris@87
|
129 /* Encodes a string object and returns the result as Python string
|
Chris@87
|
130 object.
|
Chris@87
|
131
|
Chris@87
|
132 If the codec returns an Unicode object, the object is converted
|
Chris@87
|
133 back to a string using the default encoding.
|
Chris@87
|
134
|
Chris@87
|
135 DEPRECATED - use PyString_AsEncodedObject() instead. */
|
Chris@87
|
136
|
Chris@87
|
137 PyAPI_FUNC(PyObject*) PyString_AsEncodedString(
|
Chris@87
|
138 PyObject *str, /* string object */
|
Chris@87
|
139 const char *encoding, /* encoding */
|
Chris@87
|
140 const char *errors /* error handling */
|
Chris@87
|
141 );
|
Chris@87
|
142
|
Chris@87
|
143 /* Decodes a string object and returns the result as Python
|
Chris@87
|
144 object. */
|
Chris@87
|
145
|
Chris@87
|
146 PyAPI_FUNC(PyObject*) PyString_AsDecodedObject(
|
Chris@87
|
147 PyObject *str, /* string object */
|
Chris@87
|
148 const char *encoding, /* encoding */
|
Chris@87
|
149 const char *errors /* error handling */
|
Chris@87
|
150 );
|
Chris@87
|
151
|
Chris@87
|
152 /* Decodes a string object and returns the result as Python string
|
Chris@87
|
153 object.
|
Chris@87
|
154
|
Chris@87
|
155 If the codec returns an Unicode object, the object is converted
|
Chris@87
|
156 back to a string using the default encoding.
|
Chris@87
|
157
|
Chris@87
|
158 DEPRECATED - use PyString_AsDecodedObject() instead. */
|
Chris@87
|
159
|
Chris@87
|
160 PyAPI_FUNC(PyObject*) PyString_AsDecodedString(
|
Chris@87
|
161 PyObject *str, /* string object */
|
Chris@87
|
162 const char *encoding, /* encoding */
|
Chris@87
|
163 const char *errors /* error handling */
|
Chris@87
|
164 );
|
Chris@87
|
165
|
Chris@87
|
166 /* Provides access to the internal data buffer and size of a string
|
Chris@87
|
167 object or the default encoded version of an Unicode object. Passing
|
Chris@87
|
168 NULL as *len parameter will force the string buffer to be
|
Chris@87
|
169 0-terminated (passing a string with embedded NULL characters will
|
Chris@87
|
170 cause an exception). */
|
Chris@87
|
171
|
Chris@87
|
172 PyAPI_FUNC(int) PyString_AsStringAndSize(
|
Chris@87
|
173 register PyObject *obj, /* string or Unicode object */
|
Chris@87
|
174 register char **s, /* pointer to buffer variable */
|
Chris@87
|
175 register Py_ssize_t *len /* pointer to length variable or NULL
|
Chris@87
|
176 (only possible for 0-terminated
|
Chris@87
|
177 strings) */
|
Chris@87
|
178 );
|
Chris@87
|
179
|
Chris@87
|
180
|
Chris@87
|
181 /* Using the current locale, insert the thousands grouping
|
Chris@87
|
182 into the string pointed to by buffer. For the argument descriptions,
|
Chris@87
|
183 see Objects/stringlib/localeutil.h */
|
Chris@87
|
184 PyAPI_FUNC(Py_ssize_t) _PyString_InsertThousandsGroupingLocale(char *buffer,
|
Chris@87
|
185 Py_ssize_t n_buffer,
|
Chris@87
|
186 char *digits,
|
Chris@87
|
187 Py_ssize_t n_digits,
|
Chris@87
|
188 Py_ssize_t min_width);
|
Chris@87
|
189
|
Chris@87
|
190 /* Using explicit passed-in values, insert the thousands grouping
|
Chris@87
|
191 into the string pointed to by buffer. For the argument descriptions,
|
Chris@87
|
192 see Objects/stringlib/localeutil.h */
|
Chris@87
|
193 PyAPI_FUNC(Py_ssize_t) _PyString_InsertThousandsGrouping(char *buffer,
|
Chris@87
|
194 Py_ssize_t n_buffer,
|
Chris@87
|
195 char *digits,
|
Chris@87
|
196 Py_ssize_t n_digits,
|
Chris@87
|
197 Py_ssize_t min_width,
|
Chris@87
|
198 const char *grouping,
|
Chris@87
|
199 const char *thousands_sep);
|
Chris@87
|
200
|
Chris@87
|
201 /* Format the object based on the format_spec, as defined in PEP 3101
|
Chris@87
|
202 (Advanced String Formatting). */
|
Chris@87
|
203 PyAPI_FUNC(PyObject *) _PyBytes_FormatAdvanced(PyObject *obj,
|
Chris@87
|
204 char *format_spec,
|
Chris@87
|
205 Py_ssize_t format_spec_len);
|
Chris@87
|
206
|
Chris@87
|
207 #ifdef __cplusplus
|
Chris@87
|
208 }
|
Chris@87
|
209 #endif
|
Chris@87
|
210 #endif /* !Py_STRINGOBJECT_H */
|