From 646ef07aab383dd1cdcdabcb52e222c98bfeda62 Mon Sep 17 00:00:00 2001 From: Barry Kane Date: Mon, 20 Feb 2023 23:03:07 +0000 Subject: [PATCH] Added silvermud-command-hash-tester. This is to assist with the development of SilverMUD, to allow me to calculate the hash of a string from the command line. --- silvermud-command-hash-tester.c | 36 +++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 silvermud-command-hash-tester.c diff --git a/silvermud-command-hash-tester.c b/silvermud-command-hash-tester.c new file mode 100644 index 0000000..9b71d50 --- /dev/null +++ b/silvermud-command-hash-tester.c @@ -0,0 +1,36 @@ +// A small utility to get the hash of a string for SilverMUD's command evaluator: +#include +#include +#include + +unsigned int hashCommand(char * command, unsigned int commandLength) +{ + // Lowercase the string: + for (char * character = command; *character; ++character) + { + *character = tolower(*character); + } + + // Hash the string: + char * currentCharacter = command; + unsigned int hash = 0; + + for (unsigned int index = 0; index < commandLength && *currentCharacter != '\0'; currentCharacter++) + { + hash = 37 * hash + *currentCharacter; + } + + return hash; +} + +void main(int argc, char ** argv) +{ + if(argc >= 2) + { + printf("%u\n", hashCommand(argv[1], strlen(argv[1]))); + } + else + { + printf("Please provide a string to hash as argument one.\n"); + } +}