[PATCH 2/3] speed up Hex2Num
replaced a call to isxdigit and comparisons with an array lookup
---
apt-pkg/contrib/strutl.cc | 41 ++++++++++++++++++++++++-----------------
1 file changed, 24 insertions(+), 17 deletions(-)
diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc
index 9621128..49a2fec 100644
--- a/apt-pkg/contrib/strutl.cc
+++ b/apt-pkg/contrib/strutl.cc
@@ -1047,20 +1047,26 @@ bool Base256ToNum(const char *Str,unsigned long &Res,unsigned int Len)
}
}
/*}}}*/
-// HexDigit - Convert a hex character into an integer /*{{{*/
-// ---------------------------------------------------------------------
-/* Helper for Hex2Num */
-static int HexDigit(int c)
-{
- if (c >= '0' && c <= '9')
- return c - '0';
- if (c >= 'a' && c <= 'f')
- return c - 'a' + 10;
- if (c >= 'A' && c <= 'F')
- return c - 'A' + 10;
- return 0;
-}
- /*}}}*/
+
+/* for Hex2Num */
+static unsigned char hexdigits[256] =
+ {99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
+ 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
+ 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,99,99,99,99,99,99,
+ 99,10,11,12,13,14,15,99,99,99,99,99,99,99,99,99,
+ 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
+ 99,10,11,12,13,14,15,99,99,99,99,99,99,99,99,99,
+ 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
+ 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
+ 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
+ 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
+ 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
+ 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
+ 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
+ 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
+ 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99};
+
// Hex2Num - Convert a long hex number into a buffer /*{{{*/
// ---------------------------------------------------------------------
/* The length of the buffer must be exactly 1/2 the length of the string. */
@@ -1073,11 +1079,12 @@ bool Hex2Num(const string &Str,unsigned char *Num,unsigned int Length)
int J = 0;
for (string::const_iterator I = Str.begin(); I != Str.end();J++, I += 2)
{
- if (isxdigit(*I) == 0 || isxdigit(I[1]) == 0)
+ unsigned char digit1 = hexdigits[(unsigned int)*I];
+ unsigned char digit2 = hexdigits[(unsigned int)I[1]];
+ if (digit1 == 99 || digit2 == 99)
return false;
- Num[J] = HexDigit(I[0]) << 4;
- Num[J] += HexDigit(I[1]);
+ Num[J] = (digit1 << 4) | digit2;
}
return true;
--
1.7.10.4
Reply to: