Skip to content

Commit 683d3c5

Browse files
committed
feat: embed
1 parent cd4d7b0 commit 683d3c5

File tree

2 files changed

+170
-0
lines changed

2 files changed

+170
-0
lines changed

include/boost/capy/embed.hpp

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
//
2+
// Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot 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/capy
8+
//
9+
10+
#ifndef BOOST_CAPY_EMBED_HPP
11+
#define BOOST_CAPY_EMBED_HPP
12+
13+
#include <boost/capy/detail/config.hpp>
14+
#include <boost/core/detail/string_view.hpp>
15+
16+
#if __cpp_lib_string_view >= 201606L
17+
# include <string_view>
18+
#endif
19+
20+
namespace boost {
21+
namespace capy {
22+
23+
/** Embed a string literal as a string_view
24+
25+
The embed class template is used to embed a string literal
26+
in source code as a string_view. The first character of
27+
the string literal will be removed, typically this is a
28+
newline, allowing the string to be formatted nicely in code.
29+
30+
@par Example
31+
@code
32+
embed text(R"(
33+
Hello "world"
34+
This has quotes and )
35+
)");
36+
core::string_view sv = text.get();
37+
@endcode
38+
The resulting string_view `sv` will contain:
39+
```
40+
Hello "world"
41+
This has quotes and )
42+
```
43+
*/
44+
struct embed
45+
{
46+
/** Constructor
47+
The string literal `s` should be a raw string literal.
48+
The first character (typically a newline) will be
49+
removed from the resulting string_view.
50+
@param s The string literal
51+
*/
52+
template<std::size_t N>
53+
constexpr
54+
embed(
55+
const char (&s)[N]) noexcept
56+
: s_(s + 1, N - 2)
57+
{
58+
}
59+
60+
/** Conversion to string_view
61+
*/
62+
operator core::string_view() const noexcept
63+
{
64+
return s_;
65+
}
66+
67+
#if __cpp_lib_string_view >= 201606L
68+
/** Conversion to std::string_view
69+
*/
70+
operator std::string_view() const noexcept
71+
{
72+
return std::string_view(s_.data(), s_.size());
73+
}
74+
#endif
75+
76+
/** Return the string_view
77+
*/
78+
core::string_view
79+
get() const noexcept
80+
{
81+
return s_;
82+
}
83+
84+
/** Dereference operator
85+
*/
86+
core::string_view
87+
operator*() const noexcept
88+
{
89+
return s_;
90+
}
91+
92+
/** Member access operator
93+
*/
94+
core::string_view const*
95+
operator->() const noexcept
96+
{
97+
return &s_;
98+
}
99+
100+
private:
101+
core::string_view s_;
102+
};
103+
104+
} // capy
105+
} // boost
106+
107+
#endif

test/unit/embed.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// Copyright (c) 2025 Vinnie Falco (vinnie dot falco at gmail dot 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/capy
8+
//
9+
10+
// Test that header file is self-contained.
11+
#include <boost/capy/embed.hpp>
12+
13+
#include "test_suite.hpp"
14+
15+
#if __cpp_lib_string_view >= 201606L
16+
# include <string_view>
17+
#endif
18+
19+
namespace boost {
20+
namespace capy {
21+
22+
namespace {
23+
24+
embed text(R"(
25+
Hello "world"
26+
This has quotes and )
27+
)");
28+
29+
} // (anon)
30+
31+
struct embed_test
32+
{
33+
core::string_view good =
34+
"Hello \"world\"\nThis has quotes and )\n";
35+
36+
void check(core::string_view s)
37+
{
38+
BOOST_TEST_EQ(s, good);
39+
}
40+
41+
#if __cpp_lib_string_view >= 201606L
42+
void check_std(std::string_view s)
43+
{
44+
BOOST_TEST_EQ(s, good);
45+
}
46+
#endif
47+
48+
void run()
49+
{
50+
check(text);
51+
#if __cpp_lib_string_view >= 201606L
52+
check_std(text);
53+
#endif
54+
BOOST_TEST_EQ(text.get(), good);
55+
BOOST_TEST_EQ(*text, good);
56+
BOOST_TEST_EQ(text->data(), good);
57+
}
58+
};
59+
60+
TEST_SUITE(embed_test, "boost.capy.embed");
61+
62+
} // capy
63+
} // boost

0 commit comments

Comments
 (0)