Skip to contentSkip to conversation

lib/string_choices: Add str_plural() helper

PATCH Applied Michal Wajdeczko 4 participants 13 messages Feb 14, 2024 – Feb 15, 2024 Generic string library

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/string_choices: Add str_plural() helper

Add str_plural() helper to replace existing open implementations used by many drivers and help improve future user facing messages.

Cc Andy Shevchenko <[email protected]>
Cc Jani Nikula <[email protected]>
---
 include/linux/string_choices.h | 5 +++++
 1 file changed, 5 insertions(+)
include/linux/string_choices.h +5 -0
index 3c1091941eb8..f3cd270d11fd 100644
@@ -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
Signature · 1 line
2.43.0
#1 lore raw
Quoted 2 lines · Michal Wajdeczko

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?

Quoted 4 lines
Cc Andy Shevchenko <[email protected]>
Cc Jani Nikula <[email protected]>

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

Quoted 1 line

---

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#2 lore raw
Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Michal Wajdeczko

Add str_plural() helper to replace existing open implementations used by many drivers and help improve future user facing messages.

Quoted 10 lines

Any user of this, please?

Quoted 4 lines
Cc Andy Shevchenko <[email protected]>
Cc Jani Nikula <[email protected]>

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

Quoted 1 line

---

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

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Andy Shevchenko
Quoted 2 lines · Michal Wajdeczko

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\""

Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Cc Andy Shevchenko <[email protected]>
Cc Jani Nikula <[email protected]>

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

Why?

Quoted 5 lines
Quoted 2 lines
Quoted 2 lines

---

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.

Signature · 1 line
Jani Nikula, Intel
#4 lore raw
Quoted 9 lines · Jani Nikula
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Andy Shevchenko
Quoted 2 lines · Michal Wajdeczko

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.

...

Quoted 8 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Cc Andy Shevchenko <[email protected]>
Cc Jani Nikula <[email protected]>

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).

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#5 lore raw
Quoted 7 lines · Andy Shevchenko
Quoted 6 lines · Jani Nikula
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Andy Shevchenko
Quoted 2 lines · Michal Wajdeczko

Add str_plural() helper to replace existing open implementations used by many drivers and help improve future user facing messages.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

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.

Quoted 8 lines

...

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Cc Andy Shevchenko <[email protected]>
Cc Jani Nikula <[email protected]>
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Quoted 1 line

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...

Signature · 1 line
Kees Cook
#6 lore raw
Quoted 10 lines · Kees Cook
Quoted 9 lines · Andy Shevchenko
Quoted 8 lines · Jani Nikula
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Andy Shevchenko
Quoted 2 lines · Michal Wajdeczko

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?

Quoted 8 lines
Quoted 4 lines
Quoted 1 line

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!)

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Cc Andy Shevchenko <[email protected]>
Cc Jani Nikula <[email protected]>

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

Quoted 8 lines
Quoted 4 lines
Quoted 1 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.

Signature · 1 line
Jani Nikula, Intel
#7 lore raw
Quoted 11 lines · Jani Nikula
Quoted 10 lines · Kees Cook
Quoted 9 lines · Andy Shevchenko
Quoted 8 lines · Jani Nikula
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Andy Shevchenko
Quoted 2 lines · Michal Wajdeczko

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?

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
Quoted 1 line

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)

Quoted 8 lines

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

Quoted 31 lines

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!)

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines
Quoted 4 lines
Cc Andy Shevchenko <[email protected]>
Cc Jani Nikula <[email protected]>

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

Quoted 8 lines
Quoted 4 lines
Quoted 1 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.

#8 lore raw
Quoted 3 lines · Michal Wajdeczko
Quoted 2 lines · Jani Nikula
Quoted 1 line · Kees Cook

On Wed, Feb 14, 2024 at 08:49:39PM +0200, Andy Shevchenko wrote:

...

Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

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?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#9 lore raw
Quoted 11 lines · Andy Shevchenko
Quoted 3 lines · Michal Wajdeczko
Quoted 2 lines · Jani Nikula
Quoted 1 line · Kees Cook

On Wed, Feb 14, 2024 at 08:49:39PM +0200, Andy Shevchenko wrote:

...

Quoted 4 lines
Quoted 3 lines
Quoted 3 lines

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.

Quoted 6 lines
Quoted 1 line

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."

#10 lore raw
Quoted 5 lines · Michal Wajdeczko
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Michal Wajdeczko
Quoted 2 lines · Jani Nikula
Quoted 1 line · Kees Cook

On Wed, Feb 14, 2024 at 08:49:39PM +0200, Andy Shevchenko wrote:

...

Quoted 6 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

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:

Quoted 7 lines
Quoted 3 lines

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.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#11 lore raw
Quoted 17 lines · Andy Shevchenko
Quoted 5 lines · Michal Wajdeczko
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Michal Wajdeczko
Quoted 2 lines · Jani Nikula
Quoted 1 line · Kees Cook

On Wed, Feb 14, 2024 at 08:49:39PM +0200, Andy Shevchenko wrote:

...

Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
Quoted 3 lines
Quoted 3 lines

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!

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

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 ?

Applied
#12 lore raw
In reply to Michal Wajdeczko
Quoted 2 lines · Michal Wajdeczko

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,

Signature · 1 line
Kees Cook