From 15740faf9fe9fe4be08965098bbf2947e096aeeb Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 14 Aug 2019 22:50:43 +0800 Subject: +Unity Runtime code --- Runtime/Utilities/CStringHash.h | 52 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 Runtime/Utilities/CStringHash.h (limited to 'Runtime/Utilities/CStringHash.h') diff --git a/Runtime/Utilities/CStringHash.h b/Runtime/Utilities/CStringHash.h new file mode 100644 index 0000000..a4b58ef --- /dev/null +++ b/Runtime/Utilities/CStringHash.h @@ -0,0 +1,52 @@ +#ifndef CSTRINGHASH_H +#define CSTRINGHASH_H +#include + +struct hash_cstring : std::unary_function +{ + unsigned operator ()(const char* key) const + { + unsigned h = 0; + const unsigned sr = 8 * sizeof (unsigned) - 8; + const unsigned mask = 0xF << (sr + 4); + while (*key != '\0') + { + h = (h << 4) + *key; + std::size_t g = h & mask; + h ^= g | (g >> sr); + key++; + } + return h; + } +}; + +struct equal_cstring : std::binary_function +{ + bool operator () (char* lhs, char* rhs) const + { + while (*lhs != '\0') + { + if (*lhs != *rhs) + return false; + lhs++; rhs++; + } + return *lhs == *rhs; + } +}; + +struct smaller_cstring : std::binary_function +{ + bool operator () (const char* lhs, const char* rhs) const { return strcmp (lhs, rhs) < 0; } +}; + +struct compare_cstring : public std::binary_function +{ + bool operator ()(const char* lhs, const char* rhs) const { return strcmp (lhs, rhs) < 0; } +}; + +struct compare_string_insensitive : public std::binary_function +{ + bool operator ()(const std::string& lhs, const std::string& rhs) const { return StrICmp (lhs, rhs) < 0; } +}; + +#endif -- cgit v1.1-26-g67d0