<rdar://problem/12398225>

Improve performance of StringExtractor::GetHexS8().

llvm-svn: 164852
This commit is contained in:
Greg Clayton 2012-09-28 21:51:54 +00:00
parent 845aa66a8a
commit 3852b3e189
2 changed files with 46 additions and 42 deletions

View File

@ -16,6 +16,43 @@
// Other libraries and framework includes // Other libraries and framework includes
// Project includes // Project includes
static const uint8_t
g_hex_ascii_to_hex_integer[256] = {
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7,
0x8, 0x9, 255, 255, 255, 255, 255, 255,
255, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255,
};
static inline int static inline int
xdigit_to_sint (char ch) xdigit_to_sint (char ch)
{ {
@ -101,35 +138,6 @@ StringExtractor::GetChar (char fail_value)
return fail_value; return fail_value;
} }
uint32_t
StringExtractor::GetNumHexASCIICharsAtFilePos (uint32_t max) const
{
uint32_t idx = m_index;
const size_t size = m_packet.size();
while (idx < size && idx - m_index < max && isxdigit(m_packet[idx]))
++idx;
return idx - m_index;
}
//----------------------------------------------------------------------
// Extract a signed character from two hex ASCII chars in the packet
// string
//----------------------------------------------------------------------
int8_t
StringExtractor::GetHexS8 (int8_t fail_value)
{
if (GetNumHexASCIICharsAtFilePos(2))
{
char hi_nibble_char = m_packet[m_index];
char lo_nibble_char = m_packet[m_index+1];
char hi_nibble = xdigit_to_sint (hi_nibble_char);
char lo_nibble = xdigit_to_sint (lo_nibble_char);
m_index += 2;
return (hi_nibble << 4) + lo_nibble;
}
m_index = UINT32_MAX;
return fail_value;
}
//---------------------------------------------------------------------- //----------------------------------------------------------------------
// Extract an unsigned character from two hex ASCII chars in the packet // Extract an unsigned character from two hex ASCII chars in the packet
// string // string
@ -137,14 +145,16 @@ StringExtractor::GetHexS8 (int8_t fail_value)
uint8_t uint8_t
StringExtractor::GetHexU8 (uint8_t fail_value, bool set_eof_on_fail) StringExtractor::GetHexU8 (uint8_t fail_value, bool set_eof_on_fail)
{ {
if (GetNumHexASCIICharsAtFilePos(2)) uint32_t i = m_index;
if ((i + 2) <= m_packet.size())
{ {
uint8_t hi_nibble_char = m_packet[m_index]; const uint8_t hi_nibble = g_hex_ascii_to_hex_integer[m_packet[i]];
uint8_t lo_nibble_char = m_packet[m_index+1]; const uint8_t lo_nibble = g_hex_ascii_to_hex_integer[m_packet[i+1]];
uint8_t hi_nibble = xdigit_to_uint (hi_nibble_char); if (hi_nibble < 16 && lo_nibble < 16)
uint8_t lo_nibble = xdigit_to_uint (lo_nibble_char); {
m_index += 2; m_index += 2;
return (hi_nibble << 4) + lo_nibble; return (hi_nibble << 4) + lo_nibble;
}
} }
if (set_eof_on_fail || m_index >= m_packet.size()) if (set_eof_on_fail || m_index >= m_packet.size())
m_index = UINT32_MAX; m_index = UINT32_MAX;

View File

@ -89,9 +89,6 @@ public:
char char
GetChar (char fail_value = '\0'); GetChar (char fail_value = '\0');
int8_t
GetHexS8 (int8_t fail_value = 0);
uint8_t uint8_t
GetHexU8 (uint8_t fail_value = 0, bool set_eof_on_fail = true); GetHexU8 (uint8_t fail_value = 0, bool set_eof_on_fail = true);
@ -133,9 +130,6 @@ protected:
// will march along as things get extracted. If set // will march along as things get extracted. If set
// to UINT32_MAX the end of the packet data was // to UINT32_MAX the end of the packet data was
// reached when decoding information // reached when decoding information
uint32_t
GetNumHexASCIICharsAtFilePos (uint32_t max = UINT32_MAX) const;
}; };
#endif // utility_StringExtractor_h_ #endif // utility_StringExtractor_h_