Skip to contentSkip to conversation

lib/string: introduce match_string() helper

PATCH In review Andy Shevchenko 3 participants 15 messages Jan 28, 2016 – Feb 2, 2016

Subsystems (9)
lore
Share & tools
lore
author reviewer
#1 lore raw

PATCH v4 1/9 lib/string: introduce match_string() helper

In reply to Andy Shevchenko
Quoted 1 line

From time to time we have to match a string in an array. Make a simple helper

for that purpose.

---
 include/linux/string.h |  2 ++
 lib/string.c           | 26 ++++++++++++++++++++++++++
 2 files changed, 28 insertions(+)
include/linux/string.h +2 -0
index b0a732b..07505aa 100644
@@ -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);
lib/string.c +26 -0
index 0323c0d..ba01d4f 100644
@@ -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
Signature · 1 line
2.7.0.rc3
author reviewer
#6 lore raw

PATCH v4 3/9 pinctrl: convert to use match_string() helper

In reply to Andy Shevchenko

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

---
 drivers/pinctrl/pinmux.c | 13 +++----------
 1 file changed, 3 insertions(+), 10 deletions(-)
drivers/pinctrl/pinmux.c +3 -10
index 29984b3..c223a9e 100644
@@ -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];
Signature · 1 line
2.7.0.rc3
author reviewer
#12 lore raw

PATCH v4 7/9 ata: hpt366: convert to use match_string() helper

In reply to Andy Shevchenko

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

---
 drivers/ata/pata_hpt366.c | 13 +++++--------
 1 file changed, 5 insertions(+), 8 deletions(-)
drivers/ata/pata_hpt366.c +5 -8
index 0038dc4..e5fb752 100644
@@ -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;
 }
Signature · 1 line
2.7.0.rc3
author reviewer
#13 lore raw

PATCH v4 8/9 ide: hpt366: convert to use match_string() helper

In reply to Andy Shevchenko

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

---
 drivers/ide/hpt366.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
drivers/ide/hpt366.c +2 -7
index 696b6c1..f94baad 100644
@@ -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)
Signature · 1 line
2.7.0.rc3
author reviewer
#14 lore raw

PATCH v4 9/9 usb: common: convert to use match_string() helper

In reply to Andy Shevchenko

From: Heikki Krogerus <[email protected]>

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

---
 drivers/usb/common/common.c | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)
drivers/usb/common/common.c +8 -14
index 49fbfe8..e3d0161 100644
@@ -65,18 +65,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);
 
@@ -110,13 +107,10 @@ static const char *const usb_dr_modes[] = {
 
 static enum usb_dr_mode usb_get_dr_mode_from_string(const char *str)
 {
-	int i;
-
-	for (i = 0; i < ARRAY_SIZE(usb_dr_modes); i++)
-		if (!strcmp(usb_dr_modes[i], str))
-			return i;
+	int ret;
 
-	return USB_DR_MODE_UNKNOWN;
+	ret = match_string(usb_dr_modes, ARRAY_SIZE(usb_dr_modes), str);
+	return (ret < 0) ? USB_DR_MODE_UNKNOWN : ret;
 }
 
 enum usb_dr_mode usb_get_dr_mode(struct device *dev)
Signature · 1 line
2.7.0.rc3
author reviewer
#5 lore raw

PATCH v4 2/9 device property: convert to use match_string() helper

In reply to Andy Shevchenko

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

---
 drivers/base/property.c | 10 ++--------
 1 file changed, 2 insertions(+), 8 deletions(-)
drivers/base/property.c +2 -8
index c359351..f902b55 100644
@@ -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;
Signature · 1 line
2.7.0.rc3

Andy Shevchenko

author reviewer
#0 lore raw

v4 cover lib/string: introduce match_string() helper

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(-)
Signature · 1 line
2.7.0.rc3
author reviewer
#10 lore raw

PATCH v4 5/9 power: charger_manager: convert to use match_string() helper

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

---
 drivers/power/charger-manager.c | 27 ++++-----------------------
 1 file changed, 4 insertions(+), 23 deletions(-)
drivers/power/charger-manager.c +4 -23
index 1ea5d1a..e664ca7 100644
@@ -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);
 
Signature · 1 line
2.7.0.rc3
author reviewer
#11 lore raw

PATCH v4 6/9 power: ab8500: convert to use match_string() helper

In reply to Andy Shevchenko

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

---
 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(-)
drivers/power/ab8500_btemp.c +5 -10
index 8f8044e..bf2e5dd 100644
@@ -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 */
drivers/power/ab8500_charger.c +5 -11
index e388171..30de5d4 100644
@@ -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 */
drivers/power/ab8500_fg.c +5 -10
index 3830dad..5a36cf8 100644
@@ -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 */
drivers/power/abx500_chargalg.c +5 -9
index 541f702..d9104b1 100644
@@ -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;
 
 	/*
Signature · 1 line
2.7.0.rc3
author reviewer
#9 lore raw

PATCH v4 4/9 drm/edid: convert to use match_string() helper

In reply to Andy Shevchenko

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

---
 drivers/gpu/drm/drm_edid_load.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
drivers/gpu/drm/drm_edid_load.c +6 -11
index 698b8c3..9a401ae 100644
@@ -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);
 
Signature · 1 line
2.7.0.rc3
maintainer
#7 lore raw

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

In reply to Andy Shevchenko
Quoted 23 lines · On Thu, 28 Jan 2016 15:14:19 +0200 Andy Shevchenko…

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

drivers/pinctrl/pinmux.c +0 -6
@@ -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;

drivers/pinctrl/pinmux.c int pinmux_map_to_setting(struct pinctrl_map const *map, · line 371

Quoted 10 lines
drivers/pinctrl/pinmux.c +3 -4
-			}
-		}
-		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".

Quoted 5 lines
drivers/pinctrl/pinmux.c +0 -0
 		}
 	} else {
 		group = groups[0];
Signature · 1 line
2.7.0.rc3
maintainer
#2 lore raw
In reply to Andy Shevchenko
Quoted 21 lines · On Thu, 28 Jan 2016 15:14:17 +0200 Andy Shevchenko…
Quoted 1 line

From time to time we have to match a string in an array. Make a simple helper

for that purpose.

...

include/linux/string.h +2 -0
@@ -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);
lib/string.c +0 -0
index 0323c0d..ba01d4f 100644

lib/string.c bool sysfs_streq(const char *s1, const char *s2) · line 634

Quoted 4 lines
lib/string.c +1 -0
@@ -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.

lib/string.c bool sysfs_streq(const char *s1, const char *s2) · line 647

Quoted 13 lines
lib/string.c +13 -0
+ * @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.

Quoted 10 lines
lib/string.c +10 -0
+		item = array[index];
+		if (!item)
+			break;
+		if (!strcmp(item, string))
+			return index;
+	}
+
+	return -ENODATA;
+}
+EXPORT_SYMBOL(match_string);
author reviewer
#3 lore raw
Quoted 17 lines · Andrew Morton
Quoted 14 lines · On Thu, 28 Jan 2016 15:14:17 +0200 Andy Shevchenko <andriy.shevchenko…

+ * @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)

lib/string.c +3 -0
+{
+	int index;
+	const char *item;
Quoted 8 lines

lib/string.c bool sysfs_streq(const char *s1, const char *s2) · line 647

Quoted 2 lines
lib/string.c +2 -0
+
+	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

Quoted 13 lines
Quoted 12 lines
lib/string.c +10 -0
+		item = array[index];
+		if (!item)
+			break;
+		if (!strcmp(item, string))
+			return index;
+	}
+
+	return -ENODATA;
+}
+EXPORT_SYMBOL(match_string);
Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
author reviewer
#8 lore raw

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

In reply to Andrew Morton
Quoted 35 lines · Andrew Morton
Quoted 32 lines · On Thu, 28 Jan 2016 15:14:19 +0200 Andy Shevchenko <andriy.shevchenko…

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

drivers/pinctrl/pinmux.c +0 -1
@@ -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");

drivers/pinctrl/pinmux.c +2 -8
@@ -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) {
Quoted 8 lines

drivers/pinctrl/pinmux.c int pinmux_map_to_setting(struct pinctrl_map const *map, · line 371

Quoted 6 lines
drivers/pinctrl/pinmux.c +0 -0
  			dev_err(pctldev->dev,

  "invalid group \"%s\" for function \"%s\"\n",
  group, map->data.mux.function);

drivers/pinctrl/pinmux.c +1 -1
-			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.

Quoted 11 lines

  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

Quoted 3 lines
Quoted 3 lines

  }
  } else {
  group = groups[0];

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#4 lore raw
In reply to Andrew Morton
Quoted 12 lines · Andrew Morton
Quoted 10 lines · On Thu, 28 Jan 2016 15:14:17 +0200 Andy Shevchenko…

+ * @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.
+ */

lib/string.c +3 -0
+int match_string(const char * const *array, size_t n, const char *string)
+{
+	int index;
Quoted 8 lines

lib/string.c bool sysfs_streq(const char *s1, const char *s2) · line 647

Quoted 3 lines
lib/string.c +3 -0
+	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