Re: How do online html docs in GTK+? in /portable/ way possible too?



Hi,

Valdis Kletnieks vt edu writes:

> Ahh.. but gtk uses something *other than ASCII*.
> 
> And thus  assuming that sizeof(char) * (len+1) will result in enough storage
> 
> >>                 ptr = g_malloc(sizeof(char) * (len + 1));
> 
> is a bug.  The code as written will just "happen to work" for ASCII and
> other codesets where the UTF-8 encoding happens to not require extra bytes
> (I'd have to check if all 8859-X charsets have this property - probably not,
> as the UTF-8 encoding will require at least a few bits to say which -X it is.
> 
> And if the UTF-8 encoding is longer, you won't g_malloc enough.  And THAT
> is where the bug is.

fortunately strlen() does the right thing for UTF-8. It doesn't return
the number of characters but the number of bytes used by the string
(w/o the trailing NULL). The code should work correctly even for UTF-8
encoded strings. One possible problem is that it iterates over the
string bytewise in order to compare each character:

             len = strlen(cmd);
             argc = 1;
             for(i = 0; i < len; i++) {
                 if(cmd[i] == ' ')
                     argc++;
             }

However this works since no UTF-8 string can contain the char ' '
unless it's exactly this char encoded in UTF-8. I don't say this is
overly well coded but it should work for UTF-8 strings. Nevertheless,
I strongly suggest to make use of the routines that glib-2.0 provides
to handle UTF-8 strings.


Salut, Sven



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]