[PATCH] lib: hexdump: avoid _ctype table lookup in hex_to_bin function

From: Michal Nazarewicz
Date: Wed Jun 29 2016 - 14:16:34 EST


tolower macro maps to __tolower function which calls isupper to
to determine if character is an upper case letter before converting
it to lower case. This preservers non-letters unchanged which is
what you want in usual case.

However, hex_to_bin does not care about non-letter characters so
such conversion can be performed as long as (i) upper case letters
become lower case, (ii) lower case letters are unchanged and (iii)
non-letters stay non-letters.

This is exactly what _tolower function does and using it makes it
possible to avoid _ctype table lookup performed by the isupper
table.

Furthermore, since _tolower conversion is done unconditionally, this
also eliminates a single branch.

Signed-off-by: Michal Nazarewicz <mina86@xxxxxxxxxx>
---
lib/hexdump.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lib/hexdump.c b/lib/hexdump.c
index 992457b..f184d7a 100644
--- a/lib/hexdump.c
+++ b/lib/hexdump.c
@@ -29,7 +29,7 @@ int hex_to_bin(char ch)
{
if ((ch >= '0') && (ch <= '9'))
return ch - '0';
- ch = tolower(ch);
+ ch = _tolower(ch);
if ((ch >= 'a') && (ch <= 'f'))
return ch - 'a' + 10;
return -1;
--
Best regards
ã??ã??ã?¦ â??ð??¶ð??²ð??·ð??ª86â?? ã??ã?¶ã?¬ã?´ã?¤ã??
«If at first you donâ??t succeed, give up skydiving»