src/string.cpp

91.7% Lines (22/24) 90.9% List of functions (10/11) 80.0% Branches (4/5)
string.cpp
f(x) Functions (11)
Function Calls Lines Branches Blocks
boost::burl::(anonymous namespace)::string_body::string_body(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) :32 5x 100.0% 100.0% boost::burl::(anonymous namespace)::string_body::content_type() const :38 3x 100.0% 100.0% boost::burl::(anonymous namespace)::string_body::content_length() const :44 4x 100.0% 100.0% boost::burl::(anonymous namespace)::string_body::write(boost::capy::any_buffer_sink&) const :50 8x 100.0% 100.0% 44.0% boost::burl::(anonymous namespace)::string_view_body::string_view_body(std::basic_string_view<char, std::char_traits<char> >) :65 4x 100.0% 100.0% boost::burl::(anonymous namespace)::string_view_body::content_type() const :71 4x 100.0% 100.0% boost::burl::(anonymous namespace)::string_view_body::content_length() const :77 4x 100.0% 100.0% boost::burl::(anonymous namespace)::string_view_body::write(boost::capy::any_buffer_sink&) const :83 14x 100.0% 100.0% 44.0% boost::burl::tag_invoke(boost::burl::body_from_tag<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) :94 5x 100.0% 100.0% 67.0% boost::burl::tag_invoke(boost::burl::body_from_tag<std::basic_string_view<char, std::char_traits<char> > >, std::basic_string_view<char, std::char_traits<char> >) :100 4x 100.0% 100.0% 100.0% boost::burl::tag_invoke(boost::burl::body_to_tag<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, boost::burl::response&) :106 0 0.0% 0.0% 0.0%
Line Branch TLA Hits Source Code
1 //
2 // Copyright (c) 2026 Mohammad Nejati
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/burl
8 //
9
10 #include <boost/burl/string.hpp>
11
12 #include <boost/capy/buffers/make_buffer.hpp>
13 #include <boost/capy/buffers/string_dynamic_buffer.hpp>
14 #include <boost/capy/read.hpp>
15
16 #include <cstdint>
17 #include <string_view>
18 #include <utility>
19
20 namespace boost
21 {
22 namespace burl
23 {
24 namespace
25 {
26
27 class string_body
28 {
29 std::string body_;
30
31 public:
32 5x explicit string_body(std::string body)
33 5x : body_(std::move(body))
34 {
35 5x }
36
37 std::optional<std::string>
38 3x content_type() const
39 {
40 3x return "text/plain; charset=utf-8";
41 }
42
43 std::optional<std::uint64_t>
44 4x content_length() const noexcept
45 {
46 4x return body_.size();
47 }
48
49 capy::io_task<>
50
1/1
✓ Branch 1 taken 8 times.
8x write(capy::any_buffer_sink& sink) const
51 {
52 if(auto [ec, n] =
53 co_await sink.write(capy::make_buffer(std::string_view(body_)));
54 ec)
55 co_return { ec };
56 co_return {};
57 16x }
58 };
59
60 class string_view_body
61 {
62 std::string_view body_;
63
64 public:
65 4x explicit string_view_body(std::string_view body)
66 4x : body_(body)
67 {
68 4x }
69
70 std::optional<std::string>
71 4x content_type() const
72 {
73 4x return "text/plain; charset=utf-8";
74 }
75
76 std::optional<std::uint64_t>
77 4x content_length() const noexcept
78 {
79 4x return body_.size();
80 }
81
82 capy::io_task<>
83
1/1
✓ Branch 1 taken 14 times.
14x write(capy::any_buffer_sink& sink) const
84 {
85 if(auto [ec, n] = co_await sink.write(capy::make_buffer(body_)); ec)
86 co_return { ec };
87 co_return {};
88 28x }
89 };
90
91 } // namespace
92
93 any_request_body
94 5x tag_invoke(body_from_tag<std::string>, std::string body)
95 {
96
1/1
✓ Branch 4 taken 5 times.
5x return string_body{ std::move(body) };
97 }
98
99 any_request_body
100 4x tag_invoke(body_from_tag<std::string_view>, std::string_view body)
101 {
102
1/1
✓ Branch 2 taken 4 times.
4x return string_view_body{ body };
103 }
104
105 capy::io_task<std::string>
106 tag_invoke(body_to_tag<std::string>, response& resp)
107 {
108 std::string ret;
109
110 if(auto cl = resp.content_length())
111 ret.reserve(*cl);
112
113 auto source = resp.as_read_source();
114 auto [ec, n] =
115 co_await capy::read(source, capy::string_dynamic_buffer(&ret));
116 co_return { ec, ret };
117 }
118
119 } // namespace burl
120 } // namespace boost
121