Commit f972b1ee authored by Eric Niebler's avatar Eric Niebler Committed by Facebook Github Bot

Add constexpr to the simple folly::Range accessors (begin, end, data, empty, etc)

Summary: More constexpr is better!

Reviewed By: yfeldblum, luciang, ot

Differential Revision: D4244996

fbshipit-source-id: 30a9b726c115a92bb18538d7f18e50eccb0a1ef6
parent 59a501cf
...@@ -363,16 +363,30 @@ public: ...@@ -363,16 +363,30 @@ public:
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71448 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71448
return e_ - b_; return e_ - b_;
} }
size_type walk_size() const { constexpr size_type walk_size() const {
return std::distance(b_, e_); return std::distance(b_, e_);
} }
bool empty() const { return b_ == e_; } constexpr bool empty() const {
Iter data() const { return b_; } return b_ == e_;
Iter start() const { return b_; } }
Iter begin() const { return b_; } constexpr Iter data() const {
Iter end() const { return e_; } return b_;
Iter cbegin() const { return b_; } }
Iter cend() const { return e_; } constexpr Iter start() const {
return b_;
}
constexpr Iter begin() const {
return b_;
}
constexpr Iter end() const {
return e_;
}
constexpr Iter cbegin() const {
return b_;
}
constexpr Iter cend() const {
return e_;
}
value_type& front() { value_type& front() {
assert(b_ < e_); assert(b_ < e_);
return *b_; return *b_;
......
...@@ -1297,3 +1297,15 @@ TEST(Range, Constructors) { ...@@ -1297,3 +1297,15 @@ TEST(Range, Constructors) {
EXPECT_EQ(subpiece1.begin(), subpiece2.begin()); EXPECT_EQ(subpiece1.begin(), subpiece2.begin());
EXPECT_EQ(subpiece1.end(), subpiece2.end()); EXPECT_EQ(subpiece1.end(), subpiece2.end());
} }
TEST(Range, ConstexprAccessors) {
constexpr StringPiece piece = range("hello");
static_assert(piece.size() == 6u, "");
static_assert(piece.end() - piece.begin() == 6u, "");
static_assert(piece.data() == piece.begin(), "");
static_assert(piece.start() == piece.begin(), "");
static_assert(piece.cbegin() == piece.begin(), "");
static_assert(piece.cend() == piece.end(), "");
static_assert(*piece.begin() == 'h', "");
static_assert(*(piece.end() - 1) == '\0', "");
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment