mirror of
https://github.com/Wessel/mariadb-example.git
synced 2026-06-05 23:15:41 +02:00
20 lines
533 B
C
20 lines
533 B
C
/* utils.c */
|
|
/** @brief Hash `str` to `hash` for use in a switch case
|
|
*
|
|
* A string needs to be hashed for it to be used
|
|
* inside of a switch statement. We want to use a switch
|
|
* statement instead of an if-else chain for it's
|
|
* simplicity and lower amount of low-level operations.
|
|
*
|
|
* @param str The string to hash
|
|
*
|
|
* @returns hash The hashed version of `str`
|
|
*/
|
|
const unsigned long hash(const char *str) {
|
|
unsigned long hash = 5381;
|
|
int c;
|
|
|
|
while ((c = *str++))
|
|
hash = ((hash << 5) + hash) + c;
|
|
return hash;
|
|
} |