Browse Source

[kservice] 增加printf格式2进制和8进制,RT_KPRINTF_USING_LONGLONG 和 RT_PRINTF_SPECIAL 做了相应处理 (#6361)

* 增加printf格式2进制和8进制,RT_KPRINTF_USING_LONGLONG 和 RT_PRINTF_SPECIAL 做了相应处理
winfenggao 2 years ago
parent
commit
cbbea1a0f5
1 changed files with 20 additions and 21 deletions
  1. 20 21
      src/kservice.c

+ 20 - 21
src/kservice.c

@@ -666,26 +666,13 @@ rt_inline int divide(long *n, int base)
     int res;
 
     /* optimized for processor which does not support divide instructions. */
-    if (base == 10)
-    {
-#ifdef RT_KPRINTF_USING_LONGLONG
-        res = (int)(((unsigned long long)*n) % 10U);
-        *n = (long long)(((unsigned long long)*n) / 10U);
-#else
-        res = (int)(((unsigned long)*n) % 10U);
-        *n = (long)(((unsigned long)*n) / 10U);
-#endif
-    }
-    else
-    {
 #ifdef RT_KPRINTF_USING_LONGLONG
-        res = (int)(((unsigned long long)*n) % 16U);
-        *n = (long long)(((unsigned long long)*n) / 16U);
+    res = (int)(((unsigned long long)*n) % base);
+    *n = (long long)(((unsigned long long)*n) / base);
 #else
-        res = (int)(((unsigned long)*n) % 16U);
-        *n = (long)(((unsigned long)*n) / 16U);
+    res = (int)(((unsigned long)*n) % base);
+    *n = (long)(((unsigned long)*n) / base);
 #endif
-    }
 
     return res;
 }
@@ -723,9 +710,9 @@ static char *print_number(char *buf,
 {
     char c, sign;
 #ifdef RT_KPRINTF_USING_LONGLONG
-    char tmp[32];
+    char tmp[64];
 #else
-    char tmp[16];
+    char tmp[32];
 #endif /* RT_KPRINTF_USING_LONGLONG */
     int precision_bak = precision;
     const char *digits;
@@ -759,7 +746,7 @@ static char *print_number(char *buf,
 #ifdef RT_PRINTF_SPECIAL
     if (type & SPECIAL)
     {
-        if (base == 16)
+        if (base == 2 || base == 16)
             size -= 2;
         else if (base == 8)
             size--;
@@ -809,7 +796,16 @@ static char *print_number(char *buf,
 #ifdef RT_PRINTF_SPECIAL
     if (type & SPECIAL)
     {
-        if (base == 8)
+        if (base == 2)
+        {
+            if (buf < end)
+                *buf = '0';
+            ++ buf;
+            if (buf < end)
+                *buf = 'b';
+            ++ buf;
+        }
+        else if (base == 8)
         {
             if (buf < end)
                 *buf = '0';
@@ -1068,6 +1064,9 @@ RT_WEAK int rt_vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list arg
             continue;
 
         /* integer number formats - set up the flags and "break" */
+        case 'b':
+            base = 2;
+            break;
         case 'o':
             base = 8;
             break;