From c5918ee9c2309181f8e1fd444cfe8da1f7da3c0b Mon Sep 17 00:00:00 2001
From: Moe Jette <jette1@llnl.gov>
Date: Sat, 1 Jan 2011 19:14:17 +0000
Subject: [PATCH] Patch from Gerrit: 06_hostlist__host_prefix_end.diff

host_prefix_end(): save variable, factor out common expressions

This saves one variable and factors out the condition common
to the if/else branches.06_hostlist__host_prefix_end.diff

 * return type of strlen is unsigned (size_t), hence strlen(hostname) >= 0;
 * both while-loops require idx >= 0, hence -1 is returned
   if strlen(hostname) == 0.
---
 src/common/hostlist.c | 27 ++++++++-------------------
 1 file changed, 8 insertions(+), 19 deletions(-)

diff --git a/src/common/hostlist.c b/src/common/hostlist.c
index 01176784163..78f5b80adeb 100644
--- a/src/common/hostlist.c
+++ b/src/common/hostlist.c
@@ -595,30 +595,19 @@ static int _width_equiv(unsigned long n, int *wn, unsigned long m, int *wm)
  */
 static int host_prefix_end(const char *hostname)
 {
-	int idx, len;
-	int dims = slurmdb_setup_cluster_dims();
+	int idx, dims = slurmdb_setup_cluster_dims();
 
 	assert(hostname != NULL);
 
-	len = strlen(hostname);
-
-	if(dims > 1) {
-		idx = len - 1;
+	idx = strlen(hostname) - 1;
 
-		while (idx >= 0) {
-			if (((hostname[idx] >= '0')
-			     && (hostname[idx] <= '9')) ||
-			    ((hostname[idx] >= 'A') && (hostname[idx] <= 'Z')))
-				idx--;
-			else
-				break;
-		}
+	if (dims > 1) {
+		while ((idx >= 0) &&
+		       (isdigit((int)hostname[idx]) ||
+		        isupper((int)hostname[idx])))
+			idx--;
 	} else {
-		if (len < 1)
-			return -1;
-		idx = len - 1;
-
-		while (idx >= 0 && isdigit((int)hostname[idx]))
+		while ((idx >= 0) && isdigit((int)hostname[idx]))
 			idx--;
 	}
 
-- 
GitLab