lib/string: introduce match_string() helper
Subsystems (9)
Andy Shevchenko
reviewerPATCH 2/8 device property: convert to use match_string() helper
In reply to Andy ShevchenkoThe 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
@@ -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
Andy Shevchenko
reviewerPATCH 4/8 drm/edid: convert to use match_string() helper
In reply to Andy ShevchenkoThe 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
@@ -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
Andy Shevchenko
reviewerPATCH 5/8 power: charger_manager: convert to use match_string() helper
In reply to Andy ShevchenkoThe 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
@@ -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);
Signature · 1 line
2.6.4
Andy Shevchenko
reviewerPATCH 6/8 power: ab8500: convert to use match_string() helper
In reply to Andy ShevchenkoThe 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
@@ -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
@@ -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
@@ -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
@@ -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
Andy Shevchenko
reviewerPATCH 7/8 ata: hpt366: convert to use match_string() helper
In reply to Andy ShevchenkoThe 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
@@ -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 ; }
Signature · 1 line
2.6.4
Andy Shevchenko
reviewerPATCH 8/8 ide: hpt366: convert to use match_string() helper
In reply to Andy ShevchenkoThe 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
@@ -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)
Signature · 1 line
2.6.4
Andy Shevchenko
reviewerPATCH 3/8 pinctrl: convert to use match_string() helper
In reply to Andy ShevchenkoThe 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
@@ -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
Andy Shevchenko
reviewerPATCH 1/8 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
@@ -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);
lib/string.c +26 -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 +* @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
Signature · 1 line
2.6.4
Heikki Krogerus
reviewerQuoted 2 lines · 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.
Cool! If you make v2 out of these, could you patch the following while at it:
drivers/usb/common/common.c +8 -13
@@ -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,
Signature · 1 line
heikki
Andy Shevchenko
reviewerQuoted 8 lines · Heikki Krogerus
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines
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).
Quoted 55 lines
drivers/usb/common/common.c +1
-2@@ -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)
returnUSB_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);
- returnUSB_SPEED_UNKNOWN;
+ return (ret < 0) ?USB_SPEED_UNKNOWN: ret;
}
EXPORT_SYMBOL_GPL(usb_get_maximum_speed);drivers/usb/common/common.c +0
-0@@ -109,13 +106,11 @@ static const char *const usb_dr_modes[] = {
static enumusb_dr_modeusb_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);
- returnUSB_DR_MODE_UNKNOWN;
+ return (ret < 0) ?USB_DR_MODE_UNKNOWN: ret;
}
enumusb_dr_modeusb_get_dr_mode(struct device *dev)Thanks,
Signature · 2 lines
Andy Shevchenko <[email protected]> Intel Finland Oy
Heikki Krogerus
reviewerQuoted 4 lines · Andy Shevchenko
Quoted 3 lines · Heikki Krogerus
Quoted 2 lines · Andy Shevchenko
Quoted 2 lines
From time to time we have to match a string in an array. Make a simple helper
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
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.
Signature · 1 line
heikki
Quoted 1 line
From
86d8e9fda3f6a0679c9350d950f44ca2c9bec8ceMon Sep 17 00:00:00 2001
From: Heikki Krogerus <[email protected]>
Date: Thu, 7 Jan 2016 15:22:14 +0200
Subject: [PATCH] usb: common: convert to use match_string() helper
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
@@ -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
Re: [PATCH v1 6/8] power: ab8500: convert to use match_string() helper
In reply to Andy ShevchenkoQuoted 4 lines · Andy Shevchenko
The new helper returns index of the mathing string in an array. We would use it here.
Signed-off-by Andy Shevchenko <[email protected]>
Yours,
Linus Walleij
Re: [PATCH v1 3/8] pinctrl: convert to use match_string() helper
In reply to Andy ShevchenkoQuoted 4 lines · Andy Shevchenko
The new helper returns index of the mathing string in an array. We would use it here.
Signed-off-by Andy Shevchenko <[email protected]>
Pretty cool ...
Yours,
Linus Walleij
Re: [PATCH v1 7/8] ata: hpt366: convert to use match_string() helper
In reply to Andy ShevchenkoQuoted 4 lines · Andy Shevchenko
The new helper returns index of the mathing string in an array. We would use it here.
Signed-off-by Andy Shevchenko <[email protected]>
Please feel free to route as you see fit.
Thanks.
Signature · 1 line
tejun
Quoted 45 lines · Andy Shevchenko
From time to time we have to match a string in an array. Make a simple helper for that purpose.
Signed-off-by Andy Shevchenko <[email protected]>--- include/linux/string.h | 2 ++ lib/string.c | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+)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 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);lib/string.c +15
-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 +* @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];
lib/string.c bool sysfs_streq(const char *s1, const char *s2) · line 658
Quoted 10 lines
lib/string.c +10
-0+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
Quoted 4 lines · Andy Shevchenko
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.
Quoted 1 line
+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'.
Quoted 4 lines
lib/string.c +4
-0+{ +int index =0 ; +const char *item; +
lib/string.c bool sysfs_streq(const char *s1, const char *s2) · line 656
Quoted 10 lines
lib/string.c +10
-0+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
Andy Shevchenko
reviewerQuoted 44 lines · Rasmus Villemoes
Quoted 23 lines · Andy Shevchenko
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.
+ */
+intmatch_string(const char * const *array, size_t len, const char *string)lib/string.c +6
-0+{ +int index =0 ; +const char *item; + + do { + item = array[index];lib/string.c bool sysfs_streq(const char *s1, const char *s2) · line 658
Quoted 10 lines
lib/string.c +10
-0+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
Quoted 8 lines
const char *my_array[] = {
#ifdefCONFIG_THIS
"this",
#endif
#ifdefCONFIG_THAT
"that",
#endif
};
It might make sense, though I don't remember current users with such conditions.
Quoted 1 line
I also think the condition/loop above is unreadable.
Hmm… For me looks straightforward.
Quoted 6 lines
for (index = 0; index < len; index++) {
...
}is much clearer.
If we switch to -1, it will look indeed simpler.
Quoted 3 lines
Why
-ENODATAand 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.
Quoted 41 lines
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_CASE0x01
#defineMATCH_STRING_PREFIX_OF_ARRAY_ELEM0x02/* yeah, that name sucks */
#defineMATCH_ARRAY_ELEM_PREFIX_OF_STRING0x04/* this too */int
match_string(const char * const *array, size_t len, const char *string, unsigned flags)
{
#defineMATCH_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;
}
Quoted 4 lines
(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.
Signature · 2 lines
Andy Shevchenko <[email protected]> Intel Finland Oy
Andy Shevchenko
reviewerQuoted 10 lines · Sergey Senozhatsky
Quoted 5 lines · Andy Shevchenko
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.
Quoted 6 lines
Quoted 3 lines
+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'.
Quoted 32 lines
Quoted 5 lines
lib/string.c +4
-0+{ +int index =0 ; +const char *item; +lib/string.c bool sysfs_streq(const char *s1, const char *s2) · line 656
Quoted 10 lines
lib/string.c +10
-0+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()ormatch_nstring()...
*nmatch_string()_probably_ better,match_nstring()is totally cryptic.
*/
intnmatch_string(array, array_size, string, string_len){ do { strncmp(); } while (); }
Quoted 5 lines
int
match_string(array, array_size, string)
{
return nmatch_string(array,array_size, string,
strlen(string));
}
See above.
Signature · 2 lines
Andy Shevchenko <[email protected]> Intel Finland Oy
Re: [PATCH v1 2/8] device property: convert to use match_string() helper
In reply to Andy ShevchenkoQuoted 4 lines · Andy Shevchenko
The new helper returns index of the mathing string in an array. We would use it here.
Signed-off-by Andy Shevchenko <[email protected]>
Andy Shevchenko wrote:
[..]
Quoted 4 lines
Quoted 2 lines
strncmp()case seems to be quite common.Like I answered to Rasmus, please, provide real examples.
[..]
Quoted 7 lines
Quoted 7 lines
int
nmatch_string(array, array_size, string, string_len){ do { strncmp(); } while (); }
Quoted 7 lines
Quoted 5 lines
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
Quoted 21 lines · Sergey Senozhatsky
Andy Shevchenko wrote:
[..]Quoted 4 lines
Quoted 2 lines
strncmp()case seems to be quite common.Like I answered to Rasmus, please, provide real examples.
[..]
Quoted 13 lines
Quoted 12 lines
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));
}
Quoted 3 lines
Quoted 1 line
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.
Quoted 13 lines
arch/mips/bcm63xx/boards/board_bcm963xx.cvoid__initboard_prom_init(void)net/irda/irnet/irnet_irda.cirnet_dname_to_daddr()arch/powerpc/sysdev/ppc4xx_cpm.cstaticssize_tcpm_idle_store()arch/x86/ras/mce_amd_inj.cstatic int__set_injdrivers/hwtracing/intel_th/msu.cmode_storedrivers/pci/pcie/aer/ecrc.cvoidpcie_ecrc_get_policydrivers/pci/pcie/aspm.cpcie_aspm_set_policydrivers/scsi/aic7xxx/aic7xxx_osm.caic7xxx_setupdrivers/scsi/aic7xxx/aic79xx_osm.caic79xx_setupdrivers/scsi/scsi_transport_fc.cstatic int get_fc_##title##_matchdrivers/staging/android/ion/hisilicon/hi6220_ion.cget_type_by_namedrivers/staging/lustre/lustre/lmv/lproc_lmv.cplacement_name2policydrivers/xen/sys-hypervisor.cpmu_mode_store
Signature · 2 lines
With Best Regards, Andy Shevchenko
Andy Shevchenko
reviewerQuoted 23 lines · Andy Shevchenko
Quoted 21 lines · Sergey Senozhatsky
Andy Shevchenko wrote:
[..]Quoted 4 lines
Quoted 2 lines
strncmp()case seems to be quite common.Like I answered to Rasmus, please, provide real examples.
[..]
Quoted 13 lines
Quoted 12 lines
int
nmatch_string(array, array_size, string, string_len){
do {
strncmp();
} while ();
}int
match_string(array, array_size, string)
{
returnnmatch_string(array, array_size, string, strlen(string));
}
Quoted 23 lines
Quoted 4 lines
Quoted 1 line
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.Quoted 14 lines
arch/mips/bcm63xx/boards/board_bcm963xx.c void
__initboard_prom_init(void)
net/irda/irnet/irnet_irda.c irnet_dname_to_daddr()arch/powerpc/sysdev/ppc4xx_cpm.cstaticssize_tcpm_idle_store()arch/x86/ras/mce_amd_inj.cstatic int__set_injdrivers/hwtracing/intel_th/msu.cmode_storedrivers/pci/pcie/aer/ecrc.cvoidpcie_ecrc_get_policydrivers/pci/pcie/aspm.cpcie_aspm_set_policydrivers/scsi/aic7xxx/aic7xxx_osm.caic7xxx_setupdrivers/scsi/aic7xxx/aic79xx_osm.caic79xx_setupdrivers/scsi/scsi_transport_fc.cstatic int get_fc_##title##_matchdrivers/staging/android/ion/hisilicon/hi6220_ion.cget_type_by_namedrivers/staging/lustre/lustre/lmv/lproc_lmv.cplacement_name2policydrivers/xen/sys-hypervisor.cpmu_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.
Signature · 2 lines
Andy Shevchenko <[email protected]> Intel Finland Oy
Quoted 57 lines · Andy Shevchenko
Quoted 23 lines · Andy Shevchenko
Quoted 21 lines · Sergey Senozhatsky
Andy Shevchenko wrote:
[..]Quoted 4 lines
Quoted 2 lines
strncmp()case seems to be quite common.Like I answered to Rasmus, please, provide real examples.
[..]
Quoted 13 lines
Quoted 12 lines
int
nmatch_string(array, array_size, string, string_len){
do {
strncmp();
} while ();
}int
match_string(array, array_size, string)
{
returnnmatch_string(array, array_size, string, strlen(string));
}Quoted 23 lines
Quoted 4 lines
Quoted 1 line
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.Quoted 14 lines
arch/mips/bcm63xx/boards/board_bcm963xx.c void
__initboard_prom_init(void)
net/irda/irnet/irnet_irda.c irnet_dname_to_daddr()arch/powerpc/sysdev/ppc4xx_cpm.cstaticssize_tcpm_idle_store()arch/x86/ras/mce_amd_inj.cstatic int__set_injdrivers/hwtracing/intel_th/msu.cmode_storedrivers/pci/pcie/aer/ecrc.cvoidpcie_ecrc_get_policydrivers/pci/pcie/aspm.cpcie_aspm_set_policydrivers/scsi/aic7xxx/aic7xxx_osm.caic7xxx_setupdrivers/scsi/aic7xxx/aic79xx_osm.caic79xx_setupdrivers/scsi/scsi_transport_fc.cstatic int get_fc_##title##_matchdrivers/staging/android/ion/hisilicon/hi6220_ion.cget_type_by_namedrivers/staging/lustre/lustre/lmv/lproc_lmv.cplacement_name2policydrivers/xen/sys-hypervisor.cpmu_mode_storeThought 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();
Quoted 8 lines
for (...) { size_t itemlen = (len > 0) ? len : strlen(array[index]);...
if (!strncmp(array[index], string, itemlen))
return index;
}
return-EINVAL;
}
Yeah, a separate function is probably better. But why not a more explicit name, match_prefix, match_string_prefix, match_string_starts?
I like the idea of passing the string length if one wants the "is this a prefix of some array element" semantics, and a sentinel otherwise. But I don't see any case where one would want match_string() semantics (why not call match_string directly instead?), so why not let len < 0 mean "is some array element a prefix of this string" and "len >= 0" be the other case. I don't see why one shouldn't be able to ask "is the empty string a prefix of some array element" (that is, are there any elements in the array); both the array and the string might be run-time things, so this could occur. And it's not up to a generic library routine like this to impose restrictions like "the empty string makes no sense, go away".
Rasmus
Quoted 71 lines · Rasmus Villemoes
Quoted 57 lines · Andy Shevchenko
Quoted 23 lines · Andy Shevchenko
Quoted 21 lines · Sergey Senozhatsky
Andy Shevchenko wrote:
[..]Quoted 4 lines
Quoted 2 lines
strncmp()case seems to be quite common.Like I answered to Rasmus, please, provide real examples.
[..]
Quoted 13 lines
Quoted 12 lines
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));
}Quoted 23 lines
Quoted 4 lines
Quoted 1 line
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.Quoted 14 lines
arch/mips/bcm63xx/boards/board_bcm963xx.cvoid__initboard_prom_init(void)net/irda/irnet/irnet_irda.cirnet_dname_to_daddr()arch/powerpc/sysdev/ppc4xx_cpm.cstaticssize_tcpm_idle_store()arch/x86/ras/mce_amd_inj.cstatic int__set_injdrivers/hwtracing/intel_th/msu.cmode_storedrivers/pci/pcie/aer/ecrc.cvoidpcie_ecrc_get_policydrivers/pci/pcie/aspm.cpcie_aspm_set_policydrivers/scsi/aic7xxx/aic7xxx_osm.caic7xxx_setupdrivers/scsi/aic7xxx/aic79xx_osm.caic79xx_setupdrivers/scsi/scsi_transport_fc.cstatic int get_fc_##title##_matchdrivers/staging/android/ion/hisilicon/hi6220_ion.cget_type_by_namedrivers/staging/lustre/lustre/lmv/lproc_lmv.cplacement_name2policydrivers/xen/sys-hypervisor.cpmu_mode_storeThought 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();Quoted 8 lines
for (...) { size_t itemlen = (len > 0) ? len : strlen(array[index]);...
if (!strncmp(array[index], string, itemlen))
return index;
}
return-EINVAL;
}Yeah, a separate function is probably better. But why not a more explicit name,
match_prefix,match_string_prefix,match_string_starts?
Quoted 11 lines
I like the idea of passing the string length if one wants the "is this a prefix of some array element" semantics, and a sentinel otherwise. But I don't see any case where one would want
match_string()semantics (why not callmatch_stringdirectly instead?), so why not let len < 0 mean "is some array element a prefix of this string" and "len >= 0" be the other case. I don't see why one shouldn't be able to ask "is the empty string a prefix of some array element" (that is, are there any elements in the array); both the array and the string might be run-time things, so this could occur. And it's not up to a generic library routine like this to impose restrictions like "the empty string makes no sense, go away".
I have no strong feelings to my initial proposal, and your suggestions sound sane. Unfortunately I have no time to properly implement it and convert users, so, if Sergey would like to do that, I will no object.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Hello,
On (01/11/16 23:10), Rasmus Villemoes wrote:
[..]
Quoted 9 lines
Quoted 9 lines
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();
Quoted 8 lines
Quoted 8 lines
for (...) { size_t itemlen = (len > 0) ? len : strlen(array[index]);...
if (!strncmp(array[index], string, itemlen))
return index;
}
return-EINVAL;
}
may be later this week; I'm a bit out of spare time at the moment.
Quoted 2 lines
Yeah, a separate function is probably better. But why not a more explicit name,
match_prefix,match_string_prefix,match_string_starts?
Not married to nmatch_string(), but at the same time, IMHO, *_prefix or *_starts naming is not really better. One can pass a string with offset, e.g.
FOO_starts(array, array_size, string + offset, strlen(string) - offset)
which will be equivalent to FOO_ends(), but not FOO_starts() or FOO_prefix(). Personally, I'd prefer to preserve strcmp/strncmp semantics, thus, forbidding `len < 0' case, which looks cryptic to me.
Quoted 10 lines
I like the idea of passing the string length if one wants the "is this a prefix of some array element" semantics, and a sentinel otherwise. But I don't see any case where one would want
match_string()semantics (why not callmatch_stringdirectly instead?),so why not let len < 0 mean "is some array element a prefix of this string" and "len >= 0" be the other case. I don't see why one shouldn't be able to ask "is the empty string a prefix of some array element" (that is, are there any elements in the array);
if this is a dynamic array, then there should be some function that fills in that array, so having a simple bool flag in the code will suffice; if this is a static array, then ARRAY_SIZE() should do the trick. I would never expect a string matching function to have this type of functionality, TBH.
But the question is
Quoted 1 line
is the empty string a prefix of some array element
do people really need this?
the way I see it, the idea is to have wrappers around
while (array[..]) if strcmp()/strncmp() == 0 break ...
both of which [strcmp()/strncmp()] have a well known and expected semantics, changing this can only confuse people.
-ss
Quoted 7 lines
both the array and the string might be run-time things,
so this could occur. And it's not up to a generic library routine like this to impose restrictions like "the empty string makes no sense, go away".Rasmus