17#include <daw/cpp_20.h>
18#include <daw/daw_attributes.h>
19#include <daw/daw_cpp_feature_check.h>
20#include <daw/daw_is_constant_evaluated.h>
21#include <daw/daw_likely.h>
22#include <daw/daw_string_view.h>
36#if defined( DAW_HAS_CPP23_STR_RESIZE_OVERWRITE ) and \
37 not defined( DAW_JSON_NO_LESS_SAFE_STRING_WRITER )
38#define DAW_JSON_STRING_WRITER_SKIPS_ZERO_FILL
49 std::size_t m_size = 0;
54 DAW_ATTRIB_NOINLINE DAW_CPP20_CX_ALLOC2
void grow( std::size_t needed ) {
55 auto const n = ( std::max )(
56 { m_str.size( ) * 2, m_str.capacity( ), needed } );
57#if defined( DAW_JSON_STRING_WRITER_SKIPS_ZERO_FILL )
58 if( not DAW_IS_CONSTANT_EVALUATED_COMPAT( ) ) {
59 m_str.resize_and_overwrite(
78 [[nodiscard]] DAW_ATTRIB_INLINE DAW_CPP20_CX_ALLOC2
char *
80 if( DAW_UNLIKELY( m_str.size( ) - m_size < n ) ) {
83 return m_str.data( ) + m_size;
86 DAW_ATTRIB_INLINE DAW_CPP20_CX_ALLOC2
void commit( std::size_t n ) {
90 DAW_ATTRIB_INLINE DAW_CPP20_CX_ALLOC2
void write( daw::string_view text ) {
91 (void)concepts::writeable_output_details::copy_to_buffer(
92 reserve( text.size( ) ), text );
93 commit( text.size( ) );
96 DAW_ATTRIB_INLINE DAW_CPP20_CX_ALLOC2
void put(
char c ) {
102 [[nodiscard]]
constexpr std::size_t
size( )
const {
108 [[nodiscard]] DAW_CPP20_CX_ALLOC std::string
finish( ) && {
109 m_str.resize( m_size );
111 return std::move( m_str );
121 template<
typename... StringViews>
123 StringViews
const &...svs ) {
124 static_assert(
sizeof...( StringViews ) > 0 );
125 auto const total_size = ( std::size( svs ) + ... );
126 DAW_CPP23_STATIC_LOCAL
constexpr auto writer =
131 p = writeable_output_details::copy_to_buffer( p, sv );
134 auto *ptr = out.
reserve( total_size );
135 ( (void)writer( ptr, svs ), ... );
A writable output that serializes into a std::string. The string's own output trait resizes it on eve...
DAW_ATTRIB_INLINE DAW_CPP20_CX_ALLOC2 void write(daw::string_view text)
constexpr std::size_t size() const
Bytes written so far.
DAW_ATTRIB_INLINE DAW_CPP20_CX_ALLOC2 void put(char c)
DAW_ATTRIB_INLINE DAW_CPP20_CX_ALLOC2 char * reserve(std::size_t n)
Room for n more bytes; commit( ) them once written.
DAW_CPP20_CX_ALLOC string_writer(std::size_t initial)
DAW_ATTRIB_NOINLINE DAW_CPP20_CX_ALLOC2 void grow(std::size_t needed)
Out of line so the room check in reserve( ) stays small enough to inline into every put and write....
DAW_CPP20_CX_ALLOC std::string finish() &&
The text written. Shrinking the string neither fills nor reallocates it.
DAW_ATTRIB_INLINE DAW_CPP20_CX_ALLOC2 void commit(std::size_t n)
#define DAW_JSON_CPP23_STATIC_CALL_OP
string_writer grows its string with resize_and_overwrite, leaving the new bytes uninitialized,...
static DAW_CPP20_CX_ALLOC void put(string_writer &out, char c)
static DAW_CPP20_CX_ALLOC void write(string_writer &out, StringViews const &...svs)
static DAW_CPP20_CX_ALLOC void reserve(string_writer &out, std::size_t n)
Room for at least n more bytes.
#define DAW_JSON_VER
The version string used in namespace definitions. Must be a valid namespace name.