Skip to content

Commit ce4b04f

Browse files
committed
feat: add std::string_view constructor to hashed_string
Adds a convenience constructor that accepts std::string_view directly, eliminating the need to manually pass .data() and .size(). This addresses the gap mentioned in PR #824 where implicit conversion to std::string_view was noted as desirable but not included. Changes: - Added basic_hashed_string(std::basic_string_view<value_type>) ctor - Added corresponding test case StringViewConstructor Example usage: std::string_view sv = "foobar"; auto hs = entt::hashed_string{sv}; // Cleaner! Related: PR #824
1 parent 1333fa5 commit ce4b04f

2 files changed

Lines changed: 19 additions & 0 deletions

File tree

src/entt/core/hashed_string.hpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#include <cstddef>
55
#include <cstdint>
6+
#include <string_view>
67
#include "fwd.hpp"
78

89
namespace entt {
@@ -125,6 +126,13 @@ class basic_hashed_string: internal::basic_hashed_string<Char> {
125126
// NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)
126127
}
127128

129+
/**
130+
* @brief Constructs a hashed string from a string view.
131+
* @param sv String view to hash.
132+
*/
133+
constexpr basic_hashed_string(std::basic_string_view<value_type> sv) noexcept
134+
: basic_hashed_string{sv.data(), sv.size()} {}
135+
128136
/**
129137
* @brief Constructs a hashed string from an array of const characters.
130138
* @tparam N Number of characters of the identifier.

test/entt/core/hashed_string.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ TEST_F(HashedString, Correctness) {
8585
ASSERT_EQ(entt::hashed_string{"foobar"}.size(), 6u);
8686
}
8787

88+
TEST_F(HashedString, StringViewConstructor) {
89+
const std::string_view view{"foobar"};
90+
const std::string_view view_sized{"foobar__", 6};
91+
92+
ASSERT_EQ(entt::hashed_string{view}, expected);
93+
ASSERT_EQ(entt::hashed_string{view_sized}, expected);
94+
ASSERT_EQ(entt::hashed_string{view}.size(), 6u);
95+
ASSERT_EQ(entt::hashed_string{view_sized}.size(), 6u);
96+
ASSERT_STREQ(entt::hashed_string{view}.data(), "foobar");
97+
}
98+
8899
TEST_F(HashedString, Order) {
89100
using namespace entt::literals;
90101
const entt::hashed_string lhs = "foo"_hs;

0 commit comments

Comments
 (0)