summaryrefslogtreecommitdiff
path: root/hash.c
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2023-11-04 11:28:44 -0700
committerAlan Coopersmith <alan.coopersmith@oracle.com>2023-11-06 14:19:08 -0800
commit19137ec2f129f91ce3adb46218c86e1bf547e661 (patch)
tree237a5a879a738a18009863ecd12464eaff3af544 /hash.c
parentffd69eb38f153229f5033ef5cc789c0ab3704edd (diff)
Variable scope reductions as recommended by cppcheck
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
Diffstat (limited to 'hash.c')
-rw-r--r--hash.c31
1 files changed, 11 insertions, 20 deletions
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;