Line | Branch | Exec | Source |
---|---|---|---|
1 | // | ||
2 | // Copyright (c) 2019 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/http_proto | ||
8 | // | ||
9 | |||
10 | #ifndef BOOST_HTTP_PROTO_DETAIL_COPIED_STRINGS_HPP | ||
11 | #define BOOST_HTTP_PROTO_DETAIL_COPIED_STRINGS_HPP | ||
12 | |||
13 | #include <functional> | ||
14 | |||
15 | namespace boost { | ||
16 | namespace http_proto { | ||
17 | namespace detail { | ||
18 | |||
19 | // Makes copies of string_view parameters as | ||
20 | // needed when the storage for the parameters | ||
21 | // overlap the container being modified. | ||
22 | class basic_copied_strings | ||
23 | { | ||
24 | struct dynamic_buf | ||
25 | { | ||
26 | dynamic_buf* next; | ||
27 | }; | ||
28 | |||
29 | core::string_view s_; | ||
30 | char* local_buf_; | ||
31 | std::size_t local_remain_; | ||
32 | dynamic_buf* dynamic_list_ = nullptr; | ||
33 | |||
34 | bool | ||
35 | 22 | is_overlapping( | |
36 | core::string_view s) const noexcept | ||
37 | { | ||
38 | 22 | auto const b1 = s_.data(); | |
39 | 22 | auto const e1 = b1 + s_.size(); | |
40 | 22 | auto const b2 = s.data(); | |
41 | 22 | auto const e2 = b2 + s.size(); | |
42 | auto const less_equal = | ||
43 | std::less_equal<char const*>(); | ||
44 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 21 times.
|
22 | if(less_equal(e1, b2)) |
45 | 1 | return false; | |
46 |
2/2✓ Branch 1 taken 9 times.
✓ Branch 2 taken 12 times.
|
21 | if(less_equal(e2, b1)) |
47 | 9 | return false; | |
48 | 12 | return true; | |
49 | } | ||
50 | |||
51 | public: | ||
52 | 11 | ~basic_copied_strings() | |
53 | 11 | { | |
54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
|
11 | while(dynamic_list_) |
55 | { | ||
56 | ✗ | auto p = dynamic_list_; | |
57 | ✗ | dynamic_list_ = | |
58 | ✗ | dynamic_list_->next; | |
59 | ✗ | delete[] p; | |
60 | } | ||
61 | 11 | } | |
62 | |||
63 | 11 | basic_copied_strings( | |
64 | core::string_view s, | ||
65 | char* local_buf, | ||
66 | std::size_t local_size) noexcept | ||
67 | 11 | : s_(s) | |
68 | , local_buf_(local_buf) | ||
69 | 11 | , local_remain_(local_size) | |
70 | { | ||
71 | 11 | } | |
72 | |||
73 | core::string_view | ||
74 | 22 | maybe_copy( | |
75 | core::string_view s) | ||
76 | { | ||
77 |
2/2✓ Branch 1 taken 10 times.
✓ Branch 2 taken 12 times.
|
22 | if(! is_overlapping(s)) |
78 | 10 | return s; | |
79 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | if(local_remain_ >= s.size()) |
80 | { | ||
81 | 12 | std::memcpy(local_buf_, | |
82 | 12 | s.data(), s.size()); | |
83 | 12 | s = core::string_view( | |
84 | 12 | local_buf_, s.size()); | |
85 | 12 | local_buf_ += s.size(); | |
86 | 12 | local_remain_ -= s.size(); | |
87 | 12 | return s; | |
88 | } | ||
89 | ✗ | auto const n = | |
90 | sizeof(dynamic_buf); | ||
91 | ✗ | auto p = new dynamic_buf[1 + | |
92 | ✗ | sizeof(n) * ((s.size() + | |
93 | ✗ | sizeof(n) - 1) / | |
94 | ✗ | sizeof(n))]; | |
95 | ✗ | std::memcpy(p + 1, | |
96 | ✗ | s.data(), s.size()); | |
97 | ✗ | s = core::string_view(reinterpret_cast< | |
98 | ✗ | char const*>(p + 1), s.size()); | |
99 | ✗ | p->next = dynamic_list_; | |
100 | ✗ | dynamic_list_ = p; | |
101 | ✗ | return s; | |
102 | } | ||
103 | }; | ||
104 | |||
105 | class copied_strings | ||
106 | : public basic_copied_strings | ||
107 | { | ||
108 | char buf_[4096]; | ||
109 | |||
110 | public: | ||
111 | 11 | copied_strings( | |
112 | core::string_view s) | ||
113 | 11 | : basic_copied_strings( | |
114 | 11 | s, buf_, sizeof(buf_)) | |
115 | { | ||
116 | 11 | } | |
117 | }; | ||
118 | |||
119 | } // detail | ||
120 | } // http_proto | ||
121 | } // boost | ||
122 | |||
123 | #endif | ||
124 |