Commit 51ef8c7a authored by Tudor Bosman's avatar Tudor Bosman

Range<const char*> -> Range<const unsigned char*> implicit conversion

Summary: As they can both be used to represent ranges of bytes.

Test Plan: test added

Reviewed By: andrei.alexandrescu@fb.com

FB internal diff: D518666
parent d50251a5
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <stdexcept> #include <stdexcept>
#include <type_traits>
#include <boost/operators.hpp> #include <boost/operators.hpp>
#include <boost/utility/enable_if.hpp> #include <boost/utility/enable_if.hpp>
#include <boost/type_traits.hpp> #include <boost/type_traits.hpp>
...@@ -184,6 +185,16 @@ public: ...@@ -184,6 +185,16 @@ public:
e_ = b_ + size; e_ = b_ + size;
} }
// Allow implicit conversion from Range<const char*> (aka StringPiece) to
// Range<const unsigned char*> (aka ByteRange), as they're both frequently
// used to represent ranges of bytes.
template <typename std::enable_if<
(std::is_same<Iter, const unsigned char*>::value), int>::type = 0>
/* implicit */ Range(const Range<const char*>& other)
: b_(reinterpret_cast<const unsigned char*>(other.begin())),
e_(reinterpret_cast<const unsigned char*>(other.end())) {
}
void clear() { void clear() {
b_ = Iter(); b_ = Iter();
e_ = Iter(); e_ = Iter();
...@@ -373,6 +384,7 @@ Range<Iter> makeRange(Iter first, Iter last) { ...@@ -373,6 +384,7 @@ Range<Iter> makeRange(Iter first, Iter last) {
} }
typedef Range<const char*> StringPiece; typedef Range<const char*> StringPiece;
typedef Range<const unsigned char*> ByteRange;
std::ostream& operator<<(std::ostream& os, const StringPiece& piece); std::ostream& operator<<(std::ostream& os, const StringPiece& piece);
......
...@@ -138,3 +138,12 @@ TEST(StringPiece, All) { ...@@ -138,3 +138,12 @@ TEST(StringPiece, All) {
EXPECT_EQ(s, s2); EXPECT_EQ(s, s2);
EXPECT_EQ(s2, s); EXPECT_EQ(s2, s);
} }
TEST(StringPiece, ToByteRange) {
StringPiece a("hello");
ByteRange b(a);
EXPECT_EQ(static_cast<const void*>(a.begin()),
static_cast<const void*>(b.begin()));
EXPECT_EQ(static_cast<const void*>(a.end()),
static_cast<const void*>(b.end()));
}
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