[Interest] Filling QString from raw UTF16 data

Thiago Macieira thiago at kde.org
Sat Aug 11 22:25:31 CEST 2012


On sábado, 11 de agosto de 2012 20.51.30, Konstantin Tokarev wrote:
> Hi all,
> 
> I'd like to use SCSU encoding from ICU library in my Qt application. Here is
> a code of my decoding function:
> 
>     QString result;
>     result.resize(maxSize); // maxSize is guaranteed to be >= size
>     const int size = ucnv_toUChars(m_converter, const_cast<ushort
> *>(result.utf16()), maxSize, a, inSize, &status); 

utf16() is wrong.

Instead, use:
	reinterpret_cast<ushort *>(result.data())

> result.resize(size);
>     return result;
> 
> (ucnv_toUChars fills array in second argument with UTF16 string and returns
> its size)
> 
> Is it safe to use return value of QString::utf16() this way in this context
> (QString is always created on stack locally)?

No.

-- 
Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
   Software Architect - Intel Open Source Technology Center
      PGP/GPG: 0x6EF45358; fingerprint:
      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 190 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20120811/85719190/attachment.sig>


More information about the Interest mailing list