From 19137ec2f129f91ce3adb46218c86e1bf547e661 Mon Sep 17 00:00:00 2001 From: Alan Coopersmith Date: Sat, 4 Nov 2023 11:28:44 -0700 Subject: Variable scope reductions as recommended by cppcheck Signed-off-by: Alan Coopersmith --- hash.c | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) (limited to 'hash.c') diff --git a/hash.c b/hash.c index 3a3e67e..5877dc6 100644 --- a/hash.c +++ b/hash.c @@ -37,10 +37,9 @@ static unsigned hash(const char *string) { - int i; unsigned u = 0; - for (i = 0; string[i] != '\0'; i++) + for (int i = 0; string[i] != '\0'; i++) u = (u << 5) + (u >> (LOG2_NUMBUCKETS - 5)) + (unsigned char) string[i]; return (u & (NUMBUCKETS - 1)); } @@ -63,12 +62,9 @@ makeHashTable(void) void destroyHashTable(HashTablePtr table) { - int i; - HashBucketPtr bp; - - for (i = 0; i < NUMBUCKETS; i++) { + for (int i = 0; i < NUMBUCKETS; i++) { while (table[i]) { - bp = table[i]; + HashBucketPtr bp = table[i]; table[i] = table[i]->next; free(bp->key); free(bp->value); @@ -82,9 +78,8 @@ char * getHash(HashTablePtr table, const char *key) { unsigned int i = hash(key); - HashBucketPtr bp; - for (bp = table[i]; bp; bp = bp->next) { + for (HashBucketPtr bp = table[i]; bp; bp = bp->next) { if (strcasecmp(bp->key, key) == 0) return bp->value; } @@ -144,12 +139,10 @@ putHash(HashTablePtr table, char *key, char *value, int prio) int hashElements(HashTablePtr table) { - int i, n; - HashBucketPtr bp; + int n = 0; - n = 0; - for (i = 0; i < NUMBUCKETS; i++) { - for (bp = table[i]; bp; bp = bp->next) { + for (int i = 0; i < NUMBUCKETS; i++) { + for (HashBucketPtr bp = table[i]; bp; bp = bp->next) { n++; } } @@ -181,16 +174,14 @@ value_first_cmp(const void *v1, const void *v2) HashBucketPtr * hashArray(HashTablePtr table, int value_first) { - int i, j, n; - HashBucketPtr *dst; - - n = hashElements(table); - dst = malloc((n + 1) * sizeof(HashBucketPtr)); + int j; + int n = hashElements(table); + HashBucketPtr *dst = malloc((n + 1) * sizeof(HashBucketPtr)); if (dst == NULL) return NULL; j = 0; - for (i = 0; i < NUMBUCKETS; i++) { + for (int i = 0; i < NUMBUCKETS; i++) { while (table[i]) { dst[j++] = table[i]; table[i] = table[i]->next; -- cgit v1.2.3