X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2FVarint.h;h=4664cb95d4f5c4e2aa33efdb95f3398199ff1b53;hp=5642f691ccda33a0c8460e2a1ea0920a5d53d081;hb=ee080728f82d00c1cadb9a0545da49b4a73749fa;hpb=a11dc9b7dfda07a11b702ef27578feb391caf811 diff --git a/folly/Varint.h b/folly/Varint.h index 5642f691..4664cb95 100644 --- a/folly/Varint.h +++ b/folly/Varint.h @@ -1,5 +1,5 @@ /* - * Copyright 2014 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,10 +14,10 @@ * limitations under the License. */ -#ifndef FOLLY_VARINT_H_ -#define FOLLY_VARINT_H_ +#pragma once #include +#include #include namespace folly { @@ -71,7 +71,8 @@ uint64_t decodeVarint(Range& data); inline uint64_t encodeZigZag(int64_t val) { // Bit-twiddling magic stolen from the Google protocol buffer document; // val >> 63 is an arithmetic shift because val is signed - return static_cast((val << 1) ^ (val >> 63)); + auto uval = static_cast(val); + return static_cast((uval << 1) ^ (val >> 63)); } inline int64_t decodeZigZag(uint64_t val) { @@ -86,8 +87,8 @@ inline size_t encodeVarint(uint64_t val, uint8_t* buf) { *p++ = 0x80 | (val & 0x7f); val >>= 7; } - *p++ = val; - return p - buf; + *p++ = uint8_t(val); + return size_t(p - buf); } template @@ -126,15 +127,14 @@ inline uint64_t decodeVarint(Range& data) { } if (p == end) { throw std::invalid_argument("Invalid varint value. Too small: " + - std::to_string(end - begin) + " bytes"); + folly::to(end - begin) + + " bytes"); } val |= static_cast(*p++) << shift; } - data.advance(p - begin); + data.uncheckedAdvance(p - begin); return val; } } // namespaces - -#endif /* FOLLY_VARINT_H_ */