diff mbox series

[v2,4/5] test_printf: Append strings more efficiently

Message ID 20211019142621.2810043-5-willy@infradead.org (mailing list archive)
State New
Headers show
Series Improvements to %pGp | expand

Commit Message

Matthew Wilcox Oct. 19, 2021, 2:26 p.m. UTC
Use scnprintf instead of snprintf + strlen.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
 lib/test_printf.c | 18 +++++++-----------
 1 file changed, 7 insertions(+), 11 deletions(-)

Comments

Yafang Shao Oct. 22, 2021, 3:13 p.m. UTC | #1
On Tue, Oct 19, 2021 at 10:33 PM Matthew Wilcox (Oracle)
<willy@infradead.org> wrote:
>
> Use scnprintf instead of snprintf + strlen.
>
> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>

Reviewed-by: Yafang Shao <laoar.shao@gmail.com>

> ---
>  lib/test_printf.c | 18 +++++++-----------
>  1 file changed, 7 insertions(+), 11 deletions(-)
>
> diff --git a/lib/test_printf.c b/lib/test_printf.c
> index ec584196cb99..d09993fca463 100644
> --- a/lib/test_printf.c
> +++ b/lib/test_printf.c
> @@ -614,8 +614,7 @@ page_flags_test(int section, int node, int zone, int last_cpupid,
>         int i;
>
>         if (flags & PAGEFLAGS_MASK) {
> -               snprintf(cmp_buf + size, BUF_SIZE - size, "%s", name);
> -               size = strlen(cmp_buf);
> +               size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s", name);
>                 append = true;
>         }
>
> @@ -623,17 +622,14 @@ page_flags_test(int section, int node, int zone, int last_cpupid,
>                 if (!pft[i].width)
>                         continue;
>
> -               if (append) {
> -                       snprintf(cmp_buf + size, BUF_SIZE - size, "|");
> -                       size = strlen(cmp_buf);
> -               }
> +               if (append)
> +                       size += scnprintf(cmp_buf + size, BUF_SIZE - size, "|");
>
>                 flags |= (values[i] & pft[i].mask) << pft[i].shift;
> -               snprintf(cmp_buf + size, BUF_SIZE - size, "%s=", pft[i].name);
> -               size = strlen(cmp_buf);
> -               snprintf(cmp_buf + size, BUF_SIZE - size, pft[i].fmt,
> -                        values[i] & pft[i].mask);
> -               size = strlen(cmp_buf);
> +               size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s=",
> +                               pft[i].name);
> +               size += scnprintf(cmp_buf + size, BUF_SIZE - size, pft[i].fmt,
> +                               values[i] & pft[i].mask);
>                 append = true;
>         }
>
> --
> 2.32.0
>
Petr Mladek Oct. 27, 2021, 10:14 a.m. UTC | #2
On Tue 2021-10-19 15:26:20, Matthew Wilcox (Oracle) wrote:
> Use scnprintf instead of snprintf + strlen.
> 
> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>

Reviewed-by: Petr Mladek <pmladek@suse.com>

Best Regards,
Petr
diff mbox series

Patch

diff --git a/lib/test_printf.c b/lib/test_printf.c
index ec584196cb99..d09993fca463 100644
--- a/lib/test_printf.c
+++ b/lib/test_printf.c
@@ -614,8 +614,7 @@  page_flags_test(int section, int node, int zone, int last_cpupid,
 	int i;
 
 	if (flags & PAGEFLAGS_MASK) {
-		snprintf(cmp_buf + size, BUF_SIZE - size, "%s", name);
-		size = strlen(cmp_buf);
+		size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s", name);
 		append = true;
 	}
 
@@ -623,17 +622,14 @@  page_flags_test(int section, int node, int zone, int last_cpupid,
 		if (!pft[i].width)
 			continue;
 
-		if (append) {
-			snprintf(cmp_buf + size, BUF_SIZE - size, "|");
-			size = strlen(cmp_buf);
-		}
+		if (append)
+			size += scnprintf(cmp_buf + size, BUF_SIZE - size, "|");
 
 		flags |= (values[i] & pft[i].mask) << pft[i].shift;
-		snprintf(cmp_buf + size, BUF_SIZE - size, "%s=", pft[i].name);
-		size = strlen(cmp_buf);
-		snprintf(cmp_buf + size, BUF_SIZE - size, pft[i].fmt,
-			 values[i] & pft[i].mask);
-		size = strlen(cmp_buf);
+		size += scnprintf(cmp_buf + size, BUF_SIZE - size, "%s=",
+				pft[i].name);
+		size += scnprintf(cmp_buf + size, BUF_SIZE - size, pft[i].fmt,
+				values[i] & pft[i].mask);
 		append = true;
 	}