@@ -220,109 +220,61 @@ unicode_copycharacters(PyObject *self, PyObject *args)
220
220
return Py_BuildValue ("(Nn)" , to_copy , copied );
221
221
}
222
222
223
- /* Test PyUnicode_ToLower() */
224
223
static PyObject *
225
- unicode_tolower (PyObject * self , PyObject * arg )
224
+ unicode_case_operation (PyObject * str , int ( * function )( Py_UCS4 , Py_UCS4 * , int ), const char * name )
226
225
{
227
- if (PyUnicode_GET_LENGTH (arg ) != 1 ) {
228
- PyErr_SetString (PyExc_ValueError , "unicode_tolower only accepts 1-character strings" );
226
+ if (PyUnicode_GET_LENGTH (str ) != 1 ) {
227
+ PyErr_Format (PyExc_ValueError , "%s only accepts 1-character strings" , name );
229
228
return NULL ;
230
229
}
231
230
232
- Py_UCS4 c = PyUnicode_READ_CHAR (arg , 0 );
231
+ Py_UCS4 c = PyUnicode_READ_CHAR (str , 0 );
233
232
234
- Py_UCS4 lower [3 ];
235
- int chars = PyUnicode_ToLower (c , lower , Py_ARRAY_LENGTH (lower ));
236
- assert (chars >= 1 );
233
+ Py_UCS4 buf [3 ];
234
+ int chars = function (c , buf , Py_ARRAY_LENGTH (buf ));
235
+ if (chars <= 0 ) {
236
+ PyErr_BadInternalCall ();
237
+ return NULL ;
238
+ }
237
239
238
240
PyUnicodeWriter * writer = PyUnicodeWriter_Create (1 );
239
241
if (writer == NULL ) {
240
242
return NULL ;
241
243
}
242
- if (PyUnicodeWriter_WriteUCS4 (writer , lower , chars ) < 0 ) {
244
+ if (PyUnicodeWriter_WriteUCS4 (writer , buf , chars ) < 0 ) {
243
245
PyUnicodeWriter_Discard (writer );
244
246
return NULL ;
245
247
}
246
248
return PyUnicodeWriter_Finish (writer );
247
249
}
248
250
251
+ /* Test PyUnicode_ToLower() */
252
+ static PyObject *
253
+ unicode_tolower (PyObject * self , PyObject * arg )
254
+ {
255
+ return unicode_case_operation (arg , PyUnicode_ToLower , "unicode_tolower" );
256
+ }
257
+
249
258
/* Test PyUnicode_ToUpper() */
250
259
static PyObject *
251
260
unicode_toupper (PyObject * self , PyObject * arg )
252
261
{
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 );
262
+ return unicode_case_operation (arg , PyUnicode_ToUpper , "unicode_toupper" );
273
263
}
274
264
275
265
276
266
/* Test PyUnicode_ToLower() */
277
267
static PyObject *
278
268
unicode_totitle (PyObject * self , PyObject * arg )
279
269
{
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 );
270
+ return unicode_case_operation (arg , PyUnicode_ToTitle , "unicode_totitle" );
300
271
}
301
272
302
273
/* Test PyUnicode_ToLower() */
303
274
static PyObject *
304
275
unicode_tofolded (PyObject * self , PyObject * arg )
305
276
{
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 );
277
+ return unicode_case_operation (arg , PyUnicode_ToFolded , "unicode_tofolded" );
326
278
}
327
279
328
280
0 commit comments