Universal escaping in KConfigINI: Summary and patch

Andreas Hartmetz ahartmetz at gmail.com
Thu May 24 21:07:02 BST 2007


On Thursday 24 May 2007 21:03:03 Oswald Buddenhagen wrote:
> 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? ;)
>
An artifact of the old code :)

> 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.
>
OK, done. This has the added benefit of making it possible to rename hexToChar 
to charFromHex without awkward asymmetric names.

> > 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.






More information about the kde-core-devel mailing list