1  
//
1  
//
2  
// Copyright (c) 2021 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2021 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/http
7  
// Official repository: https://github.com/cppalliance/http
8  
//
8  
//
9  

9  

10  
#include <boost/http/rfc/combine_field_values.hpp>
10  
#include <boost/http/rfc/combine_field_values.hpp>
11  

11  

12  
namespace boost {
12  
namespace boost {
13  
namespace http {
13  
namespace http {
14  

14  

15  
core::string_view
15  
core::string_view
16  
combine_field_values(
16  
combine_field_values(
17  
    fields_base::subrange const& vr,
17  
    fields_base::subrange const& vr,
18  
    grammar::recycled_ptr<std::string>& temp)
18  
    grammar::recycled_ptr<std::string>& temp)
19  
{
19  
{
20  
    core::string_view result;
20  
    core::string_view result;
21  
    bool acquired = false;
21  
    bool acquired = false;
22  
    for(auto s : vr)
22  
    for(auto s : vr)
23  
    {
23  
    {
24  
        if(s.empty())
24  
        if(s.empty())
25  
            continue;
25  
            continue;
26  
        if(result.empty())
26  
        if(result.empty())
27  
        {
27  
        {
28  
            result = s;
28  
            result = s;
29  
        }
29  
        }
30  
        else if(! acquired)
30  
        else if(! acquired)
31  
        {
31  
        {
32  
            acquired = true;
32  
            acquired = true;
33  
            if(temp.empty())
33  
            if(temp.empty())
34  
                temp.acquire();
34  
                temp.acquire();
35  
            temp->clear();
35  
            temp->clear();
36  
            temp->reserve(
36  
            temp->reserve(
37  
                result.size() +
37  
                result.size() +
38  
                1 + s.size());
38  
                1 + s.size());
39  
            *temp = result;
39  
            *temp = result;
40  
            temp->push_back(',');
40  
            temp->push_back(',');
41  
            temp->append(
41  
            temp->append(
42  
                s.data(), s.size());
42  
                s.data(), s.size());
43  
            result = *temp;
43  
            result = *temp;
44  
        }
44  
        }
45  
        else
45  
        else
46  
        {
46  
        {
47  
            temp->reserve(
47  
            temp->reserve(
48  
                temp->size() +
48  
                temp->size() +
49  
                1 + s.size());
49  
                1 + s.size());
50  
            temp->push_back(',');
50  
            temp->push_back(',');
51  
            temp->append(
51  
            temp->append(
52  
                s.data(), s.size());
52  
                s.data(), s.size());
53  
            result = *temp;
53  
            result = *temp;
54  
        }
54  
        }
55  
    }
55  
    }
56  
    return result;
56  
    return result;
57  
}
57  
}
58  

58  

59  
} // http
59  
} // http
60  
} // boost
60  
} // boost