From: Giuseppe Ottaviano Date: Wed, 28 Sep 2016 22:37:33 +0000 (-0700) Subject: Add unchecked versions of advance(), subtract(), and subpiece() in Range X-Git-Tag: v2016.10.03.00~9 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=998f63317f40d92d9e430a1c27ecd687b03c53e3;p=folly.git Add unchecked versions of advance(), subtract(), and subpiece() in Range Summary: `Range` has a somewhat inconsistent API: most methods assume argument validity as a precondition (mirroring their STL counterparts), others check the arguments and throw for invalid ones. Since `Range` is intended as a zero-cost abstraction on top of pointers/iterators, unchecked methods should be preferred. At this point however we cannot change the semantics of `advance()` and other methods. This diff adds new unchecked versions of these methods. Reviewed By: luciang Differential Revision: D3938480 fbshipit-source-id: 6952683ee0716aa1584e79584158fbf3e083b52e --- diff --git a/folly/Range.h b/folly/Range.h index 0b63f457..59e14bd5 100644 --- a/folly/Range.h +++ b/folly/Range.h @@ -465,6 +465,30 @@ public: e_ -= n; } + Range subpiece(size_type first, size_type length = npos) const { + if (UNLIKELY(first > size())) { + throw std::out_of_range("index out of range"); + } + + return Range(b_ + first, std::min(length, size() - first)); + } + + // unchecked versions + void uncheckedAdvance(size_type n) { + DCHECK_LE(n, size()); + b_ += n; + } + + void uncheckedSubtract(size_type n) { + DCHECK_LE(n, size()); + e_ -= n; + } + + Range uncheckedSubpiece(size_type first, size_type length = npos) const { + DCHECK_LE(first, size()); + return Range(b_ + first, std::min(length, size() - first)); + } + void pop_front() { assert(b_ < e_); ++b_; @@ -475,14 +499,6 @@ public: --e_; } - Range subpiece(size_type first, size_type length = npos) const { - if (UNLIKELY(first > size())) { - throw std::out_of_range("index out of range"); - } - - return Range(b_ + first, std::min(length, size() - first)); - } - // string work-alike functions size_type find(const_range_type str) const { return qfind(castToConst(), str); diff --git a/folly/Varint.h b/folly/Varint.h index e4af0c19..85953573 100644 --- a/folly/Varint.h +++ b/folly/Varint.h @@ -133,7 +133,7 @@ inline uint64_t decodeVarint(Range& data) { val |= static_cast(*p++) << shift; } - data.advance(p - begin); + data.uncheckedAdvance(p - begin); return val; }