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


Groups > linux.kernel > #1303510 > unrolled thread

[PATCH v1 1/8] lib/string: introduce match_string() helper

Started byAndy Shevchenko <andriy.shevchenko@linux.intel.com>
First post2016-01-07 13:10 +0100
Last post2016-01-12 09:30 +0100
Articles 20 on this page of 23 — 9 participants

Back to article view | Back to linux.kernel


Contents

  [PATCH v1 1/8] lib/string: introduce match_string() helper Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-07 13:10 +0100
    [PATCH v1 8/8] ide: hpt366: convert to use match_string() helper Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-07 13:10 +0100
    [PATCH v1 2/8] device property: convert to use match_string() helper Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-07 13:10 +0100
      Re: [PATCH v1 2/8] device property: convert to use match_string()  helper Mika Westerberg <mika.westerberg@linux.intel.com> - 2016-01-08 14:10 +0100
    [PATCH v1 6/8] power: ab8500: convert to use match_string() helper Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-07 13:10 +0100
      Re: [PATCH v1 6/8] power: ab8500: convert to use match_string() helper Linus Walleij <linus.walleij@linaro.org> - 2016-01-07 16:20 +0100
    [PATCH v1 7/8] ata: hpt366: convert to use match_string() helper Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-07 13:10 +0100
      Re: [PATCH v1 7/8] ata: hpt366: convert to use match_string() helper Tejun Heo <tj@kernel.org> - 2016-01-07 16:50 +0100
    [PATCH v1 5/8] power: charger_manager: convert to use match_string() helper Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-07 13:10 +0100
    [PATCH v1 4/8] drm/edid: convert to use match_string() helper Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-07 13:10 +0100
    Re: [PATCH v1 1/8] lib/string: introduce match_string() helper Heikki Krogerus <heikki.krogerus@linux.intel.com> - 2016-01-07 14:10 +0100
      Re: [PATCH v1 1/8] lib/string: introduce match_string() helper Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-07 14:20 +0100
        Re: [PATCH v1 1/8] lib/string: introduce match_string() helper Heikki Krogerus <heikki.krogerus@linux.intel.com> - 2016-01-07 14:30 +0100
    Re: [PATCH v1 1/8] lib/string: introduce match_string() helper Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2016-01-07 23:10 +0100
      Re: [PATCH v1 1/8] lib/string: introduce match_string() helper Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-08 09:50 +0100
    Re: [PATCH v1 1/8] lib/string: introduce match_string() helper Sergey Senozhatsky <sergey.senozhatsky.work@gmail.com> - 2016-01-08 01:20 +0100
      Re: [PATCH v1 1/8] lib/string: introduce match_string() helper Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-08 09:50 +0100
        Re: [PATCH v1 1/8] lib/string: introduce match_string() helper Sergey Senozhatsky <sergey.senozhatsky@gmail.com> - 2016-01-09 02:20 +0100
          Re: [PATCH v1 1/8] lib/string: introduce match_string() helper Andy Shevchenko <andy.shevchenko@gmail.com> - 2016-01-09 13:00 +0100
            Re: [PATCH v1 1/8] lib/string: introduce match_string() helper Andy Shevchenko <andriy.shevchenko@linux.intel.com> - 2016-01-11 16:00 +0100
              Re: [PATCH v1 1/8] lib/string: introduce match_string() helper Rasmus Villemoes <linux@rasmusvillemoes.dk> - 2016-01-11 23:20 +0100
                Re: [PATCH v1 1/8] lib/string: introduce match_string() helper Andy Shevchenko <andy.shevchenko@gmail.com> - 2016-01-11 23:30 +0100
                Re: [PATCH v1 1/8] lib/string: introduce match_string() helper Sergey Senozhatsky <sergey.senozhatsky.work@gmail.com> - 2016-01-12 09:30 +0100

Page 1 of 2  [1] 2  Next page →


#1303510 — [PATCH v1 1/8] lib/string: introduce match_string() helper

FromAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Date2016-01-07 13:10 +0100
Subject[PATCH v1 1/8] lib/string: introduce match_string() helper
Message-ID<qOhy9-7Vb-3@gated-at.bofh.it>
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..37062fb 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 len, 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..dd02270 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
+ * @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 %-ENODATA 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 -ENODATA;
+}
+EXPORT_SYMBOL(match_string);
+
+/**
  * strtobool - convert common user inputs into boolean values
  * @s: input string
  * @res: result
-- 
2.6.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

[toc] | [next] | [standalone]


#1303511 — [PATCH v1 8/8] ide: hpt366: convert to use match_string() helper

FromAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Date2016-01-07 13:10 +0100
Subject[PATCH v1 8/8] ide: hpt366: convert to use match_string() helper
Message-ID<qOhy9-7Vb-13@gated-at.bofh.it>
In reply to#1303510
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..f417332 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, 0, (char *)&drive->id[ATA_ID_PROD]) >= 0;
 }
 
 static struct hpt_info *hpt3xx_get_info(struct device *dev)
-- 
2.6.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

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


#1303513 — [PATCH v1 2/8] device property: convert to use match_string() helper

FromAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Date2016-01-07 13:10 +0100
Subject[PATCH v1 2/8] device property: convert to use match_string() helper
Message-ID<qOhy9-7Vb-19@gated-at.bofh.it>
In reply to#1303510
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/base/property.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)

diff --git a/drivers/base/property.c b/drivers/base/property.c
index c359351..f902b55 100644
--- a/drivers/base/property.c
+++ b/drivers/base/property.c
@@ -647,7 +647,7 @@ int fwnode_property_match_string(struct fwnode_handle *fwnode,
 	const char *propname, const char *string)
 {
 	const char **values;
-	int nval, ret, i;
+	int nval, ret;
 
 	nval = fwnode_property_read_string_array(fwnode, propname, NULL, 0);
 	if (nval < 0)
@@ -664,13 +664,7 @@ int fwnode_property_match_string(struct fwnode_handle *fwnode,
 	if (ret < 0)
 		goto out;
 
-	ret = -ENODATA;
-	for (i = 0; i < nval; i++) {
-		if (!strcmp(values[i], string)) {
-			ret = i;
-			break;
-		}
-	}
+	ret = match_string(values, nval, string);
 out:
 	kfree(values);
 	return ret;
-- 
2.6.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

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


#1304519 — Re: [PATCH v1 2/8] device property: convert to use match_string() helper

FromMika Westerberg <mika.westerberg@linux.intel.com>
Date2016-01-08 14:10 +0100
SubjectRe: [PATCH v1 2/8] device property: convert to use match_string() helper
Message-ID<qOEXM-7an-27@gated-at.bofh.it>
In reply to#1303513
On Thu, Jan 07, 2016 at 02:06:02PM +0200, Andy Shevchenko wrote:
> 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>

Reviewed-by: Mika Westerberg <mika.westerberg@linux.intel.com>

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


#1303515 — [PATCH v1 6/8] power: ab8500: convert to use match_string() helper

FromAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Date2016-01-07 13:10 +0100
Subject[PATCH v1 6/8] power: ab8500: convert to use match_string() helper
Message-ID<qOhya-7Vb-27@gated-at.bofh.it>
In reply to#1303510
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/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.6.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

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


#1303654 — Re: [PATCH v1 6/8] power: ab8500: convert to use match_string() helper

FromLinus Walleij <linus.walleij@linaro.org>
Date2016-01-07 16:20 +0100
SubjectRe: [PATCH v1 6/8] power: ab8500: convert to use match_string() helper
Message-ID<qOkw2-1qX-21@gated-at.bofh.it>
In reply to#1303515
On Thu, Jan 7, 2016 at 1:06 PM, 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.
>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

Acked-by: Linus Walleij <linus.walleij@linaro.org>

Yours,
Linus Walleij
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

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


#1303516 — [PATCH v1 7/8] ata: hpt366: convert to use match_string() helper

FromAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Date2016-01-07 13:10 +0100
Subject[PATCH v1 7/8] ata: hpt366: convert to use match_string() helper
Message-ID<qOhya-7Vb-37@gated-at.bofh.it>
In reply to#1303510
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..fb85cb2 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, 0, model_num);
+	if (i >= 0) {
+		pr_warn("%s is not supported for %s\n", modestr, list[i]);
+		return 1;
 	}
 	return 0;
 }
-- 
2.6.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

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


#1303677 — Re: [PATCH v1 7/8] ata: hpt366: convert to use match_string() helper

FromTejun Heo <tj@kernel.org>
Date2016-01-07 16:50 +0100
SubjectRe: [PATCH v1 7/8] ata: hpt366: convert to use match_string() helper
Message-ID<qOkZ3-1DY-17@gated-at.bofh.it>
In reply to#1303516
On Thu, Jan 07, 2016 at 02:06:07PM +0200, Andy Shevchenko wrote:
> 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>

Please feel free to route as you see fit.

Thanks.

-- 
tejun
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

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


#1303519 — [PATCH v1 5/8] power: charger_manager: convert to use match_string() helper

FromAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Date2016-01-07 13:10 +0100
Subject[PATCH v1 5/8] power: charger_manager: convert to use match_string() helper
Message-ID<qOhya-7Vb-33@gated-at.bofh.it>
In reply to#1303510
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..5939251 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, 0,
+				 psy->desc->name) >= 0) {
+			found_power_supply = true;
 			break;
+		}
 	}
 	mutex_unlock(&cm_list_mtx);
 
-- 
2.6.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

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


#1303520 — [PATCH v1 4/8] drm/edid: convert to use match_string() helper

FromAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Date2016-01-07 13:10 +0100
Subject[PATCH v1 4/8] drm/edid: convert to use match_string() helper
Message-ID<qOhya-7Vb-39@gated-at.bofh.it>
In reply to#1303510
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.6.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

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


#1303579

FromHeikki Krogerus <heikki.krogerus@linux.intel.com>
Date2016-01-07 14:10 +0100
Message-ID<qOiue-6b-9@gated-at.bofh.it>
In reply to#1303510
On Thu, Jan 07, 2016 at 02:06:01PM +0200, Andy Shevchenko wrote:
> >From time to time we have to match a string in an array. Make a simple helper
> for that purpose.

Cool! If you make v2 out of these, could you patch the following while
at it:

diff --git a/drivers/usb/common/common.c b/drivers/usb/common/common.c
index e6ec125..a391c81 100644
--- a/drivers/usb/common/common.c
+++ b/drivers/usb/common/common.c
@@ -64,18 +64,15 @@ EXPORT_SYMBOL_GPL(usb_speed_string);
 enum usb_device_speed usb_get_maximum_speed(struct device *dev)
 {
        const char *maximum_speed;
-       int err;
-       int i;
+       int ret;
 
-       err = device_property_read_string(dev, "maximum-speed", &maximum_speed);
-       if (err < 0)
+       ret = device_property_read_string(dev, "maximum-speed", &maximum_speed);
+       if (ret < 0)
                return USB_SPEED_UNKNOWN;
 
-       for (i = 0; i < ARRAY_SIZE(speed_names); i++)
-               if (strcmp(maximum_speed, speed_names[i]) == 0)
-                       return i;
+       ret = match_string(speed_names, ARRAY_SIZE(speed_names), maximum_speed);
 
-       return USB_SPEED_UNKNOWN;
+       return (ret < 0) ? USB_SPEED_UNKNOWN : ret;
 }
 EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
 
@@ -109,13 +106,11 @@ static const char *const usb_dr_modes[] = {
 
 static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
 {
-       int i;
+       int ret;
 
-       for (i = 0; i < ARRAY_SIZE(usb_dr_modes); i++)
-               if (!strcmp(usb_dr_modes[i], str))
-                       return i;
+       ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str);
 
-       return USB_DR_MODE_UNKNOWN;
+       return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret;
 }
 
 enum usb_dr_mode usb_get_dr_mode(struct device *dev)


Thanks,

-- 
heikki
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

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


#1303582

FromAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Date2016-01-07 14:20 +0100
Message-ID<qOiDU-9B-9@gated-at.bofh.it>
In reply to#1303579
On Thu, 2016-01-07 at 15:07 +0200, Heikki Krogerus wrote:
> On Thu, Jan 07, 2016 at 02:06:01PM +0200, Andy Shevchenko wrote:
> > > From time to time we have to match a string in an array. Make a
> > > simple helper
> > for that purpose.
> 
> Cool! If you make v2 out of these, could you patch the following
> while
> at it:

Yes, please, format as a usual patch and share with me. I will include
in v3 (actually I missed the numbering here, it should be v2 already).

> 
> diff --git a/drivers/usb/common/common.c
> b/drivers/usb/common/common.c
> index e6ec125..a391c81 100644
> --- a/drivers/usb/common/common.c
> +++ b/drivers/usb/common/common.c
> @@ -64,18 +64,15 @@ EXPORT_SYMBOL_GPL(usb_speed_string);
>  enum usb_device_speed usb_get_maximum_speed(struct device *dev)
>  {
>         const char *maximum_speed;
> -       int err;
> -       int i;
> +       int ret;
>  
> -       err = device_property_read_string(dev, "maximum-speed",
> &maximum_speed);
> -       if (err < 0)
> +       ret = device_property_read_string(dev, "maximum-speed",
> &maximum_speed);
> +       if (ret < 0)
>                 return USB_SPEED_UNKNOWN;
>  
> -       for (i = 0; i < ARRAY_SIZE(speed_names); i++)
> -               if (strcmp(maximum_speed, speed_names[i]) == 0)
> -                       return i;
> +       ret = match_string(speed_names, ARRAY_SIZE(speed_names),
> maximum_speed);
>  
> -       return USB_SPEED_UNKNOWN;
> +       return (ret < 0) ? USB_SPEED_UNKNOWN : ret;
>  }
>  EXPORT_SYMBOL_GPL(usb_get_maximum_speed);
>  
> @@ -109,13 +106,11 @@ static const char *const usb_dr_modes[] = {
>  
>  static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
>  {
> -       int i;
> +       int ret;
>  
> -       for (i = 0; i < ARRAY_SIZE(usb_dr_modes); i++)
> -               if (!strcmp(usb_dr_modes[i], str))
> -                       return i;
> +       ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes),
> str);
>  
> -       return USB_DR_MODE_UNKNOWN;
> +       return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret;
>  }
>  
>  enum usb_dr_mode usb_get_dr_mode(struct device *dev)
> 
> 
> Thanks,
> 

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

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

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


#1303589

FromHeikki Krogerus <heikki.krogerus@linux.intel.com>
Date2016-01-07 14:30 +0100
Message-ID<qOiNA-eq-5@gated-at.bofh.it>
In reply to#1303582

[Multipart message — attachments visible in raw view] — view raw

On Thu, Jan 07, 2016 at 03:12:00PM +0200, Andy Shevchenko wrote:
> On Thu, 2016-01-07 at 15:07 +0200, Heikki Krogerus wrote:
> > On Thu, Jan 07, 2016 at 02:06:01PM +0200, Andy Shevchenko wrote:
> > > > From time to time we have to match a string in an array. Make a
> > > > simple helper
> > > for that purpose.
> > 
> > Cool! If you make v2 out of these, could you patch the following
> > while
> > at it:
> 
> Yes, please, format as a usual patch and share with me. I will include
> in v3 (actually I missed the numbering here, it should be v2 already).

Sure thing. Here's the patch.

-- 
heikki

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


#1303942

FromRasmus Villemoes <linux@rasmusvillemoes.dk>
Date2016-01-07 23:10 +0100
Message-ID<qOqUO-5Sw-23@gated-at.bofh.it>
In reply to#1303510
On Thu, Jan 07 2016, 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.
>
> 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..37062fb 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 len, 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..dd02270 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
> + * @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 %-ENODATA 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 -ENODATA;
> +}
> +EXPORT_SYMBOL(match_string);
> +

I'd suggest making it -1 (which, since len is a size_t, is effectively
infinity) having the meaning "the array is terminated by a NULL
entry". match_string(..., ARRAY_SIZE(my_array), ...) will break if the
array happens to be empty, which could e.g. happen in a case like

const char *my_array[] = {
#ifdef CONFIG_THIS
       "this",
#endif
#ifdef CONFIG_THAT
       "that",
#endif
};

I also think the condition/loop above is unreadable.

for (index = 0; index < len; index++) {
    ...
}

is much clearer.

Why -ENODATA and not just -1? It is rather unlikely that anyone would
pass on that particular -Exxx value. Not a biggie, just curious.

Would there be more potential users if we had a flag argument allowing
case-insensitive matching? Would there be more potential users if a flag
allowed to ask whether the given string is a _prefix_ of one of the
strings in the array, or vice versa? Something like

#define MATCH_STRING_CASE 0x01
#define MATCH_STRING_PREFIX_OF_ARRAY_ELEM 0x02 /* yeah, that name sucks */
#define MATCH_ARRAY_ELEM_PREFIX_OF_STRING 0x04 /* this too */

int match_string(const char * const *array, size_t len, const char *string, unsigned flags)
{
#define MATCH_PREFIX (MATCH_... | MATCH_...)
    int index;
    const char *item;
    int (*match_func)(const char *, const char *) =
        flags & MATCH_STRING_CASE ? strcasecmp : strcmp;
    int (*prefix_func)(const char *, const char *, size_t) =
        flags & MATCH_STRING_CASE ? strncasecmp : strncmp;
  
    for (index = 0; index < len; ++index) {
        item = array[index];
        if (!item)
            break;
        if (flags & MATCH_PREFIX) {
            size_t len = strlen(flags & MATCH_STRING_PREFIX_OF_ARRAY_ELEM ?
                string : item);
            if (!prefix_func(item, string, len))
                return index;
        } else if (!match_func(item, string)) {
            return index;
        }
    }
    return -1;
}

(Ok, it's not that pretty; maybe it'd be better to use
switch(flags&MATCH_PREFIX) {}. Or maybe just the case-insensitive part
is worth keeping; in that case the above isn't that bad.)

Rasmus

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


#1304267

FromAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Date2016-01-08 09:50 +0100
Message-ID<qOAU9-4eB-1@gated-at.bofh.it>
In reply to#1303942
On Thu, 2016-01-07 at 23:05 +0100, Rasmus Villemoes wrote:
> On Thu, Jan 07 2016, Andy Shevchenko <andriy.shevchenko@linux.intel.c
> om> wrote:
> 
> > From time to time we have to match a string in an array. Make a
> > simple helper
> > for that purpose.

> >  /**
> > + * 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 %-ENODATA
> > 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 -ENODATA;
> > +}
> > +EXPORT_SYMBOL(match_string);
> > +
> 
> I'd suggest making it -1 (which, since len is a size_t, is
> effectively
> infinity) having the meaning "the array is terminated by a NULL
> entry". match_string(..., ARRAY_SIZE(my_array), ...) will break if
> the
> array happens to be empty, which could e.g. happen in a case like
> 
> const char *my_array[] = {
> #ifdef CONFIG_THIS
>        "this",
> #endif
> #ifdef CONFIG_THAT
>        "that",
> #endif
> };

It might make sense, though I don't remember current users with such
conditions.


> I also think the condition/loop above is unreadable.

Hmm… For me looks straightforward.

> 
> for (index = 0; index < len; index++) {
>     ...
> }
> 
> is much clearer.

If we switch to -1, it will look indeed simpler.

> 
> Why -ENODATA and not just -1? It is rather unlikely that anyone would
> pass on that particular -Exxx value. Not a biggie, just curious.

There are few of users already that would like to return error code to
upper level. In some cases better to have

return match_string();

than

ret = match_string();
if (ret < 0)
 return -EFOO;

return 0;

And returning -ENODATA doesn't prevent to have latter, but allows
former.

> 
> Would there be more potential users if we had a flag argument
> allowing
> case-insensitive matching? Would there be more potential users if a
> flag
> allowed to ask whether the given string is a _prefix_ of one of the
> strings in the array, or vice versa? Something like
> 
> #define MATCH_STRING_CASE 0x01
> #define MATCH_STRING_PREFIX_OF_ARRAY_ELEM 0x02 /* yeah, that name
> sucks */
> #define MATCH_ARRAY_ELEM_PREFIX_OF_STRING 0x04 /* this too */
> 
> int match_string(const char * const *array, size_t len, const char
> *string, unsigned flags)
> {
> #define MATCH_PREFIX (MATCH_... | MATCH_...)
>     int index;
>     const char *item;
>     int (*match_func)(const char *, const char *) =
>         flags & MATCH_STRING_CASE ? strcasecmp : strcmp;
>     int (*prefix_func)(const char *, const char *, size_t) =
>         flags & MATCH_STRING_CASE ? strncasecmp : strncmp;
>   
>     for (index = 0; index < len; ++index) {
>         item = array[index];
>         if (!item)
>             break;
>         if (flags & MATCH_PREFIX) {
>             size_t len = strlen(flags &
> MATCH_STRING_PREFIX_OF_ARRAY_ELEM ?
>                 string : item);
>             if (!prefix_func(item, string, len))
>                 return index;
>         } else if (!match_func(item, string)) {
>             return index;
>         }
>     }
>     return -1;
> }
> 
> (Ok, it's not that pretty; maybe it'd be better to use
> switch(flags&MATCH_PREFIX) {}. Or maybe just the case-insensitive
> part
> is worth keeping; in that case the above isn't that bad.)

I won't overcomplicate it until we have enough users to consider. Any
examples where we need this?

And I prefer way to have different prototypes for them instead of net
of conditions.

Thanks for review. I will send v3 (yeah, this is actually v2) with
change you proposed in the first part. For the second one I would like
to have real examples before doing anything.

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

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


#1304053

FromSergey Senozhatsky <sergey.senozhatsky.work@gmail.com>
Date2016-01-08 01:20 +0100
Message-ID<qOsWC-7b8-5@gated-at.bofh.it>
In reply to#1303510
On (01/07/16 14:06), Andy Shevchenko wrote:
> 
> From time to time we have to match a string in an array. Make a simple helper
> for that purpose.
> 

Hello,

strncmp() case seems to be quite common.

> +int match_string(const char * const *array, size_t len, const char *string)
                                                  ^^^^^^^
a nitpick, [to me] `len' looks a bit confusing, usually it's array 'size'.

> +{
> +	int index = 0;
> +	const char *item;
> +
> +	do {
> +		item = array[index];
> +		if (!item)
> +			break;
> +		if (!strcmp(item, string))
> +			return index;
> +	} while (++index < len || !len);
> +
> +	return -ENODATA;
> +}


do you want to do something like this:

/*
 * hm, how to name this thing... nmatch_string() or match_nstring()...
 * nmatch_string() _probably_ better, match_nstring() is totally cryptic.
 */
int nmatch_string(array, array_size, string, string_len)
{
	do {
		strncmp();
	} while ();
}

int match_string(array, array_size, string)
{
	return nmatch_string(array, array_size, string, strlen(string));
}

	-ss

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


#1304277

FromAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Date2016-01-08 09:50 +0100
Message-ID<qOAUc-4eB-43@gated-at.bofh.it>
In reply to#1304053
On Fri, 2016-01-08 at 09:13 +0900, Sergey Senozhatsky wrote:
> On (01/07/16 14:06), Andy Shevchenko wrote:
> > 
> > From time to time we have to match a string in an array. Make a
> > simple helper
> > for that purpose.
> > 
> 
> Hello,
> 
> strncmp() case seems to be quite common.

Like I answered to Rasmus, please, provide real examples.

> 
> > +int match_string(const char * const *array, size_t len, const char
> > *string)
>                                                   ^^^^^^^
> a nitpick, [to me] `len' looks a bit confusing, usually it's array
> 'size'.

Agreed. I would change it to plain 'n'.

> 
> > +{
> > +	int index = 0;
> > +	const char *item;
> > +
> > +	do {
> > +		item = array[index];
> > +		if (!item)
> > +			break;
> > +		if (!strcmp(item, string))
> > +			return index;
> > +	} while (++index < len || !len);
> > +
> > +	return -ENODATA;
> > +}
> 
> 
> do you want to do something like this:
> 
> /*
>  * hm, how to name this thing... nmatch_string() or
> match_nstring()...
>  * nmatch_string() _probably_ better, match_nstring() is totally
> cryptic.
>  */
> int nmatch_string(array, array_size, string, string_len)
> {
> 	do {
> 		strncmp();
> 	} while ();
> }
> 
> int match_string(array, array_size, string)
> {
> 	return nmatch_string(array, array_size, string,
> strlen(string));
> }

See above.

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

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


#1305118

FromSergey Senozhatsky <sergey.senozhatsky@gmail.com>
Date2016-01-09 02:20 +0100
Message-ID<qOQmd-6yX-1@gated-at.bofh.it>
In reply to#1304277
Andy Shevchenko wrote:
[..]
> >
> > strncmp() case seems to be quite common.
> 
> Like I answered to Rasmus, please, provide real examples.

[..]
> > int nmatch_string(array, array_size, string, string_len)
> > {
> >       do {
> >               strncmp();
> >       } while ();
> > }
> >
> > int match_string(array, array_size, string)
> > {
> >       return nmatch_string(array, array_size, string,
> > strlen(string));
> > }
> 
> See above.

after some quick and inaccurate grepping, well, probably you're right - not worth it.

arch/mips/bcm63xx/boards/board_bcm963xx.c  void __init board_prom_init(void)
net/irda/irnet/irnet_irda.c   irnet_dname_to_daddr()
arch/powerpc/sysdev/ppc4xx_cpm.c static ssize_t cpm_idle_store()
arch/x86/ras/mce_amd_inj.c static int __set_inj
drivers/hwtracing/intel_th/msu.c mode_store
drivers/pci/pcie/aer/ecrc.c void pcie_ecrc_get_policy
drivers/pci/pcie/aspm.c pcie_aspm_set_policy
drivers/scsi/aic7xxx/aic7xxx_osm.c aic7xxx_setup
drivers/scsi/aic7xxx/aic79xx_osm.c aic79xx_setup
drivers/scsi/scsi_transport_fc.c static int get_fc_##title##_match
drivers/staging/android/ion/hisilicon/hi6220_ion.c get_type_by_name
drivers/staging/lustre/lustre/lmv/lproc_lmv.c placement_name2policy
drivers/xen/sys-hypervisor.c pmu_mode_store

	-ss

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


#1305219

FromAndy Shevchenko <andy.shevchenko@gmail.com>
Date2016-01-09 13:00 +0100
Message-ID<qP0lz-54s-1@gated-at.bofh.it>
In reply to#1305118
On Sat, Jan 9, 2016 at 3:12 AM, Sergey Senozhatsky
<sergey.senozhatsky@gmail.com> wrote:
> Andy Shevchenko wrote:
> [..]
>> >
>> > strncmp() case seems to be quite common.
>>
>> Like I answered to Rasmus, please, provide real examples.
>
> [..]
>> > int nmatch_string(array, array_size, string, string_len)
>> > {
>> >       do {
>> >               strncmp();
>> >       } while ();
>> > }
>> >
>> > int match_string(array, array_size, string)
>> > {
>> >       return nmatch_string(array, array_size, string,
>> > strlen(string));
>> > }
>>
>> See above.
>
> after some quick and inaccurate grepping, well, probably you're right - not worth it.

Good grep anyway, it clearly shows that there is hard to generalize
which limit to use: a) length of a first argument / item from a list,
b) length of a second argument or a constant.

> arch/mips/bcm63xx/boards/board_bcm963xx.c  void __init board_prom_init(void)
> net/irda/irnet/irnet_irda.c   irnet_dname_to_daddr()
> arch/powerpc/sysdev/ppc4xx_cpm.c static ssize_t cpm_idle_store()
> arch/x86/ras/mce_amd_inj.c static int __set_inj
> drivers/hwtracing/intel_th/msu.c mode_store
> drivers/pci/pcie/aer/ecrc.c void pcie_ecrc_get_policy
> drivers/pci/pcie/aspm.c pcie_aspm_set_policy
> drivers/scsi/aic7xxx/aic7xxx_osm.c aic7xxx_setup
> drivers/scsi/aic7xxx/aic79xx_osm.c aic79xx_setup
> drivers/scsi/scsi_transport_fc.c static int get_fc_##title##_match
> drivers/staging/android/ion/hisilicon/hi6220_ion.c get_type_by_name
> drivers/staging/lustre/lustre/lmv/lproc_lmv.c placement_name2policy
> drivers/xen/sys-hypervisor.c pmu_mode_store

-- 
With Best Regards,
Andy Shevchenko

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


#1306361

FromAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Date2016-01-11 16:00 +0100
Message-ID<qPM6S-3KB-9@gated-at.bofh.it>
In reply to#1305219
On Sat, 2016-01-09 at 13:57 +0200, Andy Shevchenko wrote:
> On Sat, Jan 9, 2016 at 3:12 AM, Sergey Senozhatsky
> <sergey.senozhatsky@gmail.com> wrote:
> > Andy Shevchenko wrote:
> > [..]
> > > > 
> > > > strncmp() case seems to be quite common.
> > > 
> > > Like I answered to Rasmus, please, provide real examples.
> > 
> > [..]
> > > > int nmatch_string(array, array_size, string, string_len)
> > > > {
> > > >       do {
> > > >               strncmp();
> > > >       } while ();
> > > > }
> > > > 
> > > > int match_string(array, array_size, string)
> > > > {
> > > >       return nmatch_string(array, array_size, string,
> > > > strlen(string));
> > > > }
> > > 
> > > See above.
> > 
> > after some quick and inaccurate grepping, well, probably you're
> > right - not worth it.
> 
> Good grep anyway, it clearly shows that there is hard to generalize
> which limit to use: a) length of a first argument / item from a list,
> b) length of a second argument or a constant.
> 
> > arch/mips/bcm63xx/boards/board_bcm963xx.c  void __init
> > board_prom_init(void)
> > net/irda/irnet/irnet_irda.c   irnet_dname_to_daddr()
> > arch/powerpc/sysdev/ppc4xx_cpm.c static ssize_t cpm_idle_store()
> > arch/x86/ras/mce_amd_inj.c static int __set_inj
> > drivers/hwtracing/intel_th/msu.c mode_store
> > drivers/pci/pcie/aer/ecrc.c void pcie_ecrc_get_policy
> > drivers/pci/pcie/aspm.c pcie_aspm_set_policy
> > drivers/scsi/aic7xxx/aic7xxx_osm.c aic7xxx_setup
> > drivers/scsi/aic7xxx/aic79xx_osm.c aic79xx_setup
> > drivers/scsi/scsi_transport_fc.c static int get_fc_##title##_match
> > drivers/staging/android/ion/hisilicon/hi6220_ion.c get_type_by_name
> > drivers/staging/lustre/lustre/lmv/lproc_lmv.c placement_name2policy
> > drivers/xen/sys-hypervisor.c pmu_mode_store

Thought more about those cases.

If you would like you may introduce something like

int nmatch_string(array, array_size, string, int len)
{
  if (len < 0)
    return match_string();

  for (…) {
    size_t itemlen = (len > 0) ? len : strlen(array[index]);
…
    if (!strncmp(array[index], string, itemlen))
      return index;
  }
  return -EINVAL;
}

And convert existing users where it makes sense.

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

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


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | linux.kernel


csiph-web