Bläddra i källkod

hashed string: support for string views

Michele Caini 6 år sedan
förälder
incheckning
de737fc72d
2 ändrade filer med 24 tillägg och 5 borttagningar
  1. 16 5
      src/entt/core/hashed_string.hpp
  2. 8 0
      test/entt/core/hashed_string.cpp

+ 16 - 5
src/entt/core/hashed_string.hpp

@@ -101,9 +101,21 @@ public:
         return helper(traits_type::offset, wrapper.str);
         return helper(traits_type::offset, wrapper.str);
     }
     }
 
 
+    /**
+     * @brief Returns directly the numeric representation of a string view.
+     * @param str Human-readable identifer.
+     * @param size Length of the string to hash.
+     * @return The numeric representation of the string.
+     */
+    inline static hash_type to_value(const char *str, std::size_t size) ENTT_NOEXCEPT {
+        ENTT_ID_TYPE partial{traits_type::offset};
+        while(size--) { partial = (partial^(str++)[0])*traits_type::prime; }
+        return partial;
+    }
+
     /*! @brief Constructs an empty hashed string. */
     /*! @brief Constructs an empty hashed string. */
     constexpr hashed_string() ENTT_NOEXCEPT
     constexpr hashed_string() ENTT_NOEXCEPT
-        : hash{}, str{nullptr}
+        : str{nullptr}, hash{}
     {}
     {}
 
 
     /**
     /**
@@ -122,17 +134,16 @@ public:
      */
      */
     template<std::size_t N>
     template<std::size_t N>
     constexpr hashed_string(const char (&curr)[N]) ENTT_NOEXCEPT
     constexpr hashed_string(const char (&curr)[N]) ENTT_NOEXCEPT
-        : hash{helper(traits_type::offset, curr)}, str{curr}
+        : str{curr}, hash{helper(traits_type::offset, curr)}
     {}
     {}
 
 
     /**
     /**
      * @brief Explicit constructor on purpose to avoid constructing a hashed
      * @brief Explicit constructor on purpose to avoid constructing a hashed
      * string directly from a `const char *`.
      * string directly from a `const char *`.
-     *
      * @param wrapper Helps achieving the purpose by relying on overloading.
      * @param wrapper Helps achieving the purpose by relying on overloading.
      */
      */
     explicit constexpr hashed_string(const_wrapper wrapper) ENTT_NOEXCEPT
     explicit constexpr hashed_string(const_wrapper wrapper) ENTT_NOEXCEPT
-        : hash{helper(traits_type::offset, wrapper.str)}, str{wrapper.str}
+        : str{wrapper.str}, hash{helper(traits_type::offset, wrapper.str)}
     {}
     {}
 
 
     /**
     /**
@@ -170,8 +181,8 @@ public:
     }
     }
 
 
 private:
 private:
-    hash_type hash;
     const char *str;
     const char *str;
+    hash_type hash;
 };
 };
 
 
 
 

+ 8 - 0
test/entt/core/hashed_string.cpp

@@ -1,3 +1,5 @@
+#include <string>
+#include <string_view>
 #include <type_traits>
 #include <type_traits>
 #include <gtest/gtest.h>
 #include <gtest/gtest.h>
 #include <entt/core/hashed_string.hpp>
 #include <entt/core/hashed_string.hpp>
@@ -54,3 +56,9 @@ TEST(HashedString, ToValue) {
     // how would you test a constexpr otherwise?
     // how would you test a constexpr otherwise?
     (void)std::integral_constant<hash_type, entt::hashed_string::to_value("quux")>{};
     (void)std::integral_constant<hash_type, entt::hashed_string::to_value("quux")>{};
 }
 }
+
+TEST(HashedString, StringView) {
+    std::string str{"__foobar__"};
+    std::string_view view{str.data()+2, 6};
+    ASSERT_EQ(entt::hashed_string::to_value(view.data(), view.size()), 0xbf9cf968);
+}