@@ -10454,10 +10454,10 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject *const *items, Py_ssize_t seq
1045410454 PyObject * item ;
1045510455 Py_ssize_t sz , i , res_offset ;
1045610456 Py_UCS4 maxchar ;
10457- Py_UCS4 item_maxchar ;
10457+ /* kinds is the bitwise OR of the PyUnicode_KIND() values (1, 2, 4). */
10458+ unsigned int kinds = 0 , all_ascii = 1 ;
1045810459 int use_memcpy ;
1045910460 unsigned char * res_data = NULL , * sep_data = NULL ;
10460- PyObject * last_obj ;
1046110461 int kind = 0 ;
1046210462
1046310463 /* If empty sequence, return u"". */
@@ -10466,14 +10466,12 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject *const *items, Py_ssize_t seq
1046610466 }
1046710467
1046810468 /* If singleton sequence with an exact Unicode, return that. */
10469- last_obj = NULL ;
1047010469 if (seqlen == 1 ) {
1047110470 if (PyUnicode_CheckExact (items [0 ])) {
1047210471 res = items [0 ];
1047310472 return Py_NewRef (res );
1047410473 }
1047510474 seplen = 0 ;
10476- maxchar = 0 ;
1047710475 }
1047810476 else {
1047910477 /* Set up sep and seplen */
@@ -10483,7 +10481,6 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject *const *items, Py_ssize_t seq
1048310481 if (!sep )
1048410482 goto onError ;
1048510483 seplen = 1 ;
10486- maxchar = 32 ;
1048710484 }
1048810485 else {
1048910486 if (!PyUnicode_Check (separator )) {
@@ -10495,12 +10492,12 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject *const *items, Py_ssize_t seq
1049510492 }
1049610493 sep = separator ;
1049710494 seplen = PyUnicode_GET_LENGTH (separator );
10498- maxchar = PyUnicode_MAX_CHAR_VALUE (separator );
1049910495 /* inc refcount to keep this code path symmetric with the
1050010496 above case of a blank separator */
1050110497 Py_INCREF (sep );
1050210498 }
10503- last_obj = sep ;
10499+ kinds = PyUnicode_KIND (sep );
10500+ all_ascii = PyUnicode_IS_ASCII (sep );
1050410501 }
1050510502
1050610503 /* There are at least two things to join, or else we have a subclass
@@ -10524,23 +10521,34 @@ _PyUnicode_JoinArray(PyObject *separator, PyObject *const *items, Py_ssize_t seq
1052410521 i , Py_TYPE (item )-> tp_name );
1052510522 goto onError ;
1052610523 }
10527- add_sz = PyUnicode_GET_LENGTH (item );
10528- item_maxchar = PyUnicode_MAX_CHAR_VALUE (item );
10529- maxchar = Py_MAX (maxchar , item_maxchar );
10530- if (i != 0 ) {
10531- add_sz += seplen ;
10532- }
10524+ /* Count one separator per item; the extra one is removed below. */
10525+ add_sz = (size_t )PyUnicode_GET_LENGTH (item ) + (size_t )seplen ;
10526+ kinds |= PyUnicode_KIND (item );
10527+ all_ascii &= PyUnicode_IS_ASCII (item );
1053310528 if (add_sz > (size_t )(PY_SSIZE_T_MAX - sz )) {
1053410529 PyErr_SetString (PyExc_OverflowError ,
1053510530 "join() result is too long for a Python string" );
1053610531 goto onError ;
1053710532 }
1053810533 sz += add_sz ;
10539- if (use_memcpy && last_obj != NULL ) {
10540- if (PyUnicode_KIND (last_obj ) != PyUnicode_KIND (item ))
10541- use_memcpy = 0 ;
10542- }
10543- last_obj = item ;
10534+ }
10535+ sz -= seplen ;
10536+ /* memcpy() can be used if the separator and all items have the same
10537+ kind: the result then has that kind too. */
10538+ if (kinds & (kinds - 1 )) {
10539+ use_memcpy = 0 ;
10540+ }
10541+ if (all_ascii ) {
10542+ maxchar = 0x7f ;
10543+ }
10544+ else if (kinds & PyUnicode_4BYTE_KIND ) {
10545+ maxchar = MAX_UNICODE ;
10546+ }
10547+ else if (kinds & PyUnicode_2BYTE_KIND ) {
10548+ maxchar = 0xffff ;
10549+ }
10550+ else {
10551+ maxchar = 0xff ;
1054410552 }
1054510553
1054610554 res = PyUnicode_New (sz , maxchar );
0 commit comments