1#ifndef SOURCEMETA_CORE_HTML_BUFFER_H_
2#define SOURCEMETA_CORE_HTML_BUFFER_H_
4#ifndef SOURCEMETA_CORE_HTML_EXPORT
5#include <sourcemeta/core/html_export.h>
8#include <sourcemeta/core/preprocessor.h>
15namespace sourcemeta::core {
19class SOURCEMETA_CORE_HTML_EXPORT HTMLBuffer {
21 HTMLBuffer() =
default;
22 HTMLBuffer(
const HTMLBuffer &) =
delete;
23 auto operator=(
const HTMLBuffer &) -> HTMLBuffer & =
delete;
24 HTMLBuffer(HTMLBuffer &&) =
delete;
25 auto operator=(HTMLBuffer &&) -> HTMLBuffer & =
delete;
28 SOURCEMETA_FORCEINLINE
inline auto reserve(
const std::size_t bytes) ->
void {
29 this->buffer_.resize(bytes);
30 this->cursor_ = this->buffer_.data();
31 this->end_ = this->cursor_ + bytes;
35 SOURCEMETA_FORCEINLINE
inline auto append(
const char character) ->
void {
36 if (!this->cursor_ || this->cursor_ >= this->end_) [[unlikely]] {
40 *this->cursor_ = character;
45 SOURCEMETA_FORCEINLINE
inline auto append(
const std::string_view data)
47 const auto length{data.size()};
53 this->cursor_ ?
static_cast<std::size_t
>(this->end_ - this->cursor_)
55 if (remaining < length) [[unlikely]] {
59 std::memcpy(this->cursor_, data.data(), length);
60 this->cursor_ += length;
64 [[nodiscard]] SOURCEMETA_FORCEINLINE
inline auto str()
65 ->
const std::string & {
68 static_cast<std::size_t
>(this->cursor_ - this->buffer_.data()));
69 this->cursor_ =
nullptr;
77 auto write(std::ostream &stream) -> void;
80 auto grow(std::size_t needed) -> void;
86#pragma warning(disable : 4251)
90#pragma warning(default : 4251)
92 char *cursor_{
nullptr};
SOURCEMETA_FORCEINLINE auto append(const std::string_view data) -> void
Append a sequence of characters to the buffer.
Definition html_buffer.h:45
auto write(std::ostream &stream) -> void
Write the accumulated contents to an output stream.
SOURCEMETA_FORCEINLINE auto append(const char character) -> void
Append a single character to the buffer.
Definition html_buffer.h:35
SOURCEMETA_FORCEINLINE auto str() -> const std::string &
Get the accumulated contents of the buffer.
Definition html_buffer.h:64
SOURCEMETA_FORCEINLINE auto reserve(const std::size_t bytes) -> void
Reserve a number of bytes of capacity up front.
Definition html_buffer.h:28