Re: [PATCH v2 1/2] lib: add sputchar() helper

From: Yury Norov
Date: Sat Sep 04 2021 - 23:20:31 EST


On Sat, Sep 04, 2021 at 06:36:41PM -0700, Joe Perches wrote:
> On Sat, 2021-09-04 at 16:10 -0700, Yury Norov wrote:
> > There are 47 occurrences of the code snippet like this:
> > if (buf < end)
> > *buf = ' ';
> > ++buf;
> >
> > This patch adds a helper function sputchar() to replace opencoding.
> > It adds a lot to readability, and also saves 43 bytes of text on x86.
>
> I think this patch does little to improve readability.
>
> Perhaps make it void and use something like
>
> sputchar(*buf++, end, <whateverchar>);

It's better, thank you.

> Though the sputchar name doesn't seems particularly intelligible.

I'm OK with any better name.

Thanks,
Yury

> > +static inline char *sputchar(char *buf, const char *end, char c)
> > +{
> > + if (buf < end)
> > + *buf = c;
> > +
> > + return buf + 1;
> > +}
> > +
> >  /*
> >   * General tracing related utility functions - trace_printk(),
> >   * tracing_on/tracing_off and tracing_start()/tracing_stop
> > diff --git a/lib/string_helpers.c b/lib/string_helpers.c
> []
> > @@ -335,12 +335,8 @@ static bool escape_space(unsigned char c, char **dst, char *end)
> >   return false;
> >   }
> >
> > - if (out < end)
> > - *out = '\\';
> > - ++out;
> > - if (out < end)
> > - *out = to;
> > - ++out;
> > + out = sputchar(out, end, '\\');
> > + out = sputchar(out, end, to);
>
> etc...
>