Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1320657 > unrolled thread

[PATCH v4 0/9] lib/string: introduce match_string() helper

Started byAndy Shevchenko <andriy.shevchenko@linux.intel.com>
First post2016-01-28 14:20 +0100
Last post2016-01-28 14:20 +0100
Articles 13 — 3 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH v4 0/9] lib/string: introduce match_string() helper Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-28 14:20 +0100
    [PATCH v4 4/9] drm/edid: convert to use match_string() helper Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-28 14:20 +0100
    [PATCH v4 3/9] pinctrl: convert to use match_string() helper Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-28 14:20 +0100
      Re: [PATCH v4 3/9] pinctrl: convert to use match_string() helper Andrew Morton <akpm@linux-foundation.org> - 2016-02-02 07:10 +0100
        Re: [PATCH v4 3/9] pinctrl: convert to use match_string() helper Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-02-02 09:20 +0100
    [PATCH v4 8/9] ide: hpt366: convert to use match_string() helper Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-28 14:20 +0100
    [PATCH v4 5/9] power: charger_manager: convert to use match_string() helper Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-28 14:20 +0100
    [PATCH v4 6/9] power: ab8500: convert to use match_string() helper Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-28 14:20 +0100
    [PATCH v4 1/9] lib/string: introduce match_string() helper Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-28 14:20 +0100
      Re: [PATCH v4 1/9] lib/string: introduce match_string() helper Andrew Morton <akpm@linux-foundation.org> - 2016-02-02 07:10 +0100
        Re: [PATCH v4 1/9] lib/string: introduce match_string() helper Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-02-02 09:00 +0100
        Re: [PATCH v4 1/9] lib/string: introduce match_string() helper Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2016-02-02 21:00 +0100
    [PATCH v4 7/9] ata: hpt366: convert to use match_string() helper Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-28 14:20 +0100

#1320657 — [PATCH v4 0/9] lib/string: introduce match_string() helper

FromAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Date2016-01-28 14:20 +0100
Subject[PATCH v4 0/9] lib/string: introduce match_string() helper
Message-ID<qVUEp-3UD-5@gated-at.bofh.it>
There are users of a simple string matching in the array. Let's do a common
helper for that.

Several users are updated in the series.

The series is compile tested.

There is still a question what function should return. There are variants:
-1, -EINVAL, -ENODATA, something else?

Since v3:
- rebase on top of latest linux-next
- add Rafael's Ack

Since v2:
- slightly modify implementation of the helper:
  - rename len -> n in prototype (Sergey)
  - move to for-loop (Rasmus)
  - change 0 -> -1 to iterate over NULL-terminated arrays (Rasmus)
- add patch 9 from Heikki
- append tags

Since v1:
- convert few more users to get helper useful

Andy Shevchenko (8):
  lib/string: introduce match_string() helper
  device property: convert to use match_string() helper
  pinctrl: convert to use match_string() helper
  drm/edid: convert to use match_string() helper
  power: charger_manager: convert to use match_string() helper
  power: ab8500: convert to use match_string() helper
  ata: hpt366: convert to use match_string() helper
  ide: hpt366: convert to use match_string() helper

Heikki Krogerus (1):
  usb: common: convert to use match_string() helper

 drivers/ata/pata_hpt366.c       | 13 +++++--------
 drivers/base/property.c         | 10 ++--------
 drivers/gpu/drm/drm_edid_load.c | 17 ++++++-----------
 drivers/ide/hpt366.c            |  9 ++-------
 drivers/pinctrl/pinmux.c        | 13 +++----------
 drivers/power/ab8500_btemp.c    | 15 +++++----------
 drivers/power/ab8500_charger.c  | 16 +++++-----------
 drivers/power/ab8500_fg.c       | 15 +++++----------
 drivers/power/abx500_chargalg.c | 14 +++++---------
 drivers/power/charger-manager.c | 27 ++++-----------------------
 drivers/usb/common/common.c     | 22 ++++++++--------------
 include/linux/string.h          |  2 ++
 lib/string.c                    | 26 ++++++++++++++++++++++++++
 13 files changed, 78 insertions(+), 121 deletions(-)

-- 
2.7.0.rc3

[toc] | [next] | [standalone]


#1320658 — [PATCH v4 4/9] drm/edid: convert to use match_string() helper

FromAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Date2016-01-28 14:20 +0100
Subject[PATCH v4 4/9] drm/edid: convert to use match_string() helper
Message-ID<qVUEq-3UD-21@gated-at.bofh.it>
In reply to#1320657
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.7.0.rc3

[toc] | [prev] | [next] | [standalone]


#1320660 — [PATCH v4 3/9] pinctrl: convert to use match_string() helper

FromAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Date2016-01-28 14:20 +0100
Subject[PATCH v4 3/9] pinctrl: convert to use match_string() helper
Message-ID<qVUEq-3UD-37@gated-at.bofh.it>
In reply to#1320657
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>
Acked-by: Linus Walleij <linus.walleij@linaro.org>
---
 drivers/pinctrl/pinmux.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)

diff --git a/drivers/pinctrl/pinmux.c b/drivers/pinctrl/pinmux.c
index 29984b3..c223a9e 100644
--- a/drivers/pinctrl/pinmux.c
+++ b/drivers/pinctrl/pinmux.c
@@ -334,7 +334,6 @@ int pinmux_map_to_setting(struct pinctrl_map const *map,
 	unsigned num_groups;
 	int ret;
 	const char *group;
-	int i;
 
 	if (!pmxops) {
 		dev_err(pctldev->dev, "does not support mux function\n");
@@ -363,19 +362,13 @@ int pinmux_map_to_setting(struct pinctrl_map const *map,
 		return -EINVAL;
 	}
 	if (map->data.mux.group) {
-		bool found = false;
 		group = map->data.mux.group;
-		for (i = 0; i < num_groups; i++) {
-			if (!strcmp(group, groups[i])) {
-				found = true;
-				break;
-			}
-		}
-		if (!found) {
+		ret = match_string(groups, num_groups, group);
+		if (ret < 0) {
 			dev_err(pctldev->dev,
 				"invalid group \"%s\" for function \"%s\"\n",
 				group, map->data.mux.function);
-			return -EINVAL;
+			return ret;
 		}
 	} else {
 		group = groups[0];
-- 
2.7.0.rc3

[toc] | [prev] | [next] | [standalone]


#1323808 — Re: [PATCH v4 3/9] pinctrl: convert to use match_string() helper

FromAndrew Morton <akpm@linux-foundation.org>
Date2016-02-02 07:10 +0100
SubjectRe: [PATCH v4 3/9] pinctrl: convert to use match_string() helper
Message-ID<qXCk1-6AZ-1@gated-at.bofh.it>
In reply to#1320660
On Thu, 28 Jan 2016 15:14:19 +0200 Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:

> The new helper returns index of the mathing string in an array. We would use it
> here.
> 
> --- a/drivers/pinctrl/pinmux.c
> +++ b/drivers/pinctrl/pinmux.c
> @@ -334,7 +334,6 @@ int pinmux_map_to_setting(struct pinctrl_map const *map,
>  	unsigned num_groups;
>  	int ret;
>  	const char *group;
> -	int i;
>  
>  	if (!pmxops) {
>  		dev_err(pctldev->dev, "does not support mux function\n");
> @@ -363,19 +362,13 @@ int pinmux_map_to_setting(struct pinctrl_map const *map,
>  		return -EINVAL;
>  	}
>  	if (map->data.mux.group) {
> -		bool found = false;
>  		group = map->data.mux.group;
> -		for (i = 0; i < num_groups; i++) {
> -			if (!strcmp(group, groups[i])) {
> -				found = true;
> -				break;
> -			}
> -		}
> -		if (!found) {
> +		ret = match_string(groups, num_groups, group);
> +		if (ret < 0) {
>  			dev_err(pctldev->dev,
>  				"invalid group \"%s\" for function \"%s\"\n",
>  				group, map->data.mux.function);
> -			return -EINVAL;
> +			return ret;

Changes the return value from -EINVAL to -ENODATA.  I'm not reeeeeealy
sure what ENODATA means - it seems mostly a networking thing?  People
use it in various places because it kinda sounds like whatever it is
that just went wrong.

But the question is: what will the end user think when this error comes
out of the kernel?  Given that he/she has just tried to misconfigure
the pinctrl system, ENODATA will cause much head-scratching.  EINVAL is
more appropriate?  "You tried to do something invalid".

>  		}
>  	} else {
>  		group = groups[0];
> -- 
> 2.7.0.rc3

[toc] | [prev] | [next] | [standalone]


#1323881 — Re: [PATCH v4 3/9] pinctrl: convert to use match_string() helper

FromAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Date2016-02-02 09:20 +0100
SubjectRe: [PATCH v4 3/9] pinctrl: convert to use match_string() helper
Message-ID<qXElR-84M-27@gated-at.bofh.it>
In reply to#1323808
On Mon, 2016-02-01 at 22:02 -0800, Andrew Morton wrote:
> On Thu, 28 Jan 2016 15:14:19 +0200 Andy Shevchenko <andriy.shevchenko
> @linux.intel.com> wrote:
> 
> > The new helper returns index of the mathing string in an array. We
> > would use it
> > here.
> > 
> > --- a/drivers/pinctrl/pinmux.c
> > +++ b/drivers/pinctrl/pinmux.c
> > @@ -334,7 +334,6 @@ int pinmux_map_to_setting(struct pinctrl_map
> > const *map,
> >  	unsigned num_groups;
> >  	int ret;
> >  	const char *group;
> > -	int i;
> >  
> >  	if (!pmxops) {
> >  		dev_err(pctldev->dev, "does not support mux
> > function\n");
> > @@ -363,19 +362,13 @@ int pinmux_map_to_setting(struct pinctrl_map
> > const *map,
> >  		return -EINVAL;
> >  	}
> >  	if (map->data.mux.group) {
> > -		bool found = false;
> >  		group = map->data.mux.group;
> > -		for (i = 0; i < num_groups; i++) {
> > -			if (!strcmp(group, groups[i])) {
> > -				found = true;
> > -				break;
> > -			}
> > -		}
> > -		if (!found) {
> > +		ret = match_string(groups, num_groups, group);
> > +		if (ret < 0) {
> >  			dev_err(pctldev->dev,
> >  				"invalid group \"%s\" for function
> > \"%s\"\n",
> >  				group, map->data.mux.function);
> > -			return -EINVAL;
> > +			return ret;
> 
> Changes the return value from -EINVAL to -ENODATA.

Yeah, Al is concerned about this as well [1]. That's why I emphasized
this in cover letter.

>   I'm not reeeeeealy
> sure what ENODATA means - it seems mostly a networking thing?  People
> use it in various places because it kinda sounds like whatever it is
> that just went wrong.
> 
> But the question is: what will the end user think when this error
> comes
> out of the kernel?  Given that he/she has just tried to misconfigure
> the pinctrl system, ENODATA will cause much head-scratching.  EINVAL
> is
> more appropriate?  "You tried to do something invalid".

Yeah, my arguments still the same, our error reporting from 70s sucks.

Since few people are concerned about EINVAL vs. ENODATA, I will re-do
patches 1 and 2 to follow this default instead of ENODATA.

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

> >  		}
> >  	} else {
> >  		group = groups[0];

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

[toc] | [prev] | [next] | [standalone]


#1320661 — [PATCH v4 8/9] ide: hpt366: convert to use match_string() helper

FromAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Date2016-01-28 14:20 +0100
Subject[PATCH v4 8/9] ide: hpt366: convert to use match_string() helper
Message-ID<qVUEq-3UD-35@gated-at.bofh.it>
In reply to#1320657
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/ide/hpt366.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

diff --git a/drivers/ide/hpt366.c b/drivers/ide/hpt366.c
index 696b6c1..f94baad 100644
--- a/drivers/ide/hpt366.c
+++ b/drivers/ide/hpt366.c
@@ -531,14 +531,9 @@ static const struct hpt_info hpt371n = {
 	.timings	= &hpt37x_timings
 };
 
-static int check_in_drive_list(ide_drive_t *drive, const char **list)
+static bool check_in_drive_list(ide_drive_t *drive, const char **list)
 {
-	char *m = (char *)&drive->id[ATA_ID_PROD];
-
-	while (*list)
-		if (!strcmp(*list++, m))
-			return 1;
-	return 0;
+	return match_string(list, -1, (char *)&drive->id[ATA_ID_PROD]) >= 0;
 }
 
 static struct hpt_info *hpt3xx_get_info(struct device *dev)
-- 
2.7.0.rc3

[toc] | [prev] | [next] | [standalone]


#1320662 — [PATCH v4 5/9] power: charger_manager: convert to use match_string() helper

FromAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Date2016-01-28 14:20 +0100
Subject[PATCH v4 5/9] power: charger_manager: convert to use match_string() helper
Message-ID<qVUEr-3UD-49@gated-at.bofh.it>
In reply to#1320657
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/power/charger-manager.c | 27 ++++-----------------------
 1 file changed, 4 insertions(+), 23 deletions(-)

diff --git a/drivers/power/charger-manager.c b/drivers/power/charger-manager.c
index 1ea5d1a..e664ca7 100644
--- a/drivers/power/charger-manager.c
+++ b/drivers/power/charger-manager.c
@@ -2020,27 +2020,6 @@ static void __exit charger_manager_cleanup(void)
 module_exit(charger_manager_cleanup);
 
 /**
- * find_power_supply - find the associated power_supply of charger
- * @cm: the Charger Manager representing the battery
- * @psy: pointer to instance of charger's power_supply
- */
-static bool find_power_supply(struct charger_manager *cm,
-			struct power_supply *psy)
-{
-	int i;
-	bool found = false;
-
-	for (i = 0; cm->desc->psy_charger_stat[i]; i++) {
-		if (!strcmp(psy->desc->name, cm->desc->psy_charger_stat[i])) {
-			found = true;
-			break;
-		}
-	}
-
-	return found;
-}
-
-/**
  * cm_notify_event - charger driver notify Charger Manager of charger event
  * @psy: pointer to instance of charger's power_supply
  * @type: type of charger event
@@ -2057,9 +2036,11 @@ void cm_notify_event(struct power_supply *psy, enum cm_event_types type,
 
 	mutex_lock(&cm_list_mtx);
 	list_for_each_entry(cm, &cm_list, entry) {
-		found_power_supply = find_power_supply(cm, psy);
-		if (found_power_supply)
+		if (match_string(cm->desc->psy_charger_stat, -1,
+				 psy->desc->name) >= 0) {
+			found_power_supply = true;
 			break;
+		}
 	}
 	mutex_unlock(&cm_list_mtx);
 
-- 
2.7.0.rc3

[toc] | [prev] | [next] | [standalone]


#1320663 — [PATCH v4 6/9] power: ab8500: convert to use match_string() helper

FromAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Date2016-01-28 14:20 +0100
Subject[PATCH v4 6/9] power: ab8500: convert to use match_string() helper
Message-ID<qVUEr-3UD-47@gated-at.bofh.it>
In reply to#1320657
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>
Acked-by: Linus Walleij <linus.walleij@linaro.org>
---
 drivers/power/ab8500_btemp.c    | 15 +++++----------
 drivers/power/ab8500_charger.c  | 16 +++++-----------
 drivers/power/ab8500_fg.c       | 15 +++++----------
 drivers/power/abx500_chargalg.c | 14 +++++---------
 4 files changed, 20 insertions(+), 40 deletions(-)

diff --git a/drivers/power/ab8500_btemp.c b/drivers/power/ab8500_btemp.c
index 8f8044e..bf2e5dd 100644
--- a/drivers/power/ab8500_btemp.c
+++ b/drivers/power/ab8500_btemp.c
@@ -906,26 +906,21 @@ static int ab8500_btemp_get_property(struct power_supply *psy,
 static int ab8500_btemp_get_ext_psy_data(struct device *dev, void *data)
 {
 	struct power_supply *psy;
-	struct power_supply *ext;
+	struct power_supply *ext = dev_get_drvdata(dev);
+	const char **supplicants = (const char **)ext->supplied_to;
 	struct ab8500_btemp *di;
 	union power_supply_propval ret;
-	int i, j;
-	bool psy_found = false;
+	int j;
 
 	psy = (struct power_supply *)data;
-	ext = dev_get_drvdata(dev);
 	di = power_supply_get_drvdata(psy);
 
 	/*
 	 * For all psy where the name of your driver
 	 * appears in any supplied_to
 	 */
-	for (i = 0; i < ext->num_supplicants; i++) {
-		if (!strcmp(ext->supplied_to[i], psy->desc->name))
-			psy_found = true;
-	}
-
-	if (!psy_found)
+	j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
+	if (j < 0)
 		return 0;
 
 	/* Go through all properties for the psy */
diff --git a/drivers/power/ab8500_charger.c b/drivers/power/ab8500_charger.c
index e388171..30de5d4 100644
--- a/drivers/power/ab8500_charger.c
+++ b/drivers/power/ab8500_charger.c
@@ -1929,11 +1929,11 @@ static int ab8540_charger_usb_pre_chg_enable(struct ux500_charger *charger,
 static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
 {
 	struct power_supply *psy;
-	struct power_supply *ext;
+	struct power_supply *ext = dev_get_drvdata(dev);
+	const char **supplicants = (const char **)ext->supplied_to;
 	struct ab8500_charger *di;
 	union power_supply_propval ret;
-	int i, j;
-	bool psy_found = false;
+	int j;
 	struct ux500_charger *usb_chg;
 
 	usb_chg = (struct ux500_charger *)data;
@@ -1941,15 +1941,9 @@ static int ab8500_charger_get_ext_psy_data(struct device *dev, void *data)
 
 	di = to_ab8500_charger_usb_device_info(usb_chg);
 
-	ext = dev_get_drvdata(dev);
-
 	/* For all psy where the driver name appears in any supplied_to */
-	for (i = 0; i < ext->num_supplicants; i++) {
-		if (!strcmp(ext->supplied_to[i], psy->desc->name))
-			psy_found = true;
-	}
-
-	if (!psy_found)
+	j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
+	if (j < 0)
 		return 0;
 
 	/* Go through all properties for the psy */
diff --git a/drivers/power/ab8500_fg.c b/drivers/power/ab8500_fg.c
index 3830dad..5a36cf8 100644
--- a/drivers/power/ab8500_fg.c
+++ b/drivers/power/ab8500_fg.c
@@ -2168,26 +2168,21 @@ static int ab8500_fg_get_property(struct power_supply *psy,
 static int ab8500_fg_get_ext_psy_data(struct device *dev, void *data)
 {
 	struct power_supply *psy;
-	struct power_supply *ext;
+	struct power_supply *ext = dev_get_drvdata(dev);
+	const char **supplicants = (const char **)ext->supplied_to;
 	struct ab8500_fg *di;
 	union power_supply_propval ret;
-	int i, j;
-	bool psy_found = false;
+	int j;
 
 	psy = (struct power_supply *)data;
-	ext = dev_get_drvdata(dev);
 	di = power_supply_get_drvdata(psy);
 
 	/*
 	 * For all psy where the name of your driver
 	 * appears in any supplied_to
 	 */
-	for (i = 0; i < ext->num_supplicants; i++) {
-		if (!strcmp(ext->supplied_to[i], psy->desc->name))
-			psy_found = true;
-	}
-
-	if (!psy_found)
+	j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
+	if (j < 0)
 		return 0;
 
 	/* Go through all properties for the psy */
diff --git a/drivers/power/abx500_chargalg.c b/drivers/power/abx500_chargalg.c
index 541f702..d9104b1 100644
--- a/drivers/power/abx500_chargalg.c
+++ b/drivers/power/abx500_chargalg.c
@@ -975,22 +975,18 @@ static void handle_maxim_chg_curr(struct abx500_chargalg *di)
 static int abx500_chargalg_get_ext_psy_data(struct device *dev, void *data)
 {
 	struct power_supply *psy;
-	struct power_supply *ext;
+	struct power_supply *ext = dev_get_drvdata(dev);
+	const char **supplicants = (const char **)ext->supplied_to;
 	struct abx500_chargalg *di;
 	union power_supply_propval ret;
-	int i, j;
-	bool psy_found = false;
+	int j;
 	bool capacity_updated = false;
 
 	psy = (struct power_supply *)data;
-	ext = dev_get_drvdata(dev);
 	di = power_supply_get_drvdata(psy);
 	/* For all psy where the driver name appears in any supplied_to */
-	for (i = 0; i < ext->num_supplicants; i++) {
-		if (!strcmp(ext->supplied_to[i], psy->desc->name))
-			psy_found = true;
-	}
-	if (!psy_found)
+	j = match_string(supplicants, ext->num_supplicants, psy->desc->name);
+	if (j < 0)
 		return 0;
 
 	/*
-- 
2.7.0.rc3

[toc] | [prev] | [next] | [standalone]


#1320665 — [PATCH v4 1/9] lib/string: introduce match_string() helper

FromAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Date2016-01-28 14:20 +0100
Subject[PATCH v4 1/9] lib/string: introduce match_string() helper
Message-ID<qVUEr-3UD-51@gated-at.bofh.it>
In reply to#1320657
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 |  2 ++
 lib/string.c           | 26 ++++++++++++++++++++++++++
 2 files changed, 28 insertions(+)

diff --git a/include/linux/string.h b/include/linux/string.h
index b0a732b..07505aa 100644
--- a/include/linux/string.h
+++ b/include/linux/string.h
@@ -131,6 +131,8 @@ 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 n, const char *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 0323c0d..ba01d4f 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -631,6 +631,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
+ * @n:		number of strings in the array or -1 for NULL terminated arrays
+ * @string:	string to match with
+ *
+ * Return:
+ * index of a @string in the @array if matches, or %-ENODATA otherwise.
+ */
+int match_string(const char * const *array, size_t n, const char *string)
+{
+	int index;
+	const char *item;
+
+	for (index = 0; index < n; index++) {
+		item = array[index];
+		if (!item)
+			break;
+		if (!strcmp(item, string))
+			return index;
+	}
+
+	return -ENODATA;
+}
+EXPORT_SYMBOL(match_string);
+
+/**
  * strtobool - convert common user inputs into boolean values
  * @s: input string
  * @res: result
-- 
2.7.0.rc3

[toc] | [prev] | [next] | [standalone]


#1323811 — Re: [PATCH v4 1/9] lib/string: introduce match_string() helper

FromAndrew Morton <akpm@linux-foundation.org>
Date2016-02-02 07:10 +0100
SubjectRe: [PATCH v4 1/9] lib/string: introduce match_string() helper
Message-ID<qXCk1-6AZ-9@gated-at.bofh.it>
In reply to#1320665
On Thu, 28 Jan 2016 15:14:17 +0200 Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:

> >From time to time we have to match a string in an array. Make a simple helper
> for that purpose.
> 
> ...
>
> --- a/include/linux/string.h
> +++ b/include/linux/string.h
> @@ -131,6 +131,8 @@ 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 n, const char *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 0323c0d..ba01d4f 100644
> --- a/lib/string.c
> +++ b/lib/string.c
> @@ -631,6 +631,32 @@ bool sysfs_streq(const char *s1, const char *s2)
>  EXPORT_SYMBOL(sysfs_streq);
>  
>  /**
> + * match_string - matches given string in an array

I can't say I'm in love with the name, but I guess it will suffice. 
search_string() doesn't seem much better.


> + * @array:	array of strings
> + * @n:		number of strings in the array or -1 for NULL terminated arrays
> + * @string:	string to match with
> + *
> + * Return:
> + * index of a @string in the @array if matches, or %-ENODATA otherwise.
> + */
> +int match_string(const char * const *array, size_t n, const char *string)
> +{
> +	int index;
> +	const char *item;
> +
> +	for (index = 0; index < n; index++) {

So we're taking an int and comparing that with (size_t)-1, relying upon
the compiler promoting the int to an unsigned type because size_t is
unsigned.  It works, but it isn't pretty - there wasn't really much
point in making size have type size_t.


> +		item = array[index];
> +		if (!item)
> +			break;
> +		if (!strcmp(item, string))
> +			return index;
> +	}
> +
> +	return -ENODATA;
> +}
> +EXPORT_SYMBOL(match_string);

[toc] | [prev] | [next] | [standalone]


#1323860 — Re: [PATCH v4 1/9] lib/string: introduce match_string() helper

FromAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Date2016-02-02 09:00 +0100
SubjectRe: [PATCH v4 1/9] lib/string: introduce match_string() helper
Message-ID<qXE2u-7EE-7@gated-at.bofh.it>
In reply to#1323811
On Mon, 2016-02-01 at 22:05 -0800, Andrew Morton wrote:
> On Thu, 28 Jan 2016 15:14:17 +0200 Andy Shevchenko <andriy.shevchenko
> @linux.intel.com> wrote:

> > + * @array:	array of strings
> > + * @n:		number of strings in the array or -1 for
> > NULL terminated arrays
> > + * @string:	string to match with
> > + *
> > + * Return:
> > + * index of a @string in the @array if matches, or %-ENODATA
> > otherwise.
> > + */
> > +int match_string(const char * const *array, size_t n, const char
> > *string)
> > +{
> > +	int index;
> > +	const char *item;
> > +
> > +	for (index = 0; index < n; index++) {
> 
> So we're taking an int and comparing that with (size_t)-1, relying
> upon
> the compiler promoting the int to an unsigned type because size_t is
> unsigned.  It works, but it isn't pretty - there wasn't really much
> point in making size have type size_t.

It was Rasmus' idea [1], before that I used int and tristate branch. I
agreed that one of that branches wasn't a good idea, but the rest was
exactly about differentiating size to compare and infinite (till
terminator) loop.

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

> 
> 
> > +		item = array[index];
> > +		if (!item)
> > +			break;
> > +		if (!strcmp(item, string))
> > +			return index;
> > +	}
> > +
> > +	return -ENODATA;
> > +}
> > +EXPORT_SYMBOL(match_string);
> 

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

[toc] | [prev] | [next] | [standalone]


#1324573 — Re: [PATCH v4 1/9] lib/string: introduce match_string() helper

FromRasmus Villemoes <linux@rasmusvillemoes.dk>
Date2016-02-02 21:00 +0100
SubjectRe: [PATCH v4 1/9] lib/string: introduce match_string() helper
Message-ID<qXPhf-7Yl-1@gated-at.bofh.it>
In reply to#1323811
On Tue, Feb 02 2016, Andrew Morton <akpm@linux-foundation.org> wrote:

> On Thu, 28 Jan 2016 15:14:17 +0200 Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:
>
>> + * @array:	array of strings
>> + * @n:		number of strings in the array or -1 for NULL terminated arrays
>> + * @string:	string to match with
>> + *
>> + * Return:
>> + * index of a @string in the @array if matches, or %-ENODATA otherwise.
>> + */
>> +int match_string(const char * const *array, size_t n, const char *string)
>> +{
>> +	int index;
>> +	const char *item;
>> +
>> +	for (index = 0; index < n; index++) {
>
> So we're taking an int and comparing that with (size_t)-1, relying upon
> the compiler promoting the int to an unsigned type because size_t is
> unsigned.  It works, but it isn't pretty - there wasn't really much
> point in making size have type size_t.

n has unsigned type to make it easy to pass 'infinity'; *that's* where
we rely on integer promotion.

One could make index be unsigned int (or size_t), but it won't matter,
because it will never have a value where the type promotion (nor the
implicit cast back to int if/when it's used as a return value) changes
its value.

Rasmus

[toc] | [prev] | [next] | [standalone]


#1320666 — [PATCH v4 7/9] ata: hpt366: convert to use match_string() helper

FromAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Date2016-01-28 14:20 +0100
Subject[PATCH v4 7/9] ata: hpt366: convert to use match_string() helper
Message-ID<qVUEr-3UD-53@gated-at.bofh.it>
In reply to#1320657
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>
Acked-by: Tejun Heo <tj@kernel.org>
---
 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..e5fb752 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_string(list, -1, model_num);
+	if (i >= 0) {
+		pr_warn("%s is not supported for %s\n", modestr, list[i]);
+		return 1;
 	}
 	return 0;
 }
-- 
2.7.0.rc3

[toc] | [prev] | [standalone]


Back to top | Article view | linux.kernel


csiph-web