src/string.cpp

100.0% Lines (24/24) 100.0% List of functions (11/11) 100.0% Branches (5/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> >) :33 5x 100.0% 100.0% boost::burl::(anonymous namespace)::string_body::content_type() const :39 3x 100.0% 100.0% boost::burl::(anonymous namespace)::string_body::content_length() const :45 4x 100.0% 100.0% boost::burl::(anonymous namespace)::string_body::write(boost::capy::any_buffer_sink&) const :51 14x 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 18x 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 15x 100.0% 100.0% 44.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 #include "detail/util.hpp"
12
13 #include <boost/capy/buffers/make_buffer.hpp>
14 #include <boost/capy/buffers/string_dynamic_buffer.hpp>
15 #include <boost/capy/read.hpp>
16
17 #include <cstdint>
18 #include <string_view>
19 #include <utility>
20
21 namespace boost
22 {
23 namespace burl
24 {
25 namespace
26 {
27
28 class string_body
29 {
30 std::string body_;
31
32 public:
33 5x explicit string_body(std::string body)
34 5x : body_(std::move(body))
35 {
36 5x }
37
38 std::optional<std::string>
39 3x content_type() const
40 {
41 3x return "text/plain; charset=utf-8";
42 }
43
44 std::optional<std::uint64_t>
45 4x content_length() const noexcept
46 {
47 4x return body_.size();
48 }
49
50 capy::io_task<>
51
1/1
✓ Branch 1 taken 14 times.
14x write(capy::any_buffer_sink& sink) const
52 {
53 auto [ec, n] =
54 co_await sink.write_eof(
55 capy::make_buffer(std::string_view(body_)));
56 co_return { ec };
57 28x }
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 18 times.
18x write(capy::any_buffer_sink& sink) const
84 {
85 auto [ec, n] = co_await sink.write_eof(
86 capy::make_buffer(body_));
87 co_return { ec };
88 36x }
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
1/1
✓ Branch 1 taken 15 times.
15x 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(detail::clamp(*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 30x }
118
119 } // namespace burl
120 } // namespace boost
121