@@ -174,14 +174,13 @@ ascii_escape_unichar(Py_UCS4 c, unsigned char *output, Py_ssize_t chars)
174174 default :
175175 if (c >= 0x10000 ) {
176176 /* UTF-16 surrogate pair */
177- Py_UCS4 v = c - 0x10000 ;
178- c = 0xd800 | ((v >> 10 ) & 0x3ff );
177+ Py_UCS4 v = Py_UNICODE_HIGH_SURROGATE (c );
179178 output [chars ++ ] = 'u' ;
180- output [chars ++ ] = Py_hexdigits [(c >> 12 ) & 0xf ];
181- output [chars ++ ] = Py_hexdigits [(c >> 8 ) & 0xf ];
182- output [chars ++ ] = Py_hexdigits [(c >> 4 ) & 0xf ];
183- output [chars ++ ] = Py_hexdigits [(c ) & 0xf ];
184- c = 0xdc00 | ( v & 0x3ff );
179+ output [chars ++ ] = Py_hexdigits [(v >> 12 ) & 0xf ];
180+ output [chars ++ ] = Py_hexdigits [(v >> 8 ) & 0xf ];
181+ output [chars ++ ] = Py_hexdigits [(v >> 4 ) & 0xf ];
182+ output [chars ++ ] = Py_hexdigits [(v ) & 0xf ];
183+ c = Py_UNICODE_LOW_SURROGATE ( c );
185184 output [chars ++ ] = '\\' ;
186185 }
187186 output [chars ++ ] = 'u' ;
@@ -431,7 +430,7 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next
431430 }
432431 }
433432 /* Surrogate pair */
434- if (( c & 0xfc00 ) == 0xd800 ) {
433+ if (Py_UNICODE_IS_HIGH_SURROGATE ( c ) ) {
435434 Py_UCS4 c2 = 0 ;
436435 if (end + 6 >= len ) {
437436 raise_errmsg ("Unpaired high surrogate" , pystr , end - 5 );
@@ -462,13 +461,13 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next
462461 goto bail ;
463462 }
464463 }
465- if ((c2 & 0xfc00 ) != 0xdc00 ) {
464+ if (! Py_UNICODE_IS_LOW_SURROGATE (c2 ) ) {
466465 raise_errmsg ("Unpaired high surrogate" , pystr , end - 5 );
467466 goto bail ;
468467 }
469- c = 0x10000 + ((( c - 0xd800 ) << 10 ) | ( c2 - 0xdc00 ) );
468+ c = Py_UNICODE_JOIN_SURROGATES ( c , c2 );
470469 }
471- else if (( c & 0xfc00 ) == 0xdc00 ) {
470+ else if (Py_UNICODE_IS_LOW_SURROGATE ( c ) ) {
472471 raise_errmsg ("Unpaired low surrogate" , pystr , end - 5 );
473472 goto bail ;
474473 }
0 commit comments