An optimization. Faster GetHex for exact strings.

This commit is contained in:
JoelKatz
2013-04-01 21:01:06 -07:00
parent 78d1a93e69
commit d73995e695
3 changed files with 56 additions and 16 deletions

View File

@@ -219,6 +219,41 @@ public:
return strHex(begin(), size());
}
void SetHexExact(const char* psz)
{ // must be precisely the correct number of hex digits
static signed char phexdigit[256] = {
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,-1,-1, -1,-1,-1,-1,
-1,0xa,0xb,0xc, 0xd,0xe,0xf,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,0xa,0xb,0xc, 0xd,0xe,0xf,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
-1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
};
char* pOut = reinterpret_cast<char*>(pn);
for (int i = 0; i < sizeof(pn); ++i)
{
*pOut = phexdigit[*psz++] << 4;
*pOut++ |= phexdigit[*psz++];
}
assert(*psz == 0);
assert(pOut == reinterpret_cast<char*>(end()));
}
// Allow leading whitespace.
// Allow leading "0x".
// To be valid must be '\0' terminated.
@@ -291,6 +326,11 @@ public:
return SetHex(str.c_str(), bStrict);
}
void SetHexExact(const std::string& str)
{
SetHexExact(str.c_str());
}
std::string ToString() const
{
return GetHex();