Skip to contentSkip to conversation

dspbridge: Simplify Atoi() method

PATCH In review Andy Shevchenko 2 participants 6 messages Feb 3, 2010 – Feb 16, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dspbridge: Simplify Atoi() method

From: Andy Shevchenko <[email protected]>

Try to use simple_strtol() kernel native method instead.

However, there are opened questions:
- why type of Atoi() is s32 if the sign is used only to detect base?
- should we really to check hex integers like DEAD0123h?
- how many spaces could lead token?

---
 drivers/dsp/bridge/rmgr/dbdcd.c |   42 +++++---------------------------------
 1 files changed, 6 insertions(+), 36 deletions(-)
drivers/dsp/bridge/rmgr/dbdcd.c +6 -36
index caa57f1..fe2ed57 100644
@@ -1002,50 +1002,20 @@ DSP_STATUS DCD_UnregisterObject(IN struct DSP_UUID *pUuid,
  */
 static s32 Atoi(char *pszBuf)
 {
-	s32 result = 0;
 	char *pch = pszBuf;
-	char c;
-	char first;
-	s32 base = 10;
-	s32 len;
+	s32 base = 0;
 
 	while (isspace(*pch))
 		pch++;
 
-	first = *pch;
-	if (first == '-' || first == '+') {
+	if (*pch == '-' || *pch == '+') {
+		base = 10;
 		pch++;
-	} else {
-		/* Determine if base 10 or base 16 */
-		len = strlen(pch);
-		if (len  > 1) {
-			c = pch[1];
-			if ((*pch == '0' && (c == 'x' || c == 'X'))) {
-				base = 16;
-				pch += 2;
-			}
-			c = pch[len - 1];
-			if (c == 'h' || c == 'H')
-				base = 16;
-
-		}
-	}
-
-	while (isdigit(c = *pch) || ((base == 16) && isxdigit(c))) {
-		result *= base;
-		if ('A' <= c && c <= 'F') {
-			c = c - 'A' + 10;
-		} else {
-			if ('a' <= c && c <= 'f')
-				c = c - 'a' + 10;
-			else
-				c -= '0';
-		}
-		result += c;
-		++pch;
+	} else if (*pch && (pch[strlen(pch) - 1] | 0x20 == 'h')) {
+		base = 16;
 	}
 
-	return result;
+	return simple_strtoul(pch, NULL, base);
 }
 
 /*
Signature · 1 line
1.5.6.5
#1 lore raw

Hi,

Quoted 3 lines · Andy Shevchenko

From: Andy Shevchenko<[email protected]>

Try to use simple_strtol() kernel native method instead.

strtol or strtoul?

Quoted 54 lines

However, there are opened questions:
- why type of Atoi() is s32 if the sign is used only to detect base?
- should we really to check hex integers like DEAD0123h?
- how many spaces could lead token?

---
  drivers/dsp/bridge/rmgr/dbdcd.c |   42 +++++---------------------------------
  1 files changed, 6 insertions(+), 36 deletions(-)
drivers/dsp/bridge/rmgr/dbdcd.c +3 -26
index caa57f1..fe2ed57 100644
@@ -1002,50 +1002,20 @@ DSP_STATUS DCD_UnregisterObject(IN struct DSP_UUID *pUuid,
   */
  static s32 Atoi(char *pszBuf)
  {
-	s32 result = 0;
  	char *pch = pszBuf;
-	char c;
-	char first;
-	s32 base = 10;
-	s32 len;
+	s32 base = 0;
 
  	while (isspace(*pch))
  		pch++;
 
-	first = *pch;
-	if (first == '-' || first == '+') {
+	if (*pch == '-' || *pch == '+') {
+		base = 10;
  		pch++;
-	} else {
-		/* Determine if base 10 or base 16 */
-		len = strlen(pch);
-		if (len>  1) {
-			c = pch[1];
-			if ((*pch == '0'&&  (c == 'x' || c == 'X'))) {
-				base = 16;
-				pch += 2;
-			}
-			c = pch[len - 1];
-			if (c == 'h' || c == 'H')
-				base = 16;
-
-		}
-	}
-
-	while (isdigit(c = *pch) || ((base == 16)&&  isxdigit(c))) {
-		result *= base;
-		if ('A'<= c&&  c<= 'F') {

drivers/dsp/bridge/rmgr/dbdcd.c DSP_STATUS DCD_UnregisterObject(IN struct DSP_UUID *pUuid, · line 1014

Quoted 10 lines
drivers/dsp/bridge/rmgr/dbdcd.c +1 -9
-			c = c - 'A' + 10;
-		} else {
-			if ('a'<= c&&  c<= 'f')
-				c = c - 'a' + 10;
-			else
-				c -= '0';
-		}
-		result += c;
-		++pch;
+	} else if (*pch&&  (pch[strlen(pch) - 1] | 0x20 == 'h')) {

perhaps tolower(x)

otherwise: (pch[strlen(pch) - 1] | 0x20 == 'h')
should be ((pch[strlen(pch) - 1] | 0x20) == 'h')

drivers/dsp/bridge/rmgr/dbdcd.c DSP_STATUS DCD_UnregisterObject(IN struct DSP_UUID *pUuid, · line 1021

Quoted 8 lines
drivers/dsp/bridge/rmgr/dbdcd.c +2 -1
+		base = 16;
  	}
 
-	return result;
+	return simple_strtoul(pch, NULL, base);
  }
 
  /*

- omar

#2 lore raw
Quoted 2 lines · Omar Ramirez Luna
Quoted 1 line

Try to use simple_strtol() kernel native method instead.

strtol or strtoul?

I don't know for sure, see below.

Quoted 2 lines
Quoted 2 lines

However, there are opened questions:
 - why type of Atoi() is s32 if the sign is used only to detect base?

This is the question about l vs ul.

Quoted 3 lines
Quoted 1 line

+       } else if (*pch&&  (pch[strlen(pch) - 1] | 0x20 == 'h')) {

perhaps tolower(x)

Yep, I saw only internal macro in lib/vsprintf.c, but totally forgot about ctype.h.

Signature · 6 lines
With Best Regards,
Andy Shevchenko
--
To unsubscribe from this list: send the line "unsubscribe linux-omap" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html
#3 lore raw
Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Omar Ramirez Luna
Quoted 1 line

Try to use simple_strtol() kernel native method instead.

strtol or strtoul?

I don't know for sure, see below.

sorry, i meant: here it says simple_strtol but the code says simple_strtoul

Quoted 4 lines
Quoted 3 lines
Quoted 3 lines

However, there are opened questions:
- why type of Atoi() is s32 if the sign is used only to detect base?

This is the question about l vs ul.

agree, IMO this should be ul, I haven't reviewed all the places where Atoi is used but the ones I have seen use it as ul

about the sign: I haven't seen any value with '+' or '-' prefix so with this patch the decision to identify as base10 is left to simple_guess_base inside strtoul

if value strings had '0x' instead of ending 'H' for hex the whole Atoi could be replaced, i guess this would also allow to use strict_strtoul and avoid a checkpatch warning :(

- omar

#4 lore raw

PATCH 1/1 dspbridge: Simplify Atoi() method (v2)

In reply to Omar Ramirez Luna

From: Andy Shevchenko <[email protected]>

Try to use simple_strtoul() kernel native method instead.

However, there are opened questions:
- why type of Atoi() is s32 if the sign is used only to detect base?
- should we really to check hex integers like DEAD0123h?
- how many spaces could lead token?

---
 drivers/dsp/bridge/rmgr/dbdcd.c |   42 +++++---------------------------------
 1 files changed, 6 insertions(+), 36 deletions(-)
drivers/dsp/bridge/rmgr/dbdcd.c +6 -36
index 9efb7dc..a460d1a 100644
@@ -1001,50 +1001,20 @@ DSP_STATUS DCD_UnregisterObject(IN struct DSP_UUID *pUuid,
  */
 static s32 Atoi(char *pszBuf)
 {
-	s32 result = 0;
 	char *pch = pszBuf;
-	char c;
-	char first;
-	s32 base = 10;
-	s32 len;
+	s32 base = 0;
 
 	while (isspace(*pch))
 		pch++;
 
-	first = *pch;
-	if (first == '-' || first == '+') {
+	if (*pch == '-' || *pch == '+') {
+		base = 10;
 		pch++;
-	} else {
-		/* Determine if base 10 or base 16 */
-		len = strlen(pch);
-		if (len  > 1) {
-			c = pch[1];
-			if ((*pch == '0' && (c == 'x' || c == 'X'))) {
-				base = 16;
-				pch += 2;
-			}
-			c = pch[len - 1];
-			if (c == 'h' || c == 'H')
-				base = 16;
-
-		}
-	}
-
-	while (isdigit(c = *pch) || ((base == 16) && isxdigit(c))) {
-		result *= base;
-		if ('A' <= c && c <= 'F') {
-			c = c - 'A' + 10;
-		} else {
-			if ('a' <= c && c <= 'f')
-				c = c - 'a' + 10;
-			else
-				c -= '0';
-		}
-		result += c;
-		++pch;
+	} else if (*pch && tolower(pch[strlen(pch) - 1]) == 'h') {
+		base = 16;
 	}
 
-	return result;
+	return simple_strtoul(pch, NULL, base);
 }
 
 /*
Signature · 1 line
1.5.6.5
Acked-by on v1 1/1 Applied
#5 lore raw

Re: [PATCH] dspbridge: Simplify Atoi() method (v2)

Quoted 10 lines · Andy Shevchenko

From: Andy Shevchenko<[email protected]>

Try to use simple_strtoul() kernel native method instead.

However, there are opened questions:
- why type of Atoi() is s32 if the sign is used only to detect base?
- should we really to check hex integers like DEAD0123h?
- how many spaces could lead token?

Pushed to dspbridge

- omar