@@ -1910,58 +1910,6 @@ PyUnicode_Resize(PyObject **p_unicode, Py_ssize_t length)
19101910 return unicode_resize (p_unicode , length );
19111911}
19121912
1913- /* Copy an ASCII or latin1 char* string into a Python Unicode string.
1914-
1915- WARNING: The function doesn't copy the terminating null character and
1916- doesn't check the maximum character (may write a latin1 character in an
1917- ASCII string). */
1918- static void
1919- unicode_write_cstr (PyObject * unicode , Py_ssize_t index ,
1920- const char * str , Py_ssize_t len )
1921- {
1922- int kind = PyUnicode_KIND (unicode );
1923- const void * data = PyUnicode_DATA (unicode );
1924- const char * end = str + len ;
1925-
1926- assert (index + len <= PyUnicode_GET_LENGTH (unicode ));
1927- switch (kind ) {
1928- case PyUnicode_1BYTE_KIND : {
1929- #ifdef Py_DEBUG
1930- if (PyUnicode_IS_ASCII (unicode )) {
1931- Py_UCS4 maxchar = ucs1lib_find_max_char (
1932- (const Py_UCS1 * )str ,
1933- (const Py_UCS1 * )str + len );
1934- assert (maxchar < 128 );
1935- }
1936- #endif
1937- memcpy ((char * ) data + index , str , len );
1938- break ;
1939- }
1940- case PyUnicode_2BYTE_KIND : {
1941- Py_UCS2 * start = (Py_UCS2 * )data + index ;
1942- Py_UCS2 * ucs2 = start ;
1943-
1944- for (; str < end ; ++ ucs2 , ++ str )
1945- * ucs2 = (Py_UCS2 )* str ;
1946-
1947- assert ((ucs2 - start ) <= PyUnicode_GET_LENGTH (unicode ));
1948- break ;
1949- }
1950- case PyUnicode_4BYTE_KIND : {
1951- Py_UCS4 * start = (Py_UCS4 * )data + index ;
1952- Py_UCS4 * ucs4 = start ;
1953-
1954- for (; str < end ; ++ ucs4 , ++ str )
1955- * ucs4 = (Py_UCS4 )* str ;
1956-
1957- assert ((ucs4 - start ) <= PyUnicode_GET_LENGTH (unicode ));
1958- break ;
1959- }
1960- default :
1961- Py_UNREACHABLE ();
1962- }
1963- }
1964-
19651913static PyObject *
19661914get_latin1_char (Py_UCS1 ch )
19671915{
@@ -14202,12 +14150,42 @@ int
1420214150_PyUnicodeWriter_WriteLatin1String (_PyUnicodeWriter * writer ,
1420314151 const char * str , Py_ssize_t len )
1420414152{
14205- Py_UCS4 maxchar ;
14153+ if (len == 0 ) {
14154+ return 0 ;
14155+ }
1420614156
14207- maxchar = ucs1lib_find_max_char ((const Py_UCS1 * )str , (const Py_UCS1 * )str + len );
14208- if (_PyUnicodeWriter_Prepare (writer , len , maxchar ) == -1 )
14157+ const Py_UCS1 * ucs1 = (const Py_UCS1 * )str ;
14158+ Py_UCS4 maxchar = ucs1lib_find_max_char (ucs1 , ucs1 + len );
14159+ if (_PyUnicodeWriter_Prepare (writer , len , maxchar ) < 0 ) {
1420914160 return -1 ;
14210- unicode_write_cstr (writer -> buffer , writer -> pos , str , len );
14161+ }
14162+
14163+ Py_ssize_t index = writer -> pos ;
14164+ switch (writer -> kind ) {
14165+ case PyUnicode_1BYTE_KIND : {
14166+ memcpy ((Py_UCS1 * )writer -> data + index , ucs1 , len );
14167+ break ;
14168+ }
14169+ case PyUnicode_2BYTE_KIND : {
14170+ Py_UCS2 * ucs2 = (Py_UCS2 * )writer -> data + index ;
14171+ const Py_UCS1 * end = ucs1 + len ;
14172+ for (; ucs1 < end ; ++ ucs2 , ++ ucs1 ) {
14173+ * ucs2 = (Py_UCS2 )* ucs1 ;
14174+ }
14175+ break ;
14176+ }
14177+ case PyUnicode_4BYTE_KIND : {
14178+ Py_UCS4 * ucs4 = (Py_UCS4 * )writer -> data + index ;
14179+ const Py_UCS1 * end = ucs1 + len ;
14180+ for (; ucs1 < end ; ++ ucs4 , ++ ucs1 ) {
14181+ * ucs4 = (Py_UCS4 )* ucs1 ;
14182+ }
14183+ break ;
14184+ }
14185+ default :
14186+ Py_UNREACHABLE ();
14187+ }
14188+
1421114189 writer -> pos += len ;
1421214190 return 0 ;
1421314191}
0 commit comments