[Qt-interest] Does Qt support Unicode 5.1?

Constantin Makshin dinosaur-rus at users.sourceforge.net
Fri Mar 6 19:37:58 CET 2009


I'd like to apologize, I was wrong. After comparing values returned by my  
and Qt functions on whole Unicode range I confirm that Qt functions give  
correct results. There might be another mistake in the code which caused  
incorrect behavior and confused me.

But I'm still curious on your reason for choosing modulus instead of  
bitwise-and. Isn't the latter operation faster than the former one ("on  
the safe-side")?

To Shaun:
You can safely replace my HighSurrogate() and LowSurrogate() functions  
with QChar::highSurrogate() and QChar::lowSurrogate() ones, respectively.  
Sorry for confusing you.

On Fri, 06 Mar 2009 04:20:26 +0300, Thiago Macieira  
<thiago.macieira at trolltech.com> wrote:
> Constantin Makshin wrote:
>> static QChar HighSurrogate (unsigned c)
>> {
>>      return QChar(((c - 0x10000) >> 10 & 0x3ff) + 0xd800);
>> }
>>
>> static QChar LowSurrogate (unsigned c)
>> {
>>      return QChar((c & 0x3ff) + 0xdc00);
>> }
>>
>> To Qt developers: Your surrogate characters handling code seems to be  
>> wrong
>
> Would you care to elaborate? What's wrong with QChar::lowSurrogate and
> QChar::highSurrogate?
>
>     static inline ushort highSurrogate(uint ucs4) {
>         return (ucs4>>10) + 0xd7c0;
>     }
>     static inline ushort lowSurrogate(uint ucs4) {
>         return ucs4%0x400 + 0xdc00;
>     }
>
> They're written differently, but it's the same math. The low surrogate is
> easy to spot (modulus 0x400 and bitwise-and 0x3ff are the same  
> operation).
>
> As for the high surrogate, instead of subtracting 0x10000 before the
> right-shift, we subtracted it after. See the math below. Note that the
> bitwise-and for 0x3ff is unnecessary since Unicode is limited to 0x10FFFF
> anyways.
>
> 	((x - 0x10000) >> 10) + 0xd800
> 	(x >> 10) - (0x10000 >> 10) + 0xd800
> 	(x >> 10) - 0x40 + 0xd800
> 	(x >> 10) + 0xd7c0
>
> In fact, a good compiler should realise that and optimise the extra
> operation away. We're just on the safe-side and doing that work for the
> compiler.

-- 
Constantin "Dinosaur" Makshin



More information about the Qt-interest-old mailing list