Coverage Report

Created: 2025-11-24 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/glaze/include/glaze/util/dump.hpp
Line
Count
Source
1
// Glaze Library
2
// For the license information refer to glaze.hpp
3
4
#pragma once
5
6
#include <bit>
7
#include <cstddef>
8
#include <cstring>
9
#include <span>
10
#include <string_view>
11
12
#include "glaze/concepts/container_concepts.hpp"
13
#include "glaze/core/opts.hpp"
14
#include "glaze/util/convert.hpp"
15
16
namespace glz
17
{
18
   template <class T, class V = std::remove_cvref_t<T>>
19
   concept byte_sized = sizeof(T) == 1 && (std::same_as<V, char> || std::same_as<V, std::byte>);
20
21
   template <uint32_t N, class B>
22
   GLZ_ALWAYS_INLINE void maybe_pad(B& b, size_t ix) noexcept(not vector_like<B>)
23
   {
24
      if constexpr (vector_like<B>) {
25
         if (const auto k = ix + N; k > b.size()) [[unlikely]] {
26
            b.resize(2 * k);
27
         }
28
      }
29
   }
30
31
   template <class B>
32
   GLZ_ALWAYS_INLINE void maybe_pad(const size_t n, B& b, size_t ix) noexcept(not vector_like<B>)
33
   {
34
      if constexpr (vector_like<B>) {
35
         if (const auto k = ix + n; k > b.size()) [[unlikely]] {
36
            b.resize(2 * k);
37
         }
38
      }
39
   }
40
41
   template <auto c>
42
   GLZ_ALWAYS_INLINE void assign_maybe_cast(auto& b, size_t& ix) noexcept
43
0
   {
44
0
      using V = std::decay_t<decltype(b[0])>;
45
0
      using C = std::decay_t<decltype(c)>;
46
0
      if constexpr (std::same_as<V, C>) {
47
0
         b[ix] = c;
48
0
      }
49
0
      else {
50
0
         b[ix] = static_cast<V>(c);
51
0
      }
52
0
   }
Unexecuted instantiation: _ZN3glz17assign_maybe_castITnDaLc93ENSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEEvRT0_Rm
Unexecuted instantiation: _ZN3glz17assign_maybe_castITnDaLc123ENSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEEvRT0_Rm
Unexecuted instantiation: _ZN3glz17assign_maybe_castITnDaLc58ENSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEEvRT0_Rm
Unexecuted instantiation: _ZN3glz17assign_maybe_castITnDaLc125ENSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEEvRT0_Rm
53
54
   GLZ_ALWAYS_INLINE void assign_maybe_cast(const byte_sized auto c, auto& b, size_t& ix) noexcept
55
0
   {
56
0
      using V = std::decay_t<decltype(b[0])>;
57
0
      using C = std::decay_t<decltype(c)>;
58
0
      if constexpr (std::same_as<V, C>) {
59
0
         b[ix] = c;
60
0
      }
61
0
      else {
62
0
         b[ix] = static_cast<V>(c);
63
0
      }
64
0
   }
65
66
   template <bool Checked = true, class B>
67
   GLZ_ALWAYS_INLINE void dump(const byte_sized auto c, B& b, size_t& ix) noexcept(not vector_like<B>)
68
0
   {
69
0
      if constexpr (Checked && vector_like<B>) {
70
0
         if (ix == b.size()) [[unlikely]] {
71
0
            b.resize(b.size() == 0 ? 128 : b.size() * 2);
72
0
         }
73
0
      }
74
0
      assign_maybe_cast(c, b, ix);
75
0
      ++ix;
76
0
   }
77
78
   template <auto c, bool Checked = true, class B>
79
   GLZ_ALWAYS_INLINE void dump(B& b, size_t& ix) noexcept(not vector_like<B>)
80
0
   {
81
0
      if constexpr (Checked && vector_like<B>) {
82
0
         if (ix == b.size()) [[unlikely]] {
83
0
            b.resize(b.size() == 0 ? 128 : b.size() * 2);
84
0
         }
85
0
      }
86
0
      assign_maybe_cast<c>(b, ix);
87
0
      ++ix;
88
0
   }
Unexecuted instantiation: _ZN3glz4dumpITnDaLc93ELb1ENSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEEvRT1_Rm
Unexecuted instantiation: _ZN3glz4dumpITnDaLc123ELb1ENSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEEvRT1_Rm
Unexecuted instantiation: _ZN3glz4dumpITnDaLc58ELb1ENSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEEvRT1_Rm
Unexecuted instantiation: _ZN3glz4dumpITnDaLc125ELb1ENSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEEvRT1_Rm
89
90
   template <string_literal str, bool Checked = true, class B>
91
   GLZ_ALWAYS_INLINE void dump(B& b, size_t& ix) noexcept(not vector_like<B>)
92
0
   {
93
0
      static constexpr auto s = str.sv();
94
0
      static constexpr auto n = s.size();
95
0
96
0
      if constexpr (vector_like<B>) {
97
0
         if constexpr (Checked) {
98
0
            const auto k = ix + n;
99
0
            if (k > b.size()) [[unlikely]] {
100
0
               b.resize(2 * k);
101
0
            }
102
0
         }
103
0
      }
104
0
      std::memcpy(&b[ix], s.data(), n);
105
0
      ix += n;
106
0
   }
Unexecuted instantiation: _ZN3glz4dumpITnNS_14string_literalEXtlNS1_ILm7EEEtlA7_cLc105ELc110ELc100ELc101ELc120ELc32EEEELb1ENSt3__112basic_stringIcNS4_11char_traitsIcEENS4_9allocatorIcEEEEEEvRT1_Rm
Unexecuted instantiation: _ZN3glz4dumpITnNS_14string_literalEXtlNS1_ILm3EEEtlA3_cLc58ELc32EEEELb1ENSt3__112basic_stringIcNS4_11char_traitsIcEENS4_9allocatorIcEEEEEEvRT1_Rm
Unexecuted instantiation: _ZN3glz4dumpITnNS_14string_literalEXtlNS1_ILm4EEEtlA4_cLc46ELc46ELc46EEEELb1ENSt3__112basic_stringIcNS4_11char_traitsIcEENS4_9allocatorIcEEEEEEvRT1_Rm
Unexecuted instantiation: _ZN3glz4dumpITnNS_14string_literalEXtlNS1_ILm8EEEtlA8_cLc46ELc46ELc46ELc10ELc32ELc32ELc32EEEELb1ENSt3__112basic_stringIcNS4_11char_traitsIcEENS4_9allocatorIcEEEEEEvRT1_Rm
Unexecuted instantiation: _ZN3glz4dumpITnNS_14string_literalEXtlNS1_ILm5EEEtlA5_cLc10ELc32ELc32ELc32EEEELb1ENSt3__112basic_stringIcNS4_11char_traitsIcEENS4_9allocatorIcEEEEEEvRT1_Rm
Unexecuted instantiation: _ZN3glz4dumpITnNS_14string_literalEXtlNS1_ILm4EEEtlA4_cLc32ELc32ELc32EEEELb1ENSt3__112basic_stringIcNS4_11char_traitsIcEENS4_9allocatorIcEEEEEEvRT1_Rm
Unexecuted instantiation: _ZN3glz4dumpITnNS_14string_literalEXtlNS1_ILm3EEEtlA3_cLc91ELc93EEEELb1ENSt3__112basic_stringIcNS4_11char_traitsIcEENS4_9allocatorIcEEEEEEvRT1_Rm
107
108
   template <bool Checked = true, class B>
109
   GLZ_ALWAYS_INLINE void dump(const sv str, B& b, size_t& ix) noexcept(not vector_like<B>)
110
   {
111
      const auto n = str.size();
112
      if constexpr (vector_like<B>) {
113
         if constexpr (Checked) {
114
            const auto k = ix + n;
115
            if (ix + n > b.size()) [[unlikely]] {
116
               b.resize(2 * k);
117
            }
118
         }
119
      }
120
      std::memcpy(&b[ix], str.data(), n);
121
      ix += n;
122
   }
123
124
   template <auto c, class B>
125
   GLZ_ALWAYS_INLINE void dumpn(size_t n, B& b, size_t& ix) noexcept(not vector_like<B>)
126
0
   {
127
0
      if constexpr (vector_like<B>) {
128
0
         const auto k = ix + n;
129
0
         if (k > b.size()) [[unlikely]] {
130
0
            b.resize(2 * k);
131
0
         }
132
0
      }
133
0
      std::memset(&b[ix], c, n);
134
0
      ix += n;
135
0
   }
136
137
   template <auto c, class B>
138
   GLZ_ALWAYS_INLINE void dumpn_unchecked(size_t n, B& b, size_t& ix) noexcept
139
   {
140
      std::memset(&b[ix], c, n);
141
      ix += n;
142
   }
143
144
   template <char IndentChar, class B>
145
   GLZ_ALWAYS_INLINE void dump_newline_indent(size_t n, B& b, size_t& ix) noexcept(not vector_like<B>)
146
   {
147
      if constexpr (vector_like<B>) {
148
         if (const auto k = ix + n + write_padding_bytes; k > b.size()) [[unlikely]] {
149
            b.resize(2 * k);
150
         }
151
      }
152
153
      assign_maybe_cast<'\n'>(b, ix);
154
      ++ix;
155
      std::memset(&b[ix], IndentChar, n);
156
      ix += n;
157
   }
158
159
   template <const sv& str, bool Checked = true, class B>
160
   GLZ_ALWAYS_INLINE void dump(B& b, size_t& ix) noexcept(not vector_like<B> && not Checked)
161
   {
162
      static constexpr auto s = str;
163
      static constexpr auto n = s.size();
164
165
      if constexpr (vector_like<B>) {
166
         if constexpr (Checked) {
167
            const auto k = ix + n;
168
            if (k > b.size()) [[unlikely]] {
169
               b.resize(2 * k);
170
            }
171
         }
172
      }
173
      std::memcpy(&b[ix], s.data(), n);
174
      ix += n;
175
   }
176
177
   template <bool Checked = true, class B>
178
   GLZ_ALWAYS_INLINE void dump_not_empty(const sv str, B& b, size_t& ix) noexcept(not vector_like<B> && not Checked)
179
0
   {
180
0
      const auto n = str.size();
181
0
      if constexpr (vector_like<B>) {
182
0
         if constexpr (Checked) {
183
0
            const auto k = ix + n;
184
0
            if (k > b.size()) [[unlikely]] {
185
0
               b.resize(2 * k);
186
0
            }
187
0
         }
188
0
      }
189
0
      std::memcpy(&b[ix], str.data(), n);
190
0
      ix += n;
191
0
   }
192
193
   template <bool Checked = true, class B>
194
   GLZ_ALWAYS_INLINE void dump_maybe_empty(const sv str, B& b, size_t& ix) noexcept(not vector_like<B> && not Checked)
195
0
   {
196
0
      const auto n = str.size();
197
0
      if (n) {
198
0
         if constexpr (vector_like<B>) {
199
0
            if constexpr (Checked) {
200
0
               const auto k = ix + n;
201
0
               if (k > b.size()) [[unlikely]] {
202
0
                  b.resize(2 * k);
203
0
               }
204
0
            }
205
0
         }
206
0
         std::memcpy(&b[ix], str.data(), n);
207
0
         ix += n;
208
0
      }
209
0
   }
210
211
   template <class B>
212
   GLZ_ALWAYS_INLINE void dump(const vector_like auto& bytes, B& b, size_t& ix) noexcept(not vector_like<B>)
213
   {
214
      const auto n = bytes.size();
215
      if constexpr (vector_like<B>) {
216
         const auto k = ix + n;
217
         if (k > b.size()) [[unlikely]] {
218
            b.resize(2 * k);
219
         }
220
      }
221
      std::memcpy(&b[ix], bytes.data(), n);
222
      ix += n;
223
   }
224
225
   template <size_t N, class B>
226
   GLZ_ALWAYS_INLINE void dump(const std::array<uint8_t, N>& bytes, B& b, size_t& ix) noexcept(not vector_like<B>)
227
   {
228
      if constexpr (vector_like<B>) {
229
         const auto k = ix + N;
230
         if (k > b.size()) [[unlikely]] {
231
            b.resize(2 * k);
232
         }
233
      }
234
      std::memcpy(&b[ix], bytes.data(), N);
235
      ix += N;
236
   }
237
}