Skip to content

Commit

Permalink
substr: add interop with nullptr_t
Browse files Browse the repository at this point in the history
  • Loading branch information
biojppm committed Sep 26, 2020
1 parent 4e17147 commit 1ff3b64
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/c4/substr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,11 @@ struct C4CORE_EXPORT basic_substring

constexpr basic_substring(basic_substring const&) = default;
constexpr basic_substring(basic_substring &&) = default;
constexpr basic_substring(std::nullptr_t) : str(nullptr), len(0) {}

basic_substring& operator= (basic_substring const&) = default;
basic_substring& operator= (basic_substring &&) = default;
basic_substring& operator= (std::nullptr_t) { str = nullptr; len = 0; return *this; }

public:

Expand Down Expand Up @@ -223,6 +225,9 @@ struct C4CORE_EXPORT basic_substring
bool operator<= (ro_substr const that) const { return this->compare(that) <= 0; }
bool operator>= (ro_substr const that) const { return this->compare(that) >= 0; }

bool operator== (std::nullptr_t) const { return str == nullptr; }
bool operator!= (std::nullptr_t) const { return str != nullptr; }

public:

/** true if *this is a substring of that (ie, from the same buffer) */
Expand Down
39 changes: 39 additions & 0 deletions test/substr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,45 @@ TEST(csubstr, ctor_from_char)
EXPECT_EQ(s, "{foo: 2}");
}

TEST(csubstr, empty_vs_null)
{
csubstr s;
EXPECT_TRUE(s.empty());
EXPECT_TRUE(s.len == 0);
EXPECT_TRUE(s.str == nullptr);
EXPECT_TRUE(s == nullptr);

s = "";
EXPECT_TRUE(s.empty());
EXPECT_TRUE(s.len == 0);
EXPECT_TRUE(s.str != nullptr);
EXPECT_TRUE(s != nullptr);

s = nullptr;
EXPECT_TRUE(s.empty());
EXPECT_TRUE(s.len == 0);
EXPECT_TRUE(s.str == nullptr);
EXPECT_TRUE(s == nullptr);

s = "";
EXPECT_TRUE(s.empty());
EXPECT_TRUE(s.len == 0);
EXPECT_TRUE(s.str != nullptr);
EXPECT_TRUE(s != nullptr);

s = {};
EXPECT_TRUE(s.empty());
EXPECT_TRUE(s.len == 0);
EXPECT_TRUE(s.str == nullptr);
EXPECT_TRUE(s == nullptr);

csubstr pp(nullptr);
EXPECT_TRUE(pp.empty());
EXPECT_TRUE(pp.len == 0);
EXPECT_TRUE(pp.str == nullptr);
EXPECT_TRUE(pp == nullptr);
}

TEST(substr, contains)
{
csubstr buf = "0123456789";
Expand Down

0 comments on commit 1ff3b64

Please sign in to comment.