rapidyaml  0.16.0
parseandemitYAML,anddoitfast
format.cpp
Go to the documentation of this file.
1 #include "c4/format.hpp"
2 
3 #include <memory> // for std::align
4 
5 #ifdef __clang__
6 # pragma clang diagnostic push
7 # pragma clang diagnostic ignored "-Wformat-nonliteral"
8 # pragma clang diagnostic ignored "-Wold-style-cast"
9 #elif defined(__GNUC__)
10 # pragma GCC diagnostic push
11 # pragma GCC diagnostic ignored "-Wformat-nonliteral"
12 # pragma GCC diagnostic ignored "-Wold-style-cast"
13 #endif
14 
15 namespace c4 {
16 
17 
19 {
20  void * vptr = buf.str;
21  size_t space = buf.len;
22  char * ptr = (char*) std::align(r.alignment, r.len, vptr, space);
23  if(ptr == nullptr)
24  {
25  // if it was not possible to align, return a conservative estimate
26  // of the required space
27  return r.alignment + r.len;
28  }
29  C4_CHECK(ptr >= buf.begin() && ptr <= buf.end());
30  size_t sz = static_cast<size_t>(ptr - buf.str) + r.len;
31  if(sz <= buf.len)
32  {
33  memcpy(ptr, r.buf, r.len);
34  }
35  return sz;
36 }
37 
38 
40 {
41  C4_SUPPRESS_WARNING_GCC_WITH_PUSH("-Wcast-qual")
42  void * vptr = (void*)buf.str;
43  C4_SUPPRESS_WARNING_GCC_POP
44  size_t space = buf.len;
45  char * ptr = (char*) std::align(r->alignment, r->len, vptr, space);
46  C4_CHECK(ptr != nullptr);
47  C4_CHECK(ptr >= buf.begin() && ptr <= buf.end());
48  C4_SUPPRESS_WARNING_GCC_PUSH
49  #if defined(__GNUC__) && __GNUC__ > 9
50  C4_SUPPRESS_WARNING_GCC("-Wanalyzer-null-argument")
51  #endif
52  memcpy(r->buf, ptr, r->len);
53  C4_SUPPRESS_WARNING_GCC_POP
54  return true;
55 }
56 
57 
58 } // namespace c4
59 
60 #ifdef __clang__
61 # pragma clang diagnostic pop
62 #elif defined(__GNUC__)
63 # pragma GCC diagnostic pop
64 #endif
C * str
a restricted pointer to the first character of the substring
Definition: substr.hpp:216
provides type-safe facilities for formatting arguments to string buffers
iterator begin() noexcept
Definition: substr.hpp:360
bool from_chars(csubstr buf, uint8_t *v) noexcept
Definition: charconv.hpp:2383
size_t len
the length of the substring
Definition: substr.hpp:218
iterator end() noexcept
Definition: substr.hpp:361
size_t to_chars(substr buf, uint8_t v) noexcept
Definition: charconv.hpp:2342