#include <iostream>
#include <string>
#include <stdexcept>
+#include <type_traits>
#include <boost/operators.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits.hpp>
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() {
b_ = Iter();
e_ = Iter();
}
typedef Range<const char*> StringPiece;
+typedef Range<const unsigned char*> ByteRange;
std::ostream& operator<<(std::ostream& os, const StringPiece& piece);
EXPECT_EQ(s, s2);
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()));
+}