@@ -14,7 +14,148 @@ extern "C" {
1414void _PyUnicode_ExactDealloc (PyObject * op );
1515Py_ssize_t _PyUnicode_InternedSize (void );
1616
17- /* runtime lifecycle */
17+ /* --- _PyUnicodeWriter API ----------------------------------------------- */
18+
19+ typedef struct {
20+ PyObject * buffer ;
21+ void * data ;
22+ int kind ;
23+ Py_UCS4 maxchar ;
24+ Py_ssize_t size ;
25+ Py_ssize_t pos ;
26+
27+ /* minimum number of allocated characters (default: 0) */
28+ Py_ssize_t min_length ;
29+
30+ /* minimum character (default: 127, ASCII) */
31+ Py_UCS4 min_char ;
32+
33+ /* If non-zero, overallocate the buffer (default: 0). */
34+ unsigned char overallocate ;
35+
36+ /* If readonly is 1, buffer is a shared string (cannot be modified)
37+ and size is set to 0. */
38+ unsigned char readonly ;
39+ } _PyUnicodeWriter ;
40+
41+ /* Initialize a Unicode writer.
42+ *
43+ * By default, the minimum buffer size is 0 character and overallocation is
44+ * disabled. Set min_length, min_char and overallocate attributes to control
45+ * the allocation of the buffer. */
46+ PyAPI_FUNC (void )
47+ _PyUnicodeWriter_Init (_PyUnicodeWriter * writer );
48+
49+ /* Prepare the buffer to write 'length' characters
50+ with the specified maximum character.
51+
52+ Return 0 on success, raise an exception and return -1 on error. */
53+ #define _PyUnicodeWriter_Prepare (WRITER , LENGTH , MAXCHAR ) \
54+ (((MAXCHAR) <= (WRITER)->maxchar \
55+ && (LENGTH) <= (WRITER)->size - (WRITER)->pos) \
56+ ? 0 \
57+ : (((LENGTH) == 0) \
58+ ? 0 \
59+ : _PyUnicodeWriter_PrepareInternal((WRITER), (LENGTH), (MAXCHAR))))
60+
61+ /* Don't call this function directly, use the _PyUnicodeWriter_Prepare() macro
62+ instead. */
63+ PyAPI_FUNC (int )
64+ _PyUnicodeWriter_PrepareInternal (_PyUnicodeWriter * writer ,
65+ Py_ssize_t length , Py_UCS4 maxchar );
66+
67+ /* Prepare the buffer to have at least the kind KIND.
68+ For example, kind=PyUnicode_2BYTE_KIND ensures that the writer will
69+ support characters in range U+000-U+FFFF.
70+
71+ Return 0 on success, raise an exception and return -1 on error. */
72+ #define _PyUnicodeWriter_PrepareKind (WRITER , KIND ) \
73+ ((KIND) <= (WRITER)->kind \
74+ ? 0 \
75+ : _PyUnicodeWriter_PrepareKindInternal((WRITER), (KIND)))
76+
77+ /* Don't call this function directly, use the _PyUnicodeWriter_PrepareKind()
78+ macro instead. */
79+ PyAPI_FUNC (int )
80+ _PyUnicodeWriter_PrepareKindInternal (_PyUnicodeWriter * writer ,
81+ int kind );
82+
83+ /* Append a Unicode character.
84+ Return 0 on success, raise an exception and return -1 on error. */
85+ PyAPI_FUNC (int )
86+ _PyUnicodeWriter_WriteChar (_PyUnicodeWriter * writer ,
87+ Py_UCS4 ch
88+ );
89+
90+ /* Append a Unicode string.
91+ Return 0 on success, raise an exception and return -1 on error. */
92+ PyAPI_FUNC (int )
93+ _PyUnicodeWriter_WriteStr (_PyUnicodeWriter * writer ,
94+ PyObject * str /* Unicode string */
95+ );
96+
97+ /* Append a substring of a Unicode string.
98+ Return 0 on success, raise an exception and return -1 on error. */
99+ PyAPI_FUNC (int )
100+ _PyUnicodeWriter_WriteSubstring (_PyUnicodeWriter * writer ,
101+ PyObject * str , /* Unicode string */
102+ Py_ssize_t start ,
103+ Py_ssize_t end
104+ );
105+
106+ /* Append an ASCII-encoded byte string.
107+ Return 0 on success, raise an exception and return -1 on error. */
108+ PyAPI_FUNC (int )
109+ _PyUnicodeWriter_WriteASCIIString (_PyUnicodeWriter * writer ,
110+ const char * str , /* ASCII-encoded byte string */
111+ Py_ssize_t len /* number of bytes, or -1 if unknown */
112+ );
113+
114+ /* Append a latin1-encoded byte string.
115+ Return 0 on success, raise an exception and return -1 on error. */
116+ PyAPI_FUNC (int )
117+ _PyUnicodeWriter_WriteLatin1String (_PyUnicodeWriter * writer ,
118+ const char * str , /* latin1-encoded byte string */
119+ Py_ssize_t len /* length in bytes */
120+ );
121+
122+ /* Get the value of the writer as a Unicode string. Clear the
123+ buffer of the writer. Raise an exception and return NULL
124+ on error. */
125+ PyAPI_FUNC (PyObject * )
126+ _PyUnicodeWriter_Finish (_PyUnicodeWriter * writer );
127+
128+ /* Deallocate memory of a writer (clear its internal buffer). */
129+ PyAPI_FUNC (void )
130+ _PyUnicodeWriter_Dealloc (_PyUnicodeWriter * writer );
131+
132+
133+ /* Format the object based on the format_spec, as defined in PEP 3101
134+ (Advanced String Formatting). */
135+ PyAPI_FUNC (int ) _PyUnicode_FormatAdvancedWriter (
136+ _PyUnicodeWriter * writer ,
137+ PyObject * obj ,
138+ PyObject * format_spec ,
139+ Py_ssize_t start ,
140+ Py_ssize_t end );
141+
142+ /* --- Methods & Slots ---------------------------------------------------- */
143+
144+ /* Using explicit passed-in values, insert the thousands grouping
145+ into the string pointed to by buffer. For the argument descriptions,
146+ see Objects/stringlib/localeutil.h */
147+ PyAPI_FUNC (Py_ssize_t ) _PyUnicode_InsertThousandsGrouping (
148+ _PyUnicodeWriter * writer ,
149+ Py_ssize_t n_buffer ,
150+ PyObject * digits ,
151+ Py_ssize_t d_pos ,
152+ Py_ssize_t n_digits ,
153+ Py_ssize_t min_width ,
154+ const char * grouping ,
155+ PyObject * thousands_sep ,
156+ Py_UCS4 * maxchar );
157+
158+ /* --- Runtime lifecycle -------------------------------------------------- */
18159
19160extern void _PyUnicode_InitState (PyInterpreterState * );
20161extern PyStatus _PyUnicode_InitGlobalObjects (PyInterpreterState * );
@@ -24,7 +165,7 @@ extern void _PyUnicode_FiniTypes(PyInterpreterState *);
24165
25166extern PyTypeObject _PyUnicodeASCIIIter_Type ;
26167
27- /* other API */
168+ /* --- Other API ---------------------------------------------------------- */
28169
29170struct _Py_unicode_runtime_ids {
30171 PyThread_type_lock lock ;
0 commit comments