[docs] Query re array 'u' type code

Mark Summerfield list at qtrac.plus.com
Tue Oct 9 08:38:00 CEST 2012


On Mon, 8 Oct 2012 17:57:36 +0300
Andrew Svetlov <andrew.svetlov at gmail.com> wrote:
> I'm ok with utf-32 example.
> 
> Mark, can you guess exact correction text as native English person?
> Or, better, would you make an issue with patch?
[snip]

Short:

    The best way to replace an array('u') is with a string. However, if
    the code being ported needs mutability, then one solution is to use
    an array('L') and convert strings to/from the array as needed.

Long:

    The best way to replace an array('u') is with a string. However, if
    the code being ported needs mutability, then one solution is to use
    an array('L') and convert strings to/from the array as needed.

    Here are two helper functions that provide the conversions:

    def utf32_ints_for_str(s):
	'''Returns a list of UTF-32-BE ints suitable for an array('L')'''
	return [struct.unpack('I', c.encode('utf-32-be'))[0] for c in s]

    def str_for_utf32_ints(uints):
	'''Returns a str for a sequence of UTF-32-BE ints such as an array('L')'''
	return ''.join([struct.pack('I', uint).decode('utf-32-be') for uint in uints])

    (Note that array uses 'L' for unsigned 32-bit ints, but struct uses 'I'.)

    Example:

	s = "More \u20acs!"
	uints = utf32_ints_for_str(s)
	a = array.array("L")
	a.extend(uints)
	t = str_for_utf32_ints(a)
	assert s == t

-- 
Mark Summerfield, Qtrac Ltd, www.qtrac.eu
    C++, Python, Qt, PyQt - training and consultancy
        "Advanced Qt Programming" - ISBN 0321635906
            http://www.qtrac.eu/aqpbook.html


More information about the docs mailing list