lib/string: introduce match_string() helper
Subsystems (5)
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.
Two users are updated in the series. one more is coming [1].
[1] http://www.spinics.net/lists/kernel/msg2074265.html
The series is compile tested.
Andy Shevchenko (3):
lib/string: introduce match_string() helper drm/edid: convert to use match_string() helper
ata: hpt366: convert to use match_string() helper
drivers/ata/pata_hpt366.c | 13 +++++-------- drivers/gpu/drm/drm_edid_load.c | 17 ++++++----------- include/linux/string.h | 7 +++++++ lib/string.c | 26 ++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 19 deletions(-)
Signature · 1 line
2.5.1
PATCH 1/3 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 | 7 +++++++ lib/string.c | 26 ++++++++++++++++++++++++++ 2 files changed, 33 insertions(+)
include/linux/string.h +7 -0
@@ -126,6 +126,13 @@ 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); + +static inline int match_stringnul (const char *const *array,const char *string) +{ +return match_string (array,0 , 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
@@ -542,6 +542,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 -1 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 -1 ; +} +EXPORT_SYMBOL (match_string); + +/** * strtobool - convert common user inputs into boolean values * @s: input string * @res: result
Signature · 1 line
2.5.1
PATCH 2/3 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.5.1
PATCH 3/3 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_stringnul (list, 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.5.1
Hello,
Quoted 14 lines · Andy Shevchenko
There are users of a simple string matching in the array. Let's do a common helper for that.
Two users are updated in the series. one more is coming [1].
[1] http://www.spinics.net/lists/kernel/msg2074265.html
The series is compile tested.
Andy Shevchenko (3):
lib/string: introducematch_string()helper drm/edid: convert to usematch_string()helper
ata: hpt366: convert to usematch_string()helper
Quoted 5 lines
drivers/ata/pata_hpt366.c | 13 +++++-------- drivers/gpu/drm/drm_edid_load.c | 17 ++++++----------- include/linux/string.h | 7 +++++++ lib/string.c | 26 ++++++++++++++++++++++++++ 4 files changed, 44 insertions(+), 19 deletions(-)
While increase in LOC isn't the only indicator, we're adding two helper functions each for two usages. I'm not quite sure the addition of helpers is paying off here.
Thanks.
Signature · 1 line
tejun
Quoted 24 lines · Tejun Heo
Hello,
Quoted 15 lines · Andy Shevchenko
There are users of a simple string matching in the array. Let's do a common
helper for that.Two users are updated in the series. one more is coming [1].
[1] http://www.spinics.net/lists/kernel/msg2074265.html
The series is compile tested.
Andy Shevchenko (3):
lib/string: introducematch_string()helper drm/edid: convert to usematch_string()helper
ata: hpt366: convert to usematch_string()helperQuoted 5 lines
drivers/ata/pata_hpt366.c | 13 +++++--------
drivers/gpu/drm/drm_edid_load.c| 17 ++++++----------- include/linux/string.h | 7 +++++++ lib/string.c | 26 ++++++++++++++++++++++++++4 files changed, 44 insertions(+), 19 deletions(-)
Quoted 4 lines
While increase in LOC isn't the only indicator, we're adding two helper functions each for two usages. I'm not quite sure the addition
of helpers is paying off here.
Does the following sound better? (Btw, I left only one helper function)
drivers/ata/pata_hpt366.c | 13 +++++-------- drivers/base/property.c | 10 ++-------- drivers/gpu/drm/drm_edid_load.c | 17 ++++++----------- drivers/pinctrl/pinmux.c | 13 +++---------- drivers/power/ab8500_btemp.c | 11 +++-------- drivers/power/ab8500_charger.c | 11 +++-------- drivers/power/ab8500_fg.c | 11 +++-------- drivers/power/abx500_chargalg.c | 10 +++------- drivers/power/charger-manager.c | 27 ++++----------------------- include/linux/string.h | 2 ++
lib/string.c | 26 ++++++++++++++++++++++++++
11 files changed, 60 insertions(+), 91 deletions(-)
Quoted 3 lines
Thanks.
Signature · 2 lines
Andy Shevchenko <[email protected]> Intel Finland Oy