[Qt-interest] Converting first character of a char* from local charset
Nikos Chantziaras
realnc at arcor.de
Fri May 27 19:02:28 CEST 2011
On 05/27/2011 07:39 PM, Thiago Macieira wrote:
> On Friday, 27 de May de 2011 19:17:16 Nikos Chantziaras wrote:
>> On 05/27/2011 07:06 PM, Thiago Macieira wrote:
>>> On Friday, 27 de May de 2011 17:21:15 Nikos Chantziaras wrote:
>>>> I can imagine that the performance of this code ain't the best, since
>>>> what it does is call QTextCodec::toUnicode() repeatedly, increasing
>>>> the
>>>> amount of characters each time, until it hits an amount of bytes that
>>>> can be converted.
>>>>
>>>> Is there a better way to achieve this?
>>>
>>> Why do you want to achieve this first?
>>
>> I'm porting a piece of software from Windows to Unix, and the interfaces
>> (abstract classes) I need to implement work like that. The pure virtual
>> I need to implement gets a char* and needs to return the first character
>> of it converted to Unicode, from whatever character set the user's
>> system is using.
>
> That didn't answer the question...
>
> Doing it because some requirement with no reason asks for it is not enough.
> Blindly accepting bad requirements is bad practice and leads to bad code.
It's for a charset-aware getch() implementation (same semantics as the
one from curses.) This version however, needs to read the first
character (which can be multi-byte) from the input buffer, not just the
first byte.
> Try converting the first 32 bytes to QString and then get the first one out of
> the string.
This won't work, because then I'd have no idea how many bytes the
character used. getch() needs to remove the bytes that were read from
the buffer, so that the next call won't read them again.
Anyway, I guess my question was answered. There's no routine in Qt that
does this directly, so I guess I'll have to live with it. Fortunately,
the code is in a place that isn't performance-critical. Although a
getch() is needed, it's used rarely; a getstr()-like routine is used far
more often, where I simply convert the whole buffer.
More information about the Qt-interest-old
mailing list