2023-04-01 17:54:11

by Alexey Dobriyan

[permalink] [raw]
Subject: [PATCH] string: implement streq()

Most of the time strcmp() is being used, strings are checked for equality.
Add wrapper to relay such intent (it is shorter to type, too).

Use
if (streq(s, "s")) {
}
or
if (!streq(s, "s")) {
}

Signed-off-by: Alexey Dobriyan <[email protected]>
---

drivers/clk/socfpga/clk-gate-a10.c | 2 --
drivers/clk/socfpga/clk.h | 1 -
include/linux/string.h | 6 ++++++
scripts/dtc/dtc.h | 6 +++++-
4 files changed, 11 insertions(+), 4 deletions(-)

--- a/drivers/clk/socfpga/clk-gate-a10.c
+++ b/drivers/clk/socfpga/clk-gate-a10.c
@@ -11,8 +11,6 @@

#include "clk.h"

-#define streq(a, b) (strcmp((a), (b)) == 0)
-
#define to_socfpga_gate_clk(p) container_of(p, struct socfpga_gate_clk, hw.hw)

/* SDMMC Group for System Manager defines */
--- a/drivers/clk/socfpga/clk.h
+++ b/drivers/clk/socfpga/clk.h
@@ -19,7 +19,6 @@

#define SOCFPGA_MAX_PARENTS 5

-#define streq(a, b) (strcmp((a), (b)) == 0)
#define SYSMGR_SDMMC_CTRL_SET(smplsel, drvsel) \
((((smplsel) & 0x7) << 3) | (((drvsel) & 0x7) << 0))

--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -47,6 +47,12 @@ extern size_t strlcat(char *, const char *, __kernel_size_t);
#ifndef __HAVE_ARCH_STRCMP
extern int strcmp(const char *,const char *);
#endif
+
+static inline bool streq(const char *a, const char *b)
+{
+ return strcmp(a, b) == 0;
+}
+
#ifndef __HAVE_ARCH_STRNCMP
extern int strncmp(const char *,const char *,__kernel_size_t);
#endif
--- a/scripts/dtc/dtc.h
+++ b/scripts/dtc/dtc.h
@@ -88,7 +88,11 @@ static inline uint64_t dtb_ld64(const void *p)
| bp[7];
}

-#define streq(a, b) (strcmp((a), (b)) == 0)
+static inline bool streq(const char *a, const char *b)
+{
+ return strcmp(a, b) == 0;
+}
+
#define strstarts(s, prefix) (strncmp((s), (prefix), strlen(prefix)) == 0)
#define strprefixeq(a, n, b) (strlen(b) == (n) && (memcmp(a, b, n) == 0))
static inline bool strends(const char *str, const char *suffix)


2023-04-03 21:20:29

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH] string: implement streq()

On Sat, 1 Apr 2023 20:48:05 +0300 Alexey Dobriyan <[email protected]> wrote:

> Most of the time strcmp() is being used, strings are checked for equality.
> Add wrapper to relay such intent (it is shorter to type, too).
>
> Use
> if (streq(s, "s")) {
> }
> or
> if (!streq(s, "s")) {
> }

Gee, do we really want this? I mean, we all know how strcmp() works,
don't we?

I'm thinking it would be better to remove the various cute little
streq() macros and open-code strcmp(...)==0.

2023-04-04 14:56:26

by Alexey Dobriyan

[permalink] [raw]
Subject: Re: [PATCH] string: implement streq()

On Mon, Apr 03, 2023 at 02:16:41PM -0700, Andrew Morton wrote:
> On Sat, 1 Apr 2023 20:48:05 +0300 Alexey Dobriyan <[email protected]> wrote:
>
> > Most of the time strcmp() is being used, strings are checked for equality.
> > Add wrapper to relay such intent (it is shorter to type, too).
> >
> > Use
> > if (streq(s, "s")) {
> > }
> > or
> > if (!streq(s, "s")) {
> > }
>
> Gee, do we really want this? I mean, we all know how strcmp() works,
> don't we?
>
> I'm thinking it would be better to remove the various cute little
> streq() macros and open-code strcmp(...)==0.

No! It's cool, trust me. Try it out, you'll like it.
We "know" how strcmp() works because C didn't have streq() from
the very beginning.

strcmp() is only for insertion into trees. Half of the time you need
to read to the end of the expression to know if it is check for equality
or inequality. With streq(), you don't.

stralexey

2023-04-05 10:50:55

by David Laight

[permalink] [raw]
Subject: RE: [PATCH] string: implement streq()

From: Alexey Dobriyan
> Sent: 04 April 2023 15:55
>
> On Mon, Apr 03, 2023 at 02:16:41PM -0700, Andrew Morton wrote:
> > On Sat, 1 Apr 2023 20:48:05 +0300 Alexey Dobriyan <[email protected]> wrote:
> >
> > > Most of the time strcmp() is being used, strings are checked for equality.
> > > Add wrapper to relay such intent (it is shorter to type, too).
> > >
> > > Use
> > > if (streq(s, "s")) {
> > > }
> > > or
> > > if (!streq(s, "s")) {
> > > }
> >
> > Gee, do we really want this? I mean, we all know how strcmp() works,
> > don't we?
> >
> > I'm thinking it would be better to remove the various cute little
> > streq() macros and open-code strcmp(...)==0.
>
> No! It's cool, trust me. Try it out, you'll like it.
> We "know" how strcmp() works because C didn't have streq() from
> the very beginning.
>
> strcmp() is only for insertion into trees. Half of the time you need
> to read to the end of the expression to know if it is check for equality
> or inequality. With streq(), you don't.

Instead you have to go away and look up what a function
you've never heard of does.

Live with strcmp()...

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)

2023-04-05 11:43:18

by Alexey Dobriyan

[permalink] [raw]
Subject: Re: [PATCH] string: implement streq()

On Wed, Apr 05, 2023 at 10:45:36AM +0000, David Laight wrote:
> From: Alexey Dobriyan
> > Sent: 04 April 2023 15:55
> >
> > On Mon, Apr 03, 2023 at 02:16:41PM -0700, Andrew Morton wrote:
> > > On Sat, 1 Apr 2023 20:48:05 +0300 Alexey Dobriyan <[email protected]> wrote:
> > >
> > > > Most of the time strcmp() is being used, strings are checked for equality.
> > > > Add wrapper to relay such intent (it is shorter to type, too).
> > > >
> > > > Use
> > > > if (streq(s, "s")) {
> > > > }
> > > > or
> > > > if (!streq(s, "s")) {
> > > > }
> > >
> > > Gee, do we really want this? I mean, we all know how strcmp() works,
> > > don't we?
> > >
> > > I'm thinking it would be better to remove the various cute little
> > > streq() macros and open-code strcmp(...)==0.
> >
> > No! It's cool, trust me. Try it out, you'll like it.
> > We "know" how strcmp() works because C didn't have streq() from
> > the very beginning.
> >
> > strcmp() is only for insertion into trees. Half of the time you need
> > to read to the end of the expression to know if it is check for equality
> > or inequality. With streq(), you don't.
>
> Instead you have to go away and look up what a function
> you've never heard of does.
>
> Live with strcmp()...

It is obvious what it does.