@@ -246,23 +246,15 @@ Return a copy of B with all ASCII characters converted to lowercase.");
246246void
247247_Py_bytes_lower (char * result , const char * cptr , Py_ssize_t len )
248248{
249- Py_ssize_t i ;
249+ Py_ssize_t i ;
250250
251- /*
252- newobj = PyBytes_FromStringAndSize(NULL, len);
253- if (!newobj)
254- return NULL;
251+ Py_MEMCPY (result , cptr , len );
255252
256- s = PyBytes_AS_STRING(newobj);
257- */
258-
259- Py_MEMCPY (result , cptr , len );
260-
261- for (i = 0 ; i < len ; i ++ ) {
262- int c = Py_CHARMASK (result [i ]);
263- if (Py_ISUPPER (c ))
264- result [i ] = Py_TOLOWER (c );
265- }
253+ for (i = 0 ; i < len ; i ++ ) {
254+ int c = Py_CHARMASK (result [i ]);
255+ if (Py_ISUPPER (c ))
256+ result [i ] = Py_TOLOWER (c );
257+ }
266258}
267259
268260
@@ -274,23 +266,15 @@ Return a copy of B with all ASCII characters converted to uppercase.");
274266void
275267_Py_bytes_upper (char * result , const char * cptr , Py_ssize_t len )
276268{
277- Py_ssize_t i ;
278-
279- /*
280- newobj = PyBytes_FromStringAndSize(NULL, len);
281- if (!newobj)
282- return NULL;
269+ Py_ssize_t i ;
283270
284- s = PyBytes_AS_STRING(newobj);
285- */
271+ Py_MEMCPY (result , cptr , len );
286272
287- Py_MEMCPY (result , cptr , len );
288-
289- for (i = 0 ; i < len ; i ++ ) {
290- int c = Py_CHARMASK (result [i ]);
291- if (Py_ISLOWER (c ))
292- result [i ] = Py_TOUPPER (c );
293- }
273+ for (i = 0 ; i < len ; i ++ ) {
274+ int c = Py_CHARMASK (result [i ]);
275+ if (Py_ISLOWER (c ))
276+ result [i ] = Py_TOUPPER (c );
277+ }
294278}
295279
296280
@@ -303,29 +287,23 @@ characters, all remaining cased characters have lowercase.");
303287void
304288_Py_bytes_title (char * result , char * s , Py_ssize_t len )
305289{
306- Py_ssize_t i ;
307- int previous_is_cased = 0 ;
308-
309- /*
310- newobj = PyBytes_FromStringAndSize(NULL, len);
311- if (newobj == NULL)
312- return NULL;
313- s_new = PyBytes_AsString(newobj);
314- */
315- for (i = 0 ; i < len ; i ++ ) {
316- int c = Py_CHARMASK (* s ++ );
317- if (Py_ISLOWER (c )) {
318- if (!previous_is_cased )
319- c = Py_TOUPPER (c );
320- previous_is_cased = 1 ;
321- } else if (Py_ISUPPER (c )) {
322- if (previous_is_cased )
323- c = Py_TOLOWER (c );
324- previous_is_cased = 1 ;
325- } else
326- previous_is_cased = 0 ;
327- * result ++ = c ;
328- }
290+ Py_ssize_t i ;
291+ int previous_is_cased = 0 ;
292+
293+ for (i = 0 ; i < len ; i ++ ) {
294+ int c = Py_CHARMASK (* s ++ );
295+ if (Py_ISLOWER (c )) {
296+ if (!previous_is_cased )
297+ c = Py_TOUPPER (c );
298+ previous_is_cased = 1 ;
299+ } else if (Py_ISUPPER (c )) {
300+ if (previous_is_cased )
301+ c = Py_TOLOWER (c );
302+ previous_is_cased = 1 ;
303+ } else
304+ previous_is_cased = 0 ;
305+ * result ++ = c ;
306+ }
329307}
330308
331309
@@ -338,30 +316,24 @@ and the rest lower-cased.");
338316void
339317_Py_bytes_capitalize (char * result , char * s , Py_ssize_t len )
340318{
341- Py_ssize_t i ;
342-
343- /*
344- newobj = PyBytes_FromStringAndSize(NULL, len);
345- if (newobj == NULL)
346- return NULL;
347- s_new = PyBytes_AsString(newobj);
348- */
349- if (0 < len ) {
350- int c = Py_CHARMASK (* s ++ );
351- if (Py_ISLOWER (c ))
352- * result = Py_TOUPPER (c );
353- else
354- * result = c ;
355- result ++ ;
356- }
357- for (i = 1 ; i < len ; i ++ ) {
358- int c = Py_CHARMASK (* s ++ );
359- if (Py_ISUPPER (c ))
360- * result = Py_TOLOWER (c );
361- else
362- * result = c ;
363- result ++ ;
364- }
319+ Py_ssize_t i ;
320+
321+ if (0 < len ) {
322+ int c = Py_CHARMASK (* s ++ );
323+ if (Py_ISLOWER (c ))
324+ * result = Py_TOUPPER (c );
325+ else
326+ * result = c ;
327+ result ++ ;
328+ }
329+ for (i = 1 ; i < len ; i ++ ) {
330+ int c = Py_CHARMASK (* s ++ );
331+ if (Py_ISUPPER (c ))
332+ * result = Py_TOLOWER (c );
333+ else
334+ * result = c ;
335+ result ++ ;
336+ }
365337}
366338
367339
@@ -374,26 +346,20 @@ to lowercase ASCII and vice versa.");
374346void
375347_Py_bytes_swapcase (char * result , char * s , Py_ssize_t len )
376348{
377- Py_ssize_t i ;
378-
379- /*
380- newobj = PyBytes_FromStringAndSize(NULL, len);
381- if (newobj == NULL)
382- return NULL;
383- s_new = PyBytes_AsString(newobj);
384- */
385- for (i = 0 ; i < len ; i ++ ) {
386- int c = Py_CHARMASK (* s ++ );
387- if (Py_ISLOWER (c )) {
388- * result = Py_TOUPPER (c );
389- }
390- else if (Py_ISUPPER (c )) {
391- * result = Py_TOLOWER (c );
392- }
393- else
394- * result = c ;
395- result ++ ;
349+ Py_ssize_t i ;
350+
351+ for (i = 0 ; i < len ; i ++ ) {
352+ int c = Py_CHARMASK (* s ++ );
353+ if (Py_ISLOWER (c )) {
354+ * result = Py_TOUPPER (c );
396355 }
356+ else if (Py_ISUPPER (c )) {
357+ * result = Py_TOLOWER (c );
358+ }
359+ else
360+ * result = c ;
361+ result ++ ;
362+ }
397363}
398364
399365
@@ -419,47 +385,47 @@ _getbuffer(PyObject *obj, Py_buffer *view)
419385 }
420386
421387 if (buffer -> bf_getbuffer (obj , view , PyBUF_SIMPLE ) < 0 )
422- return -1 ;
388+ return -1 ;
423389 return view -> len ;
424390}
425391
426392PyObject *
427393_Py_bytes_maketrans (PyObject * args )
428394{
429- PyObject * frm , * to , * res = NULL ;
430- Py_buffer bfrm , bto ;
431- Py_ssize_t i ;
432- char * p ;
433-
434- bfrm .len = -1 ;
435- bto .len = -1 ;
436-
437- if (!PyArg_ParseTuple (args , "OO:maketrans" , & frm , & to ))
438- return NULL ;
439- if (_getbuffer (frm , & bfrm ) < 0 )
440- return NULL ;
441- if (_getbuffer (to , & bto ) < 0 )
442- goto done ;
443- if (bfrm .len != bto .len ) {
444- PyErr_Format (PyExc_ValueError ,
445- "maketrans arguments must have same length" );
446- goto done ;
447- }
448- res = PyBytes_FromStringAndSize (NULL , 256 );
449- if (!res ) {
450- goto done ;
451- }
452- p = PyBytes_AS_STRING (res );
453- for (i = 0 ; i < 256 ; i ++ )
454- p [i ] = i ;
455- for (i = 0 ; i < bfrm .len ; i ++ ) {
456- p [((unsigned char * )bfrm .buf )[i ]] = ((char * )bto .buf )[i ];
457- }
395+ PyObject * frm , * to , * res = NULL ;
396+ Py_buffer bfrm , bto ;
397+ Py_ssize_t i ;
398+ char * p ;
399+
400+ bfrm .len = -1 ;
401+ bto .len = -1 ;
402+
403+ if (!PyArg_ParseTuple (args , "OO:maketrans" , & frm , & to ))
404+ return NULL ;
405+ if (_getbuffer (frm , & bfrm ) < 0 )
406+ return NULL ;
407+ if (_getbuffer (to , & bto ) < 0 )
408+ goto done ;
409+ if (bfrm .len != bto .len ) {
410+ PyErr_Format (PyExc_ValueError ,
411+ "maketrans arguments must have same length" );
412+ goto done ;
413+ }
414+ res = PyBytes_FromStringAndSize (NULL , 256 );
415+ if (!res ) {
416+ goto done ;
417+ }
418+ p = PyBytes_AS_STRING (res );
419+ for (i = 0 ; i < 256 ; i ++ )
420+ p [i ] = i ;
421+ for (i = 0 ; i < bfrm .len ; i ++ ) {
422+ p [((unsigned char * )bfrm .buf )[i ]] = ((char * )bto .buf )[i ];
423+ }
458424
459- done :
460- if (bfrm .len != -1 )
461- PyBuffer_Release (& bfrm );
462- if (bto .len != -1 )
463- PyBuffer_Release (& bto );
464- return res ;
425+ done :
426+ if (bfrm .len != -1 )
427+ PyBuffer_Release (& bfrm );
428+ if (bto .len != -1 )
429+ PyBuffer_Release (& bto );
430+ return res ;
465431}
0 commit comments