linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1 0/3] lib/string: introduce match_string() helper
@ 2015-09-15  6:53 Andy Shevchenko
  2015-09-15  6:53 ` [PATCH v1 1/3] " Andy Shevchenko
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Andy Shevchenko @ 2015-09-15  6:53 UTC (permalink / raw)
  To: linux-kernel, Tejun Heo, Andrew Morton, David Airlie,
	Rasmus Villemoes, Mika Westerberg
  Cc: Andy Shevchenko

There are users of a simple string matching in the array. Let's do a common
helper for that.

Two users are updated in the series. one more is coming [1].

[1] http://www.spinics.net/lists/kernel/msg2074265.html

The series is compile tested.

Andy Shevchenko (3):
  lib/string: introduce match_string() helper
  drm/edid: convert to use match_string() helper
  ata: hpt366: convert to use match_string() helper

 drivers/ata/pata_hpt366.c       | 13 +++++--------
 drivers/gpu/drm/drm_edid_load.c | 17 ++++++-----------
 include/linux/string.h          |  7 +++++++
 lib/string.c                    | 26 ++++++++++++++++++++++++++
 4 files changed, 44 insertions(+), 19 deletions(-)

-- 
2.5.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v1 1/3] lib/string: introduce match_string() helper
  2015-09-15  6:53 [PATCH v1 0/3] lib/string: introduce match_string() helper Andy Shevchenko
@ 2015-09-15  6:53 ` Andy Shevchenko
  2015-09-15  6:53 ` [PATCH v1 2/3] drm/edid: convert to use " Andy Shevchenko
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 6+ messages in thread
From: Andy Shevchenko @ 2015-09-15  6:53 UTC (permalink / raw)
  To: linux-kernel, Tejun Heo, Andrew Morton, David Airlie,
	Rasmus Villemoes, Mika Westerberg
  Cc: Andy Shevchenko

>From time to time we have to match a string in an array. Make a simple helper
for that purpose.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 include/linux/string.h |  7 +++++++
 lib/string.c           | 26 ++++++++++++++++++++++++++
 2 files changed, 33 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index a8d90db..15e6116 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -126,6 +126,13 @@ extern void argv_free(char **argv);
 extern bool sysfs_streq(const char *s1, const char *s2);
 extern int strtobool(const char *s, bool *res);
 
+int match_string(const char * const *array, size_t len, const char *string);
+
+static inline int match_stringnul(const char * const *array, const char *string)
+{
+	return match_string(array, 0, string);
+}
+
 #ifdef CONFIG_BINARY_PRINTF
 int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args);
 int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf);
diff --git a/lib/string.c b/lib/string.c
index 13d1e84..0771359 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -542,6 +542,32 @@ bool sysfs_streq(const char *s1, const char *s2)
 EXPORT_SYMBOL(sysfs_streq);
 
 /**
+ * match_string - matches given string in an array
+ * @array:	array of strings
+ * @len:	number of strings in the array or 0 for NULL terminated arrays
+ * @string:	string to match with
+ *
+ * Return:
+ * index of a @string in the array if matches, or -1 otherwise.
+ */
+int match_string(const char * const *array, size_t len, const char *string)
+{
+	int index = 0;
+	const char *item;
+
+	do {
+		item = array[index];
+		if (!item)
+			break;
+		if (!strcmp(item, string))
+			return index;
+	} while (++index < len || !len);
+
+	return -1;
+}
+EXPORT_SYMBOL(match_string);
+
+/**
  * strtobool - convert common user inputs into boolean values
  * @s: input string
  * @res: result
-- 
2.5.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v1 2/3] drm/edid: convert to use match_string() helper
  2015-09-15  6:53 [PATCH v1 0/3] lib/string: introduce match_string() helper Andy Shevchenko
  2015-09-15  6:53 ` [PATCH v1 1/3] " Andy Shevchenko
@ 2015-09-15  6:53 ` Andy Shevchenko
  2015-09-15  6:53 ` [PATCH v1 3/3] ata: hpt366: " Andy Shevchenko
  2015-09-15 15:35 ` [PATCH v1 0/3] lib/string: introduce " Tejun Heo
  3 siblings, 0 replies; 6+ messages in thread
From: Andy Shevchenko @ 2015-09-15  6:53 UTC (permalink / raw)
  To: linux-kernel, Tejun Heo, Andrew Morton, David Airlie,
	Rasmus Villemoes, Mika Westerberg
  Cc: Andy Shevchenko

The new helper returns index of the mathing string in an array. We would use it
here.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/gpu/drm/drm_edid_load.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)

diff --git a/drivers/gpu/drm/drm_edid_load.c b/drivers/gpu/drm/drm_edid_load.c
index 698b8c3..9a401ae 100644
--- a/drivers/gpu/drm/drm_edid_load.c
+++ b/drivers/gpu/drm/drm_edid_load.c
@@ -170,16 +170,11 @@ static void *edid_load(struct drm_connector *connector, const char *name,
 	int i, valid_extensions = 0;
 	bool print_bad_edid = !connector->bad_edid_counter || (drm_debug & DRM_UT_KMS);
 
-	builtin = 0;
-	for (i = 0; i < GENERIC_EDIDS; i++) {
-		if (strcmp(name, generic_edid_name[i]) == 0) {
-			fwdata = generic_edid[i];
-			fwsize = sizeof(generic_edid[i]);
-			builtin = 1;
-			break;
-		}
-	}
-	if (!builtin) {
+	builtin = match_string(generic_edid_name, GENERIC_EDIDS, name);
+	if (builtin >= 0) {
+		fwdata = generic_edid[builtin];
+		fwsize = sizeof(generic_edid[builtin]);
+	} else {
 		struct platform_device *pdev;
 		int err;
 
@@ -252,7 +247,7 @@ static void *edid_load(struct drm_connector *connector, const char *name,
 	}
 
 	DRM_INFO("Got %s EDID base block and %d extension%s from "
-	    "\"%s\" for connector \"%s\"\n", builtin ? "built-in" :
+	    "\"%s\" for connector \"%s\"\n", (builtin >= 0) ? "built-in" :
 	    "external", valid_extensions, valid_extensions == 1 ? "" : "s",
 	    name, connector_name);
 
-- 
2.5.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v1 3/3] ata: hpt366: convert to use match_string() helper
  2015-09-15  6:53 [PATCH v1 0/3] lib/string: introduce match_string() helper Andy Shevchenko
  2015-09-15  6:53 ` [PATCH v1 1/3] " Andy Shevchenko
  2015-09-15  6:53 ` [PATCH v1 2/3] drm/edid: convert to use " Andy Shevchenko
@ 2015-09-15  6:53 ` Andy Shevchenko
  2015-09-15 15:35 ` [PATCH v1 0/3] lib/string: introduce " Tejun Heo
  3 siblings, 0 replies; 6+ messages in thread
From: Andy Shevchenko @ 2015-09-15  6:53 UTC (permalink / raw)
  To: linux-kernel, Tejun Heo, Andrew Morton, David Airlie,
	Rasmus Villemoes, Mika Westerberg
  Cc: Andy Shevchenko

The new helper returns index of the mathing string in an array. We would use it
here.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/ata/pata_hpt366.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)

diff --git a/drivers/ata/pata_hpt366.c b/drivers/ata/pata_hpt366.c
index 0038dc4..3dcf350 100644
--- a/drivers/ata/pata_hpt366.c
+++ b/drivers/ata/pata_hpt366.c
@@ -176,17 +176,14 @@ static int hpt_dma_blacklisted(const struct ata_device *dev, char *modestr,
 			       const char * const list[])
 {
 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
-	int i = 0;
+	int i;
 
 	ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
 
-	while (list[i] != NULL) {
-		if (!strcmp(list[i], model_num)) {
-			pr_warn("%s is not supported for %s\n",
-				modestr, list[i]);
-			return 1;
-		}
-		i++;
+	i = match_stringnul(list, model_num);
+	if (i >= 0) {
+		pr_warn("%s is not supported for %s\n", modestr, list[i]);
+		return 1;
 	}
 	return 0;
 }
-- 
2.5.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v1 0/3] lib/string: introduce match_string() helper
  2015-09-15  6:53 [PATCH v1 0/3] lib/string: introduce match_string() helper Andy Shevchenko
                   ` (2 preceding siblings ...)
  2015-09-15  6:53 ` [PATCH v1 3/3] ata: hpt366: " Andy Shevchenko
@ 2015-09-15 15:35 ` Tejun Heo
  2015-10-22 11:33   ` Andy Shevchenko
  3 siblings, 1 reply; 6+ messages in thread
From: Tejun Heo @ 2015-09-15 15:35 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: linux-kernel, Andrew Morton, David Airlie, Rasmus Villemoes,
	Mika Westerberg

Hello,

On Tue, Sep 15, 2015 at 09:53:48AM +0300, Andy Shevchenko wrote:
> There are users of a simple string matching in the array. Let's do a common
> helper for that.
> 
> Two users are updated in the series. one more is coming [1].
> 
> [1] http://www.spinics.net/lists/kernel/msg2074265.html
> 
> The series is compile tested.
> 
> Andy Shevchenko (3):
>   lib/string: introduce match_string() helper
>   drm/edid: convert to use match_string() helper
>   ata: hpt366: convert to use match_string() helper
> 
>  drivers/ata/pata_hpt366.c       | 13 +++++--------
>  drivers/gpu/drm/drm_edid_load.c | 17 ++++++-----------
>  include/linux/string.h          |  7 +++++++
>  lib/string.c                    | 26 ++++++++++++++++++++++++++
>  4 files changed, 44 insertions(+), 19 deletions(-)

While increase in LOC isn't the only indicator, we're adding two
helper functions each for two usages.  I'm not quite sure the addition
of helpers is paying off here.

Thanks.

-- 
tejun

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v1 0/3] lib/string: introduce match_string() helper
  2015-09-15 15:35 ` [PATCH v1 0/3] lib/string: introduce " Tejun Heo
@ 2015-10-22 11:33   ` Andy Shevchenko
  0 siblings, 0 replies; 6+ messages in thread
From: Andy Shevchenko @ 2015-10-22 11:33 UTC (permalink / raw)
  To: Tejun Heo
  Cc: linux-kernel, Andrew Morton, David Airlie, Rasmus Villemoes,
	Mika Westerberg

On Tue, 2015-09-15 at 11:35 -0400, Tejun Heo wrote:
> Hello,
> 
> On Tue, Sep 15, 2015 at 09:53:48AM +0300, Andy Shevchenko wrote:
> > There are users of a simple string matching in the array. Let's do
> > a common
> > helper for that.
> > 
> > Two users are updated in the series. one more is coming [1].
> > 
> > [1] http://www.spinics.net/lists/kernel/msg2074265.html
> > 
> > The series is compile tested.
> > 
> > Andy Shevchenko (3):
> >   lib/string: introduce match_string() helper
> >   drm/edid: convert to use match_string() helper
> >   ata: hpt366: convert to use match_string() helper
> > 
> >  drivers/ata/pata_hpt366.c       | 13 +++++--------
> >  drivers/gpu/drm/drm_edid_load.c | 17 ++++++-----------
> >  include/linux/string.h          |  7 +++++++
> >  lib/string.c                    | 26 ++++++++++++++++++++++++++
> >  4 files changed, 44 insertions(+), 19 deletions(-)
> 
> While increase in LOC isn't the only indicator, we're adding two
> helper functions each for two usages.  I'm not quite sure the
> addition
> of helpers is paying off here.

Does the following sound better? (Btw, I left only one helper function)

 drivers/ata/pata_hpt366.c       | 13 +++++--------
 drivers/base/property.c         | 10 ++--------
 drivers/gpu/drm/drm_edid_load.c | 17 ++++++-----------
 drivers/pinctrl/pinmux.c        | 13 +++----------
 drivers/power/ab8500_btemp.c    | 11 +++--------
 drivers/power/ab8500_charger.c  | 11 +++--------
 drivers/power/ab8500_fg.c       | 11 +++--------
 drivers/power/abx500_chargalg.c | 10 +++-------
 drivers/power/charger-manager.c | 27 ++++-----------------------
 include/linux/string.h          |  2 ++
 lib/string.c                    | 26 ++++++++++++++++++++++++++
 11 files changed, 60 insertions(+), 91 deletions(-)

> 
> Thanks.
> 

-- 
Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Intel Finland Oy


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-10-22 11:34 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-15  6:53 [PATCH v1 0/3] lib/string: introduce match_string() helper Andy Shevchenko
2015-09-15  6:53 ` [PATCH v1 1/3] " Andy Shevchenko
2015-09-15  6:53 ` [PATCH v1 2/3] drm/edid: convert to use " Andy Shevchenko
2015-09-15  6:53 ` [PATCH v1 3/3] ata: hpt366: " Andy Shevchenko
2015-09-15 15:35 ` [PATCH v1 0/3] lib/string: introduce " Tejun Heo
2015-10-22 11:33   ` Andy Shevchenko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).