@@ -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-
11865void
11966_PyUnicodeWriter_Init (_PyUnicodeWriter * writer )
12067{
@@ -550,13 +497,43 @@ int
550497_PyUnicodeWriter_WriteLatin1String (_PyUnicodeWriter * writer ,
551498 const char * str , Py_ssize_t len )
552499{
553- Py_UCS4 maxchar ;
500+ if (len == 0 ) {
501+ return 0 ;
502+ }
554503
555- maxchar = ucs1lib_find_max_char ((const Py_UCS1 * )str , (const Py_UCS1 * )str + len );
556- if (_PyUnicodeWriter_Prepare (writer , len , maxchar ) == -1 )
504+ const Py_UCS1 * ucs1 = (const Py_UCS1 * )str ;
505+ Py_UCS4 maxchar = ucs1lib_find_max_char (ucs1 , ucs1 + len );
506+ if (_PyUnicodeWriter_Prepare (writer , len , maxchar ) < 0 ) {
557507 return -1 ;
508+ }
558509 assert (_PyUnicodeWriter_CanWrite (writer ));
559- unicode_write_cstr (writer -> buffer , writer -> pos , str , len );
510+
511+ Py_ssize_t index = writer -> pos ;
512+ switch (writer -> kind ) {
513+ case PyUnicode_1BYTE_KIND : {
514+ memcpy ((Py_UCS1 * )writer -> data + index , ucs1 , len );
515+ break ;
516+ }
517+ case PyUnicode_2BYTE_KIND : {
518+ Py_UCS2 * ucs2 = (Py_UCS2 * )writer -> data + index ;
519+ const Py_UCS1 * end = ucs1 + len ;
520+ for (; ucs1 < end ; ++ ucs2 , ++ ucs1 ) {
521+ * ucs2 = (Py_UCS2 )* ucs1 ;
522+ }
523+ break ;
524+ }
525+ case PyUnicode_4BYTE_KIND : {
526+ Py_UCS4 * ucs4 = (Py_UCS4 * )writer -> data + index ;
527+ const Py_UCS1 * end = ucs1 + len ;
528+ for (; ucs1 < end ; ++ ucs4 , ++ ucs1 ) {
529+ * ucs4 = (Py_UCS4 )* ucs1 ;
530+ }
531+ break ;
532+ }
533+ default :
534+ Py_UNREACHABLE ();
535+ }
536+
560537 writer -> pos += len ;
561538 return 0 ;
562539}
0 commit comments