summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorwinfenggao <[email protected]>2022-09-02 10:00:28 +0800
committerGitHub <[email protected]>2022-09-02 10:00:28 +0800
commitcbbea1a0f598ca65c11a00dfec092988e4a0fc41 (patch)
treed2ac16efdec555f65f899153cfd084af3eb919e9 /src
parent6da3b2b4b6e7af3c1c80682706d10bdcaa61155e (diff)
[kservice] 增加printf格式2进制和8进制,RT_KPRINTF_USING_LONGLONG 和 RT_PRINTF_SPECIAL 做了相应处理 (#6361)
* 增加printf格式2进制和8进制,RT_KPRINTF_USING_LONGLONG 和 RT_PRINTF_SPECIAL 做了相应处理
Diffstat (limited to 'src')
-rw-r--r--src/kservice.c41
1 files 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;