xiaoxiang781216 commented on code in PR #7724:
URL: https://github.com/apache/nuttx/pull/7724#discussion_r1034878115


##########
libs/libc/stdlib/lib_strtof.c:
##########
@@ -142,18 +172,37 @@ float strtof(FAR const char *str, FAR char **endptr)
       break;
     }
 
+  p10          = 10.0f;
   number       = 0.0F;
   exponent     = 0;
   num_digits   = 0;
   num_decimals = 0;
 
   /* Process string of digits */
 
-  while (isdigit(*p))
+  if (*p == '0' && tolower(*(p + 1)) == 'x')
     {
-      number = number * 10.0F + (float)(*p - '0');
-      p++;
-      num_digits++;
+          p += 2;
+          flag_sixt = 1;
+          p10  = 16.0f;
+    }
+
+  if (flag_sixt == 1)
+    {
+      while (chtof(*p, p10, &number))
+        {
+          p++;
+          num_digits++;
+        }
+    }
+  else
+    {
+      while (isdigit(*p))

Review Comment:
   let's use the same code as 192 and remove flag_sixt



##########
libs/libc/stdlib/lib_strtof.c:
##########
@@ -82,6 +83,34 @@ static inline int is_real(float x)
   return (x < infinite) && (x >= -infinite);
 }
 
+int chtof(char c, float base, FAR float *number)
+{
+  /* This function is to determine if c is keyword
+   * Then set number + base
+   */
+
+  if (isdigit(c) || (c >= 'A' && c <= 'F')
+                 || (c >= 'a' && c <= 'f'))
+    {
+      if (isdigit(c))
+        {
+          *number = *number * base + (double)(c - '0');

Review Comment:
   remove all (double) cast



##########
libs/libc/stdlib/lib_strtof.c:
##########
@@ -82,6 +83,34 @@ static inline int is_real(float x)
   return (x < infinite) && (x >= -infinite);
 }
 
+int chtof(char c, float base, FAR float *number)
+{
+  /* This function is to determine if c is keyword
+   * Then set number + base
+   */
+
+  if (isdigit(c) || (c >= 'A' && c <= 'F')

Review Comment:
   ```
   float tmp = base;
   
   if (isdigit(c))
     {
       tmp = c - '0';
     }
   else if (islower(c))
     {
       tmp = c - 'a'
     }
   else if (isupper(c))
     {
       tmp = c - 'A'
     }
   
   if (c >= base)
     {
       return false;
     }
   
   *number = *number * base + tmp;
   return true;
   ```
   



##########
libs/libc/stdlib/lib_strtof.c:
##########
@@ -162,13 +211,29 @@ float strtof(FAR const char *str, FAR char **endptr)
     {
       p++;
 
-      while (isdigit(*p))
+      if (flag_sixt == 1)
         {
-          number = number * 10.0F + (float)(*p - '0');
-          p++;
-          num_digits++;
-          num_decimals++;
-        }
+          if (p10 != 16.0f)
+            {
+              p10 = 16.0f;
+            }
+          while (chtof(*p, p10, &number))
+            {
+              p++;
+              num_digits++;
+              num_decimals++;
+            }
+      }
+    else
+      {
+        while (isdigit(*p))

Review Comment:
   merge else into if



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to