libs/http/src/server/detail/stable_string.hpp

100.0% Lines (25/25) 100.0% Functions (7/7) 80.0% Branches (4/5)
libs/http/src/server/detail/stable_string.hpp
Line Branch Hits 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/http
8 //
9
10 #ifndef BOOST_HTTP_SERVER_DETAIL_STABLE_STRING_HPP
11 #define BOOST_HTTP_SERVER_DETAIL_STABLE_STRING_HPP
12
13 #include <boost/http/detail/config.hpp>
14 #include <cstdint>
15 #include <cstdlib>
16
17 namespace boost {
18 namespace http {
19 namespace detail {
20
21 // avoids SBO
22 class stable_string
23 {
24 char const* p_ = 0;
25 std::size_t n_ = 0;
26
27 public:
28 982 ~stable_string()
29 {
30
2/2
✓ Branch 0 taken 278 times.
✓ Branch 1 taken 704 times.
982 if(p_)
31
1/2
✓ Branch 0 taken 278 times.
✗ Branch 1 not taken.
278 delete[] p_;
32 982 }
33
34 stable_string() = default;
35
36 704 stable_string(
37 stable_string&& other) noexcept
38 704 : p_(other.p_)
39 704 , n_(other.n_)
40 {
41 704 other.p_ = nullptr;
42 704 other.n_ = 0;
43 704 }
44
45 stable_string& operator=(
46 stable_string&& other) noexcept
47 {
48 auto p = p_;
49 auto n = n_;
50 p_ = other.p_;
51 n_ = other.n_;
52 other.p_ = p;
53 other.n_ = n;
54 return *this;
55 }
56
57 explicit
58 278 stable_string(
59 std::string_view s)
60 556 : p_(
61 834 [&]
62 {
63 278 auto p =new char[s.size()];
64 278 std::memcpy(p, s.data(), s.size());
65 278 return p;
66
1/1
✓ Branch 1 taken 278 times.
278 }())
67 278 , n_(s.size())
68 {
69 278 }
70
71 stable_string(
72 char const* it, char const* end)
73 : stable_string(std::string_view(it, end))
74 {
75 }
76
77 180 char const* data() const noexcept
78 {
79 180 return p_;
80 }
81
82 180 std::size_t size() const noexcept
83 {
84 180 return n_;
85 }
86
87 180 operator core::string_view() const noexcept
88 {
89 180 return { data(), size() };
90 }
91
92 operator std::string_view() const noexcept
93 {
94 return { data(), size() };
95 }
96 };
97
98 } // detail
99 } // http
100 } // boost
101
102 #endif
103