Range() : Offset(0), Length(0) {}
Range(unsigned Offset, unsigned Length) : Offset(Offset), Length(Length) {}
+ /// \brief Accessors.
+ /// @{
unsigned getOffset() const { return Offset; }
unsigned getLength() const { return Length; }
+ /// @}
+
+ /// \name Range Predicates
+ /// @{
+ /// \brief Whether this range overlaps with \p RHS or not.
+ bool overlapsWith(Range RHS) const {
+ if ((Offset + Length) <= RHS.Offset || Offset >= (RHS.Offset + RHS.Length))
+ return false;
+ return true;
+ }
+
+ /// \brief Whether this range contains \p RHS or not.
+ bool contains(Range RHS) const {
+ return RHS.Offset >= Offset &&
+ (RHS.Offset + RHS.Length) <= (Offset + Length);
+ }
+ /// @}
private:
unsigned Offset;
expectReplacementAt(CallToF.Replace, "input.cc", 43, 8);
}
+TEST(Range, overlaps) {
+ EXPECT_TRUE(Range(10, 10).overlapsWith(Range(0, 11)));
+ EXPECT_TRUE(Range(0, 11).overlapsWith(Range(10, 10)));
+ EXPECT_FALSE(Range(10, 10).overlapsWith(Range(0, 10)));
+ EXPECT_FALSE(Range(0, 10).overlapsWith(Range(10, 10)));
+ EXPECT_TRUE(Range(0, 10).overlapsWith(Range(2, 6)));
+}
+
+TEST(Range, contains) {
+ EXPECT_TRUE(Range(0, 10).contains(Range(0, 10)));
+ EXPECT_TRUE(Range(0, 10).contains(Range(2, 6)));
+ EXPECT_FALSE(Range(2, 6).contains(Range(0, 10)));
+ EXPECT_FALSE(Range(0, 10).contains(Range(0, 11)));
+}
+
} // end namespace tooling
} // end namespace clang