From cbbea1a0f598ca65c11a00dfec092988e4a0fc41 Mon Sep 17 00:00:00 2001 From: winfenggao <104723992+winfenggao@users.noreply.github.com> Date: Fri, 2 Sep 2022 10:00:28 +0800 Subject: [PATCH] =?UTF-8?q?[kservice]=20=E5=A2=9E=E5=8A=A0printf=E6=A0=BC?= =?UTF-8?q?=E5=BC=8F2=E8=BF=9B=E5=88=B6=E5=92=8C8=E8=BF=9B=E5=88=B6?= =?UTF-8?q?=EF=BC=8CRT=5FKPRINTF=5FUSING=5FLONGLONG=20=E5=92=8C=20RT=5FPRI?= =?UTF-8?q?NTF=5FSPECIAL=20=E5=81=9A=E4=BA=86=E7=9B=B8=E5=BA=94=E5=A4=84?= =?UTF-8?q?=E7=90=86=20(#6361)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 增加printf格式2进制和8进制,RT_KPRINTF_USING_LONGLONG 和 RT_PRINTF_SPECIAL 做了相应处理 --- src/kservice.c | 41 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/src/kservice.c b/src/kservice.c index add8c00932..6a8336b2db 100644 --- a/src/kservice.c +++ b/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; -- GitLab