rapidyaml  0.13.0
parse and emit YAML, and do it fast
preprocess.hpp
Go to the documentation of this file.
1 #ifndef _C4_YML_PREPROCESS_HPP_
2 #define _C4_YML_PREPROCESS_HPP_
3 
4 /** @file preprocess.hpp Functions for preprocessing YAML prior to parsing. */
5 
6 #ifndef _C4_YML_COMMON_HPP_
7 #include "./common.hpp"
8 #endif
9 #include <c4/substr.hpp>
10 
11 
12 namespace c4 {
13 namespace yml {
14 
15 /** @addtogroup doc_preprocessors
16  * @{
17  */
18 
19 /** @cond dev */
20 namespace detail {
21 using Preprocessor = size_t(csubstr, substr);
22 template<Preprocessor PP, class CharContainer>
23 substr preprocess_into_container(csubstr input, CharContainer *out)
24 {
25  // try to write once. the preprocessor will stop writing at the end of
26  // the container, but will process all the input to determine the
27  // required container size.
28  size_t sz = PP(input, to_substr(*out));
29  // if the container size is not enough, resize, and run again in the
30  // resized container
31  if(sz > out->size())
32  {
33  out->resize(sz);
34  sz = PP(input, to_substr(*out));
35  }
36  return to_substr(*out).first(sz);
37 }
38 } // namespace detail
39 /** @endcond */
40 
41 
42 //-----------------------------------------------------------------------------
43 
44 /** @defgroup doc_preprocess_rxmap preprocess_rxmap
45  *
46  * @brief Convert flow-type relaxed maps (with implicit bools) into strict YAML
47  * flow map:
48  *
49  * @code{.yaml}
50  * {a, b, c, d: [e, f], g: {a, b}}
51  * # is converted into this:
52  * {a: 1, b: 1, c: 1, d: [e, f], g: {a, b}}
53  * @endcode
54 
55  * @note this is NOT recursive - conversion happens only in the top-level map
56  * @param rxmap A relaxed map
57  * @param buf output buffer
58  * @param out output container
59  *
60  * @{
61  */
62 
63 /** Write into a given output buffer. This function is safe to call with
64  * empty or small buffers; it won't write beyond the end of the buffer.
65  *
66  * @return the number of characters required for output
67  */
68 RYML_EXPORT size_t preprocess_rxmap(csubstr rxmap, substr buf);
69 
70 
71 /** Write into an existing container. It is resized to contained the output.
72  * @return a substr of the container
73  * @overload preprocess_rxmap */
74 template<class CharContainer>
75 substr preprocess_rxmap(csubstr rxmap, CharContainer *out)
76 {
77  return detail::preprocess_into_container<preprocess_rxmap>(rxmap, out);
78 }
79 
80 
81 /** Create a container with the result.
82  * @overload preprocess_rxmap */
83 template<class CharContainer>
84 CharContainer preprocess_rxmap(csubstr rxmap)
85 {
86  CharContainer out;
87  preprocess_rxmap(rxmap, &out);
88  return out;
89 }
90 
91 /** @} */ // preprocess_rxmap
92 /** @} */ // group
93 
94 } // namespace yml
95 } // namespace c4
96 
97 #endif /* _C4_YML_PREPROCESS_HPP_ */
Common utilities and infrastructure used by ryml.
#define RYML_EXPORT
Definition: export.hpp:18
size_t preprocess_rxmap(csubstr s, substr buf)
Write into a given output buffer.
Definition: preprocess.cpp:38
substr to_substr(substr s) noexcept
neutral version for use in generic code
Definition: substr.hpp:2202
(Undefined by default) Use shorter error message from checks/asserts: do not show the check condition...
Definition: common.cpp:14
read+write string views