Path: blob/master/dep/rapidyaml/include/c4/yml/preprocess.hpp
4264 views
#ifndef _C4_YML_PREPROCESS_HPP_1#define _C4_YML_PREPROCESS_HPP_23/** @file preprocess.hpp Functions for preprocessing YAML prior to parsing. */45/** @defgroup Preprocessors Preprocessor functions6*7* These are the existing preprocessors:8*9* @code{.cpp}10* size_t preprocess_json(csubstr json, substr buf)11* size_t preprocess_rxmap(csubstr json, substr buf)12* @endcode13*/1415#ifndef _C4_YML_COMMON_HPP_16#include "./common.hpp"17#endif18#include <c4/substr.hpp>192021namespace c4 {22namespace yml {2324namespace detail {25using Preprocessor = size_t(csubstr, substr);26template<Preprocessor PP, class CharContainer>27substr preprocess_into_container(csubstr input, CharContainer *out)28{29// try to write once. the preprocessor will stop writing at the end of30// the container, but will process all the input to determine the31// required container size.32size_t sz = PP(input, to_substr(*out));33// if the container size is not enough, resize, and run again in the34// resized container35if(sz > out->size())36{37out->resize(sz);38sz = PP(input, to_substr(*out));39}40return to_substr(*out).first(sz);41}42} // namespace detail434445//-----------------------------------------------------------------------------4647/** @name preprocess_rxmap48* Convert flow-type relaxed maps (with implicit bools) into strict YAML49* flow map.50*51* @code{.yaml}52* {a, b, c, d: [e, f], g: {a, b}}53* # is converted into this:54* {a: 1, b: 1, c: 1, d: [e, f], g: {a, b}}55* @endcode5657* @note this is NOT recursive - conversion happens only in the top-level map58* @param rxmap A relaxed map59* @param buf output buffer60* @param out output container61*/6263//@{6465/** Write into a given output buffer. This function is safe to call with66* empty or small buffers; it won't write beyond the end of the buffer.67*68* @return the number of characters required for output69*/70RYML_EXPORT size_t preprocess_rxmap(csubstr rxmap, substr buf);717273/** Write into an existing container. It is resized to contained the output.74* @return a substr of the container75* @overload preprocess_rxmap */76template<class CharContainer>77substr preprocess_rxmap(csubstr rxmap, CharContainer *out)78{79return detail::preprocess_into_container<preprocess_rxmap>(rxmap, out);80}818283/** Create a container with the result.84* @overload preprocess_rxmap */85template<class CharContainer>86CharContainer preprocess_rxmap(csubstr rxmap)87{88CharContainer out;89preprocess_rxmap(rxmap, &out);90return out;91}9293//@}9495} // namespace yml96} // namespace c49798#endif /* _C4_YML_PREPROCESS_HPP_ */99100101