BLI: support constructing StringRef from start and end pointer
This commit is contained in:
parent
2d695367a7
commit
4c172f7ca6
|
@ -229,6 +229,16 @@ class StringRef : public StringRefBase {
|
|||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a StringRef from a start and end pointer. This invokes undefined behavior when the
|
||||
* second point points to a smaller address than the first one.
|
||||
*/
|
||||
StringRef(const char *begin, const char *one_after_end)
|
||||
: StringRefBase(begin, (uint)(one_after_end - begin))
|
||||
{
|
||||
BLI_assert(begin <= one_after_end);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reference a std::string. Remember that when the std::string is destructed, the StringRef
|
||||
* will point to uninitialized memory.
|
||||
|
|
|
@ -36,6 +36,30 @@ TEST(string_ref, DefaultConstructor)
|
|||
EXPECT_EQ(ref.size(), 0);
|
||||
}
|
||||
|
||||
TEST(string_ref, StartEndConstructor)
|
||||
{
|
||||
const char *text = "hello world";
|
||||
StringRef ref(text, text + 5);
|
||||
EXPECT_EQ(ref.size(), 5);
|
||||
EXPECT_TRUE(ref == "hello");
|
||||
EXPECT_FALSE(ref == "hello ");
|
||||
}
|
||||
|
||||
TEST(string_ref, StartEndConstructorNullptr)
|
||||
{
|
||||
StringRef ref(nullptr, nullptr);
|
||||
EXPECT_EQ(ref.size(), 0);
|
||||
EXPECT_TRUE(ref == "");
|
||||
}
|
||||
|
||||
TEST(string_ref, StartEndConstructorSame)
|
||||
{
|
||||
const char *text = "hello world";
|
||||
StringRef ref(text, text);
|
||||
EXPECT_EQ(ref.size(), 0);
|
||||
EXPECT_TRUE(ref == "");
|
||||
}
|
||||
|
||||
TEST(string_ref, CStringConstructor)
|
||||
{
|
||||
const char *str = "Test";
|
||||
|
|
Loading…
Reference in New Issue