From 0b028d4ae1effbaf275b99d6bf0608429d9d4434 Mon Sep 17 00:00:00 2001 From: Giuseppe Ottaviano Date: Fri, 19 Dec 2014 09:50:57 -0800 Subject: [PATCH] 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 --- folly/Range.h | 31 +++++++++++++------------------ folly/test/RangeTest.cpp | 12 ++++++++++++ 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/folly/Range.h b/folly/Range.h index dccb92d0..f22d30e9 100644 --- a/folly/Range.h +++ b/folly/Range.h @@ -213,6 +213,7 @@ public: b_ = str.data() + startFrom; e_ = str.data() + str.size(); } + template ::const_type = 0> Range(const std::string& str, std::string::size_type startFrom, @@ -227,23 +228,17 @@ public: e_ = b_ + size; } } - template ::type = 0> - Range(const Range& str, - size_t startFrom, - size_t size) { - if (UNLIKELY(startFrom > str.size())) { - 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; - } - } + + Range(const Range& other, + size_type first, + size_type length = npos) + : Range(other.subpiece(first, length)) + { } + template ::const_type = 0> /* implicit */ Range(const fbstring& str) : b_(str.data()), e_(b_ + str.size()) { } + template ::const_type = 0> Range(const fbstring& str, fbstring::size_type startFrom) { if (UNLIKELY(startFrom > str.size())) { @@ -252,6 +247,7 @@ public: b_ = str.data() + startFrom; e_ = str.data() + str.size(); } + template ::const_type = 0> Range(const fbstring& str, fbstring::size_type startFrom, fbstring::size_type size) { @@ -452,13 +448,12 @@ public: --e_; } - Range subpiece(size_type first, - size_type length = std::string::npos) const { + 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)); + + return Range(b_ + first, std::min(length, size() - first)); } // string work-alike functions diff --git a/folly/test/RangeTest.cpp b/folly/test/RangeTest.cpp index 29ea85bf..02568dbc 100644 --- a/folly/test/RangeTest.cpp +++ b/folly/test/RangeTest.cpp @@ -1177,3 +1177,15 @@ TEST(ReplaceAll, BadArg) { EXPECT_EQ(count, 2); } + +TEST(Range, Constructors) { + vector c = {1, 2, 3}; + typedef Range::iterator> RangeType; + typedef Range::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()); +} -- 2.34.1