6#include "fastmm/core/config_macros.hpp"
18 explicit JsonWriter(std::span<char> buf) noexcept : buf_(buf) {}
41 std::uint64_t mag = v < 0 ? 0 -
static_cast<std::uint64_t
>(v) : static_cast<std::uint64_t>(v);
45 d[k++] =
static_cast<char>(
'0' + mag % 10);
48 if (v < 0) tmp[n++] =
'-';
49 while (k > 0) tmp[n++] = d[--k];
50 return raw(std::string_view(tmp, n));
52 JsonWriter&
boolean(
bool v)
noexcept {
54 return raw(v ?
"true" :
"false");
66 [[nodiscard]] std::string_view
view() const noexcept {
return {buf_.data(), len_}; }
67 [[nodiscard]] std::size_t
size() const noexcept {
return len_; }
68 [[nodiscard]]
bool ok() const noexcept {
return ok_; }
83 if (!first_) raw_char(
',');
87 JsonWriter& open() noexcept {
94 if (depth_ > 0) --depth_;
98 [[nodiscard]]
static bool needs_escape(std::string_view s)
noexcept {
99 constexpr std::uint64_t kOnes = 0x0101'0101'0101'0101ULL;
100 constexpr std::uint64_t kHigh = 0x8080'8080'8080'8080ULL;
102 std::uint64_t hit = 0;
103 for (; i + 8 <= s.size(); i += 8) {
105 std::memcpy(&x, s.data() + i, 8);
106 const std::uint64_t quote = x ^ (kOnes * 0x22U);
107 const std::uint64_t slash = x ^ (kOnes * 0x5CU);
108 hit |= (x - kOnes * 0x20) & ~x & kHigh;
109 hit |= (quote - kOnes) & ~quote & kHigh;
110 hit |= (slash - kOnes) & ~slash & kHigh;
113 for (; i < s.size(); ++i) {
115 any |=
static_cast<unsigned char>(c) < 0x20 || c ==
'"' || c ==
'\\';
119 void put_string(std::string_view s)
noexcept {
120 if (FASTMM_LIKELY(!needs_escape(s))) {
145 if (
static_cast<unsigned char>(c) < 0x20) {
146 static constexpr char kHex[] =
"0123456789abcdef";
148 raw_char(kHex[(
static_cast<unsigned char>(c) >> 4) & 0xF]);
149 raw_char(kHex[
static_cast<unsigned char>(c) & 0xF]);
157 JsonWriter& raw(std::string_view s)
noexcept {
158 if (!ok_)
return *
this;
159 if (s.size() > buf_.size() - len_) {
163 if (!s.empty()) std::memcpy(buf_.data() + len_, s.data(), s.size());
168 if (!ok_)
return *
this;
169 if (len_ >= buf_.size()) {
177 std::span<char> buf_;
178 std::size_t len_ = 0;
181 bool pending_key_ =
false;
Definition json_writer.hpp:18
JsonWriter & begin_array() noexcept
Definition json_writer.hpp:24
JsonWriter & raw_value(std::string_view v) noexcept
Pre-formatted JSON fragment (e.g. a nested object built elsewhere).
Definition json_writer.hpp:63
JsonWriter & boolean(bool v) noexcept
Definition json_writer.hpp:54
JsonWriter(std::span< char > buf) noexcept
Definition json_writer.hpp:20
JsonWriter & end_object() noexcept
Definition json_writer.hpp:23
std::string_view view() const noexcept
Definition json_writer.hpp:68
JsonWriter & integer(std::int64_t v) noexcept
Definition json_writer.hpp:39
JsonWriter & end_array() noexcept
Definition json_writer.hpp:25
JsonWriter & string(std::string_view v) noexcept
Definition json_writer.hpp:34
JsonWriter & key(std::string_view k) noexcept
Definition json_writer.hpp:27
std::size_t size() const noexcept
Definition json_writer.hpp:69
JsonWriter & null() noexcept
Definition json_writer.hpp:58
JsonWriter & begin_object() noexcept
Definition json_writer.hpp:22
void clear() noexcept
Definition json_writer.hpp:71
bool ok() const noexcept
Definition json_writer.hpp:70