@@ -82,22 +82,59 @@ static PyObject *_Py_strhex_impl(const char* argbuf, const Py_ssize_t arglen,
8282
8383 /* Hexlify */
8484 Py_ssize_t i , j ;
85- for (i = j = 0 ; i < arglen ; ++ i ) {
86- assert ((j + 1 ) < resultlen );
87- unsigned char c ;
88- c = (argbuf [i ] >> 4 ) & 0x0f ;
89- retbuf [j ++ ] = Py_hexdigits [c ];
90- c = argbuf [i ] & 0x0f ;
91- retbuf [j ++ ] = Py_hexdigits [c ];
92- if (bytes_per_sep_group && i < arglen - 1 ) {
93- Py_ssize_t anchor ;
94- anchor = (bytes_per_sep_group > 0 ) ? (arglen - 1 - i ) : (i + 1 );
95- if (anchor % abs_bytes_per_sep == 0 ) {
85+ unsigned char c ;
86+
87+ if (bytes_per_sep_group == 0 ) {
88+ for (i = j = 0 ; i < arglen ; ++ i ) {
89+ assert ((j + 1 ) < resultlen );
90+ c = argbuf [i ];
91+ retbuf [j ++ ] = Py_hexdigits [c >> 4 ];
92+ retbuf [j ++ ] = Py_hexdigits [c & 0x0f ];
93+ }
94+ assert (j == resultlen );
95+ }
96+ else {
97+ /* The number of complete chunk+sep periods */
98+ Py_ssize_t chunks = (arglen - 1 ) / abs_bytes_per_sep ;
99+ Py_ssize_t chunk ;
100+ unsigned int k ;
101+
102+ if (bytes_per_sep_group < 0 ) {
103+ i = j = 0 ;
104+ for (chunk = 0 ; chunk < chunks ; chunk ++ ) {
105+ for (k = 0 ; k < abs_bytes_per_sep ; k ++ ) {
106+ c = argbuf [i ++ ];
107+ retbuf [j ++ ] = Py_hexdigits [c >> 4 ];
108+ retbuf [j ++ ] = Py_hexdigits [c & 0x0f ];
109+ }
96110 retbuf [j ++ ] = sep_char ;
97111 }
112+ while (i < arglen ) {
113+ c = argbuf [i ++ ];
114+ retbuf [j ++ ] = Py_hexdigits [c >> 4 ];
115+ retbuf [j ++ ] = Py_hexdigits [c & 0x0f ];
116+ }
117+ assert (j == resultlen );
118+ }
119+ else {
120+ i = arglen - 1 ;
121+ j = resultlen - 1 ;
122+ for (chunk = 0 ; chunk < chunks ; chunk ++ ) {
123+ for (k = 0 ; k < abs_bytes_per_sep ; k ++ ) {
124+ c = argbuf [i -- ];
125+ retbuf [j -- ] = Py_hexdigits [c & 0x0f ];
126+ retbuf [j -- ] = Py_hexdigits [c >> 4 ];
127+ }
128+ retbuf [j -- ] = sep_char ;
129+ }
130+ while (i >= 0 ) {
131+ c = argbuf [i -- ];
132+ retbuf [j -- ] = Py_hexdigits [c & 0x0f ];
133+ retbuf [j -- ] = Py_hexdigits [c >> 4 ];
134+ }
135+ assert (j == -1 );
98136 }
99137 }
100- assert (j == resultlen );
101138
102139#ifdef Py_DEBUG
103140 if (!return_bytes ) {
0 commit comments