rapidyaml  0.16.0
parseandemitYAML,anddoitfast
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 
10 namespace c4 {
11 namespace yml {
12 
13 C4_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 
27 public:
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 
50 public:
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 
85 C4_SUPPRESS_WARNING_MSVC_POP
86 
87 } // namespace yml
88 } // namespace c4
89 
90 #endif /* C4_YML_WRITER_BUF_HPP_ */
void append(const char c) noexcept
Definition: writer_buf.hpp:70
bool overlaps(ro_substr const that) const noexcept
true if there is overlap of at least one element between that and *this
Definition: substr.hpp:493
C * str
a restricted pointer to the first character of the substring
Definition: substr.hpp:216
basic_substring first(size_t num) const noexcept
return the first num elements: [0,num[
Definition: substr.hpp:529
A writer to a memory buffer, in the form of a substr .
Definition: writer_buf.hpp:20
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...
Definition: writer_buf.hpp:31
void append(const char(&a)[N]) noexcept
Definition: writer_buf.hpp:53
void append(const char c, size_t num_times) noexcept
Definition: writer_buf.hpp:77
size_t len
the length of the substring
Definition: substr.hpp:218
Error utilities used by ryml.
#define RYML_EXPORT
Definition: export.hpp:18
WriterBuf(substr sp) noexcept
Definition: writer_buf.hpp:25
void append(csubstr s) noexcept
Definition: writer_buf.hpp:62