From eaef6bf23f0c03430b7ede8feda83880efaadcb5 Mon Sep 17 00:00:00 2001 From: Jessica James Date: Fri, 3 Dec 2021 23:05:23 -0600 Subject: [PATCH] Remove Hash.cpp; replace str_hash usage with jessilib::text_hashi and jessilib::text_hash; updated jessilib --- src/common/CMakeLists.txt | 1 - src/common/Hash.cpp | 83 ------------ src/include/Jupiter/Config.h | 14 +- src/include/Jupiter/HTTP_QueryString.h | 2 +- src/include/Jupiter/Hash.h | 170 ------------------------- src/include/Jupiter/IRC_Client.h | 7 +- src/jessilib | 2 +- 7 files changed, 17 insertions(+), 262 deletions(-) delete mode 100644 src/common/Hash.cpp delete mode 100644 src/include/Jupiter/Hash.h diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 3ff2753..a7083d8 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -10,7 +10,6 @@ set(SOURCE_FILES File.cpp Functions.c GenericCommand.cpp - Hash.cpp HTTP_Server.cpp INIConfig.cpp IRC_Client.cpp diff --git a/src/common/Hash.cpp b/src/common/Hash.cpp deleted file mode 100644 index 11e2e10..0000000 --- a/src/common/Hash.cpp +++ /dev/null @@ -1,83 +0,0 @@ -/** - * Copyright (C) 2015-2016 Jessica James. - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION - * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN - * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - * - * Written by Jessica James - */ - -#include "Hash.h" - -/** Constants */ -constexpr uint32_t JUPITER_FNV_1_32_OFFSET_BASIS = 2166136261UL; -constexpr uint64_t JUPITER_FNV_1_64_OFFSET_BASIS = 14695981039346656037ULL; -constexpr uint32_t JUPITER_FNV_1_32_PRIME = 16777619UL; -constexpr uint64_t JUPITER_FNV_1_64_PRIME = 1099511628211ULL; - -/** Fowler-Noll-Vo */ - -uint64_t Jupiter::fnv1(const uint8_t *data, const uint8_t *end) -{ - uint64_t hash = JUPITER_FNV_1_64_OFFSET_BASIS; - - while (data != end) - { - hash = hash * JUPITER_FNV_1_64_PRIME; - hash = hash ^ *data; - ++data; - } - - return hash; -} - -uint64_t Jupiter::fnv1a(const uint8_t *data, const uint8_t *end) -{ - uint64_t hash = JUPITER_FNV_1_64_OFFSET_BASIS; - - while (data != end) - { - hash = hash ^ *data; - hash = hash * JUPITER_FNV_1_64_PRIME; - ++data; - } - - return hash; -} - -uint32_t Jupiter::fnv1_32(const uint8_t *data, const uint8_t *end) -{ - uint32_t hash = JUPITER_FNV_1_32_OFFSET_BASIS; - - while (data != end) - { - hash = hash * JUPITER_FNV_1_32_PRIME; - hash = hash ^ *data; - ++data; - } - - return hash; -} - -uint32_t Jupiter::fnv1a_32(const uint8_t *data, const uint8_t *end) -{ - uint32_t hash = JUPITER_FNV_1_32_OFFSET_BASIS; - - while (data != end) - { - hash = hash ^ *data; - hash = hash * JUPITER_FNV_1_32_PRIME; - ++data; - } - - return hash; -} \ No newline at end of file diff --git a/src/include/Jupiter/Config.h b/src/include/Jupiter/Config.h index d1514c5..c452bcb 100644 --- a/src/include/Jupiter/Config.h +++ b/src/include/Jupiter/Config.h @@ -26,8 +26,8 @@ #include #include +#include "jessilib/unicode.hpp" #include "Jupiter.h" -#include "Hash.h" #include "Readable_String.h" // from_string /** DLL Linkage Nagging */ @@ -38,6 +38,14 @@ namespace Jupiter { +#ifdef __cpp_lib_generic_unordered_lookup + using InMapKeyType = std::string_view; +#define JUPITER_WRAP_MAP_KEY(in_key) in_key +#else // We can't use std::string_view for InKeyType until GCC 11 & clang 12, and I still want to support GCC 9 + using InMapKeyType = std::string; +#define JUPITER_WRAP_MAP_KEY(in_key) static_cast(in_key) +#endif // __cpp_lib_generic_unordered_lookup + /** * @brief Base class for all Config type files */ @@ -45,8 +53,8 @@ namespace Jupiter { public: /** Hash_Table type for sections */ - using SectionHashTable = std::unordered_map, std::equal_to<>>; - using ValuesHashTable = std::unordered_map, std::equal_to<>>; + using SectionHashTable = std::unordered_map; + using ValuesHashTable = std::unordered_map; using InKeyType = InMapKeyType; Config() = default; diff --git a/src/include/Jupiter/HTTP_QueryString.h b/src/include/Jupiter/HTTP_QueryString.h index f996346..4dc61b0 100644 --- a/src/include/Jupiter/HTTP_QueryString.h +++ b/src/include/Jupiter/HTTP_QueryString.h @@ -41,7 +41,7 @@ namespace Jupiter HTMLFormResponse() = delete; inline HTMLFormResponse(std::string_view query_string) : HTMLFormResponse(query_string.data(), query_string.size()) {} inline HTMLFormResponse(const char *ptr, size_t str_size); - using TableType = std::unordered_map, std::equal_to<>>; + using TableType = std::unordered_map; #ifdef __cpp_lib_generic_unordered_lookup using InKeyType = std::string_view; #else // We can't use std::string_view for InKeyType until GCC 11 & clang 12, and I still want to support GCC 9 diff --git a/src/include/Jupiter/Hash.h b/src/include/Jupiter/Hash.h deleted file mode 100644 index 3b5b7b0..0000000 --- a/src/include/Jupiter/Hash.h +++ /dev/null @@ -1,170 +0,0 @@ -/** - * Copyright (C) 2015-2016 Jessica James. - * - * Permission to use, copy, modify, and/or distribute this software for any - * purpose with or without fee is hereby granted, provided that the above - * copyright notice and this permission notice appear in all copies. - * - * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES - * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY - * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES - * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION - * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN - * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. - * - * Written by Jessica James - */ - -#if !defined _HASH_H_HEADER -#define _HASH_H_HEADER - -/** - * @file Hash.h - * @brief Defines some hashing algorithms. - */ - -#include -#include -#include "Jupiter.h" - -namespace Jupiter -{ - /** Sums */ - template inline R calcsum(const T *in_data, size_t in_length); - template inline R calcsum(const std::basic_string_view &str); - - /** Fowler-Noll-Vo hash algorithms */ - template inline uint64_t fnv1(const T &data); - template inline uint64_t fnv1(const T *str, size_t length); - template inline uint64_t fnv1(const std::basic_string_view &str); - - template inline uint32_t fnv1_32(const T &data); - template inline uint32_t fnv1_32(const T *str, size_t length); - template inline uint32_t fnv1_32(const std::basic_string_view &str); - - template inline uint64_t fnv1a(const T &data); - template inline uint64_t fnv1a(const T *str, size_t length); - template inline uint64_t fnv1a(const std::basic_string_view &str); - - template inline uint32_t fnv1a_32(const T &data); - template inline uint32_t fnv1a_32(const T *str, size_t length); - template inline uint32_t fnv1a_32(const std::basic_string_view &str); - - JUPITER_API uint64_t fnv1(const uint8_t *data, const uint8_t *end); - JUPITER_API uint64_t fnv1a(const uint8_t *data, const uint8_t *end); - - JUPITER_API uint32_t fnv1_32(const uint8_t *data, const uint8_t *end); - JUPITER_API uint32_t fnv1a_32(const uint8_t *data, const uint8_t *end); - - /** unordered_map helpers */ - template - struct str_hash { - using is_transparent = std::true_type; - - // C++17 introduces a requirement that these two operators return the same values for same CharT type, but not - // any requirement that std::hash<> be able to accept both key types. This just ties them for convenience - auto operator()(std::basic_string_view in_key) const noexcept { - return std::hash>()(in_key); - } - - auto operator()(const std::basic_string& in_key) const noexcept { - return std::hash>()(in_key); - } - }; - -#ifdef __cpp_lib_generic_unordered_lookup - using InMapKeyType = std::string_view; -#define JUPITER_WRAP_MAP_KEY(in_key) in_key -#else // We can't use std::string_view for InKeyType until GCC 11 & clang 12, and I still want to support GCC 9 - using InMapKeyType = std::string; -#define JUPITER_WRAP_MAP_KEY(in_key) static_cast(in_key) -#endif // __cpp_lib_generic_unordered_lookup - - using default_hash_function = str_hash; -} - -/** Calcsum implementation */ - -template inline R Jupiter::calcsum(const T *in_data, size_t in_length) -{ - const uint8_t *itr = reinterpret_cast(in_data); - const uint8_t *end = reinterpret_cast(in_data + in_length); - R sum = 0; - - while (itr != end) - { - sum += *itr; - ++itr; - } - - return sum; -} - -template inline R Jupiter::calcsum(const std::basic_string_view &str) -{ - return Jupiter::calcsum(str.data(), str.size()); -} - -/** fnv1 implementation */ -template inline uint64_t Jupiter::fnv1(const T &data) -{ - return Jupiter::fnv1(&data, &data + 1); -} - -template inline uint64_t Jupiter::fnv1(const T *data, size_t length) -{ - return Jupiter::fnv1(reinterpret_cast(data), reinterpret_cast(data + length)); -} - -template inline uint64_t Jupiter::fnv1(const std::basic_string_view &data) -{ - return Jupiter::fnv1(reinterpret_cast(data.data()), reinterpret_cast(data.data() + data.size())); -} - -template inline uint32_t Jupiter::fnv1_32(const T &data) -{ - return Jupiter::fnv1_32(&data, &data + 1); -} - -template inline uint32_t Jupiter::fnv1_32(const T *data, size_t length) -{ - return Jupiter::fnv1_32(reinterpret_cast(data), reinterpret_cast(data + length)); -} - -template inline uint32_t Jupiter::fnv1_32(const std::basic_string_view &data) -{ - return Jupiter::fnv1_32(reinterpret_cast(data.data()), reinterpret_cast(data.data() + data.size())); -} - -template inline uint64_t Jupiter::fnv1a(const T &data) -{ - return Jupiter::fnv1a(reinterpret_cast(&data), reinterpret_cast(&data + 1)); -} - -template inline uint64_t Jupiter::fnv1a(const T *data, size_t length) -{ - return Jupiter::fnv1a(reinterpret_cast(data), reinterpret_cast(data + length)); -} - -template inline uint64_t Jupiter::fnv1a(const std::basic_string_view &data) -{ - return Jupiter::fnv1a(data.data(), data.size()); -} - -template inline uint32_t Jupiter::fnv1a_32(const T &data) -{ - return Jupiter::fnv1a_32(reinterpret_cast(&data), reinterpret_cast(&data + 1)); -} - -template inline uint32_t Jupiter::fnv1a_32(const T *data, size_t length) -{ - return Jupiter::fnv1a_32(reinterpret_cast(data), reinterpret_cast(data + length)); -} - -template inline uint32_t Jupiter::fnv1a_32(const std::basic_string_view &data) -{ - return Jupiter::fnv1a_32(data.data(), data.size()); -} - -#endif // _HASH_H_HEADER \ No newline at end of file diff --git a/src/include/Jupiter/IRC_Client.h b/src/include/Jupiter/IRC_Client.h index 435515d..718ce9d 100644 --- a/src/include/Jupiter/IRC_Client.h +++ b/src/include/Jupiter/IRC_Client.h @@ -27,6 +27,7 @@ #include #include #include +#include "jessilib/unicode.hpp" #include "Jupiter.h" #include "Thinker.h" #include "IRC.h" @@ -314,7 +315,7 @@ namespace Jupiter std::string m_prefixes; }; - using UserTableType = std::unordered_map, default_hash_function, std::equal_to<>>; + using UserTableType = std::unordered_map, jessilib::text_hashi, jessilib::text_equali>; /** * @brief Returns the name of the channel. @@ -428,8 +429,8 @@ namespace Jupiter bool m_adding_names; }; // Jupiter::IRC::Client::Channel class - using ChannelTableType = std::unordered_map>; - using UserTableType = std::unordered_map, default_hash_function, std::equal_to<>>; + using ChannelTableType = std::unordered_map; + using UserTableType = std::unordered_map, jessilib::text_hashi, jessilib::text_equali>; /** * @brief Returns the name of the primary config section this client reads from. diff --git a/src/jessilib b/src/jessilib index bce3bfe..8d3efe0 160000 --- a/src/jessilib +++ b/src/jessilib @@ -1 +1 @@ -Subproject commit bce3bfefc6323754656d8153630b80155e4644e2 +Subproject commit 8d3efe083522c94a0dcfd31f34a07130b824f215