Refactor and remove useless code in FromHex().

This commit is contained in:
Kirill Zhdanovich 2013-04-14 14:02:40 +03:00 committed by Alex Zolotarev
parent 36f5a2e3f0
commit 04500a4ce8

View file

@ -21,27 +21,26 @@ namespace impl {
// static const char kFromHexTable[] = "0123456789ABCDEF";
uint8_t HexDigitToRaw(uint8_t const digit)
{
if (digit >= '0' && digit <= '9')
return (digit - '0');
else if (digit >= 'A' && digit <= 'F')
return (digit - 'A' + 10);
ASSERT(false, (digit));
return 0;
}
void FromHexRaw(void const * src, size_t size, void * dst)
{
uint8_t const * ptr = static_cast<uint8_t const *>(src);
uint8_t const * end = ptr + size;
uint8_t * out = static_cast<uint8_t*>(dst);
while (ptr < end) {
*out = 0;
if (*ptr >= '0' && *ptr <= '9') {
*out |= ((*ptr - '0') << 4);
} else if (*ptr >= 'A' && *ptr <= 'F') {
*out |= ((*ptr - 'A' + 10) << 4);
}
++ptr;
if (*ptr >= '0' && *ptr <= '9') {
*out |= ((*ptr - '0') & 0xF);
} else if (*ptr >= 'A' && *ptr <= 'F') {
*out |= ((*ptr - 'A' + 10) & 0xF);
}
++ptr;
while (ptr < end)
{
*out = HexDigitToRaw(*ptr++) << 4;
*out |= HexDigitToRaw(*ptr++);
++out;
}
}