diff mbox series

lib/string_choices: Add str_plural() helper

Message ID 20240214165015.1656-1-michal.wajdeczko@intel.com (mailing list archive)
State Mainlined
Commit 9ca5facd0400f610f3f7f71aeb7fc0b949a48c67
Headers show
Series lib/string_choices: Add str_plural() helper | expand

Commit Message

Michal Wajdeczko Feb. 14, 2024, 4:50 p.m. UTC
Add str_plural() helper to replace existing open implementations
used by many drivers and help improve future user facing messages.

Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Cc: Jani Nikula <jani.nikula@intel.com>
---
 include/linux/string_choices.h | 5 +++++
 1 file changed, 5 insertions(+)

Comments

Andy Shevchenko Feb. 14, 2024, 6:08 p.m. UTC | #1
On Wed, Feb 14, 2024 at 05:50:15PM +0100, Michal Wajdeczko wrote:
> Add str_plural() helper to replace existing open implementations
> used by many drivers and help improve future user facing messages.

Any user of this, please?

> Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>

> Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Cc: Jani Nikula <jani.nikula@intel.com>

Move these Cc to the after '---' line.

> ---
Andy Shevchenko Feb. 14, 2024, 6:09 p.m. UTC | #2
On Wed, Feb 14, 2024 at 08:08:16PM +0200, Andy Shevchenko wrote:
> On Wed, Feb 14, 2024 at 05:50:15PM +0100, Michal Wajdeczko wrote:
> > Add str_plural() helper to replace existing open implementations
> > used by many drivers and help improve future user facing messages.
> 
> Any user of this, please?
> 
> > Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
> 
> > Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> > Cc: Jani Nikula <jani.nikula@intel.com>
> 
> Move these Cc to the after '---' line.
> 
> > ---

Besides that, check with the latest MAINTAINERS updates,
you missed the maintainer of this file.
Jani Nikula Feb. 14, 2024, 6:30 p.m. UTC | #3
On Wed, 14 Feb 2024, Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> On Wed, Feb 14, 2024 at 08:08:16PM +0200, Andy Shevchenko wrote:
>> On Wed, Feb 14, 2024 at 05:50:15PM +0100, Michal Wajdeczko wrote:
>> > Add str_plural() helper to replace existing open implementations
>> > used by many drivers and help improve future user facing messages.
>> 
>> Any user of this, please?

git grep "\"\" *: *\"s\""

>> > Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
>> 
>> > Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
>> > Cc: Jani Nikula <jani.nikula@intel.com>
>> 
>> Move these Cc to the after '---' line.

Why?

>> 
>> > ---
>
> Besides that, check with the latest MAINTAINERS updates,
> you missed the maintainer of this file.

Come on Andy, you might have Cc'd Kees yourself instead of just
nitpicking about it! Done now. ;)

BR,
Jani.
Andy Shevchenko Feb. 14, 2024, 6:49 p.m. UTC | #4
On Wed, Feb 14, 2024 at 08:30:53PM +0200, Jani Nikula wrote:
> On Wed, 14 Feb 2024, Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> > On Wed, Feb 14, 2024 at 08:08:16PM +0200, Andy Shevchenko wrote:
> >> On Wed, Feb 14, 2024 at 05:50:15PM +0100, Michal Wajdeczko wrote:
> >> > Add str_plural() helper to replace existing open implementations
> >> > used by many drivers and help improve future user facing messages.
> >> 
> >> Any user of this, please?
> 
> git grep "\"\" *: *\"s\""

You know what I meant.
Second patch in the series that shows usefulness of the first patch.

...

> >> > Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
> >> 
> >> > Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> >> > Cc: Jani Nikula <jani.nikula@intel.com>
> >> 
> >> Move these Cc to the after '---' line.
> 
> Why?

At bare minimum to reduce noise in the commit message.
If going further, to be environment friendly (no jokes).
Kees Cook Feb. 14, 2024, 7:07 p.m. UTC | #5
On Wed, Feb 14, 2024 at 08:49:39PM +0200, Andy Shevchenko wrote:
> On Wed, Feb 14, 2024 at 08:30:53PM +0200, Jani Nikula wrote:
> > On Wed, 14 Feb 2024, Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
> > > On Wed, Feb 14, 2024 at 08:08:16PM +0200, Andy Shevchenko wrote:
> > >> On Wed, Feb 14, 2024 at 05:50:15PM +0100, Michal Wajdeczko wrote:
> > >> > Add str_plural() helper to replace existing open implementations
> > >> > used by many drivers and help improve future user facing messages.
> > >> 
> > >> Any user of this, please?
> > 
> > git grep "\"\" *: *\"s\""
> 
> You know what I meant.
> Second patch in the series that shows usefulness of the first patch.

Or a Coccinelle script that could do some rewrites? But, yes, if you can
include some examples, that would be nice. I think the helper is fine to
add.

> 
> ...
> 
> > >> > Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
> > >> 
> > >> > Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> > >> > Cc: Jani Nikula <jani.nikula@intel.com>
> > >> 
> > >> Move these Cc to the after '---' line.
> > 
> > Why?
> 
> At bare minimum to reduce noise in the commit message.
> If going further, to be environment friendly (no jokes).

I learned the above-the-line CC habit from akpm. But in looking through
recent commit history, this does appear to be the exception now. I'll
need to adjust my own workflow for this too...
Jani Nikula Feb. 15, 2024, 3:20 p.m. UTC | #6
On Wed, 14 Feb 2024, Kees Cook <keescook@chromium.org> wrote:
> On Wed, Feb 14, 2024 at 08:49:39PM +0200, Andy Shevchenko wrote:
>> On Wed, Feb 14, 2024 at 08:30:53PM +0200, Jani Nikula wrote:
>> > On Wed, 14 Feb 2024, Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
>> > > On Wed, Feb 14, 2024 at 08:08:16PM +0200, Andy Shevchenko wrote:
>> > >> On Wed, Feb 14, 2024 at 05:50:15PM +0100, Michal Wajdeczko wrote:
>> > >> > Add str_plural() helper to replace existing open implementations
>> > >> > used by many drivers and help improve future user facing messages.
>> > >> 
>> > >> Any user of this, please?
>> > 
>> > git grep "\"\" *: *\"s\""
>> 
>> You know what I meant.
>> Second patch in the series that shows usefulness of the first patch.
>
> Or a Coccinelle script that could do some rewrites? But, yes, if you can
> include some examples, that would be nice. I think the helper is fine to
> add.

In all kindness, I think it's actually better to get the inevitable
bikeshedding rounds done first. It's trivial to add users after
that. And for simple things like this it should really be obvious to
everyone what the usage would be like.

My first attempt at adding a plural helper seems to date back to
2019. Yes, really. Personally, I honestly can no longer be bothered to
put in the extra effort before there's some indication of
acceptance. (Like there's here now, thanks!)

>> > >> > Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
>> > >> 
>> > >> > Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
>> > >> > Cc: Jani Nikula <jani.nikula@intel.com>
>> > >> 
>> > >> Move these Cc to the after '---' line.
>> > 
>> > Why?
>> 
>> At bare minimum to reduce noise in the commit message.
>> If going further, to be environment friendly (no jokes).
>
> I learned the above-the-line CC habit from akpm. But in looking through
> recent commit history, this does appear to be the exception now. I'll
> need to adjust my own workflow for this too...

It just depends on where in the kernel you look, every subsystem is
subtly different. In the drm subsystem if you added the Cc's below
"---", you might be asked to move them above.

Trivial for whoever applies the patches to do whatever they please with
the trailers, impossible for the contributor. It's like The Trial. :p

BR,
Jani.
Michal Wajdeczko Feb. 15, 2024, 3:37 p.m. UTC | #7
On 15.02.2024 16:20, Jani Nikula wrote:
> On Wed, 14 Feb 2024, Kees Cook <keescook@chromium.org> wrote:
>> On Wed, Feb 14, 2024 at 08:49:39PM +0200, Andy Shevchenko wrote:
>>> On Wed, Feb 14, 2024 at 08:30:53PM +0200, Jani Nikula wrote:
>>>> On Wed, 14 Feb 2024, Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
>>>>> On Wed, Feb 14, 2024 at 08:08:16PM +0200, Andy Shevchenko wrote:
>>>>>> On Wed, Feb 14, 2024 at 05:50:15PM +0100, Michal Wajdeczko wrote:
>>>>>>> Add str_plural() helper to replace existing open implementations
>>>>>>> used by many drivers and help improve future user facing messages.
>>>>>>
>>>>>> Any user of this, please?
>>>>
>>>> git grep "\"\" *: *\"s\""
>>>
>>> You know what I meant.
>>> Second patch in the series that shows usefulness of the first patch.
>>
>> Or a Coccinelle script that could do some rewrites? But, yes, if you can
>> include some examples, that would be nice. I think the helper is fine to
>> add.

please find below requested Coccinelle script:

virtual patch
virtual context
virtual report

@depends on patch@
expression E;
@@
(
-	E == 1 ? "" : "s"
+	str_plural(E)
|
-	(E == 1) ? "" : "s"
+	str_plural(E)
|
-	E > 1 ? "s" : ""
+	str_plural(E)
|
-	(E > 1) ? "s" : ""
+	str_plural(E)
)

@r depends on !patch exists@
expression E;
position P;
@@
(
*	E@P == 1 ? "" : "s"
|
*	(E@P == 1) ? "" : "s"
|
*	E@P > 1 ? "s" : ""
|
*	(E@P > 1) ? "s" : ""
)

@script:python depends on report@
p << r.P;
e << r.E;
@@

coccilib.report.print_report(p[0], "opportunity for str_plural(%s)" % e)


> 
> In all kindness, I think it's actually better to get the inevitable
> bikeshedding rounds done first. It's trivial to add users after
> that. And for simple things like this it should really be obvious to
> everyone what the usage would be like.
> 
> My first attempt at adding a plural helper seems to date back to
> 2019. 

true, I do recall that, hence your invite

> Yes, really. Personally, I honestly can no longer be bothered to
> put in the extra effort before there's some indication of
> acceptance. (Like there's here now, thanks!)
> 
>>>>>>> Signed-off-by: Michal Wajdeczko <michal.wajdeczko@intel.com>
>>>>>>
>>>>>>> Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
>>>>>>> Cc: Jani Nikula <jani.nikula@intel.com>
>>>>>>
>>>>>> Move these Cc to the after '---' line.
>>>>
>>>> Why?
>>>
>>> At bare minimum to reduce noise in the commit message.
>>> If going further, to be environment friendly (no jokes).
>>
>> I learned the above-the-line CC habit from akpm. But in looking through
>> recent commit history, this does appear to be the exception now. I'll
>> need to adjust my own workflow for this too...
> 
> It just depends on where in the kernel you look, every subsystem is
> subtly different. In the drm subsystem if you added the Cc's below
> "---", you might be asked to move them above.
> 
> Trivial for whoever applies the patches to do whatever they please with
> the trailers, impossible for the contributor. It's like The Trial. :p
> 
> BR,
> Jani.
> 
>
Andy Shevchenko Feb. 15, 2024, 4:11 p.m. UTC | #8
On Thu, Feb 15, 2024 at 04:37:18PM +0100, Michal Wajdeczko wrote:
> On 15.02.2024 16:20, Jani Nikula wrote:
> > On Wed, 14 Feb 2024, Kees Cook <keescook@chromium.org> wrote:
> >> On Wed, Feb 14, 2024 at 08:49:39PM +0200, Andy Shevchenko wrote:

...

> >> Or a Coccinelle script that could do some rewrites? But, yes, if you can
> >> include some examples, that would be nice. I think the helper is fine to
> >> add.
> 
> please find below requested Coccinelle script:

Thanks!

Btw, what to do with 0?
Michal Wajdeczko Feb. 15, 2024, 4:55 p.m. UTC | #9
On 15.02.2024 17:11, Andy Shevchenko wrote:
> On Thu, Feb 15, 2024 at 04:37:18PM +0100, Michal Wajdeczko wrote:
>> On 15.02.2024 16:20, Jani Nikula wrote:
>>> On Wed, 14 Feb 2024, Kees Cook <keescook@chromium.org> wrote:
>>>> On Wed, Feb 14, 2024 at 08:49:39PM +0200, Andy Shevchenko wrote:
> 
> ...
> 
>>>> Or a Coccinelle script that could do some rewrites? But, yes, if you can
>>>> include some examples, that would be nice. I think the helper is fine to
>>>> add.
>>
>> please find below requested Coccinelle script:
> 
> Thanks!
> 
> Btw, what to do with 0?
> 

code is based on this rule:

"With countable nouns, zero is always followed by plural nouns."
Andy Shevchenko Feb. 15, 2024, 5:05 p.m. UTC | #10
On Thu, Feb 15, 2024 at 05:55:43PM +0100, Michal Wajdeczko wrote:
> On 15.02.2024 17:11, Andy Shevchenko wrote:
> > On Thu, Feb 15, 2024 at 04:37:18PM +0100, Michal Wajdeczko wrote:
> >> On 15.02.2024 16:20, Jani Nikula wrote:
> >>> On Wed, 14 Feb 2024, Kees Cook <keescook@chromium.org> wrote:
> >>>> On Wed, Feb 14, 2024 at 08:49:39PM +0200, Andy Shevchenko wrote:

...

> >>>> Or a Coccinelle script that could do some rewrites? But, yes, if you can
> >>>> include some examples, that would be nice. I think the helper is fine to
> >>>> add.
> >>
> >> please find below requested Coccinelle script:
> > 
> > Thanks!
> > 
> > Btw, what to do with 0?
> 
> code is based on this rule:
> 
> "With countable nouns, zero is always followed by plural nouns."

If it's not documented / commented, please add one.
Michal Wajdeczko Feb. 15, 2024, 5:14 p.m. UTC | #11
On 15.02.2024 18:05, Andy Shevchenko wrote:
> On Thu, Feb 15, 2024 at 05:55:43PM +0100, Michal Wajdeczko wrote:
>> On 15.02.2024 17:11, Andy Shevchenko wrote:
>>> On Thu, Feb 15, 2024 at 04:37:18PM +0100, Michal Wajdeczko wrote:
>>>> On 15.02.2024 16:20, Jani Nikula wrote:
>>>>> On Wed, 14 Feb 2024, Kees Cook <keescook@chromium.org> wrote:
>>>>>> On Wed, Feb 14, 2024 at 08:49:39PM +0200, Andy Shevchenko wrote:
> 
> ...
> 
>>>>>> Or a Coccinelle script that could do some rewrites? But, yes, if you can
>>>>>> include some examples, that would be nice. I think the helper is fine to
>>>>>> add.
>>>>
>>>> please find below requested Coccinelle script:
>>>
>>> Thanks!
>>>
>>> Btw, what to do with 0?
>>
>> code is based on this rule:
>>
>> "With countable nouns, zero is always followed by plural nouns."
> 
> If it's not documented / commented, please add one.

but this is a rule of the English language, no ?
do we really have to repeat it in the code ?
Kees Cook Feb. 15, 2024, 7:23 p.m. UTC | #12
On Wed, 14 Feb 2024 17:50:15 +0100, Michal Wajdeczko wrote:
> Add str_plural() helper to replace existing open implementations
> used by many drivers and help improve future user facing messages.

I added some kern-doc just for good measure.

Applied to for-next/hardening, thanks!

[1/1] lib/string_choices: Add str_plural() helper
      https://git.kernel.org/kees/c/bf10745c0053

Take care,
diff mbox series

Patch

diff --git a/include/linux/string_choices.h b/include/linux/string_choices.h
index 3c1091941eb8..f3cd270d11fd 100644
--- a/include/linux/string_choices.h
+++ b/include/linux/string_choices.h
@@ -42,4 +42,9 @@  static inline const char *str_yes_no(bool v)
 	return v ? "yes" : "no";
 }
 
+static inline const char *str_plural(size_t num)
+{
+	return num == 1 ? "" : "s";
+}
+
 #endif