Skip to contentSkip to conversation

lib/string: introduce match_string() helper

PATCH Superseded Andy Shevchenko 3 participants 14 messages Jan 8, 2016 – Jan 28, 2016

Subsystems (9)

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Andy Shevchenko

author reviewer
#0 lore raw

v3 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.

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.6.4
author reviewer
#1 lore raw

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

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.6.4
author reviewer
#2 lore raw

PATCH v3 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.6.4
author reviewer
#6 lore raw

PATCH v3 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.6.4
author reviewer
#7 lore raw

PATCH v3 5/9 power: charger_manager: 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/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.6.4
author reviewer
#8 lore raw

PATCH v3 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.6.4
author reviewer
#9 lore raw

PATCH v3 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.6.4
author reviewer
#11 lore raw

PATCH v3 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 e6ec125..677b3f0 100644
@@ -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,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.6.4
author reviewer
#5 lore raw

PATCH v3 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.6.4
author reviewer
#10 lore raw

PATCH v3 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.6.4
#12 lore raw
In reply to Andy Shevchenko
Quoted 2 lines · Andy Shevchenko

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

What's the reason for making it return -ENODATA when no match is found? That one of the callers wants to return that as error in such case? At least one other is returning -EINVAL in the same situation...

author reviewer
#13 lore raw
Quoted 7 lines · Al Viro
Quoted 3 lines · Andy Shevchenko

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


What's the reason for making it return -ENODATA when no match is found?

What else can be suitable?

Quoted 2 lines

That one of the callers wants to return that as error in such case? At least one other is returning -EINVAL in the same situation...

Linus Acked this, maybe he missed that one.

Linus, do we still need to return -EINVAL in pinmux?

In general our error reporting sucks, you know. So, any return value will be not ideal and self-explanatory (see json approach in perf). But I prefer return some return code instead of opaque -1, for example. This at least helps some users.

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
Acked-by on v3 2/9
#3 lore raw

Re: [PATCH v3 2/9] device property: convert to use match_string() helper

In reply to Andy Shevchenko
Quoted 5 lines · Andy Shevchenko

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

in case you need that.

I think though that it might be more efficient to simply push [1/9] first maybe with one user and then send the other patches to the relevant maintainers when that has been applied.

Quoted 33 lines
---
 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 · 2 lines
I speak only for myself.
Rafael J. Wysocki, Intel Open Source Technology Center.
author reviewer
#4 lore raw

Re: [PATCH v3 2/9] device property: convert to use match_string() helper

Quoted 11 lines · Rafael J. Wysocki
Quoted 6 lines · Andy Shevchenko

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

in case you need that.

Thanks!

Quoted 6 lines

I think though that it might be more efficient to simply push [1/9] first
maybe with one user and then send the other patches to the relevant maintainers
when that has been applied.

I tried to do such with the first version, there was a comment besides others that there is not enough '-' lines in the statistics ;-)

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy