Universal escaping in KConfigINI: Summary and patch

Oswald Buddenhagen ossi at kde.org
Thu May 24 20:03:03 BST 2007


On Thu, May 24, 2007 at 08:37:47PM +0200, Simon Hausmann wrote:
> On Thursday 24 May 2007 20:24:45 Andreas Hartmetz wrote:
> > My current versions of these functions look like this now, for the record:
> >
> > inline static char hexToChar(const char *str)
> > {
> >     char ret = 0;
> >     char c;
> >     for (int i = 0; i < 2; i++) {
> >         ret <<= 4;
> >         c = str[i];
> >
> >         if (c >= '0' && c <= '9') {
> >             ret |= c - '0';
> >         } else if (c >= 'a' && c <= 'f') {
> >             ret |= c - 'a' + 0x0a;
> >         } else if (c >= 'A' && c <= 'F') {
> >             ret |= c - 'A' + 0x0a;
> >         } else {
> >              //TODO: warning
> >             return 'x';
> >         }
> >     }
> >     return ret;
> > }
> >
> > inline static void charToHex(char c, char *str)
> > {
> >     static const char lookup[] = {
> >         '0', '1', '2', '3', '4', '5', '6', '7',
> >         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
> >     };
> >
> >     for (int i = 0; i < 2; i++) {
> >         str[i] = lookup[c >> 4];
> >         c <<= 4;
> >     }
> > }
> 
you seem to like loops, huh? ;)

return (hexToNibble(str[0]) << 4) + hexToNibble(str[1])

*r++ = hexLookup[((unsigned char)c) >> 4]; // (c >> 4) & 15 works too, but is less efficient on existing compilers
*r++ = hexLookup[c & 15];

much simpler to understand, imo.
and yes, i'd inline them.

> Why not simply use QByteArray::fromHex and QByteArray::toHex? :)
> 
"Input is not checked for validity; invalid characters in the input are
skipped [...]"
also, it is slight overkill. we don't want to convert entire hex blocks.

toInt(&ok, 16) would work, but one has to make a substring first =>
inefficient. number() doesn't work at all because of missing null
padding.

-- 
Hi! I'm a .signature virus! Copy me into your ~/.signature, please!
--
Chaos, panic, and disorder - my work here is done.




More information about the kde-core-devel mailing list