@@ -220,6 +220,112 @@ unicode_copycharacters(PyObject *self, PyObject *args)
220
220
return Py_BuildValue ("(Nn)" , to_copy , copied );
221
221
}
222
222
223
+ /* Test PyUnicode_ToLower() */
224
+ static PyObject *
225
+ unicode_tolower (PyObject * self , PyObject * arg )
226
+ {
227
+ if (PyUnicode_GET_LENGTH (arg ) != 1 ) {
228
+ PyErr_SetString (PyExc_ValueError , "unicode_tolower only accepts 1-character strings" );
229
+ return NULL ;
230
+ }
231
+
232
+ Py_UCS4 c = PyUnicode_READ_CHAR (arg , 0 );
233
+
234
+ Py_UCS4 lower [3 ];
235
+ int chars = PyUnicode_ToLower (c , lower , Py_ARRAY_LENGTH (lower ));
236
+ assert (chars >= 1 );
237
+
238
+ PyUnicodeWriter * writer = PyUnicodeWriter_Create (1 );
239
+ if (writer == NULL ) {
240
+ return NULL ;
241
+ }
242
+ if (PyUnicodeWriter_WriteUCS4 (writer , lower , chars ) < 0 ) {
243
+ PyUnicodeWriter_Discard (writer );
244
+ return NULL ;
245
+ }
246
+ return PyUnicodeWriter_Finish (writer );
247
+ }
248
+
249
+ /* Test PyUnicode_ToUpper() */
250
+ static PyObject *
251
+ unicode_toupper (PyObject * self , PyObject * arg )
252
+ {
253
+ if (PyUnicode_GET_LENGTH (arg ) != 1 ) {
254
+ PyErr_SetString (PyExc_ValueError , "unicode_toupper only accepts 1-character strings" );
255
+ return NULL ;
256
+ }
257
+
258
+ Py_UCS4 c = PyUnicode_READ_CHAR (arg , 0 );
259
+
260
+ Py_UCS4 upper [3 ];
261
+ int chars = PyUnicode_ToUpper (c , upper , Py_ARRAY_LENGTH (upper ));
262
+ assert (chars >= 1 );
263
+
264
+ PyUnicodeWriter * writer = PyUnicodeWriter_Create (1 );
265
+ if (writer == NULL ) {
266
+ return NULL ;
267
+ }
268
+ if (PyUnicodeWriter_WriteUCS4 (writer , upper , chars ) < 0 ) {
269
+ PyUnicodeWriter_Discard (writer );
270
+ return NULL ;
271
+ }
272
+ return PyUnicodeWriter_Finish (writer );
273
+ }
274
+
275
+
276
+ /* Test PyUnicode_ToLower() */
277
+ static PyObject *
278
+ unicode_totitle (PyObject * self , PyObject * arg )
279
+ {
280
+ if (PyUnicode_GET_LENGTH (arg ) != 1 ) {
281
+ PyErr_SetString (PyExc_ValueError , "unicode_totitle only accepts 1-character strings" );
282
+ return NULL ;
283
+ }
284
+
285
+ Py_UCS4 c = PyUnicode_READ_CHAR (arg , 0 );
286
+
287
+ Py_UCS4 title [3 ];
288
+ int chars = PyUnicode_ToTitle (c , title , Py_ARRAY_LENGTH (title ));
289
+ assert (chars >= 1 );
290
+
291
+ PyUnicodeWriter * writer = PyUnicodeWriter_Create (1 );
292
+ if (writer == NULL ) {
293
+ return NULL ;
294
+ }
295
+ if (PyUnicodeWriter_WriteUCS4 (writer , title , chars ) < 0 ) {
296
+ PyUnicodeWriter_Discard (writer );
297
+ return NULL ;
298
+ }
299
+ return PyUnicodeWriter_Finish (writer );
300
+ }
301
+
302
+ /* Test PyUnicode_ToLower() */
303
+ static PyObject *
304
+ unicode_tofolded (PyObject * self , PyObject * arg )
305
+ {
306
+ if (PyUnicode_GET_LENGTH (arg ) != 1 ) {
307
+ PyErr_SetString (PyExc_ValueError , "unicode_tofolded only accepts 1-character strings" );
308
+ return NULL ;
309
+ }
310
+
311
+ Py_UCS4 c = PyUnicode_READ_CHAR (arg , 0 );
312
+
313
+ Py_UCS4 folded [3 ];
314
+ int chars = PyUnicode_ToFolded (c , folded , Py_ARRAY_LENGTH (folded ));
315
+ assert (chars >= 1 );
316
+
317
+ PyUnicodeWriter * writer = PyUnicodeWriter_Create (1 );
318
+ if (writer == NULL ) {
319
+ return NULL ;
320
+ }
321
+ if (PyUnicodeWriter_WriteUCS4 (writer , folded , chars ) < 0 ) {
322
+ PyUnicodeWriter_Discard (writer );
323
+ return NULL ;
324
+ }
325
+ return PyUnicodeWriter_Finish (writer );
326
+ }
327
+
328
+
223
329
static PyObject *
224
330
unicode_GET_CACHED_HASH (PyObject * self , PyObject * arg )
225
331
{
@@ -577,6 +683,10 @@ static PyMethodDef TestMethods[] = {
577
683
{"unicode_asutf8" , unicode_asutf8 , METH_VARARGS },
578
684
{"unicode_copycharacters" , unicode_copycharacters , METH_VARARGS },
579
685
{"unicode_GET_CACHED_HASH" , unicode_GET_CACHED_HASH , METH_O },
686
+ {"unicode_tolower" , unicode_tolower , METH_O },
687
+ {"unicode_toupper" , unicode_toupper , METH_O },
688
+ {"unicode_totitle" , unicode_totitle , METH_O },
689
+ {"unicode_tofolded" , unicode_tofolded , METH_O },
580
690
{NULL },
581
691
};
582
692
0 commit comments