Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 15 additions & 0 deletions include/beman/span/span.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,21 @@ class span {
return {data_ + offset, count == dynamic_extent ? size() - offset : count};
}

constexpr void remove_prefix(size_type n) noexcept
requires(Extent == dynamic_extent)
{
assert(n <= size());
data_ += n;
size_ -= n;
}

constexpr void remove_suffix(size_type n) noexcept
requires(Extent == dynamic_extent)
{
assert(n <= size());
size_ -= n;
}

// 26.7.3.4 Observers [span.obs]

[[nodiscard]] constexpr size_type size() const noexcept { return size_; }
Expand Down
103 changes: 97 additions & 6 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 @@ -203,29 +205,29 @@ TEST(SpanElementAccess, write_through_span) {

// at() bounds-checked access (P2821R5, C++26)
TEST(SpanAt, in_bounds_returns_element) {
int arr[] = {10, 20, 30};
int arr[] = {10, 20, 30};
bsp::span<int> s(arr);
EXPECT_EQ(s.at(0), 10);
EXPECT_EQ(s.at(1), 20);
EXPECT_EQ(s.at(2), 30);
}

TEST(SpanAt, returns_lvalue_reference_for_mutable_span) {
int arr[] = {1, 2, 3};
int arr[] = {1, 2, 3};
bsp::span<int> s(arr);
s.at(1) = 42;
EXPECT_EQ(arr[1], 42);
static_assert(std::is_same_v<decltype(s.at(0)), int&>);
}

TEST(SpanAt, throws_out_of_range_at_size_boundary) {
int arr[] = {1, 2, 3};
int arr[] = {1, 2, 3};
bsp::span<int> s(arr);
EXPECT_THROW(s.at(3), std::out_of_range);
}

TEST(SpanAt, throws_out_of_range_well_past_size) {
int arr[] = {1, 2, 3};
int arr[] = {1, 2, 3};
bsp::span<int> s(arr);
EXPECT_THROW(s.at(100), std::out_of_range);
}
Expand All @@ -236,14 +238,14 @@ TEST(SpanAt, empty_span_always_throws) {
}

TEST(SpanAt, fixed_extent_in_bounds_and_out_of_range) {
int arr[] = {7, 8, 9, 10};
int arr[] = {7, 8, 9, 10};
bsp::span<int, 4> s(arr);
EXPECT_EQ(s.at(3), 10);
EXPECT_THROW(s.at(4), std::out_of_range);
}

TEST(SpanAt, const_span_returns_reference_to_const) {
const int arr[] = {1, 2, 3};
const int arr[] = {1, 2, 3};
bsp::span<const int> s(arr);
EXPECT_EQ(s.at(2), 3);
static_assert(std::is_same_v<decltype(s.at(0)), const int&>);
Expand Down Expand Up @@ -413,6 +415,95 @@ 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