Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions include/beman/span/span.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,19 @@ class span {
return {data_ + offset, count == dynamic_extent ? size() - offset : count};
}

template <std::size_t E = Extent, std::enable_if_t<E == dynamic_extent, int> = 0>
constexpr void remove_prefix(size_type n) noexcept {
assert(n <= size());
data_ += n;
size_ -= n;
}

template <std::size_t E = Extent, std::enable_if_t<E == dynamic_extent, int> = 0>
Comment thread
ssam18 marked this conversation as resolved.
Outdated
constexpr void remove_suffix(size_type n) noexcept {
assert(n <= size());
size_ -= n;
}

// 26.7.3.4 Observers [span.obs]

[[nodiscard]] constexpr size_type size() const noexcept { return size_; }
Expand Down
95 changes: 95 additions & 0 deletions tests/beman/span/span.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <array>
#include <cstddef>
#include <numeric>
#include <type_traits>
#include <utility>
#include <vector>

namespace bsp = beman::span;
Expand Down Expand Up @@ -366,6 +368,99 @@ TEST(SpanSubviews, subspan_dynamic_to_end) {
EXPECT_EQ(sub[1], 4);
}

// ---------------------------------------------------------------------------
// remove_prefix / remove_suffix (P3729)
// ---------------------------------------------------------------------------

TEST(SpanModifiers, remove_prefix_zero_is_noop) {
int arr[] = {1, 2, 3, 4, 5};
bsp::span<int> s(arr);
s.remove_prefix(0);
EXPECT_EQ(s.size(), 5u);
EXPECT_EQ(s.data(), arr);
}

TEST(SpanModifiers, remove_prefix_partial) {
int arr[] = {1, 2, 3, 4, 5};
bsp::span<int> s(arr);
s.remove_prefix(2);
EXPECT_EQ(s.size(), 3u);
EXPECT_EQ(s.data(), arr + 2);
EXPECT_EQ(s[0], 3);
EXPECT_EQ(s[2], 5);
}

TEST(SpanModifiers, remove_prefix_full_empties_span) {
int arr[] = {1, 2, 3};
bsp::span<int> s(arr);
s.remove_prefix(s.size());
EXPECT_EQ(s.size(), 0u);
EXPECT_TRUE(s.empty());
}

TEST(SpanModifiers, remove_suffix_zero_is_noop) {
int arr[] = {1, 2, 3, 4, 5};
bsp::span<int> s(arr);
s.remove_suffix(0);
EXPECT_EQ(s.size(), 5u);
EXPECT_EQ(s.data(), arr);
}

TEST(SpanModifiers, remove_suffix_partial) {
int arr[] = {1, 2, 3, 4, 5};
bsp::span<int> s(arr);
s.remove_suffix(2);
EXPECT_EQ(s.size(), 3u);
EXPECT_EQ(s.data(), arr);
EXPECT_EQ(s[0], 1);
EXPECT_EQ(s[2], 3);
}

TEST(SpanModifiers, remove_suffix_full_empties_span) {
int arr[] = {1, 2, 3};
bsp::span<int> s(arr);
s.remove_suffix(s.size());
EXPECT_EQ(s.size(), 0u);
EXPECT_TRUE(s.empty());
}

TEST(SpanModifiers, remove_prefix_and_suffix_combined) {
int arr[] = {10, 20, 30, 40, 50, 60};
bsp::span<int> s(arr);
s.remove_prefix(1);
s.remove_suffix(2);
EXPECT_EQ(s.size(), 3u);
EXPECT_EQ(s[0], 20);
EXPECT_EQ(s[2], 40);
}

namespace modifiers_detail {
template <class T, class = void>
struct has_remove_prefix : std::false_type {};

template <class T>
struct has_remove_prefix<
T,
std::void_t<decltype(std::declval<T&>().remove_prefix(std::size_t{0}))>>
: std::true_type {};

template <class T, class = void>
struct has_remove_suffix : std::false_type {};

template <class T>
struct has_remove_suffix<
T,
std::void_t<decltype(std::declval<T&>().remove_suffix(std::size_t{0}))>>
: std::true_type {};
} // namespace modifiers_detail

TEST(SpanModifiers, fixed_extent_has_no_modifiers) {
static_assert(modifiers_detail::has_remove_prefix<bsp::span<int>>::value);
static_assert(modifiers_detail::has_remove_suffix<bsp::span<int>>::value);
static_assert(!modifiers_detail::has_remove_prefix<bsp::span<int, 3>>::value);
static_assert(!modifiers_detail::has_remove_suffix<bsp::span<int, 3>>::value);
}

// ---------------------------------------------------------------------------
// as_bytes / as_writable_bytes
// ---------------------------------------------------------------------------
Expand Down