microfmt
Zero-allocation C++ formatting for constrained environments
Loading...
Searching...
No Matches
c_string_span_sink.hpp
Go to the documentation of this file.
1#pragma once
2
3// std-interop-file: opt-in std::span constructor overload for interop with APIs that already speak <span>.
5
6namespace microfmt {
7
16public:
23 explicit constexpr c_string_span_sink(span<char> buf RELOCO_LIFETIMEBOUND RELOCO_LIFETIME_CAPTURE_BY_THIS) noexcept
24 : detail::c_string_sink_base{buf.empty() ? nullptr : buf.data(), buf.empty() ? 0 : buf.size() - 1, 0} {
25 if (m_data) {
26 m_data[0] = '\0';
27 }
28 }
29
30#if RELOCO_HAS_STD_SPAN
37 template <std::size_t Extent>
38 explicit constexpr c_string_span_sink(
39 std::span<char, Extent> buf RELOCO_LIFETIMEBOUND RELOCO_LIFETIME_CAPTURE_BY_THIS) noexcept
40 : detail::c_string_sink_base{buf.empty() ? nullptr : buf.data(), buf.empty() ? 0 : buf.size() - 1, 0} {
41 if (m_data) {
42 m_data[0] = '\0';
43 }
44 }
45#endif
46
53 [[nodiscard]] sink as_sink() noexcept RELOCO_LIFETIMEBOUND {
54 return sink{this, &detail::c_string_sink_base::write_thunk};
55 }
56
63 [[nodiscard]] constexpr const char *c_str() const noexcept RELOCO_LIFETIMEBOUND { return m_data ? m_data : ""; }
64
71 [[nodiscard]] constexpr microfmt::string_view view() const noexcept RELOCO_LIFETIMEBOUND {
72 return microfmt::string_view(m_data, m_pos);
73 }
74
81 [[nodiscard]] constexpr std::size_t size() const noexcept { return m_pos; }
82
89 [[nodiscard]] constexpr std::size_t max_size() const noexcept { return m_max_payload; }
90
94 RELOCO_REINITIALIZES constexpr void reset() noexcept {
95 m_pos = 0;
96 if (m_data) {
97 m_data[0] = '\0';
98 }
99 }
100};
101
102} // namespace microfmt
Output sink that guarantees a null-terminated string within an externally provided,...
Definition c_string_span_sink.hpp:15
constexpr std::size_t max_size() const noexcept
Returns the maximum characters the sink can store (excluding null terminator).
Definition c_string_span_sink.hpp:89
sink as_sink() noexcept RELOCO_LIFETIMEBOUND
Creates a type-erased sink adapter pointing to this instance.
Definition c_string_span_sink.hpp:53
constexpr std::size_t size() const noexcept
Returns the length of the string in bytes (excluding null terminator).
Definition c_string_span_sink.hpp:81
constexpr c_string_span_sink(span< char > buf RELOCO_LIFETIMEBOUND RELOCO_LIFETIME_CAPTURE_BY_THIS) noexcept
Constructs a null-terminated string sink over a microfmt::span.
Definition c_string_span_sink.hpp:23
constexpr microfmt::string_view view() const noexcept RELOCO_LIFETIMEBOUND
Returns a string view over the characters written (excluding null terminator).
Definition c_string_span_sink.hpp:71
constexpr const char * c_str() const noexcept RELOCO_LIFETIMEBOUND
Returns a null-terminated C string pointer.
Definition c_string_span_sink.hpp:63
RELOCO_REINITIALIZES constexpr void reset() noexcept
Resets the sink to an empty, null-terminated state.
Definition c_string_span_sink.hpp:94
#define MICROFMT_API_CLASS
Definition compat.hpp:181
Core formatting primitives, sinks, and customization point.
Definition assert.hpp:19
reloco::sink sink
Type-erased, zero-allocation output sink.
Definition microfmt.hpp:46
Definition microfmt.hpp:445