rapidyaml 0.15.2
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
writer_buf.hpp
Go to the documentation of this file.
1#ifndef _C4_YML_WRITER_BUF_HPP_
2#define _C4_YML_WRITER_BUF_HPP_
3
4#ifndef _C4_YML_ERROR_HPP_
5#include "c4/yml/error.hpp"
6#endif
7
8#include <string.h> // memcpy(), memset()
9
10namespace c4 {
11namespace yml {
12
13C4_SUPPRESS_WARNING_MSVC_WITH_PUSH(4251) // substr needs to have dll-interface to be used by clients of WriterBuf
14
15/** A writer to a memory buffer, in the form of a @ref substr . No
16 * overflow occurs; the buffer size is strictly respected.
17 *
18 * @ingroup doc_writers
19 * @ingroup doc_emit_to_buffer */
21{
23 size_t m_pos;
24
25 WriterBuf(substr sp) noexcept : m_buf(sp), m_pos(0) {}
26
27public:
28
29 /** Return the buffer written so far, or optionally throw an error
30 * if the buffer was too small. */
31 substr get_result(bool error_on_excess) const
32 {
33 if(m_pos <= m_buf.len)
34 {
35 return m_buf.first(m_pos);
36 }
37 else if(!error_on_excess)
38 {
39 substr sp;
40 sp.str = nullptr;
41 sp.len = m_pos;
42 return sp;
43 }
44 else
45 {
46 _RYML_ERR_BASIC("not enough space in the given buffer");
47 }
48 }
49
50public:
51
52 template<size_t N>
53 C4_ALWAYS_INLINE void append(const char (&a)[N]) noexcept
54 {
55 static_assert(N > 1, "empty string");
56 _RYML_ASSERT_BASIC( ! m_buf.overlaps(a));
57 if(m_pos + N-1 <= m_buf.len)
58 memcpy(m_buf.str + m_pos, a, N-1);
59 m_pos += N-1;
60 }
61
62 C4_ALWAYS_INLINE void append(csubstr s) noexcept
63 {
64 _RYML_ASSERT_BASIC( ! s.overlaps(m_buf));
65 if(s.len && m_pos + s.len <= m_buf.len)
66 memcpy(m_buf.str + m_pos, s.str, s.len);
67 m_pos += s.len;
68 }
69
70 C4_ALWAYS_INLINE void append(const char c) noexcept
71 {
72 if(m_pos + 1 <= m_buf.len)
73 m_buf.str[m_pos] = c;
74 ++m_pos;
75 }
76
77 C4_ALWAYS_INLINE void append(const char c, size_t num_times) noexcept
78 {
79 if(m_pos + num_times <= m_buf.len)
80 memset(m_buf.str + m_pos, c, num_times);
81 m_pos += num_times;
82 }
83};
84
85C4_SUPPRESS_WARNING_MSVC_POP
86
87} // namespace yml
88} // namespace c4
89
90#endif /* _C4_YML_WRITER_BUF_HPP_ */
Error utilities used by ryml.
#define RYML_EXPORT
Definition export.hpp:18
basic_substring< char > substr
a mutable string view
Definition substr.hpp:2356
basic_substring< const char > csubstr
an immutable string view
Definition substr.hpp:2357
(Undefined by default) Use shorter error message from checks/asserts: do not show the check condition...
Definition common.cpp:14
size_t len
the length of the substring
Definition substr.hpp:218
C * str
a restricted pointer to the first character of the substring
Definition substr.hpp:216
void append(const char c, size_t num_times) noexcept
WriterBuf(substr sp) noexcept
substr get_result(bool error_on_excess) const
Return the buffer written so far, or optionally throw an error if the buffer was too small.
void append(csubstr s) noexcept
void append(const char c) noexcept
void append(const char(&a)[N]) noexcept