diff options
Diffstat (limited to 'src/klibc/rt_vsnprintf_tiny.c')
| -rw-r--r-- | src/klibc/rt_vsnprintf_tiny.c | 163 |
1 files changed, 103 insertions, 60 deletions
diff --git a/src/klibc/rt_vsnprintf_tiny.c b/src/klibc/rt_vsnprintf_tiny.c index 36811048a9..0d4fdda67f 100644 --- a/src/klibc/rt_vsnprintf_tiny.c +++ b/src/klibc/rt_vsnprintf_tiny.c @@ -10,7 +10,7 @@ #include <rtthread.h> -#define _ISDIGIT(c) ((unsigned)((c) - '0') < 10) +#define _ISDIGIT(c) ((unsigned)((c) - '0') < 10) /** * @brief This function will duplicate a string. @@ -45,37 +45,39 @@ rt_inline int skip_atoi(const char **s) { int i = 0; while (_ISDIGIT(**s)) + { i = i * 10 + *((*s)++) - '0'; + } return i; } -#define ZEROPAD (1 << 0) /* pad with zero */ -#define SIGN (1 << 1) /* unsigned/signed long */ -#define PLUS (1 << 2) /* show plus */ -#define SPACE (1 << 3) /* space if plus */ -#define LEFT (1 << 4) /* left justified */ -#define SPECIAL (1 << 5) /* 0x */ -#define LARGE (1 << 6) /* use 'ABCDEF' instead of 'abcdef' */ +#define ZEROPAD (1 << 0) /* pad with zero */ +#define SIGN (1 << 1) /* unsigned/signed long */ +#define PLUS (1 << 2) /* show plus */ +#define SPACE (1 << 3) /* space if plus */ +#define LEFT (1 << 4) /* left justified */ +#define SPECIAL (1 << 5) /* 0x */ +#define LARGE (1 << 6) /* use 'ABCDEF' instead of 'abcdef' */ static char *print_number(char *buf, char *end, #ifdef RT_KLIBC_USING_VSNPRINTF_LONGLONG - unsigned long long num, + unsigned long long num, #else - unsigned long num, + unsigned long num, #endif /* RT_KLIBC_USING_VSNPRINTF_LONGLONG */ - int base, - int qualifier, - int s, - int precision, - int type) + int base, + int qualifier, + int s, + int precision, + int type) { char c = 0, sign = 0; #ifdef RT_KLIBC_USING_VSNPRINTF_LONGLONG - char tmp[64] = {0}; + char tmp[64] = { 0 }; #else - char tmp[32] = {0}; + char tmp[32] = { 0 }; #endif /* RT_KLIBC_USING_VSNPRINTF_LONGLONG */ int precision_bak = precision; const char *digits = RT_NULL; @@ -158,7 +160,9 @@ static char *print_number(char *buf, else { while (num != 0) + { tmp[i++] = digits[divide(&num, base)]; + } } if (i > precision) @@ -181,7 +185,7 @@ static char *print_number(char *buf, *buf = ' '; } - ++ buf; + ++buf; } } @@ -191,8 +195,8 @@ static char *print_number(char *buf, { *buf = sign; } - -- size; - ++ buf; + --size; + ++buf; } if (type & SPECIAL) @@ -200,17 +204,23 @@ static char *print_number(char *buf, if (base == 2) { if (buf < end) + { *buf = '0'; - ++ buf; + } + ++buf; if (buf < end) + { *buf = 'b'; - ++ buf; + } + ++buf; } else if (base == 8) { if (buf < end) + { *buf = '0'; - ++ buf; + } + ++buf; } else if (base == 16) { @@ -219,12 +229,12 @@ static char *print_number(char *buf, *buf = '0'; } - ++ buf; + ++buf; if (buf < end) { *buf = type & LARGE ? 'X' : 'x'; } - ++ buf; + ++buf; } } @@ -238,7 +248,7 @@ static char *print_number(char *buf, *buf = c; } - ++ buf; + ++buf; } } @@ -249,7 +259,7 @@ static char *print_number(char *buf, *buf = '0'; } - ++ buf; + ++buf; } /* put number in the temporary buffer */ @@ -260,7 +270,7 @@ static char *print_number(char *buf, *buf = tmp[i]; } - ++ buf; + ++buf; } while (size-- > 0) @@ -270,7 +280,7 @@ static char *print_number(char *buf, *buf = ' '; } - ++ buf; + ++buf; } return buf; @@ -306,11 +316,11 @@ int rt_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) char *str = RT_NULL, *end = RT_NULL, c = 0; const char *s = RT_NULL; - rt_uint8_t base = 0; /* the base of number */ - rt_uint8_t flags = 0; /* flags to print number */ - rt_uint8_t qualifier = 0; /* 'h', 'l', or 'L' for integer fields */ - rt_int32_t field_width = 0; /* width of output field */ - int precision = 0; /* min. # of digits for integers and max for a string */ + rt_uint8_t base = 0; /* the base of number */ + rt_uint8_t flags = 0; /* flags to print number */ + rt_uint8_t qualifier = 0; /* 'h', 'l', or 'L' for integer fields */ + rt_int32_t field_width = 0; /* width of output field */ + int precision = 0; /* min. # of digits for integers and max for a string */ str = buf; end = buf + size; @@ -318,11 +328,11 @@ int rt_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) /* Make sure end is always >= buf */ if (end < buf) { - end = ((char *) - 1); + end = ((char *)-1); size = end - buf; } - for (; *fmt ; ++fmt) + for (; *fmt; ++fmt) { if (*fmt != '%') { @@ -331,7 +341,7 @@ int rt_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) *str = *fmt; } - ++ str; + ++str; continue; } @@ -342,12 +352,30 @@ int rt_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) { /* skips the first '%' also */ ++fmt; - if (*fmt == '-') flags |= LEFT; - else if (*fmt == '+') flags |= PLUS; - else if (*fmt == ' ') flags |= SPACE; - else if (*fmt == '#') flags |= SPECIAL; - else if (*fmt == '0') flags |= ZEROPAD; - else break; + if (*fmt == '-') + { + flags |= LEFT; + } + else if (*fmt == '+') + { + flags |= PLUS; + } + else if (*fmt == ' ') + { + flags |= SPACE; + } + else if (*fmt == '#') + { + flags |= SPECIAL; + } + else if (*fmt == '0') + { + flags |= ZEROPAD; + } + else + { + break; + } } /* get field width */ @@ -423,8 +451,11 @@ int rt_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) { while (--field_width > 0) { - if (str < end) *str = ' '; - ++ str; + if (str < end) + { + *str = ' '; + } + ++str; } } @@ -434,13 +465,16 @@ int rt_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) { *str = c; } - ++ str; + ++str; /* put width */ while (--field_width > 0) { - if (str < end) *str = ' '; - ++ str; + if (str < end) + { + *str = ' '; + } + ++str; } continue; @@ -462,22 +496,31 @@ int rt_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) { while (len < field_width--) { - if (str < end) *str = ' '; - ++ str; + if (str < end) + { + *str = ' '; + } + ++str; } } for (i = 0; i < len; ++i) { - if (str < end) *str = *s; - ++ str; - ++ s; + if (str < end) + { + *str = *s; + } + ++str; + ++s; } while (len < field_width--) { - if (str < end) *str = ' '; - ++ str; + if (str < end) + { + *str = ' '; + } + ++str; } continue; @@ -498,7 +541,7 @@ int rt_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) { *str = '%'; } - ++ str; + ++str; continue; /* integer number formats - set up the flags and "break" */ @@ -533,7 +576,7 @@ int rt_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) { *str = '%'; } - ++ str; + ++str; if (*fmt) { @@ -541,11 +584,11 @@ int rt_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) { *str = *fmt; } - ++ str; + ++str; } else { - -- fmt; + --fmt; } continue; } @@ -608,4 +651,4 @@ int rt_vsnprintf(char *buf, size_t size, const char *fmt, va_list args) } #if (defined(__GNUC__) && !defined(__ARMCC_VERSION) /* GCC */) && (__GNUC__ >= 7) #pragma GCC diagnostic pop /* ignored "-Wimplicit-fallthrough" */ -#endif /* (defined(__GNUC__) && !defined(__ARMCC_VERSION)) && (__GNUC__ >= 7 */ +#endif /* (defined(__GNUC__) && !defined(__ARMCC_VERSION)) && (__GNUC__ >= 7 */ |
