mirror of
https://github.com/Fishwaldo/opensbi.git
synced 2025-06-28 17:18:37 +00:00
lib: sbi: Add '+' flags for print
Adds + flags for print, prefixing positive numbers with + when this flags is present Signed-off-by: Xiang W <wxjstz@126.com> Reviewed-by: Anup Patel <anup@brainfault.org>
This commit is contained in:
parent
35ef182690
commit
40dac06e3c
1 changed files with 16 additions and 9 deletions
|
@ -120,6 +120,7 @@ unsigned long sbi_ngets(char *str, unsigned long len)
|
||||||
#define PAD_RIGHT 1
|
#define PAD_RIGHT 1
|
||||||
#define PAD_ZERO 2
|
#define PAD_ZERO 2
|
||||||
#define PAD_ALTERNATE 4
|
#define PAD_ALTERNATE 4
|
||||||
|
#define PAD_SIGN 8
|
||||||
#define PRINT_BUF_LEN 64
|
#define PRINT_BUF_LEN 64
|
||||||
|
|
||||||
#define va_start(v, l) __builtin_va_start((v), l)
|
#define va_start(v, l) __builtin_va_start((v), l)
|
||||||
|
@ -186,15 +187,18 @@ static int prints(char **out, u32 *out_len, const char *string, int width,
|
||||||
static int printi(char **out, u32 *out_len, long long i, int b, int sg,
|
static int printi(char **out, u32 *out_len, long long i, int b, int sg,
|
||||||
int width, int flags, int letbase)
|
int width, int flags, int letbase)
|
||||||
{
|
{
|
||||||
char print_buf[PRINT_BUF_LEN];
|
char *s, sign = 0, print_buf[PRINT_BUF_LEN];
|
||||||
char *s;
|
int pc = 0;
|
||||||
int neg = 0, pc = 0;
|
|
||||||
u64 t;
|
u64 t;
|
||||||
unsigned long long u = i;
|
unsigned long long u = i;
|
||||||
|
|
||||||
if (sg && b == 10 && i < 0) {
|
if (sg && b == 10) {
|
||||||
neg = 1;
|
if ((flags & PAD_SIGN) && i > 0)
|
||||||
u = -i;
|
sign = '+';
|
||||||
|
if (i < 0) {
|
||||||
|
sign = '-';
|
||||||
|
u = -i;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
s = print_buf + PRINT_BUF_LEN - 1;
|
s = print_buf + PRINT_BUF_LEN - 1;
|
||||||
|
@ -221,13 +225,13 @@ static int printi(char **out, u32 *out_len, long long i, int b, int sg,
|
||||||
*--s = '0';
|
*--s = '0';
|
||||||
}
|
}
|
||||||
|
|
||||||
if (neg) {
|
if (sign) {
|
||||||
if (width && (flags & PAD_ZERO)) {
|
if (width && (flags & PAD_ZERO)) {
|
||||||
printc(out, out_len, '-');
|
printc(out, out_len, sign);
|
||||||
++pc;
|
++pc;
|
||||||
--width;
|
--width;
|
||||||
} else {
|
} else {
|
||||||
*--s = '-';
|
*--s = sign;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -275,6 +279,9 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)
|
||||||
case '-':
|
case '-':
|
||||||
flags |= PAD_RIGHT;
|
flags |= PAD_RIGHT;
|
||||||
break;
|
break;
|
||||||
|
case '+':
|
||||||
|
flags |= PAD_SIGN;
|
||||||
|
break;
|
||||||
case '#':
|
case '#':
|
||||||
flags |= PAD_ALTERNATE;
|
flags |= PAD_ALTERNATE;
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue