#ifndef FOLLY_VARINT_H_
#define FOLLY_VARINT_H_
+#include <type_traits>
#include <folly/Range.h>
namespace folly {
/**
* Decode a value from a given buffer, advances data past the returned value.
*/
-uint64_t decodeVarint(ByteRange& data);
+template <class T>
+uint64_t decodeVarint(Range<T*>& data);
/**
* ZigZag encoding that maps signed integers with a small absolute value
return p - buf;
}
-inline uint64_t decodeVarint(ByteRange& data) {
+template <class T>
+inline uint64_t decodeVarint(Range<T*>& data) {
+ static_assert(
+ std::is_same<typename std::remove_cv<T>::type, char>::value ||
+ std::is_same<typename std::remove_cv<T>::type, unsigned char>::value,
+ "Only character ranges are supported");
+
const int8_t* begin = reinterpret_cast<const int8_t*>(data.begin());
const int8_t* end = reinterpret_cast<const int8_t*>(data.end());
const int8_t* p = begin;
}
}
+TEST(Varint, Interface) {
+ // Make sure decodeVarint() accepts all of StringPiece, MutableStringPiece,
+ // ByteRange, and MutableByteRange.
+ char c = 0;
+
+ StringPiece sp(&c, 1);
+ EXPECT_EQ(decodeVarint(sp), 0);
+
+ MutableStringPiece msp(&c, 1);
+ EXPECT_EQ(decodeVarint(msp), 0);
+
+ ByteRange br(reinterpret_cast<unsigned char*>(&c), 1);
+ EXPECT_EQ(decodeVarint(br), 0);
+
+ MutableByteRange mbr(reinterpret_cast<unsigned char*>(&c), 1);
+ EXPECT_EQ(decodeVarint(mbr), 0);
+}
+
TEST(Varint, Simple) {
testVarint(0, {0});
testVarint(1, {1});