trinity-devel@lists.pearsoncomputing.net

Message: previous - next
Month: March 2016

Re: [trinity-devel] TQString::fromUtf8 vs TQString::fromLatin1

From: Michele Calgaro <michele.calgaro@...>
Date: Wed, 23 Mar 2016 11:31:40 +0900
On 03/23/2016 09:39 AM, Fat-Zer wrote:
> 2016-03-23 2:08 GMT+03:00 deloptes <deloptes@...>:
>> Hi all,
>>
>> I need some help again.
>>
>> Which is the preferred function to use when creating TQString from
>> std::string and how can I make sure that I end up with Utf-8.
>>
>> The thing is that input in std::string can be either UTF-8 or not UTF-8.
>>
>> What is the standard way of doing this in TDE (TQt)?
>>
>> I am really confused, because I was looking in some KDE3/TDE code and I see
>> both used.
>>
>> My problem is that some older phones would most likely lack UTF and newer
>> would do only UTF. So how can I make sure to "speak the right language"
>> with them?
>>
>> A hint would be appreciated.
>>
>> regards
> 
> 1) If you construct a string from a const char * c-string in your code
> you better use fromLatin1(). e.g TQString::fromLatin1 ("blabla")
> 1.1) If the string includes some local symbols or some non-latin1
> symbols somewhy, but your source xode is strictly in utf8 you may use
> TQString::fromUtf8 ("10°") [note the degree sign], But this is kinda
> dirty practice
> 2) If you receive a string from OS e.g. a file path from system calls,
> you would likely should use TQString::fromLocal8bit(), Note that it
> will decode from utf8 on most modern linux boxes.
> 3) If you receive a string from some third party module or where ever
> else you should follow to it's documentation. It may return a text in
> some other encoding, and you will have to use TQTextCodec (or whatever
> it's called).
> 3.1) If you are not sure if it will give you either a latin1 or utf8
> string, You are safe to use TQString::fromUtf8()
> 
> Note that it's quite safe to use fromUtf8() everywhere instead of
> fromLatin1(), in most of cases you risk to get just some performance
> overhead...
> 

Internally TQString is basically a TQChar array.
As Alexander said, TQString::fromUtf8() is probably the safest way to go for most of the cases.
Use TQTextCodec::codecForCStrings() if you want to set a specific 8bit to Unicode codec for c-strings. The default is
latin1 anyway, so even a simple TQString(<your c-string>) would work in case you know the string is a latin1 string.

I disagree with Alex on point 2). I would still go for TQString::fromUtf8() if I am handling strings from the OS, just
in case ;-)

Cheers
  Michele



Attachments: