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>
18namespace sourcemeta::core {
22class SOURCEMETA_CORE_HTML_EXPORT HTMLBuffer {
24 HTMLBuffer() =
default;
25 HTMLBuffer(
const HTMLBuffer &) =
delete;
26 auto operator=(
const HTMLBuffer &) -> HTMLBuffer & =
delete;
27 HTMLBuffer(HTMLBuffer &&) =
delete;
28 auto operator=(HTMLBuffer &&) -> HTMLBuffer & =
delete;
32 SOURCEMETA_FORCEINLINE
auto reserve(
const std::size_t bytes) ->
void {
33 if (this->capacity() < bytes) {
34 this->reallocate(bytes);
39 SOURCEMETA_FORCEINLINE
auto append(
const char character) ->
void {
40 if (this->cursor_ == this->end_) [[unlikely]] {
44 *this->cursor_ = character;
49 SOURCEMETA_FORCEINLINE
auto append(
const std::string_view data) ->
void {
50 const auto length{data.size()};
55 if (std::cmp_less(this->end_ - this->cursor_, length)) [[unlikely]] {
59 std::memcpy(this->cursor_, data.data(), length);
60 this->cursor_ += length;
68 if (std::cmp_less(this->end_ - this->cursor_, bytes)) [[unlikely]] {
75 SOURCEMETA_FORCEINLINE
auto
77 assert(std::cmp_less_equal(data.size(), this->end_ - this->cursor_));
79 std::memcpy(this->cursor_, data.data(), data.size());
80 this->cursor_ += data.size();
85 [[nodiscard]] SOURCEMETA_FORCEINLINE
auto size() const noexcept
87 return static_cast<std::size_t
>(this->cursor_ - this->begin_);
91 [[nodiscard]] SOURCEMETA_FORCEINLINE
auto back() const noexcept ->
char {
92 assert(this->cursor_ != this->begin_);
93 return *(this->cursor_ - 1);
98 SOURCEMETA_FORCEINLINE
auto remove_suffix(
const std::size_t count)
noexcept
100 assert(count <= this->
size());
101 this->cursor_ -= count;
106 [[nodiscard]] SOURCEMETA_FORCEINLINE
auto view() const noexcept
107 -> std::string_view {
108 return {this->begin_, this->
size()};
112 SOURCEMETA_FORCEINLINE
auto clear() noexcept ->
void {
113 this->cursor_ = this->begin_;
117 [[nodiscard]] SOURCEMETA_FORCEINLINE
auto str() ->
const std::string & {
118 this->buffer_.resize(this->
size());
119 this->begin_ = this->buffer_.data();
120 this->cursor_ = this->begin_ + this->buffer_.size();
121 this->end_ = this->cursor_;
122 return this->buffer_;
127 [[nodiscard]] SOURCEMETA_FORCEINLINE
auto take() -> std::string {
128 this->buffer_.resize(this->
size());
129 std::string result{std::move(this->buffer_)};
130 this->buffer_.clear();
131 this->begin_ =
nullptr;
132 this->cursor_ =
nullptr;
133 this->end_ =
nullptr;
138 auto write(std::ostream &stream) -> void;
141 [[nodiscard]] SOURCEMETA_FORCEINLINE
auto capacity() const noexcept
143 return static_cast<std::size_t
>(this->end_ - this->begin_);
146 auto grow(std::size_t needed) -> void;
147 auto reallocate(std::size_t capacity) -> void;
154#pragma warning(disable : 4251)
160 char *begin_{
nullptr};
161 char *cursor_{
nullptr};
SOURCEMETA_FORCEINLINE auto size() const noexcept -> std::size_t
Get the number of bytes accumulated so far.
Definition html_buffer.h:85
SOURCEMETA_FORCEINLINE auto append(const std::string_view data) -> void
Append a sequence of characters to the buffer.
Definition html_buffer.h:49
SOURCEMETA_FORCEINLINE auto take() -> std::string
Definition html_buffer.h:127
auto write(std::ostream &stream) -> void
Write the accumulated contents to an output stream.
SOURCEMETA_FORCEINLINE auto reserve_additional(const std::size_t bytes) -> void
Definition html_buffer.h:66
SOURCEMETA_FORCEINLINE auto append(const char character) -> void
Append a single character to the buffer.
Definition html_buffer.h:39
SOURCEMETA_FORCEINLINE auto back() const noexcept -> char
Get the last character of the buffer, which must not be empty.
Definition html_buffer.h:91
SOURCEMETA_FORCEINLINE auto clear() noexcept -> void
Discard the accumulated contents, keeping the capacity for reuse.
Definition html_buffer.h:112
SOURCEMETA_FORCEINLINE auto str() -> const std::string &
Get the accumulated contents of the buffer.
Definition html_buffer.h:117
SOURCEMETA_FORCEINLINE auto view() const noexcept -> std::string_view
Definition html_buffer.h:106
SOURCEMETA_FORCEINLINE auto append_unchecked(const std::string_view data) noexcept -> void
Definition html_buffer.h:76
SOURCEMETA_FORCEINLINE auto reserve(const std::size_t bytes) -> void
Definition html_buffer.h:32
SOURCEMETA_FORCEINLINE auto remove_suffix(const std::size_t count) noexcept -> void
Definition html_buffer.h:98