[PATCH v4 2/4] Update UTF-8 charmap processing.

Carlos O'Donell carlos@redhat.com
Thu Apr 29 21:02:04 GMT 2021


On 4/29/21 10:07 AM, Florian Weimer wrote:
> * Carlos O'Donell:
> 
>>  def convert_to_hex(code_point):
>>      '''Converts a code point to a hexadecimal UTF-8 representation
>> -    like /x**/x**/x**.'''
>> -    # Getting UTF8 of Unicode characters.
>> -    # In Python3, .encode('UTF-8') does not work for
>> -    # surrogates. Therefore, we use this conversion table
>> -    surrogates = {
>> -        0xD800: '/xed/xa0/x80',
>> -        0xDB7F: '/xed/xad/xbf',
>> -        0xDB80: '/xed/xae/x80',
>> -        0xDBFF: '/xed/xaf/xbf',
>> -        0xDC00: '/xed/xb0/x80',
>> -        0xDFFF: '/xed/xbf/xbf',
>> -    }
>> -    if code_point in surrogates:
>> -        return surrogates[code_point]
>> -    return ''.join([
>> -        '/x{:02x}'.format(c) for c in chr(code_point).encode('UTF-8')
>> -    ])
>> +    ready for use in a locale character map specification e.g.
>> +    /xc2/xaf for MACRON.
>> +
>> +    '''
>> +    cp_locale = ''
>> +    cp_bytes = chr(code_point).encode('UTF-8', 'surrogatepass')
>> +    for byte in cp_bytes:
>> +       cp_locale += ''.join('/x{:02x}'.format(byte))
>> +    return cp_locale
> 
> I think you should keep the list comprehension.  That ''.join() is
> unnecessary.

Like this?

    return ''.join(['/x{:02x}'.format(c) \
        for c in chr(code_point).encode('UTF-8', 'surrogatepass')])

(tested works fine and produces the same results)

-- 
Cheers,
Carlos.



More information about the Libc-alpha mailing list