1  
//
1  
//
2  
// Copyright (c) 2023 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2023 Vinnie Falco (vinnie.falco@gmail.com)
3  
//
3  
//
4  
// Distributed under the Boost Software License, Version 1.0. (See accompanying
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)
5  
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6  
//
6  
//
7  
// Official repository: https://github.com/cppalliance/capy
7  
// Official repository: https://github.com/cppalliance/capy
8  
//
8  
//
9  

9  

10  
#include <boost/capy/buffers/buffer_pair.hpp>
10  
#include <boost/capy/buffers/buffer_pair.hpp>
11  
#include <boost/capy/buffers/slice.hpp>
11  
#include <boost/capy/buffers/slice.hpp>
12  

12  

13  
namespace boost {
13  
namespace boost {
14  
namespace capy {
14  
namespace capy {
15  

15  

16  
void
16  
void
17  
tag_invoke(
17  
tag_invoke(
18  
    slice_tag const&,
18  
    slice_tag const&,
19  
    const_buffer_pair& bs,
19  
    const_buffer_pair& bs,
20  
    slice_how how,
20  
    slice_how how,
21  
    std::size_t n) noexcept
21  
    std::size_t n) noexcept
22  
{
22  
{
23  
    switch(how)
23  
    switch(how)
24  
    {
24  
    {
25  
    case slice_how::remove_prefix:
25  
    case slice_how::remove_prefix:
26  
    {
26  
    {
27  
        auto p = &bs[0];
27  
        auto p = &bs[0];
28  
        if(n < p->size())
28  
        if(n < p->size())
29  
        {
29  
        {
30  
            remove_prefix(*p, n);
30  
            remove_prefix(*p, n);
31  
            return;
31  
            return;
32  
        }
32  
        }
33  
        n -= p->size();
33  
        n -= p->size();
34  
        *p = bs[1];
34  
        *p = bs[1];
35  
        bs[1] = {};
35  
        bs[1] = {};
36  
        remove_prefix(*p, n);
36  
        remove_prefix(*p, n);
37  
        return;
37  
        return;
38  
    }
38  
    }
39  

39  

40  
    case slice_how::keep_prefix:
40  
    case slice_how::keep_prefix:
41  
    {
41  
    {
42  
        auto p = &bs[0];
42  
        auto p = &bs[0];
43  
        if(n <= p->size())
43  
        if(n <= p->size())
44  
        {
44  
        {
45  
            keep_prefix(*p, n);
45  
            keep_prefix(*p, n);
46  
            bs[1] = {};
46  
            bs[1] = {};
47  
            return;
47  
            return;
48  
        }
48  
        }
49  
        n -= p->size();
49  
        n -= p->size();
50  
        ++p;
50  
        ++p;
51  
        keep_prefix(*p, n);
51  
        keep_prefix(*p, n);
52  
        return;
52  
        return;
53  
    }
53  
    }
54  
    }
54  
    }
55  
}
55  
}
56  

56  

57  
void
57  
void
58  
tag_invoke(
58  
tag_invoke(
59  
    slice_tag const&,
59  
    slice_tag const&,
60  
    mutable_buffer_pair& bs,
60  
    mutable_buffer_pair& bs,
61  
    slice_how how,
61  
    slice_how how,
62  
    std::size_t n) noexcept
62  
    std::size_t n) noexcept
63  
{
63  
{
64  
    switch(how)
64  
    switch(how)
65  
    {
65  
    {
66  
    case slice_how::remove_prefix:
66  
    case slice_how::remove_prefix:
67  
    {
67  
    {
68  
        auto p = &bs[0];
68  
        auto p = &bs[0];
69  
        if(n < p->size())
69  
        if(n < p->size())
70  
        {
70  
        {
71  
            remove_prefix(*p, n);
71  
            remove_prefix(*p, n);
72  
            return;
72  
            return;
73  
        }
73  
        }
74  
        n -= p->size();
74  
        n -= p->size();
75  
        *p = bs[1];
75  
        *p = bs[1];
76  
        bs[1] = {};
76  
        bs[1] = {};
77  
        remove_prefix(*p, n);
77  
        remove_prefix(*p, n);
78  
        return;
78  
        return;
79  
    }
79  
    }
80  

80  

81  
    case slice_how::keep_prefix:
81  
    case slice_how::keep_prefix:
82  
    {
82  
    {
83  
        auto p = &bs[0];
83  
        auto p = &bs[0];
84  
        if(n <= p->size())
84  
        if(n <= p->size())
85  
        {
85  
        {
86  
            keep_prefix(*p, n);
86  
            keep_prefix(*p, n);
87  
            bs[1] = {};
87  
            bs[1] = {};
88  
            return;
88  
            return;
89  
        }
89  
        }
90  
        n -= p->size();
90  
        n -= p->size();
91  
        ++p;
91  
        ++p;
92  
        keep_prefix(*p, n);
92  
        keep_prefix(*p, n);
93  
        return;
93  
        return;
94  
    }
94  
    }
95  
    }
95  
    }
96  
}
96  
}
97  

97  

98  
} // capy
98  
} // capy
99  
} // boost
99  
} // boost