@@ -62,59 +62,6 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
6262#include "stringlib/undef.h"
6363
6464
65- /* Copy an ASCII or latin1 char* string into a Python Unicode string.
66-
67- WARNING: The function doesn't copy the terminating null character and
68- doesn't check the maximum character (may write a latin1 character in an
69- ASCII string). */
70- static void
71- unicode_write_cstr (PyObject * unicode , Py_ssize_t index ,
72- const char * str , Py_ssize_t len )
73- {
74- int kind = PyUnicode_KIND (unicode );
75- const void * data = PyUnicode_DATA (unicode );
76- const char * end = str + len ;
77-
78- assert (index + len <= PyUnicode_GET_LENGTH (unicode ));
79- switch (kind ) {
80- case PyUnicode_1BYTE_KIND : {
81- #ifdef Py_DEBUG
82- if (PyUnicode_IS_ASCII (unicode )) {
83- Py_UCS4 maxchar = ucs1lib_find_max_char (
84- (const Py_UCS1 * )str ,
85- (const Py_UCS1 * )str + len );
86- assert (maxchar < 128 );
87- }
88- #endif
89- memcpy ((char * ) data + index , str , len );
90- break ;
91- }
92- case PyUnicode_2BYTE_KIND : {
93- Py_UCS2 * start = (Py_UCS2 * )data + index ;
94- Py_UCS2 * ucs2 = start ;
95-
96- for (; str < end ; ++ ucs2 , ++ str )
97- * ucs2 = (Py_UCS2 )* str ;
98-
99- assert ((ucs2 - start ) <= PyUnicode_GET_LENGTH (unicode ));
100- break ;
101- }
102- case PyUnicode_4BYTE_KIND : {
103- Py_UCS4 * start = (Py_UCS4 * )data + index ;
104- Py_UCS4 * ucs4 = start ;
105-
106- for (; str < end ; ++ ucs4 , ++ str )
107- * ucs4 = (Py_UCS4 )* str ;
108-
109- assert ((ucs4 - start ) <= PyUnicode_GET_LENGTH (unicode ));
110- break ;
111- }
112- default :
113- Py_UNREACHABLE ();
114- }
115- }
116-
117-
11865static inline void
11966_PyUnicodeWriter_Update (_PyUnicodeWriter * writer )
12067{
@@ -594,13 +541,43 @@ int
594541_PyUnicodeWriter_WriteLatin1String (_PyUnicodeWriter * writer ,
595542 const char * str , Py_ssize_t len )
596543{
597- Py_UCS4 maxchar ;
544+ if (len == 0 ) {
545+ return 0 ;
546+ }
598547
599- maxchar = ucs1lib_find_max_char ((const Py_UCS1 * )str , (const Py_UCS1 * )str + len );
600- if (_PyUnicodeWriter_Prepare (writer , len , maxchar ) == -1 )
548+ const Py_UCS1 * ucs1 = (const Py_UCS1 * )str ;
549+ Py_UCS4 maxchar = ucs1lib_find_max_char (ucs1 , ucs1 + len );
550+ if (_PyUnicodeWriter_Prepare (writer , len , maxchar ) < 0 ) {
601551 return -1 ;
552+ }
602553 assert (_PyUnicodeWriter_CanWrite (writer ));
603- unicode_write_cstr (writer -> buffer , writer -> pos , str , len );
554+
555+ Py_ssize_t index = writer -> pos ;
556+ switch (writer -> kind ) {
557+ case PyUnicode_1BYTE_KIND : {
558+ memcpy ((Py_UCS1 * )writer -> data + index , ucs1 , len );
559+ break ;
560+ }
561+ case PyUnicode_2BYTE_KIND : {
562+ Py_UCS2 * ucs2 = (Py_UCS2 * )writer -> data + index ;
563+ const Py_UCS1 * end = ucs1 + len ;
564+ for (; ucs1 < end ; ++ ucs2 , ++ ucs1 ) {
565+ * ucs2 = (Py_UCS2 )* ucs1 ;
566+ }
567+ break ;
568+ }
569+ case PyUnicode_4BYTE_KIND : {
570+ Py_UCS4 * ucs4 = (Py_UCS4 * )writer -> data + index ;
571+ const Py_UCS1 * end = ucs1 + len ;
572+ for (; ucs1 < end ; ++ ucs4 , ++ ucs1 ) {
573+ * ucs4 = (Py_UCS4 )* ucs1 ;
574+ }
575+ break ;
576+ }
577+ default :
578+ Py_UNREACHABLE ();
579+ }
580+
604581 writer -> pos += len ;
605582 return 0 ;
606583}
0 commit comments