diff mbox series

[v6,03/12] lib: sbi: Add '+' flags for print

Message ID 20230612080959.571293-4-wxjstz@126.com
State Superseded
Headers show
Series Improve sbi_console | expand

Commit Message

Xiang W June 12, 2023, 8:09 a.m. UTC
Signed-off-by: Xiang W <wxjstz@126.com>
---
 lib/sbi/sbi_console.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)

Comments

Anup Patel July 4, 2023, 4:08 a.m. UTC | #1
On Mon, Jun 12, 2023 at 1:40 PM Xiang W <wxjstz@126.com> wrote:

No patch description ?

>
> Signed-off-by: Xiang W <wxjstz@126.com>
> ---
>  lib/sbi/sbi_console.c | 25 ++++++++++++++++---------
>  1 file changed, 16 insertions(+), 9 deletions(-)
>
> diff --git a/lib/sbi/sbi_console.c b/lib/sbi/sbi_console.c
> index b7d0896..75fd5ac 100644
> --- a/lib/sbi/sbi_console.c
> +++ b/lib/sbi/sbi_console.c
> @@ -120,6 +120,7 @@ unsigned long sbi_ngets(char *str, unsigned long len)
>  #define PAD_RIGHT 1
>  #define PAD_ZERO 2
>  #define PAD_ALTERNATE 4
> +#define PAD_SIGN 8
>  #define PRINT_BUF_LEN 64
>
>  #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,
>                   int width, int flags, int letbase)
>  {
> -       char print_buf[PRINT_BUF_LEN];
> -       char *s;
> -       int neg = 0, pc = 0;
> +       char *s, sign, print_buf[PRINT_BUF_LEN];
> +       int pc = 0;
>         u64 t;
>         unsigned long long u = i;
>
> -       if (sg && b == 10 && i < 0) {
> -               neg = 1;
> -               u   = -i;
> +       if (sg && b == 10) {
> +               if ((flags & PAD_SIGN) && i > 0)
> +                       sign = '+';
> +               if (i < 0) {
> +                       sign = '-';
> +                       u = -i;
> +               }
>         }
>
>         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';
>         }
>
> -       if (neg) {
> +       if (sign) {
>                 if (width && (flags & PAD_ZERO)) {
> -                       printc(out, out_len, '-');
> +                       printc(out, out_len, sign);
>                         ++pc;
>                         --width;
>                 } else {
> -                       *--s = '-';
> +                       *--s = sign;
>                 }
>         }
>
> @@ -275,6 +279,9 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)
>                                 case '-':
>                                         flags |= PAD_RIGHT;
>                                         break;
> +                               case '+':
> +                                       flags |= PAD_SIGN;
> +                                       break;
>                                 case '#':
>                                         flags |= PAD_ALTERNATE;
>                                         break;
> --
> 2.39.2
>

Otherwise, it looks good to me.

Reviewed-by: Anup Patel <anup@brainfault.org>

Regards,
Anup
Xiang W July 4, 2023, 9:54 a.m. UTC | #2
在 2023-07-04星期二的 09:38 +0530,Anup Patel写道:
> On Mon, Jun 12, 2023 at 1:40 PM Xiang W <wxjstz@126.com> wrote:
> 
> No patch description ?
Will be done in next patch.

Regards,
Xiang W
> 
> > 
> > Signed-off-by: Xiang W <wxjstz@126.com>
> > ---
> >  lib/sbi/sbi_console.c | 25 ++++++++++++++++---------
> >  1 file changed, 16 insertions(+), 9 deletions(-)
> > 
> > diff --git a/lib/sbi/sbi_console.c b/lib/sbi/sbi_console.c
> > index b7d0896..75fd5ac 100644
> > --- a/lib/sbi/sbi_console.c
> > +++ b/lib/sbi/sbi_console.c
> > @@ -120,6 +120,7 @@ unsigned long sbi_ngets(char *str, unsigned long len)
> >  #define PAD_RIGHT 1
> >  #define PAD_ZERO 2
> >  #define PAD_ALTERNATE 4
> > +#define PAD_SIGN 8
> >  #define PRINT_BUF_LEN 64
> > 
> >  #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,
> >                   int width, int flags, int letbase)
> >  {
> > -       char print_buf[PRINT_BUF_LEN];
> > -       char *s;
> > -       int neg = 0, pc = 0;
> > +       char *s, sign, print_buf[PRINT_BUF_LEN];
> > +       int pc = 0;
> >         u64 t;
> >         unsigned long long u = i;
> > 
> > -       if (sg && b == 10 && i < 0) {
> > -               neg = 1;
> > -               u   = -i;
> > +       if (sg && b == 10) {
> > +               if ((flags & PAD_SIGN) && i > 0)
> > +                       sign = '+';
> > +               if (i < 0) {
> > +                       sign = '-';
> > +                       u = -i;
> > +               }
> >         }
> > 
> >         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';
> >         }
> > 
> > -       if (neg) {
> > +       if (sign) {
> >                 if (width && (flags & PAD_ZERO)) {
> > -                       printc(out, out_len, '-');
> > +                       printc(out, out_len, sign);
> >                         ++pc;
> >                         --width;
> >                 } else {
> > -                       *--s = '-';
> > +                       *--s = sign;
> >                 }
> >         }
> > 
> > @@ -275,6 +279,9 @@ static int print(char **out, u32 *out_len, const char *format, va_list args)
> >                                 case '-':
> >                                         flags |= PAD_RIGHT;
> >                                         break;
> > +                               case '+':
> > +                                       flags |= PAD_SIGN;
> > +                                       break;
> >                                 case '#':
> >                                         flags |= PAD_ALTERNATE;
> >                                         break;
> > --
> > 2.39.2
> > 
> 
> Otherwise, it looks good to me.
> 
> Reviewed-by: Anup Patel <anup@brainfault.org>
> 
> Regards,
> Anup
diff mbox series

Patch

diff --git a/lib/sbi/sbi_console.c b/lib/sbi/sbi_console.c
index b7d0896..75fd5ac 100644
--- a/lib/sbi/sbi_console.c
+++ b/lib/sbi/sbi_console.c
@@ -120,6 +120,7 @@  unsigned long sbi_ngets(char *str, unsigned long len)
 #define PAD_RIGHT 1
 #define PAD_ZERO 2
 #define PAD_ALTERNATE 4
+#define PAD_SIGN 8
 #define PRINT_BUF_LEN 64
 
 #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,
 		  int width, int flags, int letbase)
 {
-	char print_buf[PRINT_BUF_LEN];
-	char *s;
-	int neg = 0, pc = 0;
+	char *s, sign, print_buf[PRINT_BUF_LEN];
+	int pc = 0;
 	u64 t;
 	unsigned long long u = i;
 
-	if (sg && b == 10 && i < 0) {
-		neg = 1;
-		u   = -i;
+	if (sg && b == 10) {
+		if ((flags & PAD_SIGN) && i > 0)
+			sign = '+';
+		if (i < 0) {
+			sign = '-';
+			u = -i;
+		}
 	}
 
 	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';
 	}
 
-	if (neg) {
+	if (sign) {
 		if (width && (flags & PAD_ZERO)) {
-			printc(out, out_len, '-');
+			printc(out, out_len, sign);
 			++pc;
 			--width;
 		} else {
-			*--s = '-';
+			*--s = sign;
 		}
 	}
 
@@ -275,6 +279,9 @@  static int print(char **out, u32 *out_len, const char *format, va_list args)
 				case '-':
 					flags |= PAD_RIGHT;
 					break;
+				case '+':
+					flags |= PAD_SIGN;
+					break;
 				case '#':
 					flags |= PAD_ALTERNATE;
 					break;