Skip to content

Commit

Permalink
simplify slice some more
Browse files Browse the repository at this point in the history
  • Loading branch information
neheb committed Jul 10, 2023
1 parent 7477293 commit b5aaabb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
33 changes: 10 additions & 23 deletions include/exiv2/slice.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ struct ConstSliceBase : SliceBase {
if (new_end > this->end_) {
throw std::out_of_range("Invalid input parameters to slice");
}
return slice_type(storage_.data_, new_begin, new_end);
return {storage_.data_, new_begin, new_end};
}

protected:
Expand Down Expand Up @@ -227,7 +227,7 @@ struct MutableSliceBase : public ConstSliceBase<storage_type, data_type> {
* mutable_slice_base.
*/
template <typename slice_type>
slice_type subSlice(size_t begin, size_t end) {
[[nodiscard]] slice_type subSlice(size_t begin, size_t end) {
this->rangeCheck(begin);
// end == size() is a legal value, since end is the first
// element beyond the slice
Expand All @@ -241,7 +241,7 @@ struct MutableSliceBase : public ConstSliceBase<storage_type, data_type> {
if (new_end > this->end_) {
throw std::out_of_range("Invalid input parameters to slice");
}
return slice_type(this->storage_.data_, new_begin, new_end);
return {this->storage_.data_, new_begin, new_end};
}
};

Expand Down Expand Up @@ -429,24 +429,11 @@ struct Slice : public Internal::MutableSliceBase<Internal::ContainerStorage, con
using value_type = typename std::remove_cv<typename container::value_type>::type;
#endif

/*!
* Construct a sub-slice of this slice with the given bounds. The bounds
* are evaluated with respect to the current slice.
*
* @param[in] begin First element in the new slice.
* @param[in] end First element beyond the new slice.
*
* @throw std::out_of_range when begin or end are invalid
*/
Slice subSlice(size_t begin, size_t end) {
return Internal::MutableSliceBase<Internal::ContainerStorage, container>::template subSlice<Slice>(begin, end);
}

/*!
* Constructs a new constant subSlice. Behaves otherwise exactly like
* the non-const version.
*/
[[nodiscard]] Slice<const container> subSlice(size_t begin, size_t end) const {
Slice<const container> subSlice(size_t begin, size_t end) const {
return this->to_const_base().template subSlice<Slice<const container>>(begin, end);
}
};
Expand Down Expand Up @@ -531,23 +518,23 @@ struct Slice<T*> : public Internal::MutableSliceBase<Internal::PtrSliceStorage,
*/
template <typename T>
inline Slice<T> makeSlice(T& cont, size_t begin, size_t end) {
return Slice<T>(cont, begin, end);
return {cont, begin, end};
}

/*!
* Overload of makeSlice for slices of C-arrays.
*/
template <typename T>
inline Slice<T*> makeSlice(T* ptr, size_t begin, size_t end) {
return Slice<T*>(ptr, begin, end);
return {ptr, begin, end};
}

/*!
* @brief Return a new slice spanning the whole container.
*/
template <typename container>
inline Slice<container> makeSlice(container& cont) {
return Slice<container>(cont, 0, cont.size());
return {cont, 0, cont.size()};
}

/*!
Expand All @@ -556,23 +543,23 @@ inline Slice<container> makeSlice(container& cont) {
*/
template <typename container>
inline Slice<container> makeSliceFrom(container& cont, size_t begin) {
return Slice<container>(cont, begin, cont.size());
return {cont, begin, cont.size()};
}

/*!
* @brief Return a new slice spanning until `end`.
*/
template <typename container>
inline Slice<container> makeSliceUntil(container& cont, size_t end) {
return Slice<container>(cont, 0, end);
return {cont, 0, end};
}

/*!
* Overload of makeSliceUntil for pointer based slices.
*/
template <typename T>
inline Slice<T*> makeSliceUntil(T* ptr, size_t end) {
return Slice<T*>(ptr, 0, end);
return {ptr, 0, end};
}

} // namespace Exiv2
Expand Down
2 changes: 1 addition & 1 deletion unitTests/test_slice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ TYPED_TEST_P(slice, subSliceSuccessfulConstruction) {
}

TYPED_TEST_P(slice, subSliceFunctions) {
Slice<TypeParam> middle = this->getTestSlice(3, 7).subSlice(1, 3);
auto middle = this->getTestSlice(3, 7).subSlice(1, 3);

ASSERT_EQ(middle.size(), static_cast<size_t>(2));
ASSERT_EQ(middle.at(1), static_cast<typename Slice<TypeParam>::value_type>(5));
Expand Down

0 comments on commit b5aaabb

Please sign in to comment.