Commit 998f6331 authored by Giuseppe Ottaviano's avatar Giuseppe Ottaviano Committed by Facebook Github Bot

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
parent c85f38c3
......@@ -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);
......
......@@ -133,7 +133,7 @@ inline uint64_t decodeVarint(Range<T*>& data) {
val |= static_cast<uint64_t>(*p++) << shift;
}
data.advance(p - begin);
data.uncheckedAdvance(p - begin);
return val;
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment