LCOV - code coverage report
Current view: top level - capy/buffers - span.hpp (source / functions) Coverage Total Hit
Test: coverage_filtered.info Lines: 100.0 % 50 50
Test Date: 2026-02-01 07:03:35 Functions: 100.0 % 8 8

            Line data    Source code
       1              : //
       2              : // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
       3              : //
       4              : // Distributed under the Boost Software License, Version 1.0. (See accompanying
       5              : // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
       6              : //
       7              : // Official repository: https://github.com/cppalliance/capy
       8              : //
       9              : 
      10              : #ifndef BOOST_CAPY_BUFFERS_SPAN_HPP
      11              : #define BOOST_CAPY_BUFFERS_SPAN_HPP
      12              : 
      13              : #include <boost/capy/detail/config.hpp>
      14              : #include <boost/capy/buffers.hpp>
      15              : #include <span>
      16              : 
      17              : namespace boost {
      18              : namespace capy {
      19              : 
      20              : /** Remove bytes from the beginning of a span of const buffers.
      21              : 
      22              :     Modifies the span and its buffer contents in-place to
      23              :     remove the first `n` bytes.
      24              : 
      25              :     @param bs The span to modify.
      26              :     @param n The number of bytes to remove.
      27              : */
      28              : inline
      29              : void
      30           12 : remove_span_prefix(
      31              :     std::span<const_buffer>& bs,
      32              :     std::size_t n) noexcept
      33              : {
      34           19 :     while(bs.size() > 0)
      35              :     {
      36           15 :         if(n < bs.front().size())
      37              :         {
      38            8 :             bs.front() += n;
      39            8 :             return;
      40              :         }
      41            7 :         n -= bs.front().size();
      42            7 :         bs = bs.subspan(1);
      43              :     }
      44              : }
      45              : 
      46              : /** Remove bytes from the beginning of a span of mutable buffers.
      47              : 
      48              :     Modifies the span and its buffer contents in-place to
      49              :     remove the first `n` bytes.
      50              : 
      51              :     @param bs The span to modify.
      52              :     @param n The number of bytes to remove.
      53              : */
      54              : inline
      55              : void
      56            4 : remove_span_prefix(
      57              :     std::span<mutable_buffer>& bs,
      58              :     std::size_t n) noexcept
      59              : {
      60            6 :     while(bs.size() > 0)
      61              :     {
      62            4 :         if(n < bs.front().size())
      63              :         {
      64            2 :             bs.front() += n;
      65            2 :             return;
      66              :         }
      67            2 :         n -= bs.front().size();
      68            2 :         bs = bs.subspan(1);
      69              :     }
      70              : }
      71              : 
      72              : /** Remove bytes from the end of a span of const buffers.
      73              : 
      74              :     Modifies the span and its buffer contents in-place to
      75              :     remove the last `n` bytes.
      76              : 
      77              :     @param bs The span to modify.
      78              :     @param n The number of bytes to remove.
      79              : */
      80              : inline
      81              : void
      82           12 : remove_span_suffix(
      83              :     std::span<const_buffer>& bs,
      84              :     std::size_t n) noexcept
      85              : {
      86           19 :     while(bs.size() > 0)
      87              :     {
      88           15 :         if(n < bs.back().size())
      89              :         {
      90            8 :             auto& b = bs.back();
      91            8 :             b = const_buffer(b.data(), b.size() - n);
      92            8 :             return;
      93              :         }
      94            7 :         n -= bs.back().size();
      95            7 :         bs = bs.subspan(0, bs.size() - 1);
      96              :     }
      97              : }
      98              : 
      99              : /** Remove bytes from the end of a span of mutable buffers.
     100              : 
     101              :     Modifies the span and its buffer contents in-place to
     102              :     remove the last `n` bytes.
     103              : 
     104              :     @param bs The span to modify.
     105              :     @param n The number of bytes to remove.
     106              : */
     107              : inline
     108              : void
     109            4 : remove_span_suffix(
     110              :     std::span<mutable_buffer>& bs,
     111              :     std::size_t n) noexcept
     112              : {
     113            6 :     while(bs.size() > 0)
     114              :     {
     115            4 :         if(n < bs.back().size())
     116              :         {
     117            2 :             auto& b = bs.back();
     118            2 :             b = mutable_buffer(b.data(), b.size() - n);
     119            2 :             return;
     120              :         }
     121            2 :         n -= bs.back().size();
     122            2 :         bs = bs.subspan(0, bs.size() - 1);
     123              :     }
     124              : }
     125              : 
     126              : /** Keep only the first bytes of a span of const buffers.
     127              : 
     128              :     Modifies the span and its buffer contents in-place to
     129              :     keep only the first `n` bytes.
     130              : 
     131              :     @param bs The span to modify.
     132              :     @param n The number of bytes to keep.
     133              : */
     134              : inline
     135              : void
     136            5 : keep_span_prefix(
     137              :     std::span<const_buffer>& bs,
     138              :     std::size_t n) noexcept
     139              : {
     140            5 :     auto total = buffer_size(bs);
     141            5 :     if(n < total)
     142            3 :         remove_span_suffix(bs, total - n);
     143            5 : }
     144              : 
     145              : /** Keep only the first bytes of a span of mutable buffers.
     146              : 
     147              :     Modifies the span and its buffer contents in-place to
     148              :     keep only the first `n` bytes.
     149              : 
     150              :     @param bs The span to modify.
     151              :     @param n The number of bytes to keep.
     152              : */
     153              : inline
     154              : void
     155            2 : keep_span_prefix(
     156              :     std::span<mutable_buffer>& bs,
     157              :     std::size_t n) noexcept
     158              : {
     159            2 :     auto total = buffer_size(bs);
     160            2 :     if(n < total)
     161            2 :         remove_span_suffix(bs, total - n);
     162            2 : }
     163              : 
     164              : /** Keep only the last bytes of a span of const buffers.
     165              : 
     166              :     Modifies the span and its buffer contents in-place to
     167              :     keep only the last `n` bytes.
     168              : 
     169              :     @param bs The span to modify.
     170              :     @param n The number of bytes to keep.
     171              : */
     172              : inline
     173              : void
     174            5 : keep_span_suffix(
     175              :     std::span<const_buffer>& bs,
     176              :     std::size_t n) noexcept
     177              : {
     178            5 :     auto total = buffer_size(bs);
     179            5 :     if(n < total)
     180            3 :         remove_span_prefix(bs, total - n);
     181            5 : }
     182              : 
     183              : /** Keep only the last bytes of a span of mutable buffers.
     184              : 
     185              :     Modifies the span and its buffer contents in-place to
     186              :     keep only the last `n` bytes.
     187              : 
     188              :     @param bs The span to modify.
     189              :     @param n The number of bytes to keep.
     190              : */
     191              : inline
     192              : void
     193            2 : keep_span_suffix(
     194              :     std::span<mutable_buffer>& bs,
     195              :     std::size_t n) noexcept
     196              : {
     197            2 :     auto total = buffer_size(bs);
     198            2 :     if(n < total)
     199            2 :         remove_span_prefix(bs, total - n);
     200            2 : }
     201              : 
     202              : } // capy
     203              : } // boost
     204              : 
     205              : #endif
        

Generated by: LCOV version 2.3