Tom Lane <tgl@sss.pgh.pa.us> writes:
> I think its behavior for an odd number of digits is wrong too, or at
> least pretty nonintuitive. I like my code a lot better ;-)
Well, I like your code better too on closer inspection. I changed it
a bit, though, because I don't feel that an odd number of digits in
the hex string is wrong at all -- it's just a representation of a bit
string, with the representation padded to an even multiple of four
bits.
Anyway, here's what I ended up with for the code in question:
if (ch == '0'&& (src[0] == 'x'|| src[0] == 'X')
&& isascii(src[1]) && isxdigit(src[1]))
{
/* Hexadecimal: Eat nybble string. */
if (size <= 0)
goto emsgsize;
tmp = 0;
dirty = 0;
src++; /* skip x or X. */
while ((ch = *src++) != '\0'&&
isascii(ch) && isxdigit(ch))
{
if (isupper(ch))
ch = tolower(ch);
n = strchr(xdigits, ch) - xdigits;
assert(n >= 0 && n <= 15);
tmp = (tmp << 4) | n;
if (++dirty == 2) {
if (size-- <= 0)
goto emsgsize;
*dst++ = (u_char) tmp;
tmp = 0, dirty = 0;
}
}
if (dirty) {
if (size-- <= 0)
goto emsgsize;
tmp <<= 4;
*dst++ = (u_char) tmp;
}
}
-tih
--
Popularity is the hallmark of mediocrity. --Niles Crane, "Frasier"