@@ -106,24 +106,6 @@ unicode_to_ascii_resolve_descriptors(PyObject *NPY_UNUSED(self),
106106 return NPY_UNSAFE_CASTING ;
107107}
108108
109- static int
110- ucs4_character_is_ascii (char * buffer )
111- {
112- unsigned char first_char = buffer [0 ];
113-
114- if (first_char > 127 ) {
115- return -1 ;
116- }
117-
118- for (int i = 1 ; i < 4 ; i ++ ) {
119- if (buffer [i ] != 0 ) {
120- return -1 ;
121- }
122- }
123-
124- return 0 ;
125- }
126-
127109static int
128110unicode_to_ascii (PyArrayMethod_Context * context , char * const data [],
129111 npy_intp const dimensions [], npy_intp const strides [],
@@ -151,15 +133,15 @@ unicode_to_ascii(PyArrayMethod_Context *context, char *const data[],
151133 // copy input characters, checking that input UCS4
152134 // characters are all ascii, raising an error otherwise
153135 for (int i = 0 ; i < copy_size ; i ++ ) {
154- if (ucs4_character_is_ascii (in ) == -1 ) {
136+ Py_UCS4 c = ((Py_UCS4 * )in )[i ];
137+ if (c > 127 ) {
155138 PyErr_SetString (
156139 PyExc_TypeError ,
157140 "Can only store ASCII text in a ASCIIDType array." );
158141 return -1 ;
159142 }
160- // UCS4 character is ascii, so copy first byte of character
161- // into output, ignoring the rest
162- * (out + i ) = * (in + i * 4 );
143+ // UCS4 character is ascii, so casting to Py_UCS1 does not truncate
144+ out [i ] = (Py_UCS1 )c ;
163145 }
164146 // write zeros to remaining ASCII characters (if any)
165147 for (int i = copy_size ; i < out_size ; i ++ ) {
0 commit comments