mirror of
https://github.com/bkaradzic/bx.git
synced 2026-10-22 08:41:00 +00:00
Make StringView's copy constructor and assignment trivial. (#433)
This commit is contained in:
committed by
GitHub
parent
58d36bd042
commit
279a4f84cd
@@ -85,11 +85,6 @@ namespace bx
|
||||
{
|
||||
}
|
||||
|
||||
inline constexpr StringView::StringView(const StringView& _rhs)
|
||||
{
|
||||
set(_rhs);
|
||||
}
|
||||
|
||||
inline constexpr StringView::StringView(const StringView& _rhs, int32_t _start, int32_t _len)
|
||||
{
|
||||
set(_rhs, _start, _len);
|
||||
@@ -101,12 +96,6 @@ namespace bx
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline constexpr StringView& StringView::operator=(const StringView& _rhs)
|
||||
{
|
||||
set(_rhs);
|
||||
return *this;
|
||||
}
|
||||
|
||||
inline constexpr StringView::StringView(const char* _ptr)
|
||||
{
|
||||
set(_ptr, INT32_MAX);
|
||||
|
||||
@@ -70,7 +70,7 @@ namespace bx
|
||||
constexpr StringView(const StringLiteral& _str);
|
||||
|
||||
///
|
||||
constexpr StringView(const StringView& _rhs);
|
||||
constexpr StringView(const StringView& _rhs) = default;
|
||||
|
||||
///
|
||||
constexpr StringView(const StringView& _rhs, int32_t _start, int32_t _len);
|
||||
@@ -79,7 +79,7 @@ namespace bx
|
||||
constexpr StringView& operator=(const char* _rhs);
|
||||
|
||||
///
|
||||
constexpr StringView& operator=(const StringView& _rhs);
|
||||
constexpr StringView& operator=(const StringView& _rhs) = default;
|
||||
|
||||
///
|
||||
constexpr StringView(const char* _ptr);
|
||||
|
||||
@@ -953,3 +953,35 @@ TEST_CASE("formatHumanNumber", "[string]")
|
||||
|
||||
REQUIRE(testFormatHumanNumber("", 1389983113.891389, 2, 4) );
|
||||
}
|
||||
|
||||
TEST_CASE("StringView is trivially copyable", "[string]")
|
||||
{
|
||||
static_assert(bx::isTriviallyCopyable<bx::StringView>(), "StringView must stay trivially copyable");
|
||||
|
||||
union Variant
|
||||
{
|
||||
struct { bx::StringView name; } named;
|
||||
struct { int32_t value; } scalar;
|
||||
};
|
||||
|
||||
struct Node
|
||||
{
|
||||
int32_t kind;
|
||||
bx::StringView span;
|
||||
Variant variant;
|
||||
};
|
||||
|
||||
static_assert(bx::isTriviallyCopyable<Variant>(), "a union holding a StringView must stay copyable");
|
||||
|
||||
const Node node =
|
||||
{
|
||||
.kind = 7,
|
||||
.span = bx::StringView("span"),
|
||||
.variant = { .named = { .name = bx::StringView("hello") } },
|
||||
};
|
||||
|
||||
const Node copy = node;
|
||||
REQUIRE(7 == copy.kind);
|
||||
REQUIRE(0 == bx::strCmp(copy.span, "span") );
|
||||
REQUIRE(0 == bx::strCmp(copy.variant.named.name, "hello") );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user