diff mbox

[v2,1/3] devicetree/dt_sysinfo: clean up logic in stringlist_contains_last

Message ID 1467271061-16212-2-git-send-email-jk@ozlabs.org
State Accepted
Headers show

Commit Message

Jeremy Kerr June 30, 2016, 7:17 a.m. UTC
This change is a little cleanup of dt_fdt_stringlist_contains_last, but
retains the same functional semantics.

We remove the duplicated calls to memcmp, instead only changing the
arguments if !p. Also, we return a bool instead of an int, and change
the prefix from prd_ to dt_, as this is a device-tree helper (and not a
prd one).

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
---
 src/devicetree/dt_sysinfo/dt_sysinfo.c | 18 ++++++++----------
 1 file changed, 8 insertions(+), 10 deletions(-)

Comments

Colin Ian King June 30, 2016, 9:12 a.m. UTC | #1
On 30/06/16 08:17, Jeremy Kerr wrote:
> This change is a little cleanup of dt_fdt_stringlist_contains_last, but
> retains the same functional semantics.
> 
> We remove the duplicated calls to memcmp, instead only changing the
> arguments if !p. Also, we return a bool instead of an int, and change
> the prefix from prd_ to dt_, as this is a device-tree helper (and not a
> prd one).
> 
> Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
> ---
>  src/devicetree/dt_sysinfo/dt_sysinfo.c | 18 ++++++++----------
>  1 file changed, 8 insertions(+), 10 deletions(-)
> 
> diff --git a/src/devicetree/dt_sysinfo/dt_sysinfo.c b/src/devicetree/dt_sysinfo/dt_sysinfo.c
> index 477210a..d08cf5c 100644
> --- a/src/devicetree/dt_sysinfo/dt_sysinfo.c
> +++ b/src/devicetree/dt_sysinfo/dt_sysinfo.c
> @@ -145,7 +145,7 @@ static int dt_sysinfo_check_system_id(fwts_framework *fw)
>  	return dt_sysinfo_check_root_property(fw, "system-id", true);
>  }
>  
> -static int prd_fdt_stringlist_contains_last(const char *strlist,
> +static bool dt_fdt_stringlist_contains_last(const char *strlist,
>  	int listlen,
>  	const char *str)
>  {
> @@ -153,19 +153,17 @@ static int prd_fdt_stringlist_contains_last(const char *strlist,
>  	const char *p;
>  
>  	/* checking for either str only or last in string */
> -	if (listlen < 2 ) /* need at least one byte plus nul */
> -		return 0;
> +	if (listlen < 2) /* need at least one byte plus nul */
> +		return false;
>  
>  	p = memrchr(strlist, '\0', listlen-1);
> -	if (!p) {
> -		if (memcmp(str, strlist, len+1) == 0)
> -			return 1;
> +	if (p) {
> +		p++;
>  	} else {
> -		if (memcmp(str, p+1, len+1) == 0)
> -			return 1;
> +		p = strlist;
>  	}
>  
> -	return 0;
> +	return !memcmp(str, p, len+1);
>  }
>  
>  static bool machine_matches_reference_model(fwts_framework *fw,
> @@ -181,7 +179,7 @@ static bool machine_matches_reference_model(fwts_framework *fw,
>  		i++) {
>  		struct reference_platform *plat =
>  			&openpower_reference_platforms[i];
> -		if (prd_fdt_stringlist_contains_last(compatible,
> +		if (dt_fdt_stringlist_contains_last(compatible,
>  			compat_len, plat->compatible)) {
>  			for (j = 0; j < plat->n_models; j++) {
>  				if (!strcmp(model, plat->models[j])) {
> 
Acked-by: Colin Ian King <colin.king@canonical.com>
Alex Hung July 1, 2016, 1:29 a.m. UTC | #2
On 2016-06-30 03:17 PM, Jeremy Kerr wrote:
> This change is a little cleanup of dt_fdt_stringlist_contains_last, but
> retains the same functional semantics.
>
> We remove the duplicated calls to memcmp, instead only changing the
> arguments if !p. Also, we return a bool instead of an int, and change
> the prefix from prd_ to dt_, as this is a device-tree helper (and not a
> prd one).
>
> Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
> ---
>   src/devicetree/dt_sysinfo/dt_sysinfo.c | 18 ++++++++----------
>   1 file changed, 8 insertions(+), 10 deletions(-)
>
> diff --git a/src/devicetree/dt_sysinfo/dt_sysinfo.c b/src/devicetree/dt_sysinfo/dt_sysinfo.c
> index 477210a..d08cf5c 100644
> --- a/src/devicetree/dt_sysinfo/dt_sysinfo.c
> +++ b/src/devicetree/dt_sysinfo/dt_sysinfo.c
> @@ -145,7 +145,7 @@ static int dt_sysinfo_check_system_id(fwts_framework *fw)
>   	return dt_sysinfo_check_root_property(fw, "system-id", true);
>   }
>
> -static int prd_fdt_stringlist_contains_last(const char *strlist,
> +static bool dt_fdt_stringlist_contains_last(const char *strlist,
>   	int listlen,
>   	const char *str)
>   {
> @@ -153,19 +153,17 @@ static int prd_fdt_stringlist_contains_last(const char *strlist,
>   	const char *p;
>
>   	/* checking for either str only or last in string */
> -	if (listlen < 2 ) /* need at least one byte plus nul */
> -		return 0;
> +	if (listlen < 2) /* need at least one byte plus nul */
> +		return false;
>
>   	p = memrchr(strlist, '\0', listlen-1);
> -	if (!p) {
> -		if (memcmp(str, strlist, len+1) == 0)
> -			return 1;
> +	if (p) {
> +		p++;
>   	} else {
> -		if (memcmp(str, p+1, len+1) == 0)
> -			return 1;
> +		p = strlist;
>   	}
>
> -	return 0;
> +	return !memcmp(str, p, len+1);
>   }
>
>   static bool machine_matches_reference_model(fwts_framework *fw,
> @@ -181,7 +179,7 @@ static bool machine_matches_reference_model(fwts_framework *fw,
>   		i++) {
>   		struct reference_platform *plat =
>   			&openpower_reference_platforms[i];
> -		if (prd_fdt_stringlist_contains_last(compatible,
> +		if (dt_fdt_stringlist_contains_last(compatible,
>   			compat_len, plat->compatible)) {
>   			for (j = 0; j < plat->n_models; j++) {
>   				if (!strcmp(model, plat->models[j])) {
>

Acked-by: Alex Hung <alex.hung@canonical.com>
diff mbox

Patch

diff --git a/src/devicetree/dt_sysinfo/dt_sysinfo.c b/src/devicetree/dt_sysinfo/dt_sysinfo.c
index 477210a..d08cf5c 100644
--- a/src/devicetree/dt_sysinfo/dt_sysinfo.c
+++ b/src/devicetree/dt_sysinfo/dt_sysinfo.c
@@ -145,7 +145,7 @@  static int dt_sysinfo_check_system_id(fwts_framework *fw)
 	return dt_sysinfo_check_root_property(fw, "system-id", true);
 }
 
-static int prd_fdt_stringlist_contains_last(const char *strlist,
+static bool dt_fdt_stringlist_contains_last(const char *strlist,
 	int listlen,
 	const char *str)
 {
@@ -153,19 +153,17 @@  static int prd_fdt_stringlist_contains_last(const char *strlist,
 	const char *p;
 
 	/* checking for either str only or last in string */
-	if (listlen < 2 ) /* need at least one byte plus nul */
-		return 0;
+	if (listlen < 2) /* need at least one byte plus nul */
+		return false;
 
 	p = memrchr(strlist, '\0', listlen-1);
-	if (!p) {
-		if (memcmp(str, strlist, len+1) == 0)
-			return 1;
+	if (p) {
+		p++;
 	} else {
-		if (memcmp(str, p+1, len+1) == 0)
-			return 1;
+		p = strlist;
 	}
 
-	return 0;
+	return !memcmp(str, p, len+1);
 }
 
 static bool machine_matches_reference_model(fwts_framework *fw,
@@ -181,7 +179,7 @@  static bool machine_matches_reference_model(fwts_framework *fw,
 		i++) {
 		struct reference_platform *plat =
 			&openpower_reference_platforms[i];
-		if (prd_fdt_stringlist_contains_last(compatible,
+		if (dt_fdt_stringlist_contains_last(compatible,
 			compat_len, plat->compatible)) {
 			for (j = 0; j < plat->n_models; j++) {
 				if (!strcmp(model, plat->models[j])) {