diff mbox series

[v7,08/11] lib: sbi: Simplify prints

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

Commit Message

Xiang W July 4, 2023, 1:12 p.m. UTC
When doing width = width - strlen(string) in prints there is no need
to consider the case that witdh may be less than 0. This is because
the code to do filling needs to be executed under the condition that
width > 0.

Signed-off-by: Xiang W <wxjstz@126.com>
---
 lib/sbi/sbi_console.c | 21 ++++-----------------
 1 file changed, 4 insertions(+), 17 deletions(-)

Comments

Anup Patel July 5, 2023, 5:20 a.m. UTC | #1
On Tue, Jul 4, 2023 at 6:42 PM Xiang W <wxjstz@126.com> wrote:
>
> When doing width = width - strlen(string) in prints there is no need
> to consider the case that witdh may be less than 0. This is because
> the code to do filling needs to be executed under the condition that
> width > 0.
>
> Signed-off-by: Xiang W <wxjstz@126.com>

Looks good to me.

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

Regards,
Anup


> ---
>  lib/sbi/sbi_console.c | 21 ++++-----------------
>  1 file changed, 4 insertions(+), 17 deletions(-)
>
> diff --git a/lib/sbi/sbi_console.c b/lib/sbi/sbi_console.c
> index 963d19f..43870b8 100644
> --- a/lib/sbi/sbi_console.c
> +++ b/lib/sbi/sbi_console.c
> @@ -151,24 +151,11 @@ static void printc(char **out, u32 *out_len, char ch)
>  static int prints(char **out, u32 *out_len, const char *string, int width,
>                   int flags)
>  {
> -       int pc       = 0;
> -       char padchar = ' ';
> -
> -       if (width > 0) {
> -               int len = 0;
> -               const char *ptr;
> -               for (ptr = string; *ptr; ++ptr)
> -                       ++len;
> -               if (len >= width)
> -                       width = 0;
> -               else
> -                       width -= len;
> -               if (flags & PAD_ZERO)
> -                       padchar = '0';
> -       }
> +       int pc = 0;
> +       width -= sbi_strlen(string);
>         if (!(flags & PAD_RIGHT)) {
>                 for (; width > 0; --width) {
> -                       printc(out, out_len, padchar);
> +                       printc(out, out_len, flags & PAD_ZERO ? '0' : ' ');
>                         ++pc;
>                 }
>         }
> @@ -177,7 +164,7 @@ static int prints(char **out, u32 *out_len, const char *string, int width,
>                 ++pc;
>         }
>         for (; width > 0; --width) {
> -               printc(out, out_len, padchar);
> +               printc(out, out_len, ' ');
>                 ++pc;
>         }
>
> --
> 2.40.1
>
diff mbox series

Patch

diff --git a/lib/sbi/sbi_console.c b/lib/sbi/sbi_console.c
index 963d19f..43870b8 100644
--- a/lib/sbi/sbi_console.c
+++ b/lib/sbi/sbi_console.c
@@ -151,24 +151,11 @@  static void printc(char **out, u32 *out_len, char ch)
 static int prints(char **out, u32 *out_len, const char *string, int width,
 		  int flags)
 {
-	int pc	     = 0;
-	char padchar = ' ';
-
-	if (width > 0) {
-		int len = 0;
-		const char *ptr;
-		for (ptr = string; *ptr; ++ptr)
-			++len;
-		if (len >= width)
-			width = 0;
-		else
-			width -= len;
-		if (flags & PAD_ZERO)
-			padchar = '0';
-	}
+	int pc = 0;
+	width -= sbi_strlen(string);
 	if (!(flags & PAD_RIGHT)) {
 		for (; width > 0; --width) {
-			printc(out, out_len, padchar);
+			printc(out, out_len, flags & PAD_ZERO ? '0' : ' ');
 			++pc;
 		}
 	}
@@ -177,7 +164,7 @@  static int prints(char **out, u32 *out_len, const char *string, int width,
 		++pc;
 	}
 	for (; width > 0; --width) {
-		printc(out, out_len, padchar);
+		printc(out, out_len, ' ');
 		++pc;
 	}