Re: [PATCH 01/10] lib: introduce common method to convert hex digits

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Andrew Morton
Date: Friday, April 30, 2010 - 12:07 pm

On Fri, 30 Apr 2010 12:34:00 +0300
Andy Shevchenko <ext-andriy.shevchenko@nokia.com> wrote:


I had to fiddle with it:

- use tolower(), saving 3 bytes!

- test the more common case first - it's quicker.

diff -puN lib/hexdump.c~lib-introduce-common-method-to-convert-hex-digits-fix lib/hexdump.c
--- a/lib/hexdump.c~lib-introduce-common-method-to-convert-hex-digits-fix
+++ a/lib/hexdump.c
@@ -24,12 +24,11 @@ EXPORT_SYMBOL(hex_asc);
  */
 int hex_to_bin(char ch)
 {
-	if ((ch >= 'a') && (ch <= 'f'))
-		return ch - 'a' + 10;
+	ch = tolower(ch);
 	if ((ch >= '0') && (ch <= '9'))
 		return ch - '0';
-	if ((ch >= 'A') && (ch <= 'F'))
-		return ch - 'A' + 10;
+	if ((ch >= 'a') && (ch <= 'f'))
+		return ch - 'a' + 10;
 	return -1;
 }
 EXPORT_SYMBOL(hex_to_bin);
_


Yielding

int hex_to_bin(char ch)
{
	ch = tolower(ch);
	if ((ch >= '0') && (ch <= '9'))
		return ch - '0';
	if ((ch >= 'a') && (ch <= 'f'))
		return ch - 'a' + 10;
	return -1;
}

--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH 00/10] Introduce common hex_to_bin() helper, Andy Shevchenko, (Fri Apr 30, 2:33 am)
[PATCH 02/10] drivers: isdn: use new hex_to_bin() method, Andy Shevchenko, (Fri Apr 30, 2:34 am)
[PATCH 04/10] drivers: net: use new hex_to_bin() method, Andy Shevchenko, (Fri Apr 30, 2:34 am)
[PATCH 05/10] drivers: net: use new hex_to_bin() method, Andy Shevchenko, (Fri Apr 30, 2:34 am)
[PATCH 07/10] staging: rt2860: use new hex_to_bin() method, Andy Shevchenko, (Fri Apr 30, 2:34 am)
[PATCH 09/10] drivers: wireless: use new hex_to_bin() method, Andy Shevchenko, (Fri Apr 30, 2:34 am)
Re: [PATCH 09/10] drivers: wireless: use new hex_to_bin() ..., John W. Linville, (Fri Apr 30, 11:14 am)
Re: [PATCH 01/10] lib: introduce common method to convert ..., Andrew Morton, (Fri Apr 30, 12:07 pm)