diff mbox

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

Message ID 20160107130743.GA9935@kuha.fi.intel.com (mailing list archive)
State Not Applicable, archived
Headers show

Commit Message

Heikki Krogerus Jan. 7, 2016, 1:07 p.m. UTC
On Thu, Jan 07, 2016 at 02:06:01PM +0200, Andy Shevchenko wrote:
> >From time to time we have to match a string in an array. Make a simple helper
> for that purpose.

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



Thanks,

Comments

Andy Shevchenko Jan. 7, 2016, 1:12 p.m. UTC | #1
On Thu, 2016-01-07 at 15:07 +0200, Heikki Krogerus wrote:
> On Thu, Jan 07, 2016 at 02:06:01PM +0200, Andy Shevchenko wrote:
> > > From time to time we have to match a string in an array. Make a
> > > simple helper
> > for that purpose.
> 
> Cool! If you make v2 out of these, could you patch the following
> while
> at it:

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

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

Patch

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