Commit 0b028d4a authored by Giuseppe Ottaviano's avatar Giuseppe Ottaviano Committed by Dave Watson

Remove unnecessary constraint from Range subpiece constructor

Summary:
D1746899 enforced the constraint (previously in a comment) on the
constructor `Range(const Range&, size_t, size_t)` that `Iter` is a
`const char*`. There is however no reason for this constraint.

This patch generalizes and simplifies the constructor, and since it
has the same semantics as `subpiece`, the latter is implemented in
terms of the constructor.

Test Plan:
fbconfig -r folly && fbmake runtests_opt

Reviewed By: soren@fb.com

Subscribers: trunkagent, folly-diffs@

FB internal diff: D1747958

Signature: t1:1747958:1418930360:fcd6beeda34e64ec8a34b9491a57674ae2265596
parent 580fc806
...@@ -213,6 +213,7 @@ public: ...@@ -213,6 +213,7 @@ public:
b_ = str.data() + startFrom; b_ = str.data() + startFrom;
e_ = str.data() + str.size(); e_ = str.data() + str.size();
} }
template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0> template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0>
Range(const std::string& str, Range(const std::string& str,
std::string::size_type startFrom, std::string::size_type startFrom,
...@@ -227,23 +228,17 @@ public: ...@@ -227,23 +228,17 @@ public:
e_ = b_ + size; e_ = b_ + size;
} }
} }
template <class T = Iter, typename detail::IsCharPointer<T>::type = 0>
Range(const Range<Iter>& str, Range(const Range& other,
size_t startFrom, size_type first,
size_t size) { size_type length = npos)
if (UNLIKELY(startFrom > str.size())) { : Range(other.subpiece(first, length))
throw std::out_of_range("index out of range"); { }
}
b_ = str.b_ + startFrom;
if (str.size() - startFrom < size) {
e_ = str.e_;
} else {
e_ = b_ + size;
}
}
template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0> template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0>
/* implicit */ Range(const fbstring& str) /* implicit */ Range(const fbstring& str)
: b_(str.data()), e_(b_ + str.size()) { } : b_(str.data()), e_(b_ + str.size()) { }
template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0> template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0>
Range(const fbstring& str, fbstring::size_type startFrom) { Range(const fbstring& str, fbstring::size_type startFrom) {
if (UNLIKELY(startFrom > str.size())) { if (UNLIKELY(startFrom > str.size())) {
...@@ -252,6 +247,7 @@ public: ...@@ -252,6 +247,7 @@ public:
b_ = str.data() + startFrom; b_ = str.data() + startFrom;
e_ = str.data() + str.size(); e_ = str.data() + str.size();
} }
template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0> template <class T = Iter, typename detail::IsCharPointer<T>::const_type = 0>
Range(const fbstring& str, fbstring::size_type startFrom, Range(const fbstring& str, fbstring::size_type startFrom,
fbstring::size_type size) { fbstring::size_type size) {
...@@ -452,13 +448,12 @@ public: ...@@ -452,13 +448,12 @@ public:
--e_; --e_;
} }
Range subpiece(size_type first, Range subpiece(size_type first, size_type length = npos) const {
size_type length = std::string::npos) const {
if (UNLIKELY(first > size())) { if (UNLIKELY(first > size())) {
throw std::out_of_range("index out of range"); throw std::out_of_range("index out of range");
} }
return Range(b_ + first,
std::min<std::string::size_type>(length, size() - first)); return Range(b_ + first, std::min(length, size() - first));
} }
// string work-alike functions // string work-alike functions
......
...@@ -1177,3 +1177,15 @@ TEST(ReplaceAll, BadArg) { ...@@ -1177,3 +1177,15 @@ TEST(ReplaceAll, BadArg) {
EXPECT_EQ(count, 2); EXPECT_EQ(count, 2);
} }
TEST(Range, Constructors) {
vector<int> c = {1, 2, 3};
typedef Range<vector<int>::iterator> RangeType;
typedef Range<vector<int>::const_iterator> ConstRangeType;
RangeType cr(c.begin(), c.end());
auto subpiece1 = ConstRangeType(cr, 1, 5);
auto subpiece2 = ConstRangeType(cr, 1);
EXPECT_EQ(subpiece1.size(), 2);
EXPECT_EQ(subpiece1.begin(), subpiece2.begin());
EXPECT_EQ(subpiece1.end(), subpiece2.end());
}
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