1  
//
1  
//
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3  
// Copyright (c) 2025 Mohammad Nejati
3  
// Copyright (c) 2025 Mohammad Nejati
4  
//
4  
//
5  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
6  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7  
//
7  
//
8  
// Official repository: https://github.com/cppalliance/http
8  
// Official repository: https://github.com/cppalliance/http
9  
//
9  
//
10  

10  

11  
#include "src/detail/array_of_const_buffers.hpp"
11  
#include "src/detail/array_of_const_buffers.hpp"
12  

12  

13  
#include <boost/http/detail/except.hpp>
13  
#include <boost/http/detail/except.hpp>
14  

14  

15  
#include <boost/capy/buffers/slice.hpp>
15  
#include <boost/capy/buffers/slice.hpp>
16  

16  

17  
namespace boost {
17  
namespace boost {
18  
namespace http {
18  
namespace http {
19  
namespace detail {
19  
namespace detail {
20  

20  

21  
array_of_const_buffers::
21  
array_of_const_buffers::
22  
array_of_const_buffers(
22  
array_of_const_buffers(
23  
    value_type* p,
23  
    value_type* p,
24  
    std::uint16_t capacity) noexcept
24  
    std::uint16_t capacity) noexcept
25  
    : base_(p)
25  
    : base_(p)
26  
    , cap_(capacity)
26  
    , cap_(capacity)
27  
    , pos_(0)
27  
    , pos_(0)
28  
    , size_(0)
28  
    , size_(0)
29  
{
29  
{
30  
}
30  
}
31  

31  

32  
void
32  
void
33  
array_of_const_buffers::
33  
array_of_const_buffers::
34  
consume(std::size_t n)
34  
consume(std::size_t n)
35  
{
35  
{
36  
    while(size_ > 0)
36  
    while(size_ > 0)
37  
    {
37  
    {
38  
        auto* p = base_ + pos_;
38  
        auto* p = base_ + pos_;
39  
        if(n < p->size())
39  
        if(n < p->size())
40  
        {
40  
        {
41  
            capy::remove_prefix(*p, n);
41  
            capy::remove_prefix(*p, n);
42  
            return;
42  
            return;
43  
        }
43  
        }
44  
        n -= p->size();
44  
        n -= p->size();
45  
        ++pos_;
45  
        ++pos_;
46  
        --size_;
46  
        --size_;
47  
    }
47  
    }
48  
}
48  
}
49  

49  

50  
void
50  
void
51  
array_of_const_buffers::
51  
array_of_const_buffers::
52  
reset(std::uint16_t n) noexcept
52  
reset(std::uint16_t n) noexcept
53  
{
53  
{
54  
    BOOST_ASSERT(n <= cap_);
54  
    BOOST_ASSERT(n <= cap_);
55  
    pos_  = 0;
55  
    pos_  = 0;
56  
    size_ = n;
56  
    size_ = n;
57  
}
57  
}
58  

58  

59  
void
59  
void
60  
array_of_const_buffers::
60  
array_of_const_buffers::
61  
slide_to_front() noexcept
61  
slide_to_front() noexcept
62  
{
62  
{
63  
    auto* p = base_ + pos_; // begin
63  
    auto* p = base_ + pos_; // begin
64  
    auto* e = p + size_; // end
64  
    auto* e = p + size_; // end
65  
    auto* d = base_; // dest
65  
    auto* d = base_; // dest
66  
    while(p < e)
66  
    while(p < e)
67  
        *d++ = *p++;
67  
        *d++ = *p++;
68  
    pos_ = 0;
68  
    pos_ = 0;
69  
}
69  
}
70  

70  

71  
void
71  
void
72  
array_of_const_buffers::
72  
array_of_const_buffers::
73  
append(value_type buf) noexcept
73  
append(value_type buf) noexcept
74  
{
74  
{
75  
    BOOST_ASSERT(size_ < cap_);
75  
    BOOST_ASSERT(size_ < cap_);
76  
    base_[pos_ + size_] = buf;
76  
    base_[pos_ + size_] = buf;
77  
    ++size_;
77  
    ++size_;
78  
}
78  
}
79  

79  

80  
} // detail
80  
} // detail
81  
} // http
81  
} // http
82  
} // boost
82  
} // boost