Skip to content

Add remove_prefix and remove_suffix (P3729)#8

Merged
ssam18 merged 3 commits intomainfrom
remove-prefix-suffix-p3729
May 1, 2026
Merged

Add remove_prefix and remove_suffix (P3729)#8
ssam18 merged 3 commits intomainfrom
remove-prefix-suffix-p3729

Conversation

@SamareshSingh
Copy link
Copy Markdown
Collaborator

@SamareshSingh SamareshSingh commented Apr 26, 2026

Adds the two in-place shrink operations from P3729 — same shape as string_view's methods and Abseil's Span.

constexpr void remove_prefix(size_type n) noexcept;  // dynamic-extent only
constexpr void remove_suffix(size_type n) noexcept;  // dynamic-extent only

Both are gated on Extent == dynamic_extent via the same enable_if_t trick the default constructor already uses. A fixed-extent span<T, N> carries its size in the type, so an in-place size mutation isn't representable, there is no sensible thing to do for the fixed case, and string_view (which has no fixed-extent equivalent) sets the precedent.

Eight new tests:

  • n == 0 no-op for both
  • partial shrink (size and data() pointer move correctly for prefix; data unchanged for suffix)
  • full shrink (remove_prefix(size()) / remove_suffix(size()) both empty the span)
  • prefix and suffix combined on the same span
  • a detection-idiom check that bsp::span<int> exposes both methods and bsp::span<int, 3> does not

Local: 53/53 passing.

Fixes #3

Adds the two mutating shrink operations to dynamic-extent span:

- remove_prefix(n): drops the first n elements
- remove_suffix(n): drops the last n elements

Both are gated to dynamic_extent only via the same SFINAE pattern the
default constructor uses. Fixed-extent spans carry their size in the type,
so an in-place size mutation isn't representable - matching string_view
(no fixed extent) and Abseil's Span (dynamic only).

Eight new tests cover n=0, partial shrink, full shrink to empty, both
operations combined, and a detection-idiom check confirming fixed-extent
spans do not expose these methods.

Fixes #3
Comment thread include/beman/span/span.hpp Outdated
@JeffGarland
Copy link
Copy Markdown
Member

Also, I don't know how you want to handle this -- fine as another issue, but the readme/examples should be updated to highlight the new features

ssam18 added 2 commits May 1, 2026 08:22
Addresses Jeff's review feedback on PR #8: Beman targets
C++20 as baseline, so the new modifiers should use a requires
clause rather than the SFINAE-style enable_if_t shim. Behavior is
identical (the existing void_t-based detection tests still pass),
just clearer at the point of declaration.

Also picks up the clang-format pass over the at() tests carried
in via the merge of main.
@coveralls
Copy link
Copy Markdown

Coverage Status

coverage: 100.0%. remained the same — remove-prefix-suffix-p3729 into main

@ssam18 ssam18 merged commit 0debd28 into main May 1, 2026
80 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

c++29 feature: remove_prefix and remove_suffix

4 participants