Coverage Report

Created: 2025-08-29 06:17

/src/glaze/include/glaze/util/dump.hpp
Line
Count
Source (jump to first uncovered line)
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
74.6k
   {
44
74.6k
      using V = std::decay_t<decltype(b[0])>;
45
74.6k
      using C = std::decay_t<decltype(c)>;
46
74.6k
      if constexpr (std::same_as<V, C>) {
47
74.6k
         b[ix] = c;
48
      }
49
      else {
50
         b[ix] = static_cast<V>(c);
51
      }
52
74.6k
   }
_ZN3glz17assign_maybe_castITnDaLc44ENSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEEvRT0_Rm
Line
Count
Source
43
26.6k
   {
44
26.6k
      using V = std::decay_t<decltype(b[0])>;
45
26.6k
      using C = std::decay_t<decltype(c)>;
46
26.6k
      if constexpr (std::same_as<V, C>) {
47
26.6k
         b[ix] = c;
48
      }
49
      else {
50
         b[ix] = static_cast<V>(c);
51
      }
52
26.6k
   }
_ZN3glz17assign_maybe_castITnDaLc58ENSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEEvRT0_Rm
Line
Count
Source
43
2.89k
   {
44
2.89k
      using V = std::decay_t<decltype(b[0])>;
45
2.89k
      using C = std::decay_t<decltype(c)>;
46
2.89k
      if constexpr (std::same_as<V, C>) {
47
2.89k
         b[ix] = c;
48
      }
49
      else {
50
         b[ix] = static_cast<V>(c);
51
      }
52
2.89k
   }
_ZN3glz17assign_maybe_castITnDaLc91ENSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEEvRT0_Rm
Line
Count
Source
43
14.7k
   {
44
14.7k
      using V = std::decay_t<decltype(b[0])>;
45
14.7k
      using C = std::decay_t<decltype(c)>;
46
14.7k
      if constexpr (std::same_as<V, C>) {
47
14.7k
         b[ix] = c;
48
      }
49
      else {
50
         b[ix] = static_cast<V>(c);
51
      }
52
14.7k
   }
_ZN3glz17assign_maybe_castITnDaLc93ENSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEEvRT0_Rm
Line
Count
Source
43
9.18k
   {
44
9.18k
      using V = std::decay_t<decltype(b[0])>;
45
9.18k
      using C = std::decay_t<decltype(c)>;
46
9.18k
      if constexpr (std::same_as<V, C>) {
47
9.18k
         b[ix] = c;
48
      }
49
      else {
50
         b[ix] = static_cast<V>(c);
51
      }
52
9.18k
   }
_ZN3glz17assign_maybe_castITnDaLc123ENSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEEvRT0_Rm
Line
Count
Source
43
19.6k
   {
44
19.6k
      using V = std::decay_t<decltype(b[0])>;
45
19.6k
      using C = std::decay_t<decltype(c)>;
46
19.6k
      if constexpr (std::same_as<V, C>) {
47
19.6k
         b[ix] = c;
48
      }
49
      else {
50
         b[ix] = static_cast<V>(c);
51
      }
52
19.6k
   }
_ZN3glz17assign_maybe_castITnDaLc125ENSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEEvRT0_Rm
Line
Count
Source
43
1.57k
   {
44
1.57k
      using V = std::decay_t<decltype(b[0])>;
45
1.57k
      using C = std::decay_t<decltype(c)>;
46
1.57k
      if constexpr (std::same_as<V, C>) {
47
1.57k
         b[ix] = c;
48
      }
49
      else {
50
         b[ix] = static_cast<V>(c);
51
      }
52
1.57k
   }
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
74.6k
   {
81
      if constexpr (Checked && vector_like<B>) {
82
         if (ix == b.size()) [[unlikely]] {
83
            b.resize(b.size() == 0 ? 128 : b.size() * 2);
84
         }
85
      }
86
74.6k
      assign_maybe_cast<c>(b, ix);
87
74.6k
      ++ix;
88
74.6k
   }
_ZN3glz4dumpITnDaLc44ELb0ENSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEEvRT1_Rm
Line
Count
Source
80
26.6k
   {
81
      if constexpr (Checked && vector_like<B>) {
82
         if (ix == b.size()) [[unlikely]] {
83
            b.resize(b.size() == 0 ? 128 : b.size() * 2);
84
         }
85
      }
86
26.6k
      assign_maybe_cast<c>(b, ix);
87
26.6k
      ++ix;
88
26.6k
   }
_ZN3glz4dumpITnDaLc58ELb0ENSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEEvRT1_Rm
Line
Count
Source
80
2.89k
   {
81
      if constexpr (Checked && vector_like<B>) {
82
         if (ix == b.size()) [[unlikely]] {
83
            b.resize(b.size() == 0 ? 128 : b.size() * 2);
84
         }
85
      }
86
2.89k
      assign_maybe_cast<c>(b, ix);
87
2.89k
      ++ix;
88
2.89k
   }
_ZN3glz4dumpITnDaLc91ELb0ENSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEEvRT1_Rm
Line
Count
Source
80
14.7k
   {
81
      if constexpr (Checked && vector_like<B>) {
82
         if (ix == b.size()) [[unlikely]] {
83
            b.resize(b.size() == 0 ? 128 : b.size() * 2);
84
         }
85
      }
86
14.7k
      assign_maybe_cast<c>(b, ix);
87
14.7k
      ++ix;
88
14.7k
   }
_ZN3glz4dumpITnDaLc93ELb0ENSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEEvRT1_Rm
Line
Count
Source
80
9.18k
   {
81
      if constexpr (Checked && vector_like<B>) {
82
         if (ix == b.size()) [[unlikely]] {
83
            b.resize(b.size() == 0 ? 128 : b.size() * 2);
84
         }
85
      }
86
9.18k
      assign_maybe_cast<c>(b, ix);
87
9.18k
      ++ix;
88
9.18k
   }
_ZN3glz4dumpITnDaLc123ELb0ENSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEEvRT1_Rm
Line
Count
Source
80
19.6k
   {
81
      if constexpr (Checked && vector_like<B>) {
82
         if (ix == b.size()) [[unlikely]] {
83
            b.resize(b.size() == 0 ? 128 : b.size() * 2);
84
         }
85
      }
86
19.6k
      assign_maybe_cast<c>(b, ix);
87
19.6k
      ++ix;
88
19.6k
   }
_ZN3glz4dumpITnDaLc125ELb0ENSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEEEvRT1_Rm
Line
Count
Source
80
1.57k
   {
81
      if constexpr (Checked && vector_like<B>) {
82
         if (ix == b.size()) [[unlikely]] {
83
            b.resize(b.size() == 0 ? 128 : b.size() * 2);
84
         }
85
      }
86
1.57k
      assign_maybe_cast<c>(b, ix);
87
1.57k
      ++ix;
88
1.57k
   }
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
7.29k
   {
93
7.29k
      static constexpr auto s = str.sv();
94
7.29k
      static constexpr auto n = s.size();
95
96
7.29k
      if constexpr (vector_like<B>) {
97
         if constexpr (Checked) {
98
            const auto k = ix + n;
99
            if (k > b.size()) [[unlikely]] {
100
               b.resize(2 * k);
101
            }
102
         }
103
7.29k
      }
104
7.29k
      std::memcpy(&b[ix], s.data(), n);
105
7.29k
      ix += n;
106
7.29k
   }
_ZN3glz4dumpITnNS_14string_literalEXtlNS1_ILm5EEEtlA5_cLc110ELc117ELc108ELc108EEEELb0ENSt3__112basic_stringIcNS4_11char_traitsIcEENS4_9allocatorIcEEEEEEvRT1_Rm
Line
Count
Source
92
1.40k
   {
93
1.40k
      static constexpr auto s = str.sv();
94
1.40k
      static constexpr auto n = s.size();
95
96
1.40k
      if constexpr (vector_like<B>) {
97
         if constexpr (Checked) {
98
            const auto k = ix + n;
99
            if (k > b.size()) [[unlikely]] {
100
               b.resize(2 * k);
101
            }
102
         }
103
1.40k
      }
104
1.40k
      std::memcpy(&b[ix], s.data(), n);
105
1.40k
      ix += n;
106
1.40k
   }
_ZN3glz4dumpITnNS_14string_literalEXtlNS1_ILm5EEEtlA5_cLc116ELc114ELc117ELc101EEEELb0ENSt3__112basic_stringIcNS4_11char_traitsIcEENS4_9allocatorIcEEEEEEvRT1_Rm
Line
Count
Source
92
3.17k
   {
93
3.17k
      static constexpr auto s = str.sv();
94
3.17k
      static constexpr auto n = s.size();
95
96
3.17k
      if constexpr (vector_like<B>) {
97
         if constexpr (Checked) {
98
            const auto k = ix + n;
99
            if (k > b.size()) [[unlikely]] {
100
               b.resize(2 * k);
101
            }
102
         }
103
3.17k
      }
104
3.17k
      std::memcpy(&b[ix], s.data(), n);
105
3.17k
      ix += n;
106
3.17k
   }
_ZN3glz4dumpITnNS_14string_literalEXtlNS1_ILm6EEEtlA6_cLc102ELc97ELc108ELc115ELc101EEEELb0ENSt3__112basic_stringIcNS4_11char_traitsIcEENS4_9allocatorIcEEEEEEvRT1_Rm
Line
Count
Source
92
2.72k
   {
93
2.72k
      static constexpr auto s = str.sv();
94
2.72k
      static constexpr auto n = s.size();
95
96
2.72k
      if constexpr (vector_like<B>) {
97
         if constexpr (Checked) {
98
            const auto k = ix + n;
99
            if (k > b.size()) [[unlikely]] {
100
               b.resize(2 * k);
101
            }
102
         }
103
2.72k
      }
104
2.72k
      std::memcpy(&b[ix], s.data(), n);
105
2.72k
      ix += n;
106
2.72k
   }
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
3.37k
   {
111
3.37k
      const auto n = str.size();
112
3.37k
      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
3.37k
      }
120
3.37k
      std::memcpy(&b[ix], str.data(), n);
121
3.37k
      ix += n;
122
3.37k
   }
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
850
   {
196
850
      const auto n = str.size();
197
850
      if (n) {
198
774
         if constexpr (vector_like<B>) {
199
            if constexpr (Checked) {
200
               const auto k = ix + n;
201
               if (k > b.size()) [[unlikely]] {
202
                  b.resize(2 * k);
203
               }
204
            }
205
774
         }
206
774
         std::memcpy(&b[ix], str.data(), n);
207
774
         ix += n;
208
774
      }
209
850
   }
void glz::dump_maybe_empty<false, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::basic_string_view<char, std::__1::char_traits<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, unsigned long&)
Line
Count
Source
195
850
   {
196
850
      const auto n = str.size();
197
850
      if (n) {
198
774
         if constexpr (vector_like<B>) {
199
            if constexpr (Checked) {
200
               const auto k = ix + n;
201
               if (k > b.size()) [[unlikely]] {
202
                  b.resize(2 * k);
203
               }
204
            }
205
774
         }
206
774
         std::memcpy(&b[ix], str.data(), n);
207
774
         ix += n;
208
774
      }
209
850
   }
Unexecuted instantiation: void glz::dump_maybe_empty<true, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > >(std::__1::basic_string_view<char, std::__1::char_traits<char> >, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, unsigned long&)
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
}