From 51ef8c7a10f3fb15618174df794d4db0e6320788 Mon Sep 17 00:00:00 2001 From: Tudor Bosman Date: Thu, 12 Jul 2012 16:33:38 -0700 Subject: [PATCH] Range -> Range 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 --- folly/Range.h | 12 ++++++++++++ folly/test/RangeTest.cpp | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/folly/Range.h b/folly/Range.h index 350183a5..9ddf0452 100644 --- a/folly/Range.h +++ b/folly/Range.h @@ -25,6 +25,7 @@ #include #include #include +#include #include #include #include @@ -184,6 +185,16 @@ public: e_ = b_ + size; } + // Allow implicit conversion from Range (aka StringPiece) to + // Range (aka ByteRange), as they're both frequently + // used to represent ranges of bytes. + template ::value), int>::type = 0> + /* implicit */ Range(const Range& other) + : b_(reinterpret_cast(other.begin())), + e_(reinterpret_cast(other.end())) { + } + void clear() { b_ = Iter(); e_ = Iter(); @@ -373,6 +384,7 @@ Range makeRange(Iter first, Iter last) { } typedef Range StringPiece; +typedef Range ByteRange; std::ostream& operator<<(std::ostream& os, const StringPiece& piece); diff --git a/folly/test/RangeTest.cpp b/folly/test/RangeTest.cpp index a98e142f..65ff9e04 100644 --- a/folly/test/RangeTest.cpp +++ b/folly/test/RangeTest.cpp @@ -138,3 +138,12 @@ TEST(StringPiece, All) { EXPECT_EQ(s, s2); EXPECT_EQ(s2, s); } + +TEST(StringPiece, ToByteRange) { + StringPiece a("hello"); + ByteRange b(a); + EXPECT_EQ(static_cast(a.begin()), + static_cast(b.begin())); + EXPECT_EQ(static_cast(a.end()), + static_cast(b.end())); +} -- 2.34.1